// SPDX-License-Identifier: GPL-3.0-or-later package fleet_test import ( "strings" "testing" "git.trevon.dev/trevon/hub/internal/fleet" ) // bootHost is a telemetry host row for the kernel/module guard lines. km and outcome are raw JSON ("" leaves the field out, // like an older collector). func bootHost(kernel, km, outcome string) string { extra := "" if km != "" { extra += `,"kernel_modules":` + km } if outcome != "" { extra += `,"reboot_outcome":` + outcome } return `{"kernel":"` + kernel + `","reboot_required":true,"reboot_required_since":"2026-11-18T16:00:00Z",` + `"reboot_required_pkgs":["linux-image-amd64"],"last_report_at":"2026-11-19T15:55:00Z"` + extra + `}` } const ( // alpha: running 7.2.15, 7.3.4 installed without the zfs module, dpkg half-configured (the incident shape). kmIncident = `{"next_boot_kernel":"7.3.4+deb14-amd64","missing_modules":["zfs"],"zfs_linux_max":"7.2",` + `"dpkg_interrupted":["linux-image-7.3.4+deb14-amd64","linux-image-amd64"],"kernel_hold":{"held":false,"reason":""}}` // after the clean-up: 7.2.15 only, the metas held because the candidate is 7.3. kmClean = `{"next_boot_kernel":"7.2.15+deb14-amd64","missing_modules":[],"zfs_linux_max":"7.2",` + `"dpkg_interrupted":[],"kernel_hold":{"held":false,"reason":""}}` kmHeld = `{"next_boot_kernel":"7.2.15+deb14-amd64","missing_modules":[],"zfs_linux_max":"7.2",` + `"dpkg_interrupted":[],"kernel_hold":{"held":true,"reason":"zfs supports <=7.2, candidate 7.3"}}` kmUnknown = `{"next_boot_kernel":null,"missing_modules":["zfs"],"zfs_linux_max":"7.2",` + `"dpkg_interrupted":[],"kernel_hold":{"held":false,"reason":""}}` ) func (a *telAPI) bootView(t *testing.T, row string) (int, string) { t.Helper() a.setTelemetry(telBody(map[string]string{"alpha": row})) a.setEvents("[]") code, out, err := a.host(t, "alpha", false) if err != nil { t.Fatal(err) } return code, out } func lineWith(out, prefix string) string { for _, l := range strings.Split(out, "\n") { if strings.HasPrefix(l, prefix) { return l } } return "" } func TestHostBootLineGolden(t *testing.T) { cases := []struct { name, row, boot string code int attention string }{ {"incident: next boot loses zfs and dpkg is half-configured", bootHost("7.2.15+deb14-amd64", kmIncident, ""), "boot next boot 7.3.4 MISSING zfs; dpkg interrupted: linux-image-7.3.4+deb14-amd64, linux-image-amd64", fleet.ExitDrift, "attention: next boot kernel lacks modules: zfs; dpkg interrupted: linux-image-7.3.4+deb14-amd64, linux-image-amd64"}, {"cleaned up", bootHost("7.2.15+deb14-amd64", kmClean, ""), "boot next boot 7.2.15 ok", fleet.ExitOK, ""}, {"held by the guard", bootHost("7.2.15+deb14-amd64", kmHeld, ""), "boot next boot 7.2.15 ok; kernel held: zfs supports <=7.2, candidate 7.3", fleet.ExitOK, ""}, {"next boot kernel unknown", bootHost("7.2.15+deb14-amd64", kmUnknown, ""), "boot next boot unknown, cannot confirm zfs", fleet.ExitDrift, "attention: next boot kernel lacks modules: zfs"}, {"an Ubuntu release keeps its full name", bootHost("5.15.0-203-generic", `{"next_boot_kernel":"5.15.0-203-generic","missing_modules":[],"dpkg_interrupted":[],"kernel_hold":{"held":false,"reason":""}}`, ""), "boot next boot 5.15.0-203-generic ok", fleet.ExitOK, ""}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { a := newTelAPI(t) code, out := a.bootView(t, c.row) if got := lineWith(out, "boot "); got != c.boot { t.Errorf("boot line\n got %q\nwant %q\n%s", got, c.boot, out) } if code != c.code { t.Errorf("exit %d, want %d\n%s", code, c.code, out) } got := lineWith(out, "attention:") if got != c.attention { t.Errorf("attention\n got %q\nwant %q", got, c.attention) } }) } } func TestHostBootLineGoldenFull(t *testing.T) { a := newTelAPI(t) _, out := a.bootView(t, bootHost("7.2.15+deb14-amd64", kmIncident, `{"status":"blocked","reason":"boot_kernel_missing_modules","at":"2026-12-01T04:01:00Z"}`)) want := `alpha last report 5m ago (2026-11-19T15:55:00Z) kernel 7.2.15+deb14-amd64 boot next boot 7.3.4 MISSING zfs; dpkg interrupted: linux-image-7.3.4+deb14-amd64, linux-image-amd64 reboot REQUIRED since 2026-11-18T16:00:00Z (1d0h) by linux-image-amd64; last attempt blocked (boot_kernel_missing_modules) 2026-12-01T04:01:00Z app not reported updates - apt pending, - security, - third-party, - kept back vulns not reported ` if !strings.HasPrefix(out, want) { t.Errorf("top of the host view differs.\n--- got\n%s--- want prefix\n%s", out, want) } } func TestHostRebootLineShowsDeferredAndBlockedReasons(t *testing.T) { cases := map[string]string{ `{"status":"deferred","reason":"load","at":"2026-12-01T04:01:00Z"}`: "; last attempt deferred (load) 2026-12-01T04:01:00Z", `{"status":"deferred","reason":"hold_file","at":"2026-12-01T04:01:00Z"}`: "; last attempt deferred (hold_file) 2026-12-01T04:01:00Z", `{"status":"deferred","reason":"unit","at":"2026-12-01T04:01:00Z"}`: "; last attempt deferred (unit) 2026-12-01T04:01:00Z", `{"status":"deferred","reason":"process","at":"2026-12-01T04:01:00Z"}`: "; last attempt deferred (process) 2026-12-01T04:01:00Z", `{"status":"blocked","reason":"dpkg_interrupted","at":"2026-12-01T04:01:00Z"}`: "; last attempt blocked (dpkg_interrupted) 2026-12-01T04:01:00Z", `{"status":"reboot_requested","reason":"scheduled_window","at":"2026-12-01T04:01:00Z"}`: "; last attempt requested (scheduled_window) 2026-12-01T04:01:00Z", } for outcome, suffix := range cases { a := newTelAPI(t) _, out := a.bootView(t, bootHost("7.2.15+deb14-amd64", kmClean, outcome)) if got := lineWith(out, "reboot "); !strings.HasSuffix(got, suffix) { t.Errorf("reboot line %q lacks %q", got, suffix) } } } func TestHostBootAndRebootLinesOmittedForOldCollectors(t *testing.T) { a := newTelAPI(t) _, out := a.bootView(t, bootHost("5.15.0-203-generic", "", "")) if lineWith(out, "boot ") != "" { t.Errorf("old collector must not print a boot line:\n%s", out) } if got := lineWith(out, "reboot "); strings.Contains(got, "last attempt") { t.Errorf("reboot line: %q", got) } // present but empty sections (a host with no DKMS and an unknown next boot) print nothing and flag nothing code, out := a.bootView(t, bootHost("5.15.0-203-generic", `{"next_boot_kernel":null,"missing_modules":[],"dpkg_interrupted":[],"kernel_hold":{"held":false,"reason":""}}`, "")) if lineWith(out, "boot ") != "" || code != fleet.ExitOK { t.Errorf("empty guard data: code=%d\n%s", code, out) } } func TestHostJSONCarriesKernelModulesAndOutcome(t *testing.T) { a := newTelAPI(t) a.setTelemetry(telBody(map[string]string{"alpha": bootHost("7.2.15+deb14-amd64", kmIncident, `{"status":"deferred","reason":"load","at":"2026-12-01T04:01:00Z"}`)})) a.setEvents("[]") code, out, err := a.host(t, "alpha", true) if err != nil || code != fleet.ExitDrift { t.Fatalf("code=%d err=%v", code, err) } for _, want := range []string{`"kernel_modules"`, `"missing_modules"`, `"reboot_outcome"`, `next boot kernel lacks modules: zfs`} { if !strings.Contains(out, want) { t.Errorf("json lacks %s:\n%s", want, out) } } } func TestGuardFlagWeightsAndHints(t *testing.T) { for flag, w := range map[string]int{"boot_kernel_missing_modules": 4, "dpkg_interrupted": 4, "kernel_held_for_module": 1, "reboot_deferred_long": 2} { if got := fleet.FlagWeight(flag); got != w { t.Errorf("%s weight %d, want %d", flag, got, w) } if fleet.FlagHint(flag) == "see the host report" { t.Errorf("%s has no hint", flag) } } }