Files
felhom-controller/controller/internal/offsiteapply/d84_window_client_test.go
T
admin c5acb91a44
gates / gates (push) Successful in 32s
Review fix 5: the box sends its end records only to a hub that says it reads them (version skew)
A new box with an older hub sent `ended` in the window request; that hub reads
4 KiB of it, decided the window on a wrong count, and the weekly clean-up
stalled.

- The box sends `ended` only when the hub's last window answer said
  accepts_ended (kept in settings, re-read from every answer, so a rolled-back
  hub stops getting them after one answer). Otherwise the request is the one
  every hub always got, and the ordinary clean-up runs.
- One request carries at most 5000 snapshot ids.

Tests seen red before the fix; 6 mutations seen red (felhom.eu
documentation/audits/build-release-4-2026-11-15/A/redproof-review-5.txt).

Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
2026-11-15 15:05:38 +01:00

79 lines
3.7 KiB
Go

package offsiteapply
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"gitea.dooplex.hu/admin/felhom-controller/internal/backup"
)
// D72 / D84 (`07-backup-architecture.md` §8.2f): the window request carries the box's due end records, the answer
// carries what the hub agreed to — and only a GRANTED window carries an agreement.
// RED-PROOF: felhom.eu/documentation/audits/build-release-4-2026-11-15/A/redproof-ctrl-window.txt.
func TestD84_WindowClient_SendsTheClaimsAndReadsTheAgreement(t *testing.T) {
var gotPath string
var gotBody map[string]json.RawMessage
answer := `{"granted":true,"window_id":9,"newest_allowed":"2026-12-20T03:00:00Z","max_remove":10,"ended_allowed":["aa","bb"],"ended_refused":[{"app":"immich","reason":"the box's newest report lists the app as installed"}]}`
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
b, _ := io.ReadAll(r.Body)
gotBody = map[string]json.RawMessage{}
_ = json.Unmarshal(b, &gotBody)
_, _ = w.Write([]byte(answer))
}))
defer srv.Close()
c := HubWindowClient{Registrar: HubRegistrar{HubURL: srv.URL, CustomerID: "c1", APIKey: "k"}}
claims := []backup.OffsiteEndedClaim{{App: "vikunja", RemovedAt: "2026-11-15T10:00:00Z", EndsAt: "2026-12-15T10:00:00Z", SnapshotIDs: []string{"aa", "bb", "cc"}}}
w, err := c.Open(context.Background(), 20, claims)
if err != nil {
t.Fatal(err)
}
if gotPath != "/api/v1/offsite/window-open/c1" || string(gotBody["count_before"]) != "20" {
t.Fatalf("request: path=%q body=%v", gotPath, gotBody)
}
var sent []backup.OffsiteEndedClaim
if err := json.Unmarshal(gotBody["ended"], &sent); err != nil || len(sent) != 1 || sent[0].App != "vikunja" || strings.Join(sent[0].SnapshotIDs, ",") != "aa,bb,cc" || sent[0].RemovedAt != "2026-11-15T10:00:00Z" {
t.Fatalf("the hub did not receive the claim: %s (%v)", gotBody["ended"], err)
}
if w.AcceptsEnded {
t.Fatal("an answer WITHOUT accepts_ended (an older hub) reads as a hub that knows end records")
}
if !w.Granted || w.ID != 9 || w.MaxRemove != 10 || strings.Join(w.EndedAllowed, ",") != "aa,bb" || len(w.EndedRefused) != 1 || w.EndedRefused[0].App != "immich" {
t.Fatalf("window = %+v, want the agreement (aa,bb) and the refusal of immich", w)
}
// With no due record the body is the one an older hub always saw: no `ended` key at all.
if _, err := c.Open(context.Background(), 20, nil); err != nil {
t.Fatal(err)
}
if _, has := gotBody["ended"]; has {
t.Fatalf("a request with no claim carries `ended`: %v", gotBody)
}
// A window that is NOT granted carries no agreement, whatever the answer holds — and it does carry the hub's
// word that it reads end records (the box learns it on an ordinary night).
answer = `{"granted":false,"reason":"not due","ended_allowed":["aa","bb"],"accepts_ended":true}`
w2, err := c.Open(context.Background(), 20, claims)
if err == nil && !w2.AcceptsEnded {
t.Fatal("accepts_ended of a not-granted answer was not read")
}
if err != nil || w2.Granted || len(w2.EndedAllowed) != 0 {
t.Fatalf("not granted: %+v %v — an agreement without a window must not reach the box", w2, err)
}
// The close names the removed ids for the hub's audit.
answer = `{"closed":true}`
if err := c.Close(context.Background(), backup.OffsiteWindowResult{ID: 9, CountBefore: 20, CountAfter: 18, Removed: 2, Outcome: "pruned", RemovedForEndedApps: []string{"aa", "bb"}}); err != nil {
t.Fatal(err)
}
var removed []string
if err := json.Unmarshal(gotBody["removed_for_ended_apps"], &removed); err != nil || strings.Join(removed, ",") != "aa,bb" || gotPath != "/api/v1/offsite/window-close/c1" {
t.Fatalf("close: path=%q removed_for_ended_apps=%s", gotPath, gotBody["removed_for_ended_apps"])
}
}