| new file mode 100644 |
| index 000000000..0df9de2e5 |
| --- /dev/null |
| +++ b/internal/host/systemapply_rootfs_test.go |
| @@ -0,0 +1,365 @@ |
| +package host |
| + |
| +// The rootfs rollback floor (rootfs apps Plan 3, Task 15; spec §5.6): a box |
| +// that has rows with a runtime never installs a platform release that |
| +// cannot run them. No t.Parallel(): the floor's tests share the applier's |
| +// seams with the layout floor's. |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "go/ast" |
| + "go/parser" |
| + "go/token" |
| + "os" |
| + "path/filepath" |
| + "slices" |
| + "strconv" |
| + "strings" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// runtimeRow is a nested, always-on row with a runtime, as Plan 2 mints one. |
| +func runtimeRow(dataDir, host, runtime string) registry.Route { |
| + rt := nestedRow(dataDir, host) |
| + rt.Runtime, rt.Hibernate = runtime, false |
| + return rt |
| +} |
| + |
| +// withRootfsUnit gives the fixture's candidate the rootfs instance template, |
| +// which every release from Plan 2 on ships. |
| +func (f *applyFixture) withRootfsUnit() { |
| + staged := filepath.Join(f.a.stagingDir(), "carlos") |
| + f.answer["units:"+staged] += " " + rootfsInstanceUnit |
| +} |
| + |
| +// countOpens wraps the fixture's openRegistry seam and counts its calls. |
| +func (f *applyFixture) countOpens() *int { |
| + n := 0 |
| + open := f.a.openRegistry |
| + f.a.openRegistry = func() (*registry.Registry, error) { n++; return open() } |
| + return &n |
| +} |
| + |
| +// assertUntouched is what a refusal before the journal leaves: no journal, |
| +// the old binary, and systemd never stopped or restarted. |
| +func (f *applyFixture) assertUntouched() { |
| + f.t.Helper() |
| + if j, _ := readSystemJournal(f.a.optDir); j != nil { |
| + f.t.Fatalf("a refused apply wrote a journal: %+v", j) |
| + } |
| + if bin, _ := os.ReadFile(systemBinaryPath(f.a.optDir)); !strings.Contains(string(bin), "carlos v1") { |
| + f.t.Fatal("a refused apply swapped the binary") |
| + } |
| + if f.called("systemctl stop") || f.called("systemctl restart") { |
| + f.t.Fatalf("a refused apply touched systemd: %v", f.calls) |
| + } |
| +} |
| + |
| +func rootfsRefusalFor(version, hosts string) string { |
| + return "refused: " + version + " cannot run rootfs instances, and this box still has: " + hosts + |
| + ". delete those instances before installing it" |
| +} |
| + |
| +// A candidate at or above the floor that does not ship the rootfs template |
| +// cannot run a rootfs row, so the apply refuses it before the journal and |
| +// names the row. The binary row beside it is not named. |
| +func TestApplyRefusesACandidateWithoutRootfsUnits(t *testing.T) { |
| + f, req := applyHappyVersion(t, "v0.29.0") |
| + f.postLayout() |
| + dd := f.a.dataDir |
| + f.seedRoutes(flatRow(dd, "flat.example.com"), runtimeRow(dd, "shop.example.com", registry.RuntimeRootfsV1)) |
| + if err := f.a.Run(context.Background(), false); err != nil { |
| + t.Fatal(err) |
| + } |
| + r := f.result() |
| + if r == nil || r.Outcome != systemOutcomeRefused || !r.matches("stable", "v0.29.0", req.PromotedAt) { |
| + t.Fatalf("result = %+v", r) |
| + } |
| + if want := rootfsRefusalFor("v0.29.0", "shop.example.com"); r.Reason != want { |
| + t.Fatalf("reason = %q\nwant %q", r.Reason, want) |
| + } |
| + f.assertUntouched() |
| +} |
| + |
| +// The same candidates on a box with no row that has a runtime install as |
| +// they do on main: no unit set, version or registry content of a binary-only |
| +// box is a reason. |
| +func TestApplyAllowsWithoutRootfsRows(t *testing.T) { |
| + for _, tc := range []struct { |
| + name, version string |
| + rootfsUnit bool |
| + }{ |
| + {"no rootfs unit", "v0.29.0", false}, |
| + {"Plan 2's release", "v0.28.25", true}, |
| + {"an unparseable version", "v2", true}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + f, req := applyHappyVersion(t, tc.version) |
| + f.postLayout() |
| + if tc.rootfsUnit { |
| + f.withRootfsUnit() |
| + } |
| + dd := f.a.dataDir |
| + f.seedRoutes(flatRow(dd, "flat.example.com"), nestedRow(dd, "acme.example.com")) |
| + if err := f.a.Run(context.Background(), false); err != nil { |
| + t.Fatal(err) |
| + } |
| + if r := f.result(); r == nil || r.Outcome != systemOutcomeApplied || !r.matches("stable", tc.version, req.PromotedAt) { |
| + t.Fatalf("result = %+v, want applied", r) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// A candidate that ships the rootfs template at or above the floor runs |
| +// rootfs rows, so the floor never even opens the registry for it. |
| +func TestApplyAllowsACandidateWithRootfsUnits(t *testing.T) { |
| + for _, version := range []string{"v0.29.0", "v0.30.1", "v1.0.0"} { |
| + t.Run(version, func(t *testing.T) { |
| + f, req := applyHappyVersion(t, version) |
| + f.postLayout() |
| + f.withRootfsUnit() |
| + f.seedRoutes(runtimeRow(f.a.dataDir, "shop.example.com", registry.RuntimeRootfsV1)) |
| + opened := f.countOpens() |
| + if err := f.a.Run(context.Background(), false); err != nil { |
| + t.Fatal(err) |
| + } |
| + if r := f.result(); r == nil || r.Outcome != systemOutcomeApplied || !r.matches("stable", version, req.PromotedAt) { |
| + t.Fatalf("result = %+v, want applied", r) |
| + } |
| + if *opened != 0 { |
| + t.Errorf("the floor opened the registry %d times for a candidate that runs rootfs rows", *opened) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// [Astra R1 10] The unit set alone cannot tell Plan 3's lifecycle from |
| +// Plan 2's: Plan 2's releases ship the rootfs templates without it. So a |
| +// candidate with the templates and a version below the floor is refused |
| +// while a row has a runtime, and so is one whose version does not parse (a |
| +// local build named by a sha), whose unit set says nothing here. A row |
| +// with a runtime this build does not know is named too, and every host is |
| +// named once, sorted. |
| +func TestApplyRefusesPlan2WithRootfsUnits(t *testing.T) { |
| + for _, tc := range []struct { |
| + name, version string |
| + rows func(dd string) []registry.Route |
| + hosts string |
| + }{ |
| + {"Plan 2's release", "v0.28.25", func(dd string) []registry.Route { |
| + return []registry.Route{flatRow(dd, "flat.example.com"), runtimeRow(dd, "shop.example.com", registry.RuntimeRootfsV1)} |
| + }, "shop.example.com"}, |
| + {"just below the floor", "v0.28.99", func(dd string) []registry.Route { |
| + return []registry.Route{runtimeRow(dd, "shop.example.com", registry.RuntimeRootfsV1)} |
| + }, "shop.example.com"}, |
| + {"an unparseable version", "4f3c2a1", func(dd string) []registry.Route { |
| + return []registry.Route{runtimeRow(dd, "shop.example.com", registry.RuntimeRootfsV1)} |
| + }, "shop.example.com"}, |
| + {"a runtime this build does not know", "v0.28.25", func(dd string) []registry.Route { |
| + return []registry.Route{ |
| + runtimeRow(dd, "shop.example.com", registry.RuntimeRootfsV1), |
| + runtimeRow(dd, "bab.example.com", "rootfs-v9"), |
| + nestedRow(dd, "acme.example.com"), |
| + } |
| + }, "bab.example.com, shop.example.com"}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + f, req := applyHappyVersion(t, tc.version) |
| + f.postLayout() |
| + f.withRootfsUnit() |
| + f.seedRoutes(tc.rows(f.a.dataDir)...) |
| + if err := f.a.Run(context.Background(), false); err != nil { |
| + t.Fatal(err) |
| + } |
| + r := f.result() |
| + if r == nil || r.Outcome != systemOutcomeRefused || !r.matches("stable", tc.version, req.PromotedAt) { |
| + t.Fatalf("result = %+v", r) |
| + } |
| + if want := rootfsRefusalFor(tc.version, tc.hosts); r.Reason != want { |
| + t.Fatalf("reason = %q\nwant %q", r.Reason, want) |
| + } |
| + f.assertUntouched() |
| + }) |
| + } |
| +} |
| + |
| +// A registry the floor cannot open cannot vouch that no row has a runtime, |
| +// so its error is the reason and nothing on the box changes. |
| +func TestApplyRefusesWhenTheRegistryWillNotOpen(t *testing.T) { |
| + f, _ := applyHappyVersion(t, "v0.29.0") |
| + f.postLayout() |
| + f.a.openRegistry = func() (*registry.Registry, error) { return nil, errors.New("registry.db: disk I/O error") } |
| + if err := f.a.Run(context.Background(), false); err != nil { |
| + t.Fatal(err) |
| + } |
| + r := f.result() |
| + if r == nil || r.Outcome != systemOutcomeRefused { |
| + t.Fatalf("result = %+v", r) |
| + } |
| + if want := rootfsRefusalFor("v0.29.0", "registry.db: disk I/O error"); r.Reason != want { |
| + t.Fatalf("reason = %q\nwant %q", r.Reason, want) |
| + } |
| + f.assertUntouched() |
| +} |
| + |
| +// rootfsRowsPresent names every row with any non-empty runtime, once and |
| +// sorted, and a registry it cannot list is an error, never an empty box. |
| +func TestRootfsRowsPresentNamesEveryRuntimeRow(t *testing.T) { |
| + dd := t.TempDir() |
| + reg, err := registry.Open(filepath.Join(dd, "registry.db")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, rt := range []registry.Route{ |
| + flatRow(dd, "flat.example.com"), |
| + runtimeRow(dd, "shop.example.com", registry.RuntimeRootfsV1), |
| + runtimeRow(dd, "bab.example.com", "rootfs-v9"), |
| + nestedRow(dd, "acme.example.com"), |
| + } { |
| + if err := reg.Upsert(rt); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + hosts, err := rootfsRowsPresent(reg) |
| + if err != nil || !slices.Equal(hosts, []string{"bab.example.com", "shop.example.com"}) { |
| + t.Fatalf("rootfsRowsPresent = %v, %v", hosts, err) |
| + } |
| + // List reads through a fresh connection on the file, so a file that is |
| + // no longer a database is a registry that cannot be listed. |
| + reg.Close() |
| + path := filepath.Join(dd, "registry.db") |
| + for _, side := range []string{path + "-wal", path + "-shm"} { |
| + if err := os.Remove(side); err != nil && !errors.Is(err, os.ErrNotExist) { |
| + t.Fatal(err) |
| + } |
| + } |
| + if err := os.WriteFile(path, []byte(strings.Repeat("not a database ", 512)), 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + if hosts, err := rootfsRowsPresent(reg); err == nil { |
| + t.Fatalf("an unreadable registry listed as %v", hosts) |
| + } |
| +} |
| + |
| +// [pf 4.2] Root reads SQLite only through the service-user child. The |
| +// census stood in for that rule; this test does now. systemapply.go names |
| +// no registry opener of its own (registry.Open or OpenReadOnly, called or |
| +// passed as a value), and the one caller of rootfsRowsPresent hands it the |
| +// *Registry the openRegistry seam returned, whose default is |
| +// openSystemRegistry. |
| +func TestSystemApplyOpensNoRegistryItself(t *testing.T) { |
| + fset := token.NewFileSet() |
| + f, err := parser.ParseFile(fset, "systemapply.go", nil, 0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if problems := systemApplyRegistryProblems(fset, f); len(problems) > 0 { |
| + t.Fatalf("systemapply.go:\n%s", strings.Join(problems, "\n")) |
| + } |
| + // The guard catches what it looks for. |
| + for name, src := range map[string]string{ |
| + "a direct open": `package host |
| +import "github.com/carlosframework/platform/internal/registry" |
| +func (a *systemApplier) x() { registry.Open("p") } |
| +func (a *systemApplier) y() { open := a.openRegistry; reg, _ := open(); rootfsRowsPresent(reg) }`, |
| + "a renamed import": `package host |
| +import reg "github.com/carlosframework/platform/internal/registry" |
| +var o = reg.OpenReadOnly |
| +func (a *systemApplier) y() { open := a.openRegistry; r, _ := open(); rootfsRowsPresent(r) }`, |
| + "a registry from elsewhere": `package host |
| +func (a *systemApplier) y(r *registry.Registry) { rootfsRowsPresent(r) }`, |
| + "no caller": `package host |
| +func (a *systemApplier) y() {}`, |
| + } { |
| + fx, err := parser.ParseFile(fset, name+".go", src, 0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if len(systemApplyRegistryProblems(fset, fx)) == 0 { |
| + t.Errorf("%s: the guard reported nothing", name) |
| + } |
| + } |
| +} |
| + |
| +// systemApplyRegistryProblems is the guard's reading of one file. |
| +func systemApplyRegistryProblems(fset *token.FileSet, f *ast.File) []string { |
| + const regPath = "github.com/carlosframework/platform/internal/registry" |
| + var problems []string |
| + regName := "" |
| + for _, imp := range f.Imports { |
| + if p, _ := strconv.Unquote(imp.Path.Value); p == regPath { |
| + regName = "registry" |
| + if imp.Name != nil { |
| + regName = imp.Name.Name |
| + } |
| + } |
| + } |
| + ast.Inspect(f, func(n ast.Node) bool { |
| + sel, ok := n.(*ast.SelectorExpr) |
| + if !ok { |
| + return true |
| + } |
| + if x, ok := sel.X.(*ast.Ident); ok && regName != "" && x.Name == regName && strings.HasPrefix(sel.Sel.Name, "Open") { |
| + problems = append(problems, fset.Position(sel.Pos()).String()+": "+regName+"."+sel.Sel.Name) |
| + } |
| + return true |
| + }) |
| + callers := 0 |
| + for _, d := range f.Decls { |
| + fd, ok := d.(*ast.FuncDecl) |
| + if !ok || fd.Body == nil { |
| + continue |
| + } |
| + // Which identifiers hold the seam, and which hold what it returned. |
| + seam, fromSeam := map[string]bool{}, map[string]bool{} |
| + ast.Inspect(fd.Body, func(n ast.Node) bool { |
| + as, ok := n.(*ast.AssignStmt) |
| + if !ok || len(as.Rhs) != 1 { |
| + return true |
| + } |
| + switch rhs := as.Rhs[0].(type) { |
| + case *ast.SelectorExpr: |
| + if rhs.Sel.Name == "openRegistry" { |
| + for _, l := range as.Lhs { |
| + if id, ok := l.(*ast.Ident); ok { |
| + seam[id.Name] = true |
| + } |
| + } |
| + } |
| + case *ast.CallExpr: |
| + if id, ok := rhs.Fun.(*ast.Ident); ok && seam[id.Name] && len(as.Lhs) > 0 { |
| + if l, ok := as.Lhs[0].(*ast.Ident); ok { |
| + fromSeam[l.Name] = true |
| + } |
| + } |
| + } |
| + return true |
| + }) |
| + ast.Inspect(fd.Body, func(n ast.Node) bool { |
| + call, ok := n.(*ast.CallExpr) |
| + if !ok { |
| + return true |
| + } |
| + if id, ok := call.Fun.(*ast.Ident); !ok || id.Name != "rootfsRowsPresent" { |
| + return true |
| + } |
| + callers++ |
| + if len(call.Args) != 1 { |
| + problems = append(problems, fset.Position(call.Pos()).String()+": rootfsRowsPresent takes one argument") |
| + return true |
| + } |
| + if arg, ok := call.Args[0].(*ast.Ident); !ok || !fromSeam[arg.Name] { |
| + problems = append(problems, fset.Position(call.Pos()).String()+": rootfsRowsPresent is not handed the openRegistry seam's *Registry") |
| + } |
| + return true |
| + }) |
| + } |
| + if callers != 1 { |
| + problems = append(problems, "rootfsRowsPresent has "+strconv.Itoa(callers)+" callers, want 1") |
| + } |
| + return problems |
| +} |