| new file mode 100644 |
| index 000000000..8d6f2ca0d |
| --- /dev/null |
| +++ b/internal/provision/runtime_test.go |
| @@ -0,0 +1,273 @@ |
| +package provision |
| + |
| +import ( |
| + "bytes" |
| + "context" |
| + "encoding/json" |
| + "errors" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +func boolPtr(b bool) *bool { return &b } |
| + |
| +// rootfsTestRecord is a rootfs-v1 instance record as Plan 4's create door |
| +// mints it: an instance, hibernation stated, no upstream, unit, role, |
| +// replicas, pool or seed. |
| +func rootfsTestRecord(host string, hibernate bool) Record { |
| + return Record{ |
| + Schema: 1, Host: host, Slug: "shop", App: "shop", Account: "bab", |
| + Region: "ie", Channel: "stable", CreatedAt: time.Date(2026, 10, 26, 9, 0, 0, 0, time.UTC), |
| + Runtime: RuntimeRootfsV1, Hibernate: boolPtr(hibernate), |
| + } |
| +} |
| + |
| +func TestValidateShapeRuntime(t *testing.T) { |
| + base := func() Record { return rootfsTestRecord("a.shop.example.com", false) } |
| + ok := []struct { |
| + name string |
| + rec Record |
| + }{ |
| + {"binary", Record{}}, |
| + {"binary with hibernate", Record{Hibernate: boolPtr(true)}}, |
| + {"rootfs-v1 hibernate false", base()}, |
| + {"rootfs-v1 hibernate true", rootfsTestRecord("a.shop.example.com", true)}, |
| + } |
| + for _, c := range ok { |
| + if err := ValidateShape(c.rec); err != nil { |
| + t.Errorf("%s: ValidateShape = %v, want nil", c.name, err) |
| + } |
| + } |
| + refused := []struct { |
| + name string |
| + mut func(*Record) |
| + }{ |
| + {"unknown runtime", func(r *Record) { r.Runtime = "rootfs-v2" }}, |
| + {"unknown runtime on a binary shape", func(r *Record) { r.Runtime = "Rootfs-v1"; r.Hibernate = nil }}, |
| + {"nil hibernate", func(r *Record) { r.Hibernate = nil }}, |
| + {"kind static", func(r *Record) { r.Kind = "static" }}, |
| + {"kind service", func(r *Record) { r.Kind = "service"; r.Addr = "127.0.0.1:8090" }}, |
| + {"addr", func(r *Record) { r.Addr = "127.0.0.1:8090" }}, |
| + {"addrs", func(r *Record) { r.Addrs = []string{"127.0.0.1:8090"} }}, |
| + {"unit", func(r *Record) { r.Unit = "shop.service" }}, |
| + {"role replica", func(r *Record) { r.Role = "replica" }}, |
| + {"replicas", func(r *Record) { r.Replicas = []string{"uk"} }}, |
| + {"pool", func(r *Record) { r.Pool = "shop" }}, |
| + {"seed", func(r *Record) { r.Seed = &SeedSource{} }}, |
| + } |
| + for _, c := range refused { |
| + rec := base() |
| + c.mut(&rec) |
| + if err := ValidateShape(rec); err == nil { |
| + t.Errorf("%s: ValidateShape = nil, want a refusal", c.name) |
| + } |
| + } |
| + // Kind "instance" spelled out is the default, not a refusal. |
| + rec := base() |
| + rec.Kind = "instance" |
| + if err := ValidateShape(rec); err != nil { |
| + t.Errorf("kind instance spelled out: ValidateShape = %v, want nil", err) |
| + } |
| +} |
| + |
| +func TestRuntimeFromManifest(t *testing.T) { |
| + for _, hib := range []bool{false, true} { |
| + m := &release.Manifest{Kind: release.KindRootfs, Hibernate: hib} |
| + rt, h := RuntimeFromManifest(m) |
| + if rt != RuntimeRootfsV1 || h == nil || *h != hib { |
| + t.Fatalf("rootfs hibernate %v: got (%q, %v), want (%q, &%v)", hib, rt, h, RuntimeRootfsV1, hib) |
| + } |
| + m.Hibernate = !hib |
| + if *h != hib { |
| + t.Errorf("rootfs hibernate %v: the pointer followed the manifest; want a copy", hib) |
| + } |
| + } |
| + if rt, h := RuntimeFromManifest(&release.Manifest{Hibernate: true}); rt != "" || h != nil { |
| + t.Errorf("binary manifest: got (%q, %v), want (\"\", nil)", rt, h) |
| + } |
| + if rt, h := RuntimeFromManifest(nil); rt != "" || h != nil { |
| + t.Errorf("nil manifest: got (%q, %v), want (\"\", nil)", rt, h) |
| + } |
| + if RuntimeRootfsV1 != "rootfs-v1" { |
| + t.Errorf("RuntimeRootfsV1 = %q", RuntimeRootfsV1) |
| + } |
| +} |
| + |
| +func TestOrdinaryWritersCannotChangeRuntime(t *testing.T) { |
| + ctx := context.Background() |
| + s := dirStore(t) |
| + rec := rootfsTestRecord("a.shop.example.com", false) |
| + if err := CreateRecord(ctx, s, rec); err != nil { |
| + t.Fatal(err) |
| + } |
| + key := RecordKey(rec.Account, rec.App, rec.Host) |
| + stored, _, err := s.Get(ctx, key) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + refused := []struct { |
| + name string |
| + mut func(*Record) |
| + }{ |
| + {"runtime cleared", func(r *Record) { r.Runtime = "" }}, |
| + {"runtime changed", func(r *Record) { r.Runtime = "rootfs-v2" }}, |
| + {"hibernate true", func(r *Record) { r.Hibernate = boolPtr(true) }}, |
| + {"hibernate nil", func(r *Record) { r.Hibernate = nil }}, |
| + } |
| + for _, c := range refused { |
| + cur, etag, err := GetRecordETag(ctx, s, rec.Account, rec.App, rec.Host) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + c.mut(cur) |
| + if err := UpdateRecord(ctx, s, *cur, etag); !errors.Is(err, ErrRuntimeProtected) { |
| + t.Errorf("UpdateRecord, %s: err = %v, want ErrRuntimeProtected", c.name, err) |
| + } |
| + if _, err := updateRecord(ctx, s, rec.Account, rec.App, rec.Host, func(r *Record) error { c.mut(r); return nil }); !errors.Is(err, ErrRuntimeProtected) { |
| + t.Errorf("updateRecord, %s: err = %v, want ErrRuntimeProtected", c.name, err) |
| + } |
| + if now, _, err := s.Get(ctx, key); err != nil || !bytes.Equal(now, stored) { |
| + t.Fatalf("%s: stored body moved: %s (%v), want %s", c.name, now, err, stored) |
| + } |
| + } |
| + |
| + cur, etag, err := GetRecordETag(ctx, s, rec.Account, rec.App, rec.Host) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + cur.Environment = "staging" |
| + if err := UpdateRecord(ctx, s, *cur, etag); err != nil { |
| + t.Fatalf("UpdateRecord, environment: %v", err) |
| + } |
| + got, err := GetRecord(ctx, s, rec.Account, rec.App, rec.Host) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if got.Environment != "staging" || got.Runtime != RuntimeRootfsV1 || got.Hibernate == nil || *got.Hibernate { |
| + t.Fatalf("after the environment write: environment %q runtime %q hibernate %v, want staging, rootfs-v1, false", got.Environment, got.Runtime, got.Hibernate) |
| + } |
| + |
| + // A binary record's hibernation stays an ordinary setting, and it |
| + // cannot take a runtime after its mint. |
| + bin := Record{Schema: 1, Host: "b.shop.example.com", Slug: "shop", App: "shop", Account: "bab", Channel: "stable", CreatedAt: rec.CreatedAt} |
| + if err := CreateRecord(ctx, s, bin); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, hib := range []bool{true, false} { |
| + cur, etag, err := GetRecordETag(ctx, s, bin.Account, bin.App, bin.Host) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + cur.Hibernate = boolPtr(hib) |
| + if err := UpdateRecord(ctx, s, *cur, etag); err != nil { |
| + t.Fatalf("binary hibernate %v: %v", hib, err) |
| + } |
| + } |
| + cur, etag, err = GetRecordETag(ctx, s, bin.Account, bin.App, bin.Host) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + cur.Runtime = RuntimeRootfsV1 |
| + if err := UpdateRecord(ctx, s, *cur, etag); !errors.Is(err, ErrRuntimeProtected) { |
| + t.Fatalf("binary record taking a runtime: err = %v, want ErrRuntimeProtected", err) |
| + } |
| +} |
| + |
| +func TestRawWriteThatDropsRuntimeIsRefused(t *testing.T) { |
| + ctx := context.Background() |
| + s := dirStore(t) |
| + rec := rootfsTestRecord("a.shop.example.com", true) |
| + if err := CreateRecord(ctx, s, rec); err != nil { |
| + t.Fatal(err) |
| + } |
| + key := RecordKey(rec.Account, rec.App, rec.Host) |
| + stored, etag, err := s.Get(ctx, key) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + without := func(drop ...string) []byte { |
| + var m map[string]json.RawMessage |
| + if err := json.Unmarshal(stored, &m); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, k := range drop { |
| + delete(m, k) |
| + } |
| + b, err := json.Marshal(m) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return b |
| + } |
| + // An old writer's shape: its struct has no runtime field. |
| + if _, _, err := WriteRecord(ctx, s, key, stored, without("runtime"), etag); !errors.Is(err, ErrRuntimeProtected) { |
| + t.Errorf("drop runtime: err = %v, want ErrRuntimeProtected", err) |
| + } |
| + if _, _, err := WriteRecord(ctx, s, key, stored, without("hibernate"), etag); !errors.Is(err, ErrRuntimeProtected) { |
| + t.Errorf("drop hibernate: err = %v, want ErrRuntimeProtected", err) |
| + } |
| + // A case alias decodes like the field it names, for every reader. |
| + alias := bytes.Replace(stored, []byte(`"runtime":"rootfs-v1"`), []byte(`"runtime":"rootfs-v1","RUNTIME":""`), 1) |
| + if bytes.Equal(alias, stored) { |
| + t.Fatal("test body: no runtime key to alias") |
| + } |
| + if _, _, err := WriteRecord(ctx, s, key, stored, alias, etag); !errors.Is(err, ErrRuntimeProtected) { |
| + t.Errorf("aliased runtime: err = %v, want ErrRuntimeProtected", err) |
| + } |
| + // A body whose runtime does not decode answers as RecordSequence does. |
| + bad := bytes.Replace(stored, []byte(`"runtime":"rootfs-v1"`), []byte(`"runtime":5`), 1) |
| + if _, _, err := WriteRecord(ctx, s, key, stored, bad, etag); !errors.Is(err, ErrRecordSequence) { |
| + t.Errorf("undecodable runtime: err = %v, want ErrRecordSequence", err) |
| + } |
| + // Nearly a tombstone (retired, runtime gone, a field kept) is not one. |
| + var tm map[string]json.RawMessage |
| + _ = json.Unmarshal(without("runtime", "hibernate"), &tm) |
| + tm["retired"] = json.RawMessage("true") |
| + near, _ := json.Marshal(tm) |
| + if _, _, err := WriteRecord(ctx, s, key, stored, near, etag); !errors.Is(err, ErrRuntimeProtected) { |
| + t.Errorf("retired body that is not the tombstone: err = %v, want ErrRuntimeProtected", err) |
| + } |
| + if now, _, err := s.Get(ctx, key); err != nil || !bytes.Equal(now, stored) { |
| + t.Fatalf("stored body moved: %s (%v)", now, err) |
| + } |
| + // The stored bytes written back unchanged (a fence) pass. |
| + etag, _, err = WriteRecord(ctx, s, key, stored, stored, etag) |
| + if err != nil { |
| + t.Fatalf("identity write: %v", err) |
| + } |
| + // Retirement is the end of the life, not an ordinary write: the exact |
| + // tombstone of the stored bytes passes, sequenced or not. |
| + tomb, err := TombstoneBody(stored) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, _, err := WriteRecord(WithRecordSequence(ctx), s, key, stored, tomb, etag); err != nil { |
| + t.Fatalf("tombstone: %v", err) |
| + } |
| + now, _, err := s.Get(ctx, key) |
| + if err != nil || strings.Contains(string(now), `"runtime"`) || !strings.Contains(string(now), `"retired":true`) { |
| + t.Fatalf("after the tombstone: %s (%v)", now, err) |
| + } |
| +} |
| + |
| +func TestBinaryRecordBytesUnchanged(t *testing.T) { |
| + rec := Record{Schema: 1, Host: "a.shop.example.com", Slug: "shop", App: "shop", Account: "bab", Channel: "stable", Hibernate: boolPtr(true)} |
| + body, err := json.Marshal(rec) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if strings.Contains(string(body), `"runtime"`) { |
| + t.Fatalf("binary record marshals a runtime key: %s", body) |
| + } |
| + rec.Runtime = RuntimeRootfsV1 |
| + body, err = json.Marshal(rec) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if !strings.Contains(string(body), `"runtime":"rootfs-v1"`) { |
| + t.Fatalf("rootfs record marshals no runtime: %s", body) |
| + } |
| +} |