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

102 lines
4.1 KiB
Go
Raw Normal View History

package db
import (
"errors"
"reflect"
"testing"
"time"
)
// RegistryTrend over three finalized runs and one open one: the points per
// target or site, the subnet, the period, the run cap and validation.
func TestRegistryTrend(t *testing.T) {
d, ctx := newTestDB(t)
const a1, a2, a3 = "10.0.0.1", "10.0.0.2", "10.0.1.1"
s1 := InboundSource(1)
// run 1: three addresses; run 2: a re-check of a1 (T2 missing); run 3: a1, a2.
submit(t, d, RunManual, a1, a2, a3)
for _, c := range []struct {
addr, source, typ, target string
ok bool
}{
{a1, SourceEgress, "https", "T1", true}, {a1, SourceEgress, "https", "T2", false}, {a1, s1, "tcp-22", a1, true},
{a2, SourceEgress, "https", "T1", false}, {a2, SourceEgress, "https", "T2", true},
{a3, SourceEgress, "https", "T1", true}, {a3, SourceEgress, "https", "T2", true},
} {
addCheck(t, d, c.addr, c.source, c.typ, c.target, c.ok)
}
finish(t, d, a1, ResultPartial, -1)
finish(t, d, a2, ResultPartial, -1)
finish(t, d, a3, ResultPass, -1)
submit(t, d, RunManual, a1)
addCheck(t, d, a1, SourceEgress, "https", "T1", true)
finish(t, d, a1, ResultPass, -1)
submit(t, d, RunManual, a1, a2)
for _, a := range []string{a1, a2} {
addCheck(t, d, a, SourceEgress, "https", "T1", false)
addCheck(t, d, a, SourceEgress, "https", "T2", true)
finish(t, d, a, ResultPartial, -1)
}
submit(t, d, RunManual, a1) // open: never on the chart
rs := runs(t, d)
if len(rs) != 4 || rs[0].State != RunOpen {
t.Fatalf("runs: %+v", rs)
}
r1, r2, r3 := rs[3].ID, rs[2].ID, rs[1].ID
for i, day := range []string{"2026-09-01T10:00:00Z", "2026-09-05T23:59:59Z", "2026-09-06T00:00:00.5Z"} {
if _, err := d.ExecContext(ctx, `UPDATE check_runs SET started_at = ? WHERE id = ?`, day, []int64{r1, r2, r3}[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 []int64
addrs []int
points []TrendPoint
}{
{"all runs, the open one absent", eg, nil, nil, []int64{r1, r2, r3}, []int{3, 1, 2},
[]TrendPoint{{r1, "T1", 3, 2}, {r2, "T1", 1, 1}, {r3, "T1", 2, 0}, {r1, "T2", 3, 2}, {r3, "T2", 2, 2}}},
{"subnet", RegistryFilter{Level: LevelEgress, Family: "https", Subnet: "10.0.1.0/24"}, nil, nil, []int64{r1, r2, r3}, []int{1, 0, 0},
[]TrendPoint{{r1, "T1", 1, 1}, {r1, "T2", 1, 1}}},
{"period, to inclusive", eg, day("2026-09-02"), day("2026-09-05"), []int64{r2}, []int{1},
[]TrendPoint{{r2, "T1", 1, 1}}},
{"ingress by site, tcp", RegistryFilter{Level: LevelIngress, Family: "tcp"}, nil, nil, []int64{r1, r2, r3}, []int{3, 1, 2},
[]TrendPoint{{r1, s1, 1, 1}}},
{"no protocol: all checks of the direction", RegistryFilter{Level: LevelEgress, RunID: r1, Query: "x", LastResult: ResultFail}, day("2026-09-06"), nil, []int64{r3}, []int{2},
[]TrendPoint{{r3, "T1", 2, 0}, {r3, "T2", 2, 2}}},
{"subnet without addresses", RegistryFilter{Level: LevelEgress, Subnet: "192.0.2.0/24"}, nil, nil, []int64{r1, r2, r3}, []int{0, 0, 0}, nil},
} {
got, err := d.RegistryTrend(ctx, tc.f, tc.from, tc.to)
if err != nil {
t.Fatalf("%s: %v", tc.name, err)
}
var ids []int64
var addrs []int
for _, r := range got.Runs {
ids, addrs = append(ids, r.ID), append(addrs, r.Addresses)
}
if !reflect.DeepEqual(ids, tc.runs) || !reflect.DeepEqual(addrs, tc.addrs) || !reflect.DeepEqual(got.Points, tc.points) || got.Truncated {
t.Errorf("%s: runs=%v addresses=%v points=%v truncated=%v, want %v %v %v", tc.name, ids, addrs, got.Points, got.Truncated, tc.runs, tc.addrs, tc.points)
}
}
if g, _ := d.RegistryTrend(ctx, RegistryFilter{Level: LevelIngress}, nil, nil); g.Group != BreakdownSite {
t.Errorf("ingress group: %q", g.Group)
}
for _, f := range []RegistryFilter{{}, {Family: "https"}, {Level: "sideways"}, {Level: LevelEgress, Family: "dns"}, {Level: LevelEgress, Subnet: "nonsense"}} {
if _, err := d.RegistryTrend(ctx, f, nil, nil); !errors.Is(err, ErrValidation) {
t.Errorf("%+v: %v", f, err)
}
}
}