| new file mode 100644 |
| index 000000000..9b05fb68e |
| --- /dev/null |
| +++ b/internal/console/restore_origin_test.go |
| @@ -0,0 +1,312 @@ |
| +package console |
| + |
| +// The restore origin (instance restore #658, plan Task 3; spec §4 stage 2 |
| +// item 3): the create door's own plain create records restore_origin on its |
| +// allocation when, at its mark step, lifetimes were on, the history was |
| +// active and the mark was fresh. Nothing else records it. |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "net/http" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// lineage is HostLineage(host), failing the test on an error. |
| +func lineage(t *testing.T, st *Store, host string) []Allocation { |
| + t.Helper() |
| + rows, err := st.HostLineage(host) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return rows |
| +} |
| + |
| +// noOrigin fails when any allocation of host carries a restore column. |
| +func noOrigin(t *testing.T, st *Store, host string) []Allocation { |
| + t.Helper() |
| + rows := lineage(t, st, host) |
| + for _, a := range rows { |
| + if a.RestoreOrigin || a.RestoreOf != "" { |
| + t.Fatalf("allocation %+v of %s carries a restore column, want neither", a, host) |
| + } |
| + } |
| + return rows |
| +} |
| + |
| +// TestOriginOnFreshActiveCreate: a plain data-kind create through the door, |
| +// lifetimes on and the history active, records its origin. |
| +func TestOriginOnFreshActiveCreate(t *testing.T) { |
| + l := newLtOn(t) |
| + forceHostsState(t, l.fx.srv, provision.HostsState{Mode: provision.HostsModeActive}) |
| + if code, out := l.create(t, ltOne); code != http.StatusCreated { |
| + t.Fatalf("create = %d %s", code, out) |
| + } |
| + rows := lineage(t, l.fx.st, ltOne) |
| + if len(rows) != 1 || !rows[0].RestoreOrigin || rows[0].RestoreOf != "" || rows[0].State != lifetimeCommitted { |
| + t.Fatalf("lineage of %s = %+v, want one committed row with the origin", ltOne, rows) |
| + } |
| +} |
| + |
| +// TestNoOriginBeforeActivation: the same create before the history is |
| +// active (incomplete) records no origin, though its mark is fresh. |
| +func TestNoOriginBeforeActivation(t *testing.T) { |
| + l := newLtOn(t) |
| + forceHostsState(t, l.fx.srv, provision.HostsState{Mode: provision.HostsModeIncomplete}) |
| + if code, out := l.create(t, ltOne); code != http.StatusCreated { |
| + t.Fatalf("create = %d %s", code, out) |
| + } |
| + if !marked(t, l.fx.rel, ltOne) { |
| + t.Fatal("fixture: the create did not mark") |
| + } |
| + if rows := noOrigin(t, l.fx.st, ltOne); len(rows) != 1 { |
| + t.Fatalf("lineage of %s = %+v, want one row", ltOne, rows) |
| + } |
| +} |
| + |
| +// TestNoOriginOnSeed: with lifetimes on and the history active, the seed |
| +// (`from`) and import doors write their allocation with no origin, while the |
| +// instance door, the control, records one. The seed doors run the same core |
| +// as the instance door; only the door's own flag sets the origin. |
| +func TestNoOriginOnSeed(t *testing.T) { |
| + for _, tc := range []struct { |
| + door string |
| + origin bool |
| + }{{"instance", true}, {"seed", false}, {"import", false}} { |
| + t.Run(tc.door, func(t *testing.T) { |
| + clock := useLifetimesClock(t) |
| + c := markDoorCase(t, tc.door) |
| + turnLifetimesOn(t, c.fx.srv, clock) |
| + forceHostsMode(t, c.fx.srv, provision.HostsModeActive) |
| + if code, out := c.create(t); code != c.created { |
| + t.Fatalf("create = %d %s, want %d", code, out, c.created) |
| + } |
| + if !recordPresent(t, c, c.recordKey) { |
| + t.Fatal("no record after the create") |
| + } |
| + rows := lineage(t, c.fx.st, c.host) |
| + if len(rows) != 1 { |
| + t.Fatalf("lineage of %s = %+v, want one row", c.host, rows) |
| + } |
| + if rows[0].RestoreOrigin != tc.origin || rows[0].RestoreOf != "" { |
| + t.Fatalf("allocation %+v, want origin %v", rows[0], tc.origin) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// markDoorCase builds the markDoors case named door. |
| +func markDoorCase(t *testing.T, door string) markCase { |
| + t.Helper() |
| + for _, d := range markDoors { |
| + if d.name == door { |
| + return d.build(t) |
| + } |
| + } |
| + t.Fatalf("no mark door %q", door) |
| + return markCase{} |
| +} |
| + |
| +// TestNoOriginOnExemptKind: a static create, which never marks, records no |
| +// origin (it may write an allocation row; that row carries neither column). |
| +func TestNoOriginOnExemptKind(t *testing.T) { |
| + l := newLtOn(t) |
| + forceHostsState(t, l.fx.srv, provision.HostsState{Mode: provision.HostsModeActive}) |
| + host := "site.shop.example.com" |
| + if code, out := createAs(t, l.lifetimesFx, `{"host":"`+host+`","kind":"static"}`); code != http.StatusCreated { |
| + t.Fatalf("static create = %d %s", code, out) |
| + } |
| + if marked(t, l.fx.rel, host) { |
| + t.Fatal("fixture: the static create marked its hostname") |
| + } |
| + noOrigin(t, l.fx.st, host) |
| +} |
| + |
| +// TestNoOriginLifetimesOff: with lifetimes off the create writes no |
| +// allocation row at all, so nothing can carry an origin. |
| +func TestNoOriginLifetimesOff(t *testing.T) { |
| + l := newLifetimesFx(t) |
| + forceHostsState(t, l.fx.srv, provision.HostsState{Mode: provision.HostsModeActive}) |
| + if code, out := createAs(t, l, `{"host":"`+ltOne+`"}`); code != http.StatusCreated { |
| + t.Fatalf("create = %d %s", code, out) |
| + } |
| + if !marked(t, l.fx.rel, ltOne) { |
| + t.Fatal("fixture: the create did not mark") |
| + } |
| + if rows := lineage(t, l.fx.st, ltOne); len(rows) != 0 { |
| + t.Fatalf("lineage of %s = %+v, want none with lifetimes off", ltOne, rows) |
| + } |
| +} |
| + |
| +// TestMarkStepUnchangedForCreates: the doors' answers at the mark step are |
| +// what they were. Once active, a hostname marked already is S1 and nothing |
| +// is persisted; before activation it passes, the first mark kept. |
| +func TestMarkStepUnchangedForCreates(t *testing.T) { |
| + other := provision.HostUse{Host: ltOne, Account: "zzz", App: "gone", Kind: "instance", Source: "test"} |
| + t.Run("active", func(t *testing.T) { |
| + l := newLtOn(t) |
| + if _, err := provision.MarkHostUsed(context.Background(), l.fx.rel, other); err != nil { |
| + t.Fatal(err) |
| + } |
| + forceHostsState(t, l.fx.srv, provision.HostsState{Mode: provision.HostsModeActive}) |
| + if code, out := l.create(t, ltOne); code != http.StatusConflict || ltErr(out) != msgHostUsed(ltOne) { |
| + t.Fatalf("create of a marked host once active = %d %s, want 409 %q", code, out, msgHostUsed(ltOne)) |
| + } |
| + if rows := lineage(t, l.fx.st, ltOne); len(rows) != 0 { |
| + t.Fatalf("lineage = %+v, want none", rows) |
| + } |
| + if raw, _ := rawOf(t, l.fx.rel, ltApp, ltOne); raw != nil { |
| + t.Fatalf("a record was written over a marked host: %s", raw) |
| + } |
| + }) |
| + t.Run("incomplete", func(t *testing.T) { |
| + l := newLtOn(t) |
| + if _, err := provision.MarkHostUsed(context.Background(), l.fx.rel, other); err != nil { |
| + t.Fatal(err) |
| + } |
| + forceHostsState(t, l.fx.srv, provision.HostsState{Mode: provision.HostsModeIncomplete}) |
| + if code, out := l.create(t, ltOne); code != http.StatusCreated { |
| + t.Fatalf("create of a marked host before activation = %d %s, want 201", code, out) |
| + } |
| + // Not fresh and not active: no origin either way. |
| + if rows := noOrigin(t, l.fx.st, ltOne); len(rows) != 1 { |
| + t.Fatalf("lineage = %+v, want one row", rows) |
| + } |
| + }) |
| +} |
| + |
| +// TestNoOriginWithoutTheDoorFlag: the lifetime create core called as every |
| +// other door calls it (no withOriginEligible) records no origin for a plain |
| +// data-kind record, with lifetimes on, the history active and a fresh mark. |
| +// With the flag it does; with the flag and a seed marker on the record it |
| +// does not (the second check). |
| +func TestNoOriginWithoutTheDoorFlag(t *testing.T) { |
| + for _, tc := range []struct { |
| + name string |
| + flag bool |
| + seeded bool |
| + origin bool |
| + }{{"no flag", false, false, false}, {"flag", true, false, true}, {"flag on a seeded record", true, true, false}} { |
| + t.Run(tc.name, func(t *testing.T) { |
| + l := newLtOn(t) |
| + forceHostsState(t, l.fx.srv, provision.HostsState{Mode: provision.HostsModeActive}) |
| + rec := provision.Record{Schema: 1, Host: ltOne, Slug: "one", App: ltApp, Account: "bab", Channel: "stable", CreatedAt: time.Now().UTC()} |
| + if tc.seeded { |
| + rec.Seed = &provision.SeedSource{ID: "s-1", Kind: "replica", From: "src.shop.example.com", State: "staged", By: "alice@keymail.dev", At: time.Now().UTC()} |
| + } |
| + life, err := l.fx.st.AppLife(OpsAccountID, ltApp) |
| + if err != nil || life == nil { |
| + t.Fatalf("app life = %+v %v", life, err) |
| + } |
| + ctx := onCtx(t, l.fx.srv) |
| + if tc.flag { |
| + ctx = withOriginEligible(ctx) |
| + } |
| + if _, err := l.fx.srv.createRecordWithLifetime(ctx, rec, life.Lifetime); err != nil { |
| + t.Fatalf("create = %v", err) |
| + } |
| + if !marked(t, l.fx.rel, ltOne) { |
| + t.Fatal("fixture: the create did not mark") |
| + } |
| + rows := lineage(t, l.fx.st, ltOne) |
| + if len(rows) != 1 || rows[0].RestoreOrigin != tc.origin || rows[0].RestoreOf != "" { |
| + t.Fatalf("lineage of %s = %+v, want one row with origin %v", ltOne, rows, tc.origin) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// TestMarkHostObservations: markHost's result and refusals are what they |
| +// were; it only adds what it saw. markForCreate and markBeforeCreate answer |
| +// exactly as before for every mode and mark. |
| +func TestMarkHostObservations(t *testing.T) { |
| + ctx := context.Background() |
| + other := provision.HostUse{Host: ltOne, Account: "zzz", App: "gone", Kind: "instance", Source: "test"} |
| + use := provision.HostUse{Host: ltOne, Account: "bab", App: ltApp, Kind: "instance", Source: "create"} |
| + for _, tc := range []struct { |
| + mode string |
| + premark bool |
| + want markObservation |
| + }{ |
| + {provision.HostsModeIncomplete, false, markObservation{result: markPass, fresh: true}}, |
| + {provision.HostsModeIncomplete, true, markObservation{result: markPass}}, |
| + {provision.HostsModeActive, false, markObservation{result: markPass, fresh: true, active: true}}, |
| + {provision.HostsModeActive, true, markObservation{result: markConflict, active: true}}, |
| + } { |
| + name := tc.mode |
| + if tc.premark { |
| + name += " marked" |
| + } |
| + t.Run(name, func(t *testing.T) { |
| + setup := func(t *testing.T) ltOn { |
| + t.Helper() |
| + l := newLtOn(t) |
| + if tc.premark { |
| + if _, err := provision.MarkHostUsed(ctx, l.fx.rel, other); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + forceHostsState(t, l.fx.srv, provision.HostsState{Mode: tc.mode}) |
| + return l |
| + } |
| + t.Run("markHost", func(t *testing.T) { |
| + l := setup(t) |
| + obs, ref := l.fx.srv.markHost(ctx, use) |
| + if ref != nil || obs != tc.want { |
| + t.Fatalf("markHost = %+v %+v, want %+v", obs, ref, tc.want) |
| + } |
| + }) |
| + t.Run("markForCreate", func(t *testing.T) { |
| + l := setup(t) |
| + obs, ref := l.fx.srv.markForCreate(ctx, use) |
| + if tc.want.result == markConflict { |
| + if ref == nil || ref.Status != http.StatusConflict || ref.Msg != msgHostUsed(ltOne) || obs != (markObservation{}) { |
| + t.Fatalf("markForCreate = %+v %+v, want S1 and no observation", obs, ref) |
| + } |
| + return |
| + } |
| + if ref != nil || obs != tc.want { |
| + t.Fatalf("markForCreate = %+v %+v, want %+v", obs, ref, tc.want) |
| + } |
| + }) |
| + t.Run("markBeforeCreate", func(t *testing.T) { |
| + l := setup(t) |
| + key := recKey(ltApp, ltOne) |
| + err := l.fx.srv.markBeforeCreate(ctx, use, key) |
| + if tc.want.result != markConflict { |
| + if err != nil { |
| + t.Fatalf("markBeforeCreate = %v, want nil", err) |
| + } |
| + return |
| + } |
| + var hu *hostsUsedError |
| + if !errors.As(err, &hu) || hu.ref.Status != http.StatusConflict || hu.ref.Msg != msgHostUsed(ltOne) { |
| + t.Fatalf("markBeforeCreate with no record = %v, want S1", err) |
| + } |
| + // A record of its own under the key: the write's own answer. |
| + legacyRecord(t, l.fx.rel, ltOne, "stable") |
| + if err := l.fx.srv.markBeforeCreate(ctx, use, key); !errors.Is(err, release.ErrCASConflict) { |
| + t.Fatalf("markBeforeCreate with its record there = %v, want ErrCASConflict", err) |
| + } |
| + }) |
| + }) |
| + } |
| + t.Run("invalidated", func(t *testing.T) { |
| + l := newLtOn(t) |
| + forceHostsState(t, l.fx.srv, provision.HostsState{Mode: provision.HostsModeInvalidated, Reason: "test"}) |
| + obs, ref := l.fx.srv.markHost(ctx, use) |
| + if ref == nil || ref.Status != http.StatusServiceUnavailable || ref.Msg != msgHostsInvalidated || obs != (markObservation{}) { |
| + t.Fatalf("markHost once invalidated = %+v %+v, want S5 and no observation", obs, ref) |
| + } |
| + if obs, ref := l.fx.srv.markForCreate(ctx, use); ref == nil || ref.Msg != msgHostsInvalidated || obs != (markObservation{}) { |
| + t.Fatalf("markForCreate once invalidated = %+v %+v, want S5", obs, ref) |
| + } |
| + if marked(t, l.fx.rel, ltOne) { |
| + t.Fatal("an invalidated history marked the hostname") |
| + } |
| + }) |
| +} |