The off-site copies of a removed app end 30 days after „delete backup data” — the box's half (felhom.eu R-1374 part 1; D72, D84)
gates / gates (push) Successful in 41s
gates / gates (push) Successful in 41s
Built, not released; no box has run it. felhom.eu `07-backup-architecture.md` §8.2f.
It does nothing without a hub that knows it: an older hub agrees to no id.
- The record: settings `offsite_ends` {app, removed_at, ends_at}. One writer
(backup.StartOffsiteEnd), called by a removal only with „delete backup data”
and by „Törlés 30 nap múlva” on the restore page. A second press changes
nothing. No record, no deletion. Only on the hub-provisioned tier.
- The date is on the remove dialog before the press; the restore page shows
„Törlésre kijelölve …” with „Mégis megtartom”, and one history line per
carried-out end. An install of the same app deletes the record.
- The weekly window: after the guard has passed the policy's plan under the cap
as before, it adds the snapshots whose app tags are ALL due records, that the
hub agreed to and the box named in the same request; the result names them
apart (removed_for_ended_apps). `_shares`, the box's own stacks and an
installed app are never due.
- The report carries the pending records; the household gets one mail per
press (offsite_end_scheduled), in both languages.
MinAgent unchanged (0.158.1). 143 parity fixtures gained the dialog's one new
script line (patched, not regenerated); one new parity case.
Tests TestD72_*, TestD84_*; 56 mutations seen red
(felhom.eu documentation/audits/build-release-4-2026-11-15/A/redproof-ctrl-*.txt).
Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
This commit is contained in:
1 parent
591195fadd
commit
f628cb5082
177 files changed
+3705
-164
No files matched your search
@@ -127,6 +127,11 @@ type Manager struct {
|
||||
offsiteMoveAside func(ctx context.Context) (string, error)
|
||||
// offsiteWindow (v0.289.0, decision 68) asks the hub for a clean-up window. nil → no box retention.
|
||||
offsiteWindow OffsiteWindowClient
|
||||
// offsiteEndLiving (D72): "installed now, or a protected stack" — nil → no ended app is ever due (offbox_ended.go).
|
||||
offsiteEndLiving func(app string) bool
|
||||
// offsiteEndNotify (D84) tells the household ONCE per press that an app's off-site copies are marked for
|
||||
// deletion. Wired in main.go to the notifier; nil-safe. Called only when a record was really written.
|
||||
offsiteEndNotify func(rec settings.OffsiteEnd)
|
||||
// offsiteAbandon (v0.290.0, decision 74) hands a due set-aside deletion to the hub.
|
||||
offsiteAbandon OffsiteAbandonClient
|
||||
|
||||
|
||||
@@ -1680,9 +1680,14 @@ type OffboxReportStatus struct {
|
||||
// hub cannot see that and must not infer it. It keeps being sent until the ACK stops reporting a
|
||||
// superseded package, so a lost request retries by itself rather than leaving the pair half-gone.
|
||||
// Absent/false on every other box, so a healthy report is byte-identical to v0.205.0's.
|
||||
AbandonPurgeRequested bool `json:"abandon_purge_requested,omitempty"`
|
||||
LastRun string `json:"last_run,omitempty"` // RFC3339
|
||||
LastStatus string `json:"last_status,omitempty"` // "ok" | "incomplete" (R-203) | "error" | "running"
|
||||
// EndedApps (D72/D84, `07` §8.2f part 4) — the pending off-site end records: apps the household removed with
|
||||
// „delete backup data”, whose off-site copies end 30 days later. The hub starts ITS OWN clock from its own
|
||||
// records (and, for an app it never saw installed, from the first report that names it here); the dates in
|
||||
// this list decide nothing there. Absent when there is none, so a report without one is byte-identical.
|
||||
EndedApps []OffsiteEndedApp `json:"ended_apps,omitempty"`
|
||||
AbandonPurgeRequested bool `json:"abandon_purge_requested,omitempty"`
|
||||
LastRun string `json:"last_run,omitempty"` // RFC3339
|
||||
LastStatus string `json:"last_status,omitempty"` // "ok" | "incomplete" (R-203) | "error" | "running"
|
||||
// LastSuccess (R-100) is the last run that SUCCEEDED — the hub's staleness anchor. Absent on a
|
||||
// pre-v0.181.0 controller, which the hub must degrade on explicitly rather than by accident:
|
||||
// treating absence as failure alarms every un-upgraded box, treating it as success keeps the bug.
|
||||
@@ -1944,7 +1949,8 @@ func (m *Manager) OffboxReportStatus() *OffboxReportStatus {
|
||||
LastProofSnapshot: t.LastProofSnapshot,
|
||||
LastProofResult: t.LastProofResult,
|
||||
LastProofReason: t.LastProofReason,
|
||||
AbandonPurgeRequested: t.AbandonPurgeRequested, // R-241: declared until the hub drops the package
|
||||
AbandonPurgeRequested: t.AbandonPurgeRequested, // R-241: declared until the hub drops the package
|
||||
EndedApps: m.OffsiteEndedAppsForReport(), // D72: the pending off-site end records
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,257 @@
|
||||
package backup
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"gitea.dooplex.hu/admin/felhom-controller/internal/settings"
|
||||
)
|
||||
|
||||
// D72 / D84 (`09` §3 decisions 292 and 309; `07-backup-architecture.md` §8.2f, parts 3 and 4) — THE OFF-SITE COPIES OF
|
||||
// A REMOVED APP END 30 DAYS AFTER „DELETE BACKUP DATA”, IN THE WEEKLY WINDOW AND NOWHERE ELSE.
|
||||
//
|
||||
// What this file adds to the clean-up window (offbox_window.go) and what it must never do:
|
||||
//
|
||||
// - NO RECORD, NO DELETION. Only an off-site end record (settings.OffsiteEnd, written by the household's press)
|
||||
// that is DUE makes a snapshot a candidate. A removal that kept the backups, a rebuilt guest, an app removed
|
||||
// before this existed: there is no record, so there is no candidate.
|
||||
// - A LIVING APP'S COPY IS NEVER REMOVED BY ANOTHER APP'S END. A snapshot is a candidate only when EVERY app tag
|
||||
// it carries belongs to a due record — a snapshot with two apps' tags stays until both have ended. The shares
|
||||
// pseudo-app (`_shares`), the infrastructure stacks and an app that is installed right now are never due.
|
||||
// - THE HUB AGREES FIRST. The candidates go to the hub with the window request; the box removes ONLY the ids the
|
||||
// hub sends back (OffsiteWindow.EndedAllowed) — and of those, only ids it named itself in this same window.
|
||||
// A hub that does not know this (an older hub) sends none, and then nothing of an ended app is removed.
|
||||
// - ONE GUARD, NO SECOND PATH. The fake-snapshot guard runs first, on the whole repository and on the policy's own
|
||||
// plan under the window's cap, exactly as before. If it refuses, the ended apps wait with everything else.
|
||||
// The ended ids are OUTSIDE the cap (D84) and are named apart in the result (removed_for_ended_apps), so the
|
||||
// hub's 07:10 audit can explain them.
|
||||
|
||||
// OffsiteEndedClaim is one due end record as the box sends it with the window request: the app, the record's own
|
||||
// dates (the hub does not decide by them) and the snapshot ids the box would remove.
|
||||
type OffsiteEndedClaim struct {
|
||||
App string `json:"app"`
|
||||
RemovedAt string `json:"removed_at"`
|
||||
EndsAt string `json:"ends_at"`
|
||||
SnapshotIDs []string `json:"snapshot_ids"`
|
||||
}
|
||||
|
||||
// OffsiteEndedRefusal is one refusal the hub sent back, in its own words (logged).
|
||||
type OffsiteEndedRefusal struct {
|
||||
App string `json:"app"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
// OffsiteEndedApp is one pending end record on the hub report (`offsite.ended_apps`): it lets the hub start its own
|
||||
// clock for an app it never saw installed (`07` §8.2f part 4). The dates are the box's; the hub decides by its own.
|
||||
type OffsiteEndedApp struct {
|
||||
App string `json:"app"`
|
||||
RemovedAt string `json:"removed_at"`
|
||||
EndsAt string `json:"ends_at"`
|
||||
}
|
||||
|
||||
// SetOffsiteEndLiving wires "is this app installed on this box right now, or one of the box's own protected stacks".
|
||||
// An app for which it answers true is never due. NOT WIRED (nil) = NOTHING IS DUE: a box that cannot tell what it
|
||||
// runs must not delete any app's history. Wired in cmd/controller/main.go; pinned by TestD72_MainWiresTheLivingCheck.
|
||||
func (m *Manager) SetOffsiteEndLiving(fn func(app string) bool) { m.offsiteEndLiving = fn }
|
||||
|
||||
// appTags returns a snapshot's app tags: every tag but the tier's marker.
|
||||
func appTags(s guardSnap) []string {
|
||||
var out []string
|
||||
for _, t := range s.Tags {
|
||||
if t != "" && t != OffboxMarkerTag {
|
||||
out = append(out, t)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// dueOffsiteEnds is the PURE first half: the records that may be claimed now. living == nil → none.
|
||||
func dueOffsiteEnds(ends []settings.OffsiteEnd, now time.Time, living func(string) bool) map[string]settings.OffsiteEnd {
|
||||
due := map[string]settings.OffsiteEnd{}
|
||||
if living == nil {
|
||||
return due
|
||||
}
|
||||
for _, e := range ends {
|
||||
if !settings.ValidOffsiteEndApp(e.App) || e.App == SharesPseudoStack || !e.Due(now) || living(e.App) {
|
||||
continue
|
||||
}
|
||||
due[e.App] = e
|
||||
}
|
||||
return due
|
||||
}
|
||||
|
||||
// endedCandidates is the PURE second half: from the repository's snapshots and the due records, the claims to send
|
||||
// (sorted by app; an app with no candidate is not claimed) and the set of candidate ids.
|
||||
//
|
||||
// A snapshot is a candidate only when it carries at least one app tag, EVERY app tag it carries is a due record,
|
||||
// and it is not younger than any of those records' removal (a snapshot made under that tag AFTER the removal is not
|
||||
// the removed app's copy — a later install's, or a planted one).
|
||||
func endedCandidates(snaps []guardSnap, due map[string]settings.OffsiteEnd) (claims []OffsiteEndedClaim, candidate map[string]bool) {
|
||||
candidate = map[string]bool{}
|
||||
byApp := map[string][]string{}
|
||||
for _, s := range snaps {
|
||||
apps := appTags(s)
|
||||
if len(apps) == 0 || s.ID == "" {
|
||||
continue
|
||||
}
|
||||
ok := true
|
||||
for _, a := range apps {
|
||||
e, isDue := due[a]
|
||||
if !isDue || s.Time.After(e.Removed().Add(offsiteGuardSkew)) {
|
||||
ok = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
candidate[s.ID] = true
|
||||
for _, a := range apps {
|
||||
byApp[a] = append(byApp[a], s.ID)
|
||||
}
|
||||
}
|
||||
for app, ids := range byApp {
|
||||
sort.Strings(ids)
|
||||
e := due[app]
|
||||
claims = append(claims, OffsiteEndedClaim{App: app, RemovedAt: e.RemovedAt, EndsAt: e.EndsAt, SnapshotIDs: ids})
|
||||
}
|
||||
sort.Slice(claims, func(i, j int) bool { return claims[i].App < claims[j].App })
|
||||
return claims, candidate
|
||||
}
|
||||
|
||||
// endedToRemove is the PURE third half: of the ids the hub agreed to, those the box itself named as candidates in
|
||||
// this window and that the policy's own plan does not already remove (those stay under the cap). Sorted.
|
||||
func endedToRemove(allowed []string, candidate map[string]bool, policyIDs []string) []string {
|
||||
inPolicy := make(map[string]bool, len(policyIDs))
|
||||
for _, id := range policyIDs {
|
||||
inPolicy[id] = true
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
var out []string
|
||||
for _, id := range allowed {
|
||||
if candidate[id] && !inPolicy[id] && !seen[id] {
|
||||
seen[id] = true
|
||||
out = append(out, id)
|
||||
}
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// appStillInStore reports whether any snapshot still carries app's tag.
|
||||
func appStillInStore(snaps []guardSnap, app string) bool {
|
||||
for _, s := range snaps {
|
||||
for _, t := range s.Tags {
|
||||
if t == app {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// dropEndsWithNoCopies deletes the due records of apps that have NO snapshot in the repository at all: there is
|
||||
// nothing to end, and a record that can never complete would be asked of the hub every week for ever. It writes no
|
||||
// history line (nothing was deleted). Called only after a listing that succeeded.
|
||||
func (m *Manager) dropEndsWithNoCopies(snaps []guardSnap, due map[string]settings.OffsiteEnd) {
|
||||
for app, e := range due {
|
||||
if appStillInStore(snaps, app) {
|
||||
continue
|
||||
}
|
||||
if done, err := m.settings.CompleteOffsiteEnd(app, e.RemovedAt, m.now(), 0); err != nil {
|
||||
m.logger.Printf("[WARN] [offbox] ended app %s: the store holds no copy of it, but its end record could not be closed: %v", app, err)
|
||||
} else if done {
|
||||
m.logger.Printf("[INFO] [offbox] ended app %s: the off-site store holds no copy of it — its end record is closed, nothing was deleted", app)
|
||||
}
|
||||
delete(due, app)
|
||||
}
|
||||
}
|
||||
|
||||
// completeOffsiteEnds closes the record of every app whose copies this window removed: at least one of its claimed
|
||||
// ids is gone and no snapshot carries its tag any more. An app that still has a snapshot (one shared with a living
|
||||
// app, or one the hub did not agree to) keeps its record and is asked again at the next window.
|
||||
func (m *Manager) completeOffsiteEnds(claims []OffsiteEndedClaim, removed []string, after []guardSnap) {
|
||||
gone := make(map[string]bool, len(removed))
|
||||
for _, id := range removed {
|
||||
gone[id] = true
|
||||
}
|
||||
for _, c := range claims {
|
||||
n := 0
|
||||
for _, id := range c.SnapshotIDs {
|
||||
if gone[id] {
|
||||
n++
|
||||
}
|
||||
}
|
||||
if n == 0 || appStillInStore(after, c.App) {
|
||||
continue
|
||||
}
|
||||
if _, err := m.settings.CompleteOffsiteEnd(c.App, c.RemovedAt, m.now(), n); err != nil {
|
||||
m.logger.Printf("[WARN] [offbox] ended app %s: its %d off-site snapshot(s) are removed, but the end record could not be closed: %v", c.App, n, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// OffsiteEndedAppsForReport returns the pending end records for the hub report.
|
||||
func (m *Manager) OffsiteEndedAppsForReport() []OffsiteEndedApp {
|
||||
var out []OffsiteEndedApp
|
||||
for _, e := range m.settings.GetOffsiteEnds() {
|
||||
out = append(out, OffsiteEndedApp{App: e.App, RemovedAt: e.RemovedAt, EndsAt: e.EndsAt})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// ── The household's side: the press, the take-back, the date ────────────────────────────────────────────────────────
|
||||
|
||||
// ErrOffsiteEndNotOffered: this box has no hub-provisioned off-site tier, so there is no weekly window that could
|
||||
// carry the deletion out — and no record is written. (The household's own NAS target is pruned by the box itself and
|
||||
// keeps a removed app's copies, as before: `07` §8.2f.)
|
||||
var ErrOffsiteEndNotOffered = errors.New("no hub-provisioned off-site tier on this box")
|
||||
|
||||
// ErrOffsiteEndLiving: the app is installed right now, is one of the box's own stacks, or the box cannot tell.
|
||||
var ErrOffsiteEndLiving = errors.New("the app is installed or is not an app a household removes")
|
||||
|
||||
// OffsiteEndOffered reports whether a press can start the 30 days here: the box has the hub-provisioned tier
|
||||
// (switched on or not — a tier that is off only makes the deletion wait).
|
||||
func (m *Manager) OffsiteEndOffered() bool {
|
||||
return m != nil && m.settings != nil && m.settings.GetOffboxTarget().Pinned()
|
||||
}
|
||||
|
||||
// SetOffsiteEndNotify wires the household's one mail per press (D84).
|
||||
func (m *Manager) SetOffsiteEndNotify(fn func(rec settings.OffsiteEnd)) { m.offsiteEndNotify = fn }
|
||||
|
||||
// OffsiteEndDateIfPressedNow is the date the remove dialog shows BEFORE the press.
|
||||
func (m *Manager) OffsiteEndDateIfPressedNow() time.Time {
|
||||
return m.now().Add(settings.OffsiteEndAfter)
|
||||
}
|
||||
|
||||
// StartOffsiteEnd is THE press: „delete backup data” at a removal, or „delete in 30 days” on the Backups page. It
|
||||
// writes the end record and tells the household once. A second press for the same app changes nothing and tells
|
||||
// nobody (created == false). The ONLY writer of an end record.
|
||||
func (m *Manager) StartOffsiteEnd(app string) (rec settings.OffsiteEnd, created bool, err error) {
|
||||
if !m.OffsiteEndOffered() {
|
||||
return settings.OffsiteEnd{}, false, ErrOffsiteEndNotOffered
|
||||
}
|
||||
if !settings.ValidOffsiteEndApp(app) || app == SharesPseudoStack || m.offsiteEndLiving == nil || m.offsiteEndLiving(app) {
|
||||
return settings.OffsiteEnd{}, false, ErrOffsiteEndLiving
|
||||
}
|
||||
rec, created, err = m.settings.StartOffsiteEnd(app, m.now())
|
||||
if err != nil || !created {
|
||||
return rec, false, err
|
||||
}
|
||||
if m.offsiteEndNotify != nil {
|
||||
m.offsiteEndNotify(rec)
|
||||
}
|
||||
return rec, true, nil
|
||||
}
|
||||
|
||||
// KeepOffsiteCopies is „keep it after all”: it deletes the end record. Returns whether there was one.
|
||||
func (m *Manager) KeepOffsiteCopies(app string) (bool, error) {
|
||||
return m.settings.CancelOffsiteEnd(app, "the household keeps the copies")
|
||||
}
|
||||
|
||||
// PendingOffsiteEnds and OffsiteEndsHistory are what the Backups page shows.
|
||||
func (m *Manager) PendingOffsiteEnds() []settings.OffsiteEnd { return m.settings.GetOffsiteEnds() }
|
||||
func (m *Manager) OffsiteEndsHistory() []settings.OffsiteEndDone {
|
||||
return m.settings.GetOffsiteEndsDone()
|
||||
}
|
||||
@@ -0,0 +1,570 @@
|
||||
package backup
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.dooplex.hu/admin/felhom-controller/internal/settings"
|
||||
)
|
||||
|
||||
// D72 / D84 (`09` §3 decisions 292 and 309; `07-backup-architecture.md` §8.2f): the off-site copies of a removed app
|
||||
// end 30 days after „delete backup data”, in the weekly window, for ids the hub agreed to. restic is the package's
|
||||
// fake runner (as in offbox_window_test.go); here it keeps state, so a forget really removes what it names.
|
||||
// RED-PROOFS: felhom.eu/documentation/audits/build-release-4-2026-11-15/A/redproof-ctrl-window.txt.
|
||||
|
||||
// endedRunner: `snapshots` lists what is left, `forget --dry-run` gives the policy's plan, a real `forget` removes
|
||||
// the ids it names (and is recorded).
|
||||
type endedRunner struct {
|
||||
snaps []guardSnap
|
||||
plan []guardSnap
|
||||
forgets [][]string
|
||||
}
|
||||
|
||||
func (e *endedRunner) run(_ context.Context, _ []string, args ...string) ([]byte, error) {
|
||||
switch {
|
||||
case contains(args, "forget") && contains(args, "--dry-run"):
|
||||
return planJSON(e.plan), nil
|
||||
case contains(args, "forget"):
|
||||
e.forgets = append(e.forgets, append([]string{}, args...))
|
||||
gone := map[string]bool{}
|
||||
for _, a := range args {
|
||||
gone[a] = true
|
||||
}
|
||||
var left []guardSnap
|
||||
for _, s := range e.snaps {
|
||||
if !gone[s.ID] {
|
||||
left = append(left, s)
|
||||
}
|
||||
}
|
||||
e.snaps = left
|
||||
return nil, nil
|
||||
case contains(args, "snapshots"):
|
||||
return snapJSON(e.snaps), nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// removedIDs are the snapshot ids every real forget named, sorted.
|
||||
func (e *endedRunner) removedIDs() []string {
|
||||
var out []string
|
||||
for _, f := range e.forgets {
|
||||
for _, a := range f {
|
||||
if strings.HasSuffix(a, "-full") {
|
||||
out = append(out, a)
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
|
||||
func tagged(id string, at time.Time, tags ...string) guardSnap {
|
||||
s := snap(id, at)
|
||||
s.Tags = append([]string{OffboxMarkerTag}, tags...)
|
||||
return s
|
||||
}
|
||||
|
||||
func ids(names ...string) []string {
|
||||
out := make([]string, 0, len(names))
|
||||
for _, n := range names {
|
||||
out = append(out, n+"-full")
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// agreeingHub is the fake hub: it grants the window and agrees to exactly the ids in `allow` — or, with allowAll, to
|
||||
// EVERY id in the repository (a hub that would agree to anything: the box must still remove only its own candidates).
|
||||
type agreeingHub struct {
|
||||
fakeWindow
|
||||
allow []string
|
||||
allowAll func() []string
|
||||
refuse []OffsiteEndedRefusal
|
||||
maxRm int
|
||||
}
|
||||
|
||||
func (h *agreeingHub) Open(ctx context.Context, n int, ended []OffsiteEndedClaim) (OffsiteWindow, error) {
|
||||
h.opened++
|
||||
h.claims = ended
|
||||
w := OffsiteWindow{Granted: true, ID: 7, NewestAllowed: time.Now(), MaxRemove: h.maxRm, EndedAllowed: h.allow, EndedRefused: h.refuse}
|
||||
if h.allowAll != nil {
|
||||
w.EndedAllowed = h.allowAll()
|
||||
}
|
||||
return w, nil
|
||||
}
|
||||
|
||||
// endedBox: a pinned box whose living check says `living` apps are installed. The repository, 60 days old:
|
||||
// vikunja ×3 (v1..v3), immich ×2 (i1,i2), one snapshot of BOTH (vi), the shares (s1), kimai ×1 (k1).
|
||||
func endedBox(t *testing.T, living ...string) (*Manager, *settings.Settings, *endedRunner, *agreeingHub) {
|
||||
t.Helper()
|
||||
m, sett := newOffboxManager(t)
|
||||
pinTarget(t, sett)
|
||||
old := time.Now().Add(-60 * 24 * time.Hour)
|
||||
er := &endedRunner{snaps: []guardSnap{
|
||||
tagged("v1", old, "vikunja"), tagged("v2", old.Add(time.Hour), "vikunja"), tagged("v3", old.Add(2*time.Hour), "vikunja"),
|
||||
tagged("i1", old, "immich"), tagged("i2", old.Add(time.Hour), "immich"),
|
||||
tagged("vi", old, "vikunja", "immich"),
|
||||
tagged("s1", old, SharesPseudoStack),
|
||||
tagged("k1", old, "kimai"),
|
||||
}}
|
||||
m.SetOffboxRunner(er.run)
|
||||
hub := &agreeingHub{maxRm: 5}
|
||||
hub.allowAll = func() []string {
|
||||
var all []string
|
||||
for _, s := range er.snaps {
|
||||
all = append(all, s.ID)
|
||||
}
|
||||
return all
|
||||
}
|
||||
m.SetOffsiteWindowClient(hub)
|
||||
live := map[string]bool{}
|
||||
for _, l := range living {
|
||||
live[l] = true
|
||||
}
|
||||
m.SetOffsiteEndLiving(func(app string) bool { return live[app] })
|
||||
return m, sett, er, hub
|
||||
}
|
||||
|
||||
// pressed writes an end record as the household's press `daysAgo` days ago.
|
||||
func pressed(t *testing.T, sett *settings.Settings, app string, daysAgo int) settings.OffsiteEnd {
|
||||
t.Helper()
|
||||
rec, created, err := sett.StartOffsiteEnd(app, time.Now().Add(-time.Duration(daysAgo)*24*time.Hour))
|
||||
if err != nil || !created {
|
||||
t.Fatalf("press for %s: created=%v err=%v", app, created, err)
|
||||
}
|
||||
return rec
|
||||
}
|
||||
|
||||
// MUST-HOLD 1 — NO RECORD, NO DELETION, EVER. The hub here agrees to EVERY id in the repository, and still nothing is
|
||||
// removed: a removal that kept the backups, a rebuilt guest and an app removed before this was built all look the
|
||||
// same on the box — there is no record.
|
||||
func TestD72_NoRecordNoDeletion(t *testing.T) {
|
||||
for _, name := range []string{
|
||||
"removed without „delete backup data”",
|
||||
"a rebuilt guest (its settings are new)",
|
||||
"an app removed before this was built",
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
m, _, er, hub := endedBox(t, "immich")
|
||||
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
|
||||
if hub.opened != 1 || len(hub.claims) != 0 {
|
||||
t.Fatalf("opened=%d claims=%+v — with no record the box must claim nothing", hub.opened, hub.claims)
|
||||
}
|
||||
if got := er.removedIDs(); len(got) != 0 {
|
||||
t.Fatalf("with NO end record the window removed %v", got)
|
||||
}
|
||||
if len(er.snaps) != 8 {
|
||||
t.Fatalf("%d snapshots left of 8", len(er.snaps))
|
||||
}
|
||||
if len(hub.closed) != 1 || len(hub.closed[0].RemovedForEndedApps) != 0 {
|
||||
t.Fatalf("the result names ended removals: %+v", hub.closed)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// MUST-HOLD 3 and 5 — the due app's own snapshots go, by id, in the window; the snapshot it shares with a living
|
||||
// app, the living app's snapshots and the shares stay; the result names them apart; the record becomes history.
|
||||
func TestD72_TheEndedAppsOwnSnapshotsGoAndNothingElse(t *testing.T) {
|
||||
m, sett, er, hub := endedBox(t, "immich")
|
||||
rec := pressed(t, sett, "vikunja", 31)
|
||||
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
|
||||
|
||||
if len(hub.claims) != 1 || hub.claims[0].App != "vikunja" || strings.Join(hub.claims[0].SnapshotIDs, ",") != strings.Join(ids("v1", "v2", "v3"), ",") {
|
||||
t.Fatalf("the claim sent to the hub = %+v, want vikunja with v1,v2,v3 only (not the snapshot shared with immich)", hub.claims)
|
||||
}
|
||||
if hub.claims[0].RemovedAt != rec.RemovedAt || hub.claims[0].EndsAt != rec.EndsAt {
|
||||
t.Fatalf("the claim does not carry the record's dates: %+v vs %+v", hub.claims[0], rec)
|
||||
}
|
||||
if got := er.removedIDs(); strings.Join(got, ",") != strings.Join(ids("v1", "v2", "v3"), ",") {
|
||||
t.Fatalf("removed %v, want exactly vikunja's own three", got)
|
||||
}
|
||||
left := map[string]bool{}
|
||||
for _, s := range er.snaps {
|
||||
left[s.ShortID] = true
|
||||
}
|
||||
for _, keep := range []string{"i1", "i2", "vi", "s1", "k1"} {
|
||||
if !left[keep] {
|
||||
t.Errorf("%s was removed — a living app's copy, the shares, or another app's", keep)
|
||||
}
|
||||
}
|
||||
if len(er.forgets) != 1 || !contains(er.forgets[0], "--prune") {
|
||||
t.Fatalf("want ONE forget … --prune (the window's own call), got %v", er.forgets)
|
||||
}
|
||||
if len(hub.closed) != 1 {
|
||||
t.Fatalf("the window was not closed once: %+v", hub.closed)
|
||||
}
|
||||
r := hub.closed[0]
|
||||
if r.Outcome != "pruned" || r.CountBefore != 8 || r.CountAfter != 5 || strings.Join(r.RemovedForEndedApps, ",") != strings.Join(ids("v1", "v2", "v3"), ",") {
|
||||
t.Fatalf("result = %+v, want pruned 8 -> 5 with removed_for_ended_apps = v1,v2,v3", r)
|
||||
}
|
||||
// vikunja still has the shared snapshot in the store, so its record stays pending (asked again next week).
|
||||
if _, ok := sett.GetOffsiteEnd("vikunja"); !ok {
|
||||
t.Fatal("the record was closed although a snapshot with vikunja's tag is still in the store")
|
||||
}
|
||||
if len(sett.GetOffsiteEndsDone()) != 0 {
|
||||
t.Fatalf("a history line was written while a copy is left: %+v", sett.GetOffsiteEndsDone())
|
||||
}
|
||||
}
|
||||
|
||||
// A snapshot with two apps' tags goes only when BOTH have ended — and then the records become history.
|
||||
func TestD72_ASharedSnapshotGoesWhenBothHaveEnded(t *testing.T) {
|
||||
m, sett, er, hub := endedBox(t) // nothing is installed
|
||||
pressed(t, sett, "vikunja", 40)
|
||||
pressed(t, sett, "immich", 31)
|
||||
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
|
||||
if got := er.removedIDs(); strings.Join(got, ",") != strings.Join(ids("i1", "i2", "v1", "v2", "v3", "vi"), ",") {
|
||||
t.Fatalf("removed %v, want both apps' snapshots and the shared one", got)
|
||||
}
|
||||
if len(er.snaps) != 2 {
|
||||
t.Fatalf("left %d, want the shares and kimai", len(er.snaps))
|
||||
}
|
||||
if n := len(sett.GetOffsiteEnds()); n != 0 {
|
||||
t.Fatalf("%d record(s) still pending after every copy went", n)
|
||||
}
|
||||
h := sett.GetOffsiteEndsDone()
|
||||
if len(h) != 2 {
|
||||
t.Fatalf("history = %+v, want one line per app", h)
|
||||
}
|
||||
got := map[string]int{}
|
||||
for _, d := range h {
|
||||
got[d.App] = d.Snapshots
|
||||
}
|
||||
if got["vikunja"] != 4 || got["immich"] != 3 {
|
||||
t.Fatalf("history counts = %v, want vikunja 4 (three + the shared), immich 3", got)
|
||||
}
|
||||
_ = hub
|
||||
}
|
||||
|
||||
// MUST-HOLD 3 — `_shares` and the box's own stacks are never eligible, even with a record that somehow exists.
|
||||
func TestD72_SharesAndInfraAreNeverEligible(t *testing.T) {
|
||||
now := time.Now()
|
||||
old := now.Add(-60 * 24 * time.Hour)
|
||||
rec := func(app string) settings.OffsiteEnd {
|
||||
return settings.OffsiteEnd{App: app, RemovedAt: now.Add(-40 * 24 * time.Hour).Format(time.RFC3339), EndsAt: now.Add(-10 * 24 * time.Hour).Format(time.RFC3339)}
|
||||
}
|
||||
living := func(app string) bool { return app == "traefik" } // the box's own stacks answer true (main.go: IsProtectedStack)
|
||||
due := dueOffsiteEnds([]settings.OffsiteEnd{rec(SharesPseudoStack), rec(OffboxMarkerTag), rec("traefik"), rec("vikunja")}, now, living)
|
||||
if len(due) != 1 || due["vikunja"].App != "vikunja" {
|
||||
t.Fatalf("due = %v, want vikunja only", due)
|
||||
}
|
||||
claims, cand := endedCandidates([]guardSnap{tagged("s1", old, SharesPseudoStack), tagged("t1", old, "traefik"), tagged("v1", old, "vikunja"),
|
||||
{ID: "u1-full", ShortID: "u1", Time: old, Tags: []string{OffboxMarkerTag}}, {ID: "n1-full", ShortID: "n1", Time: old}}, due)
|
||||
if len(claims) != 1 || len(cand) != 1 || !cand["v1-full"] {
|
||||
t.Fatalf("claims=%+v candidates=%v — the shares, an infra stack, a marker-only and an untagged snapshot are never candidates", claims, cand)
|
||||
}
|
||||
// and no such record can be written by the press
|
||||
m, sett, _, _ := endedBox(t, "traefik")
|
||||
for _, app := range []string{SharesPseudoStack, OffboxMarkerTag, "traefik"} {
|
||||
if _, created, err := m.StartOffsiteEnd(app); err == nil || created {
|
||||
t.Errorf("%q got an end record", app)
|
||||
}
|
||||
}
|
||||
if n := len(sett.GetOffsiteEnds()); n != 0 {
|
||||
t.Fatalf("%d record(s) written", n)
|
||||
}
|
||||
}
|
||||
|
||||
// MUST-HOLD 2, the box's half — THE BOX REMOVES ONLY WHAT THE HUB AGREED TO. An older hub (no answer), a refusal, and
|
||||
// a box whose clock is a month ahead (it asks on day 2; the hub, by its own clock, refuses): nothing of the app goes
|
||||
// and the record stays for the next window.
|
||||
func TestD84_TheBoxRemovesOnlyWhatTheHubAgreedTo(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
allow []string
|
||||
refuse []OffsiteEndedRefusal
|
||||
want []string
|
||||
}{
|
||||
{"a hub that does not know ended apps answers nothing", nil, nil, nil},
|
||||
{"the hub refuses the app (the box's clock is a month ahead: absent 2 days by the hub's clock)", nil,
|
||||
[]OffsiteEndedRefusal{{App: "vikunja", Reason: "the hub has seen the app absent since … — 2 full day(s), 30 needed"}}, nil},
|
||||
{"the hub agrees to two of the three ids", ids("v1", "v3"), nil, ids("v1", "v3")},
|
||||
{"the hub „agrees” to ids the box never named (a living app's, the shares)", ids("i1", "i2", "s1", "vi", "v2"), nil, ids("v2")},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
m, sett, er, hub := endedBox(t, "immich")
|
||||
hub.allowAll, hub.allow, hub.refuse = nil, c.allow, c.refuse
|
||||
pressed(t, sett, "vikunja", 31)
|
||||
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
|
||||
if got := er.removedIDs(); strings.Join(got, ",") != strings.Join(c.want, ",") {
|
||||
t.Fatalf("removed %v, want %v", got, c.want)
|
||||
}
|
||||
if _, ok := sett.GetOffsiteEnd("vikunja"); !ok {
|
||||
t.Fatal("the record is gone although copies of the app are left — the next window could not finish the job")
|
||||
}
|
||||
if got := hub.closed[0].RemovedForEndedApps; strings.Join(got, ",") != strings.Join(c.want, ",") {
|
||||
t.Fatalf("removed_for_ended_apps = %v, want %v", got, c.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// MUST-HOLD 5 — BEHIND THE SAME GUARD, AND THE CAP'S OWN RULE IS NOT TOUCHED. (a) the guard refuses (a future-dated
|
||||
// snapshot): the ended apps wait with everything else. (b) the policy's plan is above the cap: refused as always —
|
||||
// the ended apps do not push it through and do not go themselves. (c) the plan is exactly the cap: it goes, and the
|
||||
// agreed ended ids go BESIDE it — more than the cap in one window.
|
||||
func TestD84_OneGuard_AndTheAllowanceIsBesideTheCap(t *testing.T) {
|
||||
old := time.Now().Add(-60 * 24 * time.Hour)
|
||||
policy := func(n int) []guardSnap { // n old snapshots of the living app the policy wants to remove
|
||||
var out []guardSnap
|
||||
for i := 0; i < n; i++ {
|
||||
out = append(out, tagged("p"+string(rune('a'+i)), old.Add(time.Duration(i)*time.Minute), "immich"))
|
||||
}
|
||||
return out
|
||||
}
|
||||
t.Run("the guard refuses: nothing goes, the ended apps wait", func(t *testing.T) {
|
||||
m, sett, er, hub := endedBox(t, "immich")
|
||||
pressed(t, sett, "vikunja", 31)
|
||||
er.snaps = append(er.snaps, tagged("fut", time.Now().Add(72*time.Hour), "immich"))
|
||||
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
|
||||
if len(er.forgets) != 0 || hub.closed[0].Outcome != "guard-refused" || len(hub.closed[0].RemovedForEndedApps) != 0 {
|
||||
t.Fatalf("forgets=%v result=%+v — a refused guard must delete nothing", er.forgets, hub.closed[0])
|
||||
}
|
||||
if _, ok := sett.GetOffsiteEnd("vikunja"); !ok {
|
||||
t.Fatal("the record was lost at a refused window")
|
||||
}
|
||||
})
|
||||
t.Run("the policy's plan is above the cap: refused, with or without ended apps", func(t *testing.T) {
|
||||
m, sett, er, hub := endedBox(t, "immich")
|
||||
pressed(t, sett, "vikunja", 31)
|
||||
er.plan = policy(6) // cap 5
|
||||
er.snaps = append(er.snaps, er.plan...)
|
||||
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
|
||||
if len(er.forgets) != 0 || hub.closed[0].Outcome != "guard-refused" || !strings.Contains(hub.closed[0].Reason, "more than one week") {
|
||||
t.Fatalf("forgets=%v result=%+v — the cap's own refusal must stand", er.forgets, hub.closed[0])
|
||||
}
|
||||
})
|
||||
t.Run("the plan is exactly the cap: it goes, and the ended ids go beside it", func(t *testing.T) {
|
||||
m, sett, er, hub := endedBox(t, "immich")
|
||||
pressed(t, sett, "vikunja", 31)
|
||||
er.plan = policy(5) // cap 5
|
||||
er.snaps = append(er.snaps, er.plan...)
|
||||
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
|
||||
got := er.removedIDs()
|
||||
if len(got) != 8 {
|
||||
t.Fatalf("removed %d (%v), want the policy's 5 and vikunja's 3", len(got), got)
|
||||
}
|
||||
r := hub.closed[0]
|
||||
if r.Outcome != "pruned" || r.CountBefore-r.CountAfter != 8 || len(r.RemovedForEndedApps) != 3 {
|
||||
t.Fatalf("result = %+v, want a fall of 8 with 3 named for ended apps (so 5 are under the cap)", r)
|
||||
}
|
||||
})
|
||||
t.Run("an ended id the policy already removes stays under the cap and is not named twice", func(t *testing.T) {
|
||||
m, sett, er, hub := endedBox(t, "immich")
|
||||
pressed(t, sett, "vikunja", 31)
|
||||
er.plan = []guardSnap{er.snaps[0]} // v1 is in the policy's own plan
|
||||
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
|
||||
if got := hub.closed[0].RemovedForEndedApps; strings.Join(got, ",") != strings.Join(ids("v2", "v3"), ",") {
|
||||
t.Fatalf("removed_for_ended_apps = %v, want v2,v3 (v1 went under the cap)", got)
|
||||
}
|
||||
if got := er.removedIDs(); len(got) != 3 {
|
||||
t.Fatalf("removed %v", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// WRONG CASE — an ended app that is INSTALLED AGAIN the day before the window. The install deletes the record
|
||||
// (stacks.DeployStack; TestD72_AnInstallDeletesTheEndRecord). And if a record were still there: an installed app is
|
||||
// never due — nothing is claimed, nothing goes.
|
||||
func TestD72_AnAppInstalledAgainIsNeverDue(t *testing.T) {
|
||||
m, sett, er, hub := endedBox(t, "immich", "vikunja") // vikunja is installed again
|
||||
pressed(t, sett, "vikunja", 31)
|
||||
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
|
||||
if len(hub.claims) != 0 || len(er.removedIDs()) != 0 {
|
||||
t.Fatalf("claims=%+v removed=%v — an installed app's history must not be touched", hub.claims, er.removedIDs())
|
||||
}
|
||||
}
|
||||
|
||||
// A box that cannot tell what it runs (the seam is not wired) has nothing due and can write no record.
|
||||
func TestD72_WithoutTheLivingCheckNothingIsDue(t *testing.T) {
|
||||
m, sett, er, hub := endedBox(t)
|
||||
m.SetOffsiteEndLiving(nil)
|
||||
pressed(t, sett, "vikunja", 31)
|
||||
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
|
||||
if len(hub.claims) != 0 || len(er.removedIDs()) != 0 {
|
||||
t.Fatalf("claims=%+v removed=%v", hub.claims, er.removedIDs())
|
||||
}
|
||||
if _, created, err := m.StartOffsiteEnd("kimai"); err == nil || created {
|
||||
t.Fatal("a press wrote a record on a box that cannot tell what is installed")
|
||||
}
|
||||
}
|
||||
|
||||
// A record that is not due yet (29 days) claims nothing; a snapshot made under the app's tag AFTER the removal is
|
||||
// not the removed app's copy and is never a candidate.
|
||||
func TestD72_NotDueYet_AndASnapshotYoungerThanTheRemoval(t *testing.T) {
|
||||
m, sett, er, hub := endedBox(t, "immich")
|
||||
pressed(t, sett, "vikunja", 29)
|
||||
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
|
||||
if len(hub.claims) != 0 || len(er.removedIDs()) != 0 {
|
||||
t.Fatalf("29 days: claims=%+v removed=%v", hub.claims, er.removedIDs())
|
||||
}
|
||||
|
||||
m2, sett2, er2, hub2 := endedBox(t, "immich")
|
||||
pressed(t, sett2, "kimai", 31)
|
||||
er2.snaps = append(er2.snaps, tagged("k2", time.Now().Add(-20*24*time.Hour), "kimai")) // 11 days AFTER the removal
|
||||
m2.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
|
||||
if got := er2.removedIDs(); strings.Join(got, ",") != strings.Join(ids("k1"), ",") {
|
||||
t.Fatalf("removed %v, want k1 only — k2 was made after the removal", got)
|
||||
}
|
||||
if _, ok := sett2.GetOffsiteEnd("kimai"); !ok {
|
||||
t.Fatal("the record closed although a snapshot with the tag is left")
|
||||
}
|
||||
_ = hub2
|
||||
}
|
||||
|
||||
// An app that has NO copy in the store: its due record is closed with no history line and nothing is asked of the hub.
|
||||
func TestD72_ARecordWithNoCopiesIsClosedQuietly(t *testing.T) {
|
||||
m, sett, er, hub := endedBox(t, "immich")
|
||||
pressed(t, sett, "neverbackedup", 31)
|
||||
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
|
||||
if len(hub.claims) != 0 || len(er.removedIDs()) != 0 {
|
||||
t.Fatalf("claims=%+v removed=%v", hub.claims, er.removedIDs())
|
||||
}
|
||||
if _, ok := sett.GetOffsiteEnd("neverbackedup"); ok || len(sett.GetOffsiteEndsDone()) != 0 {
|
||||
t.Fatalf("want the record closed with no history line; pending=%v history=%+v", ok, sett.GetOffsiteEndsDone())
|
||||
}
|
||||
}
|
||||
|
||||
// WRONG CASE — THE OFF-SITE TIER IS SWITCHED OFF AT THE DATE. No run, no window, no call to the store; the record
|
||||
// stays and does nothing. And the household's own NAS target (no hub window) never removes an ended app's copies.
|
||||
func TestD72_TierOffAtTheDate_TheRecordWaits(t *testing.T) {
|
||||
m, sett, er, hub := endedBox(t, "immich")
|
||||
rec := pressed(t, sett, "vikunja", 31)
|
||||
if err := sett.UpdateOffboxStatus(func(o *settings.OffboxTarget) { o.Enabled = false }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = m.RunOffboxBackup(context.Background())
|
||||
if hub.opened != 0 || len(er.forgets) != 0 {
|
||||
t.Fatalf("with the tier OFF the box opened %d window(s) and ran %d forget(s)", hub.opened, len(er.forgets))
|
||||
}
|
||||
if got, ok := sett.GetOffsiteEnd("vikunja"); !ok || got != rec {
|
||||
t.Fatalf("the record changed while the tier was off: %+v ok=%v", got, ok)
|
||||
}
|
||||
|
||||
// The household's own NAS: the box prunes by the policy alone; no id of an ended app is ever named.
|
||||
m2, sett2 := newOffboxManager(t) // Transport "" = the NAS
|
||||
if _, _, err := sett2.StartOffsiteEnd("vikunja", time.Now().Add(-31*24*time.Hour)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
er2 := &endedRunner{snaps: []guardSnap{tagged("v1", time.Now().Add(-60*24*time.Hour), "vikunja")}}
|
||||
m2.SetOffboxRunner(er2.run)
|
||||
m2.SetOffsiteEndLiving(func(string) bool { return false })
|
||||
m2.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
|
||||
if got := er2.removedIDs(); len(got) != 0 {
|
||||
t.Fatalf("on the household's own NAS the box removed %v by id", got)
|
||||
}
|
||||
if _, created, err := m2.StartOffsiteEnd("kimai"); err != ErrOffsiteEndNotOffered || created {
|
||||
t.Fatalf("a press on a box without the hub's tier: created=%v err=%v, want ErrOffsiteEndNotOffered and no record", created, err)
|
||||
}
|
||||
}
|
||||
|
||||
// MUST-HOLD 7 and the wrong case TWO PRESSES — one record, ONE mail; the date does not move. A press for an
|
||||
// installed app writes nothing and mails nobody.
|
||||
func TestD84_OnePressOneMail(t *testing.T) {
|
||||
m, sett, _, _ := endedBox(t, "immich")
|
||||
var mails []settings.OffsiteEnd
|
||||
m.SetOffsiteEndNotify(func(rec settings.OffsiteEnd) { mails = append(mails, rec) })
|
||||
first, created, err := m.StartOffsiteEnd("vikunja")
|
||||
if err != nil || !created {
|
||||
t.Fatalf("first press: created=%v err=%v", created, err)
|
||||
}
|
||||
if first.Ends().Sub(first.Removed()) != settings.OffsiteEndAfter {
|
||||
t.Fatalf("the record's end is not 30 days after the press: %+v", first)
|
||||
}
|
||||
second, created2, err := m.StartOffsiteEnd("vikunja")
|
||||
if err != nil || created2 || second != first {
|
||||
t.Fatalf("second press: created=%v rec=%+v err=%v — want the first record, unchanged", created2, second, err)
|
||||
}
|
||||
if len(mails) != 1 || mails[0] != first {
|
||||
t.Fatalf("two presses gave %d mail(s): %+v — want ONE, for the record", len(mails), mails)
|
||||
}
|
||||
if _, c, err := m.StartOffsiteEnd("immich"); err != ErrOffsiteEndLiving || c {
|
||||
t.Fatalf("a press for an INSTALLED app: created=%v err=%v", c, err)
|
||||
}
|
||||
if len(mails) != 1 || len(sett.GetOffsiteEnds()) != 1 {
|
||||
t.Fatalf("mails=%d records=%d after a refused press", len(mails), len(sett.GetOffsiteEnds()))
|
||||
}
|
||||
// MUST-HOLD 4 — the household takes it back: the record is gone, and the next window claims nothing.
|
||||
if kept, err := m.KeepOffsiteCopies("vikunja"); err != nil || !kept {
|
||||
t.Fatalf("keep: %v %v", kept, err)
|
||||
}
|
||||
if n := len(sett.GetOffsiteEnds()); n != 0 {
|
||||
t.Fatalf("%d record(s) after the take-back", n)
|
||||
}
|
||||
if len(mails) != 1 {
|
||||
t.Fatalf("the take-back sent a mail: %d", len(mails))
|
||||
}
|
||||
}
|
||||
|
||||
// The take-back before the window: with the record deleted, the window removes nothing of the app.
|
||||
func TestD72_AfterTheTakeBackTheWindowRemovesNothing(t *testing.T) {
|
||||
m, sett, er, hub := endedBox(t, "immich")
|
||||
pressed(t, sett, "vikunja", 31)
|
||||
if kept, err := m.KeepOffsiteCopies("vikunja"); err != nil || !kept {
|
||||
t.Fatalf("keep: %v %v", kept, err)
|
||||
}
|
||||
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
|
||||
if len(hub.claims) != 0 || len(er.removedIDs()) != 0 {
|
||||
t.Fatalf("claims=%+v removed=%v after „keep it after all”", hub.claims, er.removedIDs())
|
||||
}
|
||||
}
|
||||
|
||||
// The hub report carries the pending records (so the hub can start its own clock for an app it never saw).
|
||||
func TestD72_TheReportCarriesThePendingRecords(t *testing.T) {
|
||||
m, sett, _, _ := endedBox(t, "immich")
|
||||
if st := m.OffboxReportStatus(); st == nil || len(st.EndedApps) != 0 {
|
||||
t.Fatalf("no record: %+v", st)
|
||||
}
|
||||
rec := pressed(t, sett, "vikunja", 3)
|
||||
st := m.OffboxReportStatus()
|
||||
if st == nil || len(st.EndedApps) != 1 || st.EndedApps[0].App != "vikunja" || st.EndedApps[0].RemovedAt != rec.RemovedAt || st.EndedApps[0].EndsAt != rec.EndsAt {
|
||||
t.Fatalf("report = %+v, want the pending record", st)
|
||||
}
|
||||
}
|
||||
|
||||
// The living check and the mail are WIRED in main.go — without the first nothing is ever due, without the second no
|
||||
// household is told. (A seam nobody wires is a feature that does not exist.)
|
||||
func TestD72_MainWiresTheLivingCheck(t *testing.T) {
|
||||
raw, err := os.ReadFile(filepath.Join("..", "..", "cmd", "controller", "main.go"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
src := string(raw)
|
||||
for _, want := range []string{"backupMgr.SetOffsiteEndLiving(func(app string) bool {", "cfg.IsProtectedStack(app)", "st.Deployed || st.Deploying || st.Protected",
|
||||
"backupMgr.SetOffsiteEndNotify(func(rec settings.OffsiteEnd) {", "notifier.NotifyOffsiteEndScheduled(rec.App, rec.RemovedAt, rec.EndsAt"} {
|
||||
if !strings.Contains(src, want) {
|
||||
t.Errorf("main.go lacks %q", want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The result names for the hub's audit only ids that are REALLY gone: a forget that removed nothing (the provider
|
||||
// refused, the call failed) names none, and the record stays for the next window.
|
||||
func TestD84_TheResultNamesOnlyIdsThatAreReallyGone(t *testing.T) {
|
||||
m, sett, er, hub := endedBox(t, "immich")
|
||||
pressed(t, sett, "kimai", 31)
|
||||
base := er.run
|
||||
m.SetOffboxRunner(func(ctx context.Context, env []string, args ...string) ([]byte, error) {
|
||||
if contains(args, "forget") && !contains(args, "--dry-run") {
|
||||
er.forgets = append(er.forgets, append([]string{}, args...))
|
||||
return []byte("Fatal: unable to remove"), context.DeadlineExceeded // nothing is removed
|
||||
}
|
||||
return base(ctx, env, args...)
|
||||
})
|
||||
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
|
||||
if len(er.forgets) != 1 || len(hub.closed) != 1 {
|
||||
t.Fatalf("forgets=%d closed=%d", len(er.forgets), len(hub.closed))
|
||||
}
|
||||
r := hub.closed[0]
|
||||
if r.Outcome != "error" || len(r.RemovedForEndedApps) != 0 || r.CountAfter != r.CountBefore {
|
||||
t.Fatalf("result = %+v — a failed forget removed nothing, so nothing may be named as removed for ended apps", r)
|
||||
}
|
||||
if _, ok := sett.GetOffsiteEnd("kimai"); !ok || len(sett.GetOffsiteEndsDone()) != 0 {
|
||||
t.Fatal("the record was closed although its copy is still in the store")
|
||||
}
|
||||
}
|
||||
@@ -56,6 +56,10 @@ type OffsiteWindow struct {
|
||||
NewestAllowed time.Time
|
||||
MaxRemove int
|
||||
Reason string // why not granted (logged)
|
||||
// D84 (`09` §3 decision 309): the snapshot ids of ENDED apps the hub agreed to — their own allowance, outside
|
||||
// MaxRemove — and what it refused. Empty from a hub that does not know ended apps: then none is removed.
|
||||
EndedAllowed []string
|
||||
EndedRefused []OffsiteEndedRefusal
|
||||
}
|
||||
|
||||
// OffsiteWindowResult is what the box reports when it is done (the hub closes the window on it).
|
||||
@@ -66,11 +70,16 @@ type OffsiteWindowResult struct {
|
||||
Removed int `json:"removed"`
|
||||
Outcome string `json:"outcome"` // pruned | nothing | guard-refused | error
|
||||
Reason string `json:"reason,omitempty"`
|
||||
// RemovedForEndedApps names the snapshots this window removed for ended apps (D84) — only ids the hub agreed
|
||||
// to, and only those that are really gone. The hub's 07:10 audit explains its vanished files by this list.
|
||||
RemovedForEndedApps []string `json:"removed_for_ended_apps,omitempty"`
|
||||
}
|
||||
|
||||
// OffsiteWindowClient is the hub side (offsiteapply.HubWindowClient in production).
|
||||
type OffsiteWindowClient interface {
|
||||
Open(ctx context.Context, countBefore int) (OffsiteWindow, error)
|
||||
// Open asks for a window. ended are the box's DUE off-site end records with the ids it would remove (D72/D84);
|
||||
// the hub answers which ids it agrees to.
|
||||
Open(ctx context.Context, countBefore int, ended []OffsiteEndedClaim) (OffsiteWindow, error)
|
||||
Close(ctx context.Context, r OffsiteWindowResult) error
|
||||
}
|
||||
|
||||
@@ -225,7 +234,11 @@ func (m *Manager) offsiteWindowRetention(ctx context.Context, base, env []string
|
||||
m.logger.Printf("[WARN] [offbox] retention skipped (%s): %v", why, err)
|
||||
return
|
||||
}
|
||||
w, err := m.offsiteWindow.Open(ctx, len(snaps))
|
||||
// D72/D84: the ended apps' candidates, from the records that are due. No record → no candidate.
|
||||
due := dueOffsiteEnds(m.settings.GetOffsiteEnds(), m.now(), m.offsiteEndLiving)
|
||||
m.dropEndsWithNoCopies(snaps, due)
|
||||
claims, candidate := endedCandidates(snaps, due)
|
||||
w, err := m.offsiteWindow.Open(ctx, len(snaps), claims)
|
||||
if err != nil {
|
||||
m.logger.Printf("[WARN] [offbox] retention skipped (%s): asking the hub for a window failed: %v", why, err)
|
||||
return
|
||||
@@ -255,6 +268,15 @@ func (m *Manager) offsiteWindowRetention(ctx context.Context, base, env []string
|
||||
m.logger.Printf("[ERROR] [offbox] clean-up window %d: the fake-snapshot guard REFUSED — nothing deleted: %s (R-822)", w.ID, refuse)
|
||||
return
|
||||
}
|
||||
for _, r := range w.EndedRefused {
|
||||
m.logger.Printf("[WARN] [offbox] clean-up window %d: the hub REFUSED the ended app %q — nothing of it is removed: %s", w.ID, r.App, r.Reason)
|
||||
}
|
||||
// The ended apps' ids: outside the cap, only what the hub agreed to AND this box named in this window.
|
||||
endedIDs := endedToRemove(w.EndedAllowed, candidate, ids)
|
||||
if len(endedIDs) > 0 {
|
||||
m.logger.Printf("[INFO] [offbox] clean-up window %d: %d snapshot(s) of removed apps are due and agreed by the hub — removed beside the policy's %d (D84)", w.ID, len(endedIDs), len(ids))
|
||||
}
|
||||
ids = append(ids, endedIDs...)
|
||||
if len(ids) == 0 {
|
||||
res.Outcome = "nothing"
|
||||
m.logger.Printf("[INFO] [offbox] clean-up window %d: the policy removes nothing", w.ID)
|
||||
@@ -271,8 +293,19 @@ func (m *Manager) offsiteWindowRetention(ctx context.Context, base, env []string
|
||||
}
|
||||
if after, lerr := m.listGuardSnaps(ctx, base, env); lerr == nil {
|
||||
res.CountAfter = len(after)
|
||||
// Named apart for the hub's audit — only the agreed ids that are REALLY gone from the repository.
|
||||
still := make(map[string]bool, len(after))
|
||||
for _, s := range after {
|
||||
still[s.ID] = true
|
||||
}
|
||||
for _, id := range endedIDs {
|
||||
if !still[id] {
|
||||
res.RemovedForEndedApps = append(res.RemovedForEndedApps, id)
|
||||
}
|
||||
}
|
||||
m.completeOffsiteEnds(claims, res.RemovedForEndedApps, after)
|
||||
}
|
||||
res.Removed = res.CountBefore - res.CountAfter
|
||||
m.logger.Printf("[INFO] [offbox] clean-up window %d (%s): %d of %d planned snapshot(s) removed, %d -> %d, in %s",
|
||||
w.ID, why, res.Removed, len(plan), res.CountBefore, res.CountAfter, time.Since(start).Round(time.Second))
|
||||
m.logger.Printf("[INFO] [offbox] clean-up window %d (%s): %d of %d planned snapshot(s) removed (%d of them for removed apps), %d -> %d, in %s",
|
||||
w.ID, why, res.Removed, len(ids), len(res.RemovedForEndedApps), res.CountBefore, res.CountAfter, time.Since(start).Round(time.Second))
|
||||
}
|
||||
@@ -56,13 +56,15 @@ func (w *windowRunner) run(_ context.Context, _ []string, args ...string) ([]byt
|
||||
}
|
||||
|
||||
type fakeWindow struct {
|
||||
claims []OffsiteEndedClaim
|
||||
grant OffsiteWindow
|
||||
opened int
|
||||
closed []OffsiteWindowResult
|
||||
}
|
||||
|
||||
func (f *fakeWindow) Open(context.Context, int) (OffsiteWindow, error) {
|
||||
func (f *fakeWindow) Open(_ context.Context, _ int, ended []OffsiteEndedClaim) (OffsiteWindow, error) {
|
||||
f.opened++
|
||||
f.claims = ended
|
||||
return f.grant, nil
|
||||
}
|
||||
func (f *fakeWindow) Close(_ context.Context, r OffsiteWindowResult) error {
|
||||
|
||||
Reference in new issue
Block a user