Files
mesh-catalog/modules/restic/cmd/restic-backups/retention_test.go
T
jschoubben d649309895
mesh/merge-gate pass: builds restic → ace, g14, novox, shanks; no bus step; every machine composes with the change as it did without (4 of 4 compose)
mesh/repo-check pass: its merge-check.sh passed, each part in its toolchain: merge-check.sh (go), merge-check-typescript.sh (typescript)
mesh/delivery superseded: a newer head of the same pull request
restic: raise a failed backup, a failed thinning and a refused retention as the module's health (novox/hq issue 454)
A failed night was a journal line only, so a typo in a retention setting
let the repository grow until a disk filled. The night and the measuring
now run as processes the node-engine judges: the night by backup_health,
which is unhealthy for a module not backed up, a night that did not run,
a failed thinning or a missing retention file, and waits naming the
setting when one is refused; the measuring by its unit. A file lock
keeps a night and the tool runtime's now and restore apart.

The retention file is root's alone, so the account reads it through sudo:
until now every night would have refused it as unreadable. Every machine
has the file, so a missing one is refused instead of thinned by 14/8/6.
backed-up says the retention in force and the last thinning per module.
2026-10-11 11:56:23 +02:00

178 lines
6.8 KiB
Go

package main
// The retention is a preference (novox/hq issue 416, ADR 0262): the mesh writes the keep-daily,
// keep-weekly and keep-monthly settings into a file in this module's state, and a night thins the
// restore points by what that file says. A value that is not a count thins nothing.
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"time"
)
// thinning runs one night over a fake restic and answers the forget line it ran ("" when none) and
// what the module said.
func thinning(t *testing.T, retention *string) (string, []string) {
t.Helper()
where := placed(t, "# pg\npath /\n")
path := filepath.Join(where.State, RetentionFile)
if retention != nil {
must(t, os.WriteFile(path, []byte(*retention), 0o600))
} else {
must(t, os.Remove(path))
}
forget := ""
run := func(_ context.Context, name string, args ...string) (string, error) {
if name == "restic" && args[5] == "forget" {
forget = "restic " + strings.Join(args[5:], " ")
}
if name == "restic" && args[5] == "backup" {
return "{\"message_type\":\"summary\",\"snapshot_id\":\"abcdef0123456789\"}\n", nil
}
return "", nil
}
var said []string
say := func(format string, args ...any) { said = append(said, fmt.Sprintf(format, args...)) }
b := &Backups{Where: where, Run: run, Now: func() time.Time { return time.Date(2026, 10, 11, 3, 0, 0, 0, time.UTC) }, Say: say}
outcome, err := b.BackUp(context.Background(), "")
must(t, err)
if !outcome["pg"].OK {
t.Fatalf("the night itself failed: %+v", outcome["pg"])
}
return forget, said
}
func TestANightThinsByTheRetentionTheMeshWrote(t *testing.T) {
conf := "# Written by the mesh.\ndaily=7\nweekly=4\nmonthly=24\n"
got, _ := thinning(t, &conf)
if want := "restic forget --prune --group-by host,tags --keep-daily 7 --keep-weekly 4 --keep-monthly 24"; got != want {
t.Errorf("ran %q, want %q", got, want)
}
}
// Every machine has had the file since mesh-catalog #200 rolled out (novox/hq issue 454), so a missing one is
// a fault, never a reason to thin by 14/8/6: a lost file would otherwise thin by a number nobody set.
func TestWithoutTheFileANightThinsNothingAndSaysTheFileIsMissing(t *testing.T) {
got, said := thinning(t, nil)
if got != "" {
t.Errorf("ran %q without a retention file", got)
}
all := strings.Join(said, "\n")
if !strings.Contains(all, "retention.conf is missing") || !strings.Contains(all, "every restore point is kept") {
t.Errorf("said %q, want it to say the file is missing and every restore point is kept", all)
}
}
// The mesh writes the file as root's alone (a trusted file, novox/hq issue 339), and the night runs as the
// operator account: what the account cannot read is read as root, as every other root's file here is
// (novox/hq issue 454). Before, every machine's night refused it as unreadable and thinned nothing.
func TestARetentionOnlyRootMayReadIsReadAsRoot(t *testing.T) {
where := placed(t, "# pg\npath /\n")
path := filepath.Join(where.State, RetentionFile)
must(t, os.WriteFile(path, []byte("daily=7\nweekly=4\nmonthly=24\n"), 0o600))
must(t, os.Chmod(path, 0))
if os.Getuid() == 0 {
t.Skip("root reads a file of mode 0 itself; nothing to escalate")
}
forget, catted := "", ""
run := func(_ context.Context, name string, args ...string) (string, error) {
switch {
case name == "cat":
catted = strings.Join(args, " ")
return "daily=7\nweekly=4\nmonthly=24\n", nil
case name == "restic" && args[5] == "forget":
forget = "restic " + strings.Join(args[5:], " ")
case name == "restic" && args[5] == "backup":
return "{\"message_type\":\"summary\",\"snapshot_id\":\"abcdef0123456789\"}\n", nil
}
return "", nil
}
b := &Backups{Where: where, Run: run, Now: time.Now, Say: quiet}
_, err := b.BackUp(context.Background(), "")
must(t, err)
if catted != "-- "+path {
t.Errorf("read %q as root, want %q", catted, "-- "+path)
}
if want := "restic forget --prune --group-by host,tags --keep-daily 7 --keep-weekly 4 --keep-monthly 24"; forget != want {
t.Errorf("ran %q, want %q", forget, want)
}
}
// A value that is not a count from 1 to 9999 is never handed to restic: nothing is thinned, every
// restore point is kept, and the module says which setting is wrong (issue 346: a raw setting is an
// injection point).
func TestARetentionThatIsNotACountThinsNothingAndSaysWhich(t *testing.T) {
for _, c := range []struct{ conf, names string }{
{"daily=0\nweekly=8\nmonthly=6\n", "keep-daily"},
{"daily=14\nweekly=-1\nmonthly=6\n", "keep-weekly"},
{"daily=14\nweekly=8\nmonthly=6 --keep-last 1\n", "keep-monthly"},
{"daily=14\nweekly=8\nmonthly=10000\n", "keep-monthly"},
{"daily=fourteen\nweekly=8\nmonthly=6\n", "keep-daily"},
{"daily=\nweekly=8\nmonthly=6\n", "keep-daily"},
{"daily=014\nweekly=8\nmonthly=6\n", "keep-daily"},
{"weekly=8\nmonthly=6\n", "keep-daily"},
{"daily=14\nweekly=8\nmonthly=6\nlast=1\n", "last"},
{"daily=14\ndaily=3\nweekly=8\nmonthly=6\n", "daily"},
{"daily 14\nweekly=8\nmonthly=6\n", "key=value"},
} {
got, said := thinning(t, &c.conf)
if got != "" {
t.Errorf("%q: ran %q", c.conf, got)
}
all := strings.Join(said, "\n")
if !strings.Contains(all, c.names) || !strings.Contains(all, "every restore point is kept") {
t.Errorf("%q: said %q, want it to name %s and say every restore point is kept", c.conf, all, c.names)
}
}
}
// The manifest declares the three preferences with today's values as their defaults, and its file,
// filled with those defaults, reads as today's retention: nothing changes on a machine that sets none.
func TestTheManifestsDefaultsAreTodaysRetention(t *testing.T) {
raw, err := os.ReadFile("../../module.json")
must(t, err)
var m struct {
Settings map[string]struct {
Kind string `json:"kind"`
Default any `json:"default"`
Why string `json:"why"`
} `json:"settings"`
Resources []map[string]any `json:"resources"`
}
must(t, json.Unmarshal(raw, &m))
want := map[string]float64{"keep-daily": 14, "keep-weekly": 8, "keep-monthly": 6}
for k, v := range want {
s, ok := m.Settings[k]
if !ok || s.Kind != "preference" || s.Default != v || s.Why == "" {
t.Errorf("setting %s: %+v, want a preference defaulting to %v with why", k, s, v)
}
}
content := ""
for _, r := range m.Resources {
if r["id"] == "retention" {
if r["path"] != "${dir:state}/"+RetentionFile {
t.Errorf("the retention file is placed at %v, where this program does not read it", r["path"])
}
content, _ = r["content"].(string)
}
}
if content == "" {
t.Fatal("no retention resource in the manifest")
}
filled := regexp.MustCompile(`\$\{setting:([a-z-]+)\}`).ReplaceAllStringFunc(content, func(p string) string {
k := p[len("${setting:") : len(p)-1]
return fmt.Sprint(m.Settings[k].Default)
})
got, err := ParseRetention(filled)
must(t, err)
if got != TodaysRetention {
t.Errorf("the defaults read as %+v, today is %+v", got, TodaysRetention)
}
}