Fix registry badge showing pass despite a partial/failed cycle

fillRegistrySummary derived LastResult from whichever single checks row
happened to have the latest checked_at, not the cycle's actual aggregated
result — a cycle with a mix of passing and failing checks (e.g. one egress
target timed out while the rest, including the chronologically-last check,
succeeded) rendered as a green "pass" badge on /registry, disagreeing with
the correct "partial" badge already shown on /ips for the same address.

Now prefers ip_queue.overall_result (the orchestrator's own aggregation)
when a live queue row has a finished cycle, leaves the badge blank while a
cycle is still in progress, and only falls back to classifying the most
recent cycle's own checks (pass/fail/partial) once the address has been
deleted from the queue and overall_result is no longer available.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
ayurishchevandClaude Sonnet 5 committed 2026-09-23 11:03:56 +03:00
1 parent 3b6b2d04c9
commit db97b83cd6
9 files changed
+207 -17

No files matched your search

+128
View File
@@ -3,6 +3,7 @@ package db
import (
"errors"
"testing"
"time"
"cloudipvalidator/internal/config"
)
@@ -240,3 +241,130 @@ func TestGetSetHistoryRetentionCyclesRoundTrip(t *testing.T) {
t.Fatalf("expected 5, got %d", s.HistoryRetentionCycles)
}
}
// TestRegistryLastResultReflectsAggregatedOutcome guards against the badge
// bug where LastResult was derived from whichever single check happened to
// have the latest checked_at, instead of the cycle's actual aggregated
// result — a cycle with one failing check and several passing ones is
// "partial", even if the chronologically-last check to report in passed.
func TestRegistryLastResultReflectsAggregatedOutcome(t *testing.T) {
d, ctx := newTestDB(t)
if _, err := d.SubmitIPs(ctx, []string{"1.2.3.4"}); err != nil {
t.Fatalf("submit ips: %v", err)
}
ip, err := d.GetIPByAddress(ctx, "1.2.3.4")
if err != nil {
t.Fatalf("get ip: %v", err)
}
// The chronologically-last check (icmp, checked_at later) passes, but
// an earlier one (https) failed — the cycle as a whole is partial.
now := Now()
if err := d.UpsertCheck(ctx, Check{
IPID: ip.ID, IPAddress: ip.IPAddress, AttemptNumber: ip.AttemptNumber,
Source: SourceEgress, CheckType: "https", Target: "https://example.test",
Success: false, CheckedAt: now,
}); err != nil {
t.Fatalf("upsert check (https, fail): %v", err)
}
if err := d.UpsertCheck(ctx, Check{
IPID: ip.ID, IPAddress: ip.IPAddress, AttemptNumber: ip.AttemptNumber,
Source: SourceEgress, CheckType: "icmp", Target: "https://example.test",
Success: true, CheckedAt: now.Add(time.Second),
}); err != nil {
t.Fatalf("upsert check (icmp, pass): %v", err)
}
// Simulate what orchestrator.aggregateAndRelease actually does: compute
// the aggregated result and persist it via FinishIP.
if err := d.FinishIP(ctx, ip.ID, ResultPartial); err != nil {
t.Fatalf("finish ip: %v", err)
}
summary, err := d.GetRegistryByAddress(ctx, "1.2.3.4")
if err != nil {
t.Fatalf("get registry: %v", err)
}
if summary.LastResult != ResultPartial {
t.Fatalf("expected LastResult=partial (aggregated outcome), got %q", summary.LastResult)
}
if !summary.InQueue || summary.CurrentState != IPDone {
t.Fatalf("expected still in queue as done, got in_queue=%v state=%q", summary.InQueue, summary.CurrentState)
}
all, err := d.ListRegistry(ctx)
if err != nil {
t.Fatalf("list registry: %v", err)
}
if len(all) != 1 || all[0].LastResult != ResultPartial {
t.Fatalf("expected ListRegistry to agree, got %+v", all)
}
}
// TestRegistryLastResultEmptyWhileCycleInProgress proves an address with a
// live but not-yet-finished cycle (overall_result still empty) doesn't get
// a premature pass/fail verdict.
func TestRegistryLastResultEmptyWhileCycleInProgress(t *testing.T) {
d, ctx := newTestDB(t)
if _, err := d.SubmitIPs(ctx, []string{"1.2.3.4"}); err != nil {
t.Fatalf("submit ips: %v", err)
}
summary, err := d.GetRegistryByAddress(ctx, "1.2.3.4")
if err != nil {
t.Fatalf("get registry: %v", err)
}
if summary.LastResult != "" {
t.Fatalf("expected no verdict yet for a still-queued address, got %q", summary.LastResult)
}
if !summary.InQueue || summary.CurrentState != IPQueued {
t.Fatalf("expected in_queue=true state=queued, got in_queue=%v state=%q", summary.InQueue, summary.CurrentState)
}
}
// TestRegistryLastResultFallsBackToChecksAfterDeletion proves that once an
// address's ip_queue row is gone (no more overall_result to read), the
// registry still derives a sensible partial/pass/fail verdict from the
// last recorded cycle's own checks.
func TestRegistryLastResultFallsBackToChecksAfterDeletion(t *testing.T) {
d, ctx := newTestDB(t)
if _, err := d.SubmitIPs(ctx, []string{"1.2.3.4"}); err != nil {
t.Fatalf("submit ips: %v", err)
}
ip, err := d.GetIPByAddress(ctx, "1.2.3.4")
if err != nil {
t.Fatalf("get ip: %v", err)
}
now := Now()
if err := d.UpsertCheck(ctx, Check{
IPID: ip.ID, IPAddress: ip.IPAddress, AttemptNumber: ip.AttemptNumber,
Source: SourceEgress, CheckType: "https", Target: "https://example.test",
Success: false, CheckedAt: now,
}); err != nil {
t.Fatalf("upsert check (fail): %v", err)
}
if err := d.UpsertCheck(ctx, Check{
IPID: ip.ID, IPAddress: ip.IPAddress, AttemptNumber: ip.AttemptNumber,
Source: SourceEgress, CheckType: "icmp", Target: "https://example.test",
Success: true, CheckedAt: now.Add(time.Second),
}); err != nil {
t.Fatalf("upsert check (pass): %v", err)
}
if err := d.DeleteIP(ctx, ip.ID); err != nil {
t.Fatalf("delete ip: %v", err)
}
summary, err := d.GetRegistryByAddress(ctx, "1.2.3.4")
if err != nil {
t.Fatalf("get registry: %v", err)
}
if summary.InQueue {
t.Fatalf("expected address no longer in queue")
}
if summary.LastResult != ResultPartial {
t.Fatalf("expected fallback classification partial (mixed pass/fail checks), got %q", summary.LastResult)
}
}