checker-ping/checker/rule_test.go
Pierre-Olivier Mercier 086492b03c Address publication review feedback
Add the AGPL LICENSE file and a deployment-security note in the README
to clarify that the unauthenticated /collect endpoint must run on a
trusted network.

Fix the IPv6 reachability rule so it consults the IP actually probed:
PingTargetResult now carries ResolvedIP populated from pinger.IPAddr(),
which lets the rule classify hostname targets correctly instead of
always reporting "No IPv6 target pinged".

Tighten error handling: ipsFromService now propagates JSON errors,
ExtractMetrics wraps decode failures, the count option returns an
explicit error when out of range instead of silently clamping, and the
"all pings failed" message no longer concatenates every per-target
error. Threshold validation is factored into validateThresholdPair and
shared between the RTT and packet-loss rules.

Add unit tests covering address resolution, threshold validation, and
each rule's evaluation paths.
2026-04-26 10:40:42 +07:00

101 lines
2.5 KiB
Go

// This file is part of the happyDomain (R) project.
// Copyright (c) 2020-2026 happyDomain
// Authors: Pierre-Olivier Mercier, et al.
//
// This program is offered under a commercial and under the AGPL license.
// For commercial licensing, contact us at <contact@happydomain.org>.
//
// SPDX-License-Identifier: AGPL-3.0-or-later
package checker
import (
"context"
"encoding/json"
"errors"
"testing"
sdk "git.happydns.org/checker-sdk-go/checker"
)
// stubObs implements sdk.ObservationGetter for tests. If err is non-nil, Get
// returns it; otherwise it JSON-roundtrips data into dest so callers see the
// same shape they would get over HTTP.
type stubObs struct {
data any
err error
}
func (s stubObs) Get(_ context.Context, _ sdk.ObservationKey, dest any) error {
if s.err != nil {
return s.err
}
raw, err := json.Marshal(s.data)
if err != nil {
return err
}
return json.Unmarshal(raw, dest)
}
func (s stubObs) GetRelated(_ context.Context, _ sdk.ObservationKey) ([]sdk.RelatedObservation, error) {
return nil, nil
}
func obsWith(targets ...PingTargetResult) stubObs {
return stubObs{data: PingData{Targets: targets}}
}
func TestIsIPv6(t *testing.T) {
cases := []struct {
addr string
want bool
}{
{"::1", true},
{"2001:db8::1", true},
{"127.0.0.1", false},
{"::ffff:192.0.2.1", false}, // IPv4-mapped is treated as IPv4
{"example.com", false},
{"", false},
{"not-an-ip", false},
}
for _, c := range cases {
if got := isIPv6(c.addr); got != c.want {
t.Errorf("isIPv6(%q) = %v, want %v", c.addr, got, c.want)
}
}
}
func TestLoadPingDataError(t *testing.T) {
_, st := loadPingData(context.Background(), stubObs{err: errors.New("boom")})
if st == nil {
t.Fatal("expected error CheckState, got nil")
}
if st.Status != sdk.StatusError {
t.Errorf("status = %v, want StatusError", st.Status)
}
if st.Code != "ping.observation_error" {
t.Errorf("code = %q, want ping.observation_error", st.Code)
}
}
func TestLoadPingDataOK(t *testing.T) {
d, st := loadPingData(context.Background(), obsWith(PingTargetResult{Address: "1.1.1.1"}))
if st != nil {
t.Fatalf("unexpected error state: %+v", st)
}
if len(d.Targets) != 1 || d.Targets[0].Address != "1.1.1.1" {
t.Errorf("unexpected data: %+v", d)
}
}
func TestRulesContainsAll(t *testing.T) {
names := map[string]bool{}
for _, r := range Rules() {
names[r.Name()] = true
}
for _, want := range []string{"ping.reachable", "ping.packet_loss", "ping.rtt", "ping.ipv6_reachable"} {
if !names[want] {
t.Errorf("Rules() missing %q", want)
}
}
}