Files
cloud-ip-validator/internal/db/queries_registry_always_pass_test.go
T

97 lines
4.2 KiB
Go
Raw Normal View History

package db
import (
"errors"
"reflect"
"testing"
"time"
)
// RegistryAlwaysPass and its list over two finalized runs and an open one: an
// address checked in only some runs still counts, one failure rules it out, an
// address without checks of the slice is not counted; the filters, the period
// and validation.
func TestRegistryAlwaysPass(t *testing.T) {
d, ctx := newTestDB(t)
const a1, a2, a3, a4, a5 = "10.0.0.1", "10.0.0.2", "10.0.1.1", "10.0.1.2", "10.0.0.5"
// run 1: a2 fails T2 (and passes in run 2); a4 has only an ingress check.
submit(t, d, RunManual, a1, a2, a3, a4)
addCheck(t, d, a1, SourceEgress, "https", "T1", true)
addCheck(t, d, a1, SourceEgress, "https", "T2", true)
addCheck(t, d, a2, SourceEgress, "https", "T1", true)
addCheck(t, d, a2, SourceEgress, "https", "T2", false)
addCheck(t, d, a3, SourceEgress, "https", "T1", true)
addCheck(t, d, a4, InboundSource(1), "tcp-22", a4, true)
for _, a := range []string{a1, a2, a3, a4} {
finish(t, d, a, ResultPartial, -1)
}
// run 2: a1 only on T1, a2 clean, a5 is new.
submit(t, d, RunManual, a1, a2, a5)
for _, a := range []string{a1, a2, a5} {
addCheck(t, d, a, SourceEgress, "https", "T1", true)
finish(t, d, a, ResultPass, -1)
}
submit(t, d, RunManual, a1) // open: never counted
rs := runs(t, d)
if len(rs) != 3 || rs[0].State != RunOpen {
t.Fatalf("runs: %+v", rs)
}
r1, r2 := rs[2].ID, rs[1].ID
for i, day := range []string{"2026-09-01T10:00:00Z", "2026-09-05T12:00:00Z"} {
if _, err := d.ExecContext(ctx, `UPDATE check_runs SET started_at = ? WHERE id = ?`, day, []int64{r1, r2}[i]); err != nil {
t.Fatal(err)
}
}
day := func(s string) *time.Time {
v, err := time.Parse("2006-01-02", s)
if err != nil {
t.Fatal(err)
}
return &v
}
eg := RegistryFilter{Level: LevelEgress, Family: "https"}
for _, tc := range []struct {
name string
f RegistryFilter
from, to *time.Time
runs, m int
passed []string
checksFirst int // checks of the first passed address
runsOfFirst int
firstRunDate string
}{
{"all runs", eg, nil, nil, 2, 4, []string{a1, a5, a3}, 3, 2, "2026-09-01"},
{"no protocol, run and status ignored", RegistryFilter{Level: LevelEgress, RunID: 99, LastResult: ResultFail}, nil, nil, 2, 4, []string{a1, a5, a3}, 3, 2, "2026-09-01"},
{"subnet", RegistryFilter{Level: LevelEgress, Family: "https", Subnet: "10.0.1.0/24"}, nil, nil, 2, 1, []string{a3}, 1, 1, "2026-09-01"},
{"address search", RegistryFilter{Level: LevelEgress, Query: "0.0.5"}, nil, nil, 2, 1, []string{a5}, 1, 1, "2026-09-05"},
{"period: the failure is outside", eg, day("2026-09-02"), day("2026-09-05"), 1, 3, []string{a1, a2, a5}, 1, 1, "2026-09-05"},
{"ingress", RegistryFilter{Level: LevelIngress, Family: "tcp"}, nil, nil, 2, 1, []string{a4}, 1, 1, "2026-09-01"},
{"protocol without checks", RegistryFilter{Level: LevelEgress, Family: "icmp"}, nil, nil, 2, 0, nil, 0, 0, ""},
{"subnet without addresses", RegistryFilter{Level: LevelEgress, Subnet: "192.0.2.0/24"}, nil, nil, 2, 0, nil, 0, 0, ""},
{"no runs in the period", eg, day("2000-01-01"), day("2000-01-02"), 0, 0, nil, 0, 0, ""},
} {
got, err := d.RegistryAlwaysPass(ctx, tc.f, tc.from, tc.to)
if err != nil {
t.Fatalf("%s: %v", tc.name, err)
}
list, nruns, err := d.RegistryAlwaysPassList(ctx, tc.f, tc.from, tc.to)
if err != nil {
t.Fatalf("%s: %v", tc.name, err)
}
var ips []string
for _, r := range list {
ips = append(ips, r.IPAddress)
}
if got.Runs != tc.runs || got.Addresses != tc.m || got.Passed != len(tc.passed) || got.Truncated || !reflect.DeepEqual(ips, tc.passed) || nruns != tc.runs {
t.Errorf("%s: %+v list=%v runs=%d, want runs=%d M=%d passed=%v", tc.name, got, ips, nruns, tc.runs, tc.m, tc.passed)
}
if len(list) > 0 && (list[0].Checks != tc.checksFirst || list[0].RunsChecked != tc.runsOfFirst || list[0].FirstRunAt.Format("2006-01-02") != tc.firstRunDate) {
t.Errorf("%s: first row %+v", tc.name, list[0])
}
}
for _, f := range []RegistryFilter{{}, {Family: "https"}, {Level: "sideways"}, {Level: LevelEgress, Family: "dns"}, {Level: LevelEgress, Subnet: "nonsense"}} {
if _, err := d.RegistryAlwaysPass(ctx, f, nil, nil); !errors.Is(err, ErrValidation) {
t.Errorf("%+v: %v", f, err)
}
}
}