| new file mode 100644 |
| index 000000000..2c1daeb15 |
| --- /dev/null |
| +++ b/internal/ops/stateless_test.go |
| @@ -0,0 +1,91 @@ |
| +package ops |
| + |
| +import ( |
| + "os" |
| + "path/filepath" |
| + "slices" |
| + "strings" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +func TestStatelessExcludedFromBoxLitestream(t *testing.T) { |
| + dir := t.TempDir() |
| + reg, err := registry.Open(filepath.Join(dir, "registry.db")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer reg.Close() |
| + inactive := []registry.Route{ |
| + {Host: "flat.example.com", DBPath: filepath.Join(dir, "flat.db")}, |
| + {Host: "nested.example.com", DBPath: provision.NestedDBPath(dir, "nested.example.com")}, |
| + {Host: "asleep.example.com", DBPath: filepath.Join(dir, "asleep.db"), Hibernate: true}, |
| + {Host: "rootfs.example.com", DBPath: provision.NestedDBPath(dir, "rootfs.example.com"), Runtime: registry.RuntimeRootfsV1}, |
| + {Host: "unit.example.com", DBPath: filepath.Join(t.TempDir(), "unit.db"), Backing: registry.BackingUnit}, |
| + {Host: "tree.example.com", DBPath: t.TempDir(), Backing: registry.BackingUnit}, |
| + } |
| + snapshots := map[string]string{} |
| + for i, rt := range inactive { |
| + rt.Kind = "instance" |
| + rt.Stateless = true |
| + path := rt.DBPath |
| + if i == len(inactive)-1 { |
| + path = filepath.Join(path, "child.db") |
| + } |
| + if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, suffix := range []string{"", "-wal", "-shm"} { |
| + body := "preserved" + suffix |
| + if err := os.WriteFile(path+suffix, []byte(body), 0600); err != nil { |
| + t.Fatal(err) |
| + } |
| + snapshots[path+suffix] = body |
| + } |
| + if err := reg.Upsert(rt); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + // An active unit tree can overlap another route's inactive database. |
| + if err := reg.Upsert(registry.Route{Host: "parent.example.com", Backing: registry.BackingUnit, DBPath: filepath.Join(dir, "instances")}); err != nil { |
| + t.Fatal(err) |
| + } |
| + active := filepath.Join(dir, "active.db") |
| + if err := os.WriteFile(active, []byte("active"), 0600); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := reg.Upsert(registry.Route{Host: "active.example.com", DBPath: active}); err != nil { |
| + t.Fatal(err) |
| + } |
| + dbs, skipped, err := FleetDBsWithSkipped(reg, dir, nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + config := LitestreamConfig(dbs, "bucket", "prefix", "region") |
| + for path := range snapshots { |
| + if slices.Contains(dbs, path) || strings.Contains(config, path) { |
| + t.Errorf("stateless database entered replication config: %s", path) |
| + } |
| + } |
| + if !slices.Contains(dbs, active) || !slices.Contains(dbs, filepath.Join(dir, "registry.db")) { |
| + t.Fatal("active or registry database excluded") |
| + } |
| + if !slices.Contains(skipped, inactive[0].DBPath) || !slices.Contains(skipped, inactive[1].DBPath) { |
| + t.Fatalf("old databases not reported as skipped: %v", skipped) |
| + } |
| + maintenance, err := HibernatingHosts(reg) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if len(maintenance) != 0 { |
| + t.Fatalf("stateless databases entered maintenance: %+v", maintenance) |
| + } |
| + for path, want := range snapshots { |
| + raw, err := os.ReadFile(path) |
| + if err != nil || string(raw) != want { |
| + t.Fatalf("unused bytes changed: %s: %v", path, err) |
| + } |
| + } |
| +} |