All checks were successful
release / build (push) Successful in 4m4s
One block per job (state, last success, age, overdue, upload progress, datasets). Exit 1 when a job is failed, never succeeded or overdue; running is not flagged. An older fleet API (404) gets a clear message and exit 3.
181 lines
6.5 KiB
Go
181 lines
6.5 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package fleet_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.trevon.dev/trevon/hub/internal/fleet"
|
|
)
|
|
|
|
const backupsHead = `{"generated_at":"2026-11-08T12:00:00Z","snapshot_age_seconds":5,"stale":false,"coverage_complete":true,"data":`
|
|
|
|
func backupsServer(t *testing.T, body string) *httptest.Server {
|
|
t.Helper()
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Header.Get("Authorization") != "Bearer "+secretToken {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
if r.URL.Path != "/v1/backups" {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
return
|
|
}
|
|
_, _ = w.Write([]byte(body))
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
return srv
|
|
}
|
|
|
|
func runBackups(t *testing.T, body string, asJSON bool) (int, string) {
|
|
t.Helper()
|
|
srv := backupsServer(t, body)
|
|
var out bytes.Buffer
|
|
code, err := fleet.Backups(context.Background(), newClient(t, srv.URL, secretToken), &out, asJSON)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(out.String(), secretToken) {
|
|
t.Fatal("token in output")
|
|
}
|
|
return code, out.String()
|
|
}
|
|
|
|
func okJobs(jobs string) string {
|
|
return backupsHead + `{"status":"ok","host":"alpha","checked_at":"2026-11-08T12:00:00Z","jobs":{` + jobs + `}}}`
|
|
}
|
|
|
|
const (
|
|
jobOK = `"alpha":{"state":"ok","last_success_at":"2026-11-08T11:00:00Z","age_h":1.0,"max_age_hours":26,"overdue":false,` +
|
|
`"datasets":[{"source":"pool/home","target":"backup/alpha/home","returncode":0,"latest_snapshot_at":"2026-11-08T10:59:00Z"}]}`
|
|
jobRunning = `"alpha-files":{"state":"running","last_success_at":"2026-11-07T11:00:00Z","age_h":25.0,"max_age_hours":26,"overdue":false,` +
|
|
`"progress":{"dataset":"pool/home","index":1,"total":2,"bytes_received":1288490189},` +
|
|
`"previous":{"status":"ok","finished_at":"2026-11-07T11:00:00Z"},"datasets":[]}`
|
|
)
|
|
|
|
func TestBackupsOKAndRunningLines(t *testing.T) {
|
|
code, out := runBackups(t, okJobs(jobOK+","+jobRunning), false)
|
|
if code != 0 {
|
|
t.Errorf("ok and running must exit 0, got %d\n%s", code, out)
|
|
}
|
|
want := []string{
|
|
"alpha ok last_success=2026-11-08T11:00:00Z age_h=1.0 max_age_h=26\n",
|
|
" pool/home -> backup/alpha/home rc=0 snapshot=2026-11-08T10:59:00Z\n",
|
|
"alpha-files running last_success=2026-11-07T11:00:00Z age_h=25.0 max_age_h=26\n",
|
|
" progress pool/home 1/2 received=1.2 GiB\n",
|
|
" previous ok finished 2026-11-07T11:00:00Z\n",
|
|
}
|
|
for _, w := range want {
|
|
if !strings.Contains(out, w) {
|
|
t.Errorf("missing %q in:\n%s", w, out)
|
|
}
|
|
}
|
|
if strings.Contains(out, "OVERDUE") {
|
|
t.Errorf("unexpected OVERDUE:\n%s", out)
|
|
}
|
|
}
|
|
|
|
func TestBackupsFailedNeverSucceededOverdueExitOne(t *testing.T) {
|
|
cases := map[string]string{
|
|
"failed": `"alpha":{"state":"failed","last_success_at":null,"age_h":null,"max_age_hours":26,"overdue":false,"reason":"disk full"}`,
|
|
"never_succeeded": `"alpha":{"state":"never_succeeded","last_success_at":null,"age_h":null,"max_age_hours":26,"overdue":false}`,
|
|
"overdue": `"alpha":{"state":"ok","last_success_at":"2026-11-01T11:00:00Z","age_h":200.0,"max_age_hours":26,"overdue":true}`,
|
|
}
|
|
for name, job := range cases {
|
|
code, out := runBackups(t, okJobs(job), false)
|
|
if code != 1 {
|
|
t.Errorf("%s: exit %d, want 1\n%s", name, code, out)
|
|
}
|
|
switch name {
|
|
case "failed":
|
|
if !strings.Contains(out, "alpha failed last_success=- age_h=- max_age_h=26 reason=disk full\n") {
|
|
t.Errorf("failed line wrong:\n%s", out)
|
|
}
|
|
case "overdue":
|
|
if !strings.Contains(out, "max_age_h=26 OVERDUE\n") {
|
|
t.Errorf("overdue line wrong:\n%s", out)
|
|
}
|
|
}
|
|
if code, _ := runBackups(t, okJobs(job), true); code != 1 {
|
|
t.Errorf("%s --json: exit %d, want 1", name, code)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBackupsSortedByName(t *testing.T) {
|
|
_, out := runBackups(t, okJobs(jobRunning+","+jobOK), false)
|
|
if strings.Index(out, "alpha ok") > strings.Index(out, "alpha-files running") || !strings.HasPrefix(out, "alpha ok") {
|
|
t.Errorf("jobs not sorted by name:\n%s", out)
|
|
}
|
|
// "b" sorts after "a" regardless of the order the API lists them in
|
|
_, out = runBackups(t, okJobs(`"zed":{"state":"ok","max_age_hours":1},"beta":{"state":"ok","max_age_hours":1}`), false)
|
|
if strings.Index(out, "beta ok") > strings.Index(out, "zed ok") {
|
|
t.Errorf("not sorted:\n%s", out)
|
|
}
|
|
}
|
|
|
|
func TestBackupsJSONPassthrough(t *testing.T) {
|
|
body := okJobs(jobOK)
|
|
code, out := runBackups(t, body, true)
|
|
if code != 0 {
|
|
t.Errorf("exit %d", code)
|
|
}
|
|
if !strings.Contains(out, `"max_age_hours": 26`) || !strings.Contains(out, `"age_h": 1.0`) || strings.Contains(out, "last_success=") {
|
|
t.Errorf("--json must print the raw envelope with numbers as written:\n%s", out)
|
|
}
|
|
}
|
|
|
|
func TestBackupsNonOKStatus(t *testing.T) {
|
|
code, out := runBackups(t, backupsHead+`{"status":"unavailable"}}`, false)
|
|
if code != 0 || out != "backups unavailable\n" {
|
|
t.Errorf("code=%d out=%q", code, out)
|
|
}
|
|
code, out = runBackups(t, backupsHead+`{}}`, false)
|
|
if code != 0 || out != "backups unavailable\n" {
|
|
t.Errorf("missing status: code=%d out=%q", code, out)
|
|
}
|
|
// jobs of a non-ok section are ignored for the exit code
|
|
code, _ = runBackups(t, backupsHead+`{"status":"stale","jobs":{"alpha":{"state":"failed"}}}}`, false)
|
|
if code != 0 {
|
|
t.Errorf("non-ok section exit %d, want 0", code)
|
|
}
|
|
}
|
|
|
|
func TestBackups404IsTransportWithMessage(t *testing.T) {
|
|
a := &api{} // serves only summary/jobs/modules: /v1/backups is 404
|
|
srv := httptest.NewServer(a.handler())
|
|
defer srv.Close()
|
|
var out bytes.Buffer
|
|
code, err := fleet.Backups(context.Background(), newClient(t, srv.URL, secretToken), &out, false)
|
|
if err == nil {
|
|
t.Fatalf("expected error, got code %d out %q", code, out.String())
|
|
}
|
|
if fleet.ExitCodeFor(err) != fleet.ExitTransport {
|
|
t.Errorf("exit %d, want 3", fleet.ExitCodeFor(err))
|
|
}
|
|
if want := "backups: the fleet API does not have /v1/backups yet (update the controller)"; err.Error() != want {
|
|
t.Errorf("message %q", err.Error())
|
|
}
|
|
if strings.Contains(err.Error(), secretToken) {
|
|
t.Error("token in error")
|
|
}
|
|
}
|
|
|
|
func TestHumanBytes(t *testing.T) {
|
|
cases := map[string]string{
|
|
`0`: "0 B", `1023`: "1023 B", `1024`: "1.0 KiB", `1572864`: "1.5 MiB",
|
|
`1288490189`: "1.2 GiB", `1099511627776`: "1.0 TiB", `null`: "-",
|
|
}
|
|
for in, want := range cases {
|
|
body := okJobs(`"alpha":{"state":"running","max_age_hours":1,"progress":{"dataset":"pool/home","index":1,"total":1,"bytes_received":` + in + `}}`)
|
|
_, out := runBackups(t, body, false)
|
|
if !strings.Contains(out, "received="+want+"\n") {
|
|
t.Errorf("bytes %s: want received=%s in:\n%s", in, want, out)
|
|
}
|
|
}
|
|
}
|