package osupdates import ( "context" "crypto/sha256" "encoding/hex" "testing" "time" "gitea.dooplex.hu/admin/felhom-hub/internal/bundlefiles" "gitea.dooplex.hu/admin/felhom-hub/internal/store" ) func bundleReport(t *testing.T, f *fix, host, version, sha string) { t.Helper() body := `{"system":{"pve_version":"pve-manager/9.2.2/x","config_bundle":{"version":"` + version + `","bundle_sha256":"` + sha + `"}}}` h, err := f.s.Store.GetHost(host) if err != nil || h == nil { t.Fatalf("no host %s", host) } if err := f.s.Store.SaveHostReport(host, h.CustomerID, []byte(body), store.HostReportDenorm{AgentVersion: "0.143.0"}); err != nil { t.Fatal(err) } } func vouch(t *testing.T, f *fix, sha string) { t.Helper() if err := f.s.Store.SetArtifactManifest(store.ArtifactManifest{AgentVersion: "0.143.0", AgentSHA256: "x", BundleSHA256: sha}); err != nil { t.Fatal(err) } } func count(sent []string, typ string) int { n := 0 for _, s := range sent { if s == typ { n++ } } return n } // R-840: a box behind the vouched bundle is told to the operator after 7 days — not before, once, and the clock // restarts when the box catches up. "none" (no bundle ever) counts; "unknown" never does. func TestBundleAlarm_AfterSevenDaysBehind(t *testing.T) { f := newFix(t) vouch(t, f, "new") bundleReport(t, f, "cust1", "none", "") sent, _ := f.s.Alarms() if count(sent, EventBundleBehind) != 0 { t.Fatal("alarm on the first sight") } f.now = f.now.Add(6 * 24 * time.Hour) if sent, _ = f.s.Alarms(); count(sent, EventBundleBehind) != 0 { t.Fatal("alarm before 7 days") } f.now = f.now.Add(25 * time.Hour) if sent, _ = f.s.Alarms(); count(sent, EventBundleBehind) != 1 { t.Fatalf("no alarm after 7 days: %v", sent) } if sent, _ = f.s.Alarms(); count(sent, EventBundleBehind) != 0 { t.Fatal("the alarm must not repeat at once") } bundleReport(t, f, "cust1", "0.143.0", "new") f.s.Alarms() if !f.s.Store.BundleBehindSince("cust1").IsZero() { t.Fatal("caught up: the clock must clear") } } func TestBundleAlarm_UnknownAndNoVouchedBundleSayNothing(t *testing.T) { f := newFix(t) vouch(t, f, "new") bundleReport(t, f, "cust1", "unknown", "") f.s.Alarms() f.now = f.now.Add(30 * 24 * time.Hour) if sent, _ := f.s.Alarms(); count(sent, EventBundleBehind) != 0 || !f.s.Store.BundleBehindSince("cust1").IsZero() { t.Fatalf("unknown is not a fact: %v", sent) } // control: the same box saying "none" IS behind (proves the report above was read at all) bundleReport(t, f, "cust1", "none", "") f.s.Alarms() if f.s.Store.BundleBehindSince("cust1").IsZero() { t.Fatal("control: a box saying none must start the clock") } g := newFix(t) vouch(t, g, "") bundleReport(t, g, "cust1", "0.143.0", "some-bundle") g.s.Alarms() g.now = g.now.Add(30 * 24 * time.Hour) if sent, _ := g.s.Alarms(); count(sent, EventBundleBehind) != 0 { t.Fatalf("nothing vouched, nothing behind: %v", sent) } } // R-1392 (`09` §3 decision 304): an agent release that changed no root file gives the bundle a new label and a new // sha256. A box on the same files is not behind: no alarm after the wait, and a "behind since" mark set before the // fix is cleared. A box whose files differ, and a box that never got a bundle, still alarm. func TestBundleAlarm_R1392_SameFilesNewLabel(t *testing.T) { f := newFix(t) if err := f.s.Store.SetArtifactManifest(store.ArtifactManifest{AgentVersion: "0.173.0", AgentSHA256: "x", BundleSHA256: "new"}); err != nil { t.Fatal(err) } bundleReport(t, f, "cust1", "0.172.1", "old") if _, err := f.s.Alarms(); err != nil { // before the fix: the mark is set t.Fatal(err) } if f.s.Store.BundleBehindSince("cust1").IsZero() { t.Fatal("fixture: without the answer the box must be marked behind") } f.s.SameBundleFiles = func(bv, bs, vv, vs string) bool { return bv == "0.172.1" && bs == "old" && vv == "0.173.0" && vs == "new" } f.now = f.now.Add(8 * 24 * time.Hour) if sent, _ := f.s.Alarms(); count(sent, EventBundleBehind) != 0 { t.Fatalf("same files under a new label alarmed: %v", sent) } if !f.s.Store.BundleBehindSince("cust1").IsZero() { t.Fatal("the behind-since mark was not cleared") } // One file differs: behind, and the alarm after its wait. bundleReport(t, f, "cust1", "0.171.0", "older") f.s.Alarms() f.now = f.now.Add(8 * 24 * time.Hour) if sent, _ := f.s.Alarms(); count(sent, EventBundleBehind) != 1 { t.Fatalf("different files did not alarm: %v", sent) } } // The same question through the REAL resolver (a fake registry): a box that never got a bundle has no sha256 to // verify, so it stays behind and alarms. func TestBundleAlarm_R1392_NeverGotABundleStillAlarms(t *testing.T) { f := newFix(t) vouched := []byte(`{"agent_version":"0.143.0","files":[{"path":"/x","sha256":"aa"}],"format":1}`) sum := sha256.Sum256(vouched) vouch(t, f, hex.EncodeToString(sum[:])) r := &bundlefiles.Resolver{Fetch: func(_ context.Context, v string) ([]byte, error) { return vouched, nil }} f.s.SameBundleFiles = r.Same bundleReport(t, f, "cust1", "none", "") f.s.Alarms() f.now = f.now.Add(8 * 24 * time.Hour) if sent, _ := f.s.Alarms(); count(sent, EventBundleBehind) != 1 { t.Fatalf("a box with no bundle did not alarm: %v", sent) } }