| index 839fee007..171c008e1 100644 |
| --- a/internal/console/rootfs_move_test.go |
| +++ b/internal/console/rootfs_move_test.go |
| @@ -7,14 +7,17 @@ import ( |
| "go/ast" |
| "go/parser" |
| "go/token" |
| + "maps" |
| "net/http" |
| "path/filepath" |
| "slices" |
| "strings" |
| + "sync" |
| "testing" |
| "time" |
| |
| "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/registry" |
| "github.com/carlosframework/platform/internal/release" |
| ) |
| |
| @@ -47,15 +50,27 @@ func wantRootfsMoveWords(host string) string { |
| |
| // POST .../move (the legacy door and the Phase 1 door) and the offline |
| // door (start and stage) answer 409 with C23 for a rootfs instance, |
| -// hibernating or always awake, and write nothing. |
| +// hibernating or always awake, in a region or placed on a fleet, and write |
| +// nothing. |
| func TestMoveDoorsRefuseRootfs(t *testing.T) { |
| - for _, door := range []struct { |
| + type door struct { |
| name string |
| phase1 bool |
| hibernate bool |
| - }{{"legacy", false, false}, {"legacy", false, true}, {"phase 1", true, false}, {"phase 1", true, true}} { |
| + region string |
| + } |
| + var doors []door |
| + for _, d := range []door{{name: "legacy"}, {name: "phase 1", phase1: true}} { |
| + for _, h := range []bool{false, true} { |
| + for _, region := range []string{"ie", "fleet/pi"} { |
| + d.hibernate, d.region = h, region |
| + doors = append(doors, d) |
| + } |
| + } |
| + } |
| + for _, door := range doors { |
| fx, token := seedMoveFixture(t, "code-rootfs-move", door.phase1) |
| - putRuntimeRecordHibernating(t, fx.rel, release.OpsAccount, rootfsMoveHost, "ie", provision.RuntimeRootfsV1, door.hibernate) |
| + putRuntimeRecordHibernating(t, fx.rel, release.OpsAccount, rootfsMoveHost, door.region, provision.RuntimeRootfsV1, door.hibernate) |
| key := provision.RecordKey(release.OpsAccount, "shop", rootfsMoveHost) |
| stored, _, err := fx.rel.Get(context.Background(), key) |
| if err != nil { |
| @@ -63,7 +78,7 @@ func TestMoveDoorsRefuseRootfs(t *testing.T) { |
| } |
| code, _, raw := rawReq(t, http.MethodPost, instancesURL(fx, release.OpsAccount, "shop")+"/"+rootfsMoveHost+"/move", token, `{"region":"uk"}`) |
| if code != http.StatusConflict || errorText(raw) != wantRootfsMoveWords(rootfsMoveHost) { |
| - t.Fatalf("%s door = %d %s", door.name, code, raw) |
| + t.Fatalf("%s door (hibernate %v, %s) = %d %s", door.name, door.hibernate, door.region, code, raw) |
| } |
| if got, _, err := fx.rel.Get(context.Background(), key); err != nil || string(got) != string(stored) { |
| t.Fatalf("%s door: a refused move wrote the record: %s, %v", door.name, got, err) |
| @@ -73,10 +88,15 @@ func TestMoveDoorsRefuseRootfs(t *testing.T) { |
| } |
| } |
| |
| - for _, h := range []bool{false, true} { |
| + for _, c := range []struct { |
| + hibernate bool |
| + region string |
| + }{{false, "ie"}, {true, "ie"}, {false, "fleet/pi"}, {true, "fleet/pi"}} { |
| + h := c.hibernate |
| w := newOfflineWorld(t, ieOff("box-ie"), ukOff("box-uk")) |
| rec := w.record() |
| rec.Runtime, rec.Hibernate, rec.Seed, rec.Pool, rec.Addr, rec.Addrs = provision.RuntimeRootfsV1, &h, nil, "", "", nil |
| + rec.Region = c.region |
| body, err := json.Marshal(rec) |
| if err != nil { |
| t.Fatal(err) |
| @@ -88,34 +108,249 @@ func TestMoveDoorsRefuseRootfs(t *testing.T) { |
| } |
| } |
| |
| -// carlos apps place refuses an app with any rootfs instance with C24 |
| -// before it writes the app's placement or any record. |
| -func TestPlaceRefusesAnAppWithRootfsInstances(t *testing.T) { |
| - const want = "this app's instances are rootfs instances, which do not move. create new instances where you want them" |
| - fx, token := placeSeedFixture(t, "code-place-rootfs") |
| +const rootfsPlaceWant = "this app's instances are rootfs instances, which do not move. create new instances where you want them" |
| + |
| +// placeRootfsWorld is the shop app with the given routes seeded (before |
| +// the console opens its registry, see openTestReg), a binary record |
| +// plain.example.com in ie with its route, and an owned fleet "pi". |
| +func placeRootfsWorld(t *testing.T, code string, routes ...registry.Route) (*consoleFixture, string, *registry.Registry) { |
| + t.Helper() |
| + fx, token := fleetFixture(t, code) |
| + if _, err := fx.st.CreateAppIn(OpsAccountID, "shop", "paul@keymail.dev", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + reg := openTestReg(t, fx) |
| + routes = append(routes, registry.Route{Host: "plain.example.com", Kind: "instance", App: "shop", Account: fx.sqid}) |
| + for _, rt := range routes { |
| + if rt.Account == "" { |
| + rt.Account = fx.sqid |
| + } |
| + if err := reg.Upsert(rt); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if status, body := postJSON(t, fleetsURL(fx, fx.sqid)+"/new", |
| + map[string]string{"name": "pi", "bucket": "pi-fleet-data"}, token); status != http.StatusOK { |
| + t.Fatalf("create fleet = %d %v", status, body) |
| + } |
| putRuntimeRecord(t, fx.rel, fx.sqid, "plain.example.com", "ie", "") |
| + return fx, token, reg |
| +} |
| + |
| +// placeState is everything a place writes: the app's placement, the region |
| +// of each of its routes, and the bytes of each of its records. |
| +type placeState struct { |
| + placement string |
| + routes map[string]string |
| + records map[string]string |
| +} |
| + |
| +func readPlaceState(t *testing.T, fx *consoleFixture, reg *registry.Registry) placeState { |
| + t.Helper() |
| + p, err := fx.st.AppPlacement(OpsAccountID, "shop") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + st := placeState{placement: p, routes: map[string]string{}, records: map[string]string{}} |
| + rts, err := reg.List() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, rt := range rts { |
| + if rt.App == "shop" { |
| + st.routes[rt.Host] = rt.Region |
| + } |
| + } |
| + keys, err := fx.rel.List(context.Background(), provision.RecordsPrefix(fx.sqid, "shop")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, k := range keys { |
| + b, _, err := fx.rel.Get(context.Background(), k) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + st.records[k] = string(b) |
| + } |
| + return st |
| +} |
| + |
| +func (a placeState) equal(b placeState) bool { |
| + return a.placement == b.placement && maps.Equal(a.routes, b.routes) && maps.Equal(a.records, b.records) |
| +} |
| + |
| +// carlos apps place refuses an app with any rootfs record, or any route |
| +// with a runtime, with C24 before it writes anything: the app placement, |
| +// every route and every record are as they were. |
| +func TestPlaceRefusesAnAppWithRootfsInstances(t *testing.T) { |
| + fx, token, reg := placeRootfsWorld(t, "code-place-rootfs") |
| putRuntimeRecord(t, fx.rel, fx.sqid, rootfsMoveHost, "ie", provision.RuntimeRootfsV1) |
| + before := readPlaceState(t, fx, reg) |
| code, _, raw := rawReq(t, http.MethodPost, placeURL(fx, "shop"), token, `{"target":"fleet/pi"}`) |
| - if code != http.StatusConflict || errorText(raw) != want { |
| + if code != http.StatusConflict || errorText(raw) != rootfsPlaceWant { |
| t.Fatalf("place = %d %s", code, raw) |
| } |
| - assertNotPlaced(t, fx, "plain.example.com", rootfsMoveHost) |
| + if after := readPlaceState(t, fx, reg); !after.equal(before) { |
| + t.Fatalf("a refused place wrote:\nbefore %+v\nafter %+v", before, after) |
| + } |
| |
| - // The backstop: a rootfs record created after the place's read is |
| - // refused under SetRecordRegion's CAS, and the place answers C24. |
| - fx, token = placeSeedFixture(t, "code-place-rootfs-backstop") |
| - putRuntimeRecord(t, fx.rel, fx.sqid, "plain.example.com", "ie", "") |
| - fx.srv.cfg.Releases = &createOnSecondListStore{Store: fx.rel, prefix: provision.RecordsPrefix(fx.sqid, "shop"), create: func(st release.Store) { |
| - putRuntimeRecord(t, st, fx.sqid, rootfsMoveHost, "ie", provision.RuntimeRootfsV1) |
| - }} |
| + // A route with a runtime whose record is gone (or not yet listed). |
| + fx, token, reg = placeRootfsWorld(t, "code-place-rootfs-route", |
| + registry.Route{Host: rootfsMoveHost, Kind: "instance", App: "shop", Runtime: registry.RuntimeRootfsV1}) |
| + before = readPlaceState(t, fx, reg) |
| code, _, raw = rawReq(t, http.MethodPost, placeURL(fx, "shop"), token, `{"target":"fleet/pi"}`) |
| - if code != http.StatusConflict || errorText(raw) != want { |
| + if code != http.StatusConflict || errorText(raw) != rootfsPlaceWant { |
| + t.Fatalf("route only: place = %d %s", code, raw) |
| + } |
| + if after := readPlaceState(t, fx, reg); !after.equal(before) { |
| + t.Fatalf("route only: a refused place wrote:\nbefore %+v\nafter %+v", before, after) |
| + } |
| +} |
| + |
| +// appLockWaits calls signal once when anyone starts waiting for the shop |
| +// app's lock from now on. |
| +func appLockWaits(t *testing.T, sqid string, signal chan struct{}) { |
| + t.Helper() |
| + var once sync.Once |
| + h := func(kind, key, event string) { |
| + if kind == "app" && key == sqid+"/shop" && event == lockWaiting { |
| + once.Do(func() { close(signal) }) |
| + } |
| + } |
| + lifetimeLockHook.Store(&h) |
| + t.Cleanup(func() { lifetimeLockHook.Store(nil) }) |
| +} |
| + |
| +func rootfsCreateRecord(sqid string) provision.Record { |
| + h := false |
| + return provision.Record{Schema: 1, Host: rootfsMoveHost, Slug: "bab", App: "shop", Account: sqid, |
| + Region: "ie", Channel: "stable", CreatedAt: time.Now().UTC(), Runtime: provision.RuntimeRootfsV1, Hibernate: &h} |
| +} |
| + |
| +// Round 1 finding 1, a create first: a console create of a rootfs record is |
| +// writing it (holding the app lock) when the place arrives. The place waits |
| +// for the lock, then reads the record and refuses with C24, and writes |
| +// nothing: the app placement, the routes and the binary record are as they |
| +// were. |
| +func TestPlaceWaitsForACreateInFlight(t *testing.T) { |
| + fx, token, reg := placeRootfsWorld(t, "code-place-rootfs-create-first") |
| + before := readPlaceState(t, fx, reg) |
| + gate := &putGateStore{Store: fx.rel, key: provision.RecordKey(fx.sqid, "shop", rootfsMoveHost), |
| + entered: make(chan struct{}), release: make(chan struct{})} |
| + fx.srv.cfg.Releases = gate |
| + created := make(chan error, 1) |
| + go func() { created <- fx.srv.createRecordMarked(context.Background(), rootfsCreateRecord(fx.sqid)) }() |
| + waitForSignal(t, gate.entered, "the rootfs record's create") |
| + placeWaiting := make(chan struct{}) |
| + appLockWaits(t, fx.sqid, placeWaiting) |
| + placed := make(chan string, 1) |
| + go func() { |
| + code, _, raw := rawReq(t, http.MethodPost, placeURL(fx, "shop"), token, `{"target":"fleet/pi"}`) |
| + placed <- fmt.Sprintf("%d %s", code, errorText(raw)) |
| + }() |
| + select { |
| + case <-placeWaiting: |
| + case <-time.After(2 * time.Second): |
| + // The place never waited for the app lock: let the create finish, |
| + // and the checks below say what the place did without it. |
| + } |
| + close(gate.release) |
| + if err := <-created; err != nil { |
| + t.Fatal(err) |
| + } |
| + if got, want := <-placed, fmt.Sprintf("%d %s", http.StatusConflict, rootfsPlaceWant); got != want { |
| + t.Fatalf("place = %s, want %s", got, want) |
| + } |
| + after := readPlaceState(t, fx, reg) |
| + rootfsKey := provision.RecordKey(fx.sqid, "shop", rootfsMoveHost) |
| + if _, ok := after.records[rootfsKey]; !ok { |
| + t.Fatal("the create did not land") |
| + } |
| + delete(after.records, rootfsKey) |
| + if !after.equal(before) { |
| + t.Fatalf("a refused place wrote:\nbefore %+v\nafter %+v", before, after) |
| + } |
| +} |
| + |
| +// Round 1 finding 1, the place first: a console create of a rootfs record |
| +// that starts after the place's check waits for the app lock, so the place |
| +// lands whole (placement, route, binary record) and the new record is |
| +// created afterwards where it asked to be, not re-homed. |
| +func TestACreateWaitsForAPlaceInFlight(t *testing.T) { |
| + fx, token, reg := placeRootfsWorld(t, "code-place-rootfs-place-first") |
| + created := make(chan error, 1) |
| + createWaiting := make(chan struct{}) |
| + lists := &listHookStore{Store: fx.rel, prefix: provision.RecordsPrefix(fx.sqid, "shop")} |
| + lists.onList = func(n int) { |
| + switch n { |
| + case 1: // the place's check, under its lock |
| + appLockWaits(t, fx.sqid, createWaiting) |
| + go func() { created <- fx.srv.createRecordMarked(context.Background(), rootfsCreateRecord(fx.sqid)) }() |
| + case 2: // the place's walk: the create is waiting, or (without the lock) done |
| + select { |
| + case <-createWaiting: |
| + case err := <-created: |
| + created <- err |
| + case <-time.After(5 * time.Second): |
| + t.Error("the create neither waited nor finished") |
| + } |
| + } |
| + } |
| + fx.srv.cfg.Releases = lists |
| + code, _, raw := rawReq(t, http.MethodPost, placeURL(fx, "shop"), token, `{"target":"fleet/pi"}`) |
| + if code != http.StatusOK { |
| t.Fatalf("place = %d %s", code, raw) |
| } |
| - got, err := provision.GetRecord(context.Background(), fx.rel, fx.sqid, "shop", rootfsMoveHost) |
| - if err != nil || got.Region != "ie" { |
| - t.Fatalf("the late rootfs record was re-homed: %+v, %v", got, err) |
| + if err := <-created; err != nil { |
| + t.Fatal(err) |
| + } |
| + after := readPlaceState(t, fx, reg) |
| + if after.placement != "fleet/pi" || after.routes["plain.example.com"] != "fleet/pi" { |
| + t.Fatalf("the place did not land: %+v", after) |
| + } |
| + for host, want := range map[string]string{"plain.example.com": "fleet/pi", rootfsMoveHost: "ie"} { |
| + got, err := provision.GetRecord(context.Background(), fx.rel, fx.sqid, "shop", host) |
| + if err != nil || got.Region != want { |
| + t.Fatalf("%s: %+v, %v, want region %s", host, got, err, want) |
| + } |
| + } |
| +} |
| + |
| +// putGateStore holds the Put of one key until release, after closing |
| +// entered. |
| +type putGateStore struct { |
| + release.Store |
| + key string |
| + entered, release chan struct{} |
| +} |
| + |
| +func (g *putGateStore) Put(ctx context.Context, key string, body []byte, etag string) (string, error) { |
| + if key == g.key { |
| + close(g.entered) |
| + <-g.release |
| + } |
| + return g.Store.Put(ctx, key, body, etag) |
| +} |
| + |
| +// listHookStore calls onList with the count of Lists of prefix so far, |
| +// before each one. |
| +type listHookStore struct { |
| + release.Store |
| + prefix string |
| + onList func(n int) |
| + mu sync.Mutex |
| + n int |
| +} |
| + |
| +func (l *listHookStore) List(ctx context.Context, prefix string) ([]string, error) { |
| + if prefix == l.prefix { |
| + l.mu.Lock() |
| + l.n++ |
| + n := l.n |
| + l.mu.Unlock() |
| + l.onList(n) |
| } |
| + return l.Store.List(ctx, prefix) |
| } |
| |
| // rootfsMoveDoors is every function of the package that answers a move |