package stacks import ( "io" "log" "os" "path/filepath" "regexp" "sort" "strings" "testing" "time" "gitea.dooplex.hu/admin/felhom-controller/internal/settings" ) // Review of 2026-11-15, finding 3: an IMPORT installed the app and its off-site end record survived — only // DeployStack deleted it. The rule „an install of the same app deletes the record” (`07` §8.2f) now sits at ONE // point every install passes: SaveAppConfig, the only writer of an installed app's app.yaml. A test per path, and a // source pin that lists the writers so a new one cannot appear unseen. // RED-PROOF: felhom.eu/documentation/audits/build-release-4-2026-11-15/A/redproof-review-3.txt. func endRecordManager(t *testing.T) (*Manager, *settings.Settings, string) { t.Helper() m := gateManager(t, gatedYml) sett, err := settings.Load(filepath.Join(t.TempDir(), "settings.json"), log.New(io.Discard, "", 0)) if err != nil { t.Fatal(err) } m.SetMigrationDeps(sett, func() bool { return false }) if _, _, err := sett.StartOffsiteEnd("gapp", time.Now().Add(-29*24*time.Hour)); err != nil { t.Fatal(err) } if _, _, err := sett.StartOffsiteEnd("otherapp", time.Now().Add(-29*24*time.Hour)); err != nil { t.Fatal(err) } m.mu.Lock() dir := filepath.Dir(m.stacks["gapp"].ComposePath) m.mu.Unlock() return m, sett, dir } func assertEndRecordGone(t *testing.T, sett *settings.Settings, how string) { t.Helper() if rec, ok := sett.GetOffsiteEnd("gapp"); ok { t.Fatalf("%s: the app is installed again and its off-site end record survives (%+v) — the window would delete a living app's history", how, rec) } if _, ok := sett.GetOffsiteEnd("otherapp"); !ok { t.Fatalf("%s: another app's record was deleted", how) } } // PATH: a `.fab` import. func TestD72_AnImportDeletesTheEndRecord(t *testing.T) { m, sett, dir := endRecordManager(t) if _, err := m.SaveImportedAppConfig(dir, map[string]string{}); err != nil { t.Fatal(err) } assertEndRecordGone(t, sett, "import") } // PATH: a restore that installs the app again from its recovery unit („use my kept data”, the off-site reinstall). func TestD72_ARestoreIntoAnInstallDeletesTheEndRecord(t *testing.T) { m, sett, _ := endRecordManager(t) if err := m.PersistUnitRedeployConfig("gapp", map[string]string{"DOMAIN": "example.hu", "SUBDOMAIN": "gapp"}); err != nil { t.Fatal(err) } assertEndRecordGone(t, sett, "unit restore") } // PATH: the data move's record write (it sets Deployed on an app.yaml it loaded). func TestD72_ADriveFlipRecordWriteDeletesTheEndRecord(t *testing.T) { m, sett, dir := endRecordManager(t) if err := os.WriteFile(filepath.Join(dir, "app.yaml"), []byte("deployed: false\nenv: {}\n"), 0o600); err != nil { t.Fatal(err) } if err := m.persistDriveFlip("gapp", "/mnt/felhom-drives/x"); err != nil { t.Fatal(err) } assertEndRecordGone(t, sett, "drive flip") } // THE ONE POINT: any write of an app.yaml that says „deployed” — whoever makes it — deletes the record; a write that // says „not deployed” (the install's transitional record, a removal's) does not. func TestD72_SaveAppConfigIsTheOnePoint(t *testing.T) { m, sett, dir := endRecordManager(t) if err := SaveAppConfig(dir, &AppConfig{Deployed: false, Env: map[string]string{}}, m.encKey, nil); err != nil { t.Fatal(err) } if _, ok := sett.GetOffsiteEnd("gapp"); !ok { t.Fatal("a „not deployed” write deleted the end record") } if err := SaveAppConfig(dir, &AppConfig{Deployed: true, Env: map[string]string{}}, m.encKey, nil); err != nil { t.Fatal(err) } assertEndRecordGone(t, sett, "SaveAppConfig(Deployed: true)") // a second manager (another stacks directory) is not touched by this one's writes m2, sett2, _ := endRecordManager(t) if err := SaveAppConfig(dir, &AppConfig{Deployed: true, Env: map[string]string{}}, m.encKey, nil); err != nil { t.Fatal(err) } if _, ok := sett2.GetOffsiteEnd("gapp"); !ok { t.Fatal("a write in ANOTHER stacks directory deleted this box's record") } _ = m2 } // THE SOURCE PIN: who can make an app „installed”. Every writer of an app's app.yaml and every place that sets // Deployed is listed here. A new one fails this test: route it through SaveAppConfig (which deletes the off-site end // record) or say here why it is not an install. func TestD72_TheInstallWritersAreListed(t *testing.T) { root := filepath.Join("..", "..") find := func(re *regexp.Regexp) []string { var out []string for _, top := range []string{"internal", "cmd"} { _ = filepath.Walk(filepath.Join(root, top), func(p string, fi os.FileInfo, err error) error { if err != nil || fi.IsDir() || !strings.HasSuffix(p, ".go") || strings.HasSuffix(p, "_test.go") { return nil } raw, rerr := os.ReadFile(p) if rerr != nil { t.Fatal(rerr) } if n := len(re.FindAll(raw, -1)); n > 0 { rel, _ := filepath.Rel(root, p) out = append(out, filepath.ToSlash(rel)) } return nil }) } sort.Strings(out) return out } for _, c := range []struct { what string re string want []string }{ {"a struct literal that says Deployed: true", `\bDeployed:\s*true\b`, []string{ "internal/backup/recovery_unit.go", // the stripped copy INSIDE a recovery unit — not the app's own app.yaml "internal/stacks/deploy.go", // DeployStack, PersistUnitRedeployConfigGate → SaveAppConfig "internal/stacks/import_record.go", // SaveImportedAppConfig → SaveAppConfig "internal/stacks/install_interrupted.go", // a comment only: it reads the record, it writes none }}, {"an assignment cfg.Deployed = true", `\bcfg\.Deployed = true\b`, []string{ "internal/stacks/migrate.go", // persistDriveFlip → SaveAppConfig }}, {"a direct write of an app.yaml", `WriteFile\([^\n]*"app\.yaml"`, []string{ "internal/backup/offbox_reconstitute.go", // `install_id` only, into a scratch folder — never „deployed” }}, {"the app.yaml writer itself", `filepath\.Join\(stackDir, "app\.yaml"\)\s*\n\s*header`, []string{ "internal/stacks/deploy.go", // SaveAppConfig }}, } { if got := find(regexp.MustCompile(c.re)); strings.Join(got, ",") != strings.Join(c.want, ",") { t.Errorf("%s:\n found %v\n listed %v\nA new writer must go through SaveAppConfig, or be listed here with the reason it is not an install.", c.what, got, c.want) } } // and SaveAppConfig really is the point raw, err := os.ReadFile("deploy.go") if err != nil { t.Fatal(err) } if !regexp.MustCompile(`(?s)func SaveAppConfig\(.*?noteAppInstalled\(stackDir\)`).Match(raw) { t.Error("SaveAppConfig does not call noteAppInstalled — no install path deletes the off-site end record") } } // THE LIVING CHECK, by behaviour (main.go wires exactly this method into the clean-up window): an installed app, an // app being installed, a protected stack and an installed app the manager does NOT list are all living; a catalog // app that is not installed and an app that left the box are not. func TestD72_IsLivingApp(t *testing.T) { m := gateManager(t, gatedYml) m.cfg.Stacks.Protected = []string{"traefik"} set := func(fn func(s *Stack)) { m.mu.Lock() fn(m.stacks["gapp"]) m.mu.Unlock() } if m.IsLivingApp("gapp") { t.Fatal("a catalog app that is NOT installed reads as living — its end could never be carried out") } if m.IsLivingApp("left-the-box") { t.Fatal("an app with no template and no record reads as living") } for _, c := range []struct { name string prep func() app string }{ {"installed", func() { set(func(s *Stack) { s.Deployed = true }) }, "gapp"}, {"being installed (Deploying)", func() { set(func(s *Stack) { s.Deployed, s.Deploying = false, true }) }, "gapp"}, {"marked protected in the list", func() { set(func(s *Stack) { s.Deploying, s.Protected = false, true }) }, "gapp"}, {"a protected stack by the config, not in the list", func() { set(func(s *Stack) { s.Protected = false }) }, "traefik"}, {"always-protected samba", func() {}, "samba"}, {"the empty name", func() {}, ""}, {"a path, not a name", func() {}, "../gapp"}, } { c.prep() if !m.IsLivingApp(c.app) { t.Errorf("%s: %q does not read as living", c.name, c.app) } } set(func(s *Stack) { s.Deployed, s.Deploying, s.Protected = false, false, false }) if m.IsLivingApp("gapp") { t.Fatal("fixture: gapp must be not-installed again") } // An installed app the manager does NOT list (GetStack finds nothing): its record on disk says it is installed. orphan := filepath.Join(m.cfg.Paths.StacksDir, "orphanapp") if err := os.MkdirAll(orphan, 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(orphan, "app.yaml"), []byte("deployed: true\nenv: {}\n"), 0o600); err != nil { t.Fatal(err) } if _, listed := m.GetStack("orphanapp"); listed { t.Fatal("fixture: orphanapp must not be in the manager's list") } if !m.IsLivingApp("orphanapp") { t.Fatal("an INSTALLED app the manager does not list reads as removed — the window could end its copies") } if err := os.WriteFile(filepath.Join(orphan, "app.yaml"), []byte("deployed: false\nenv: {}\n"), 0o600); err != nil { t.Fatal(err) } if m.IsLivingApp("orphanapp") { t.Fatal("a record that says not deployed reads as living") } }