| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "github.com/carlosframework/platform/internal/hostactors" |
| 5 | "github.com/carlosframework/platform/internal/hostclaim" |
| 6 | "github.com/carlosframework/platform/internal/park" |
| 7 | "github.com/carlosframework/platform/internal/provision" |
| 8 | "github.com/carlosframework/platform/internal/release" |
| 9 | "net/http" |
| 10 | "testing" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | func TestPlan13CompleteUnnamedHomeCreate(t *testing.T) { |
| 15 | cx := newClaimCaseWith(t, func(cfg *Config) { |
| 16 | cfg.HomeRegion = "" |
| 17 | cfg.HibernateRegion = "eu-west-1" |
| 18 | cfg.HibernateBucket = "data" |
| 19 | }) |
| 20 | d := cx.srv.cfg.ClaimAuthority.Directory |
| 21 | roster, err := d.Read(t.Context()) |
| 22 | if err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | for ref, b := range roster.Roster.Boxes { |
| 26 | b.Region = "eu-west-1" |
| 27 | roster.Roster.Boxes[ref] = b |
| 28 | } |
| 29 | roster.Roster.Regions["eu-west-1"] = roster.Roster.Regions["ie"] |
| 30 | delete(roster.Roster.Regions, "ie") |
| 31 | body, err := release.SignRootEnvelope(release.RootSigner(cx.priv), roster.Roster) |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | if _, err = cx.rel.Put(t.Context(), hostactors.Key, body, roster.ETag); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | manifest := titoManifest() |
| 39 | manifest.Regions = nil |
| 40 | enableInstances(t, cx.consoleFixture, "titogo", manifest) |
| 41 | cx.mustAPI(t, http.MethodPost, "/api/cli/apps/bab/titogo/instances", `{"host":"acme.go.tito.io"}`) |
| 42 | cx.readyClaims(t) |
| 43 | rec, err := provision.GetRecord(t.Context(), cx.rel, "bab", "titogo", "acme.go.tito.io") |
| 44 | if err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | if rec.Region != "" { |
| 48 | t.Fatal("record lost home placement", rec.Region) |
| 49 | } |
| 50 | o, err := hostclaim.Read(t.Context(), cx.rel, rec.Host) |
| 51 | if err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | if o.Absent || o.Claim.Holder == nil || o.Claim.Op.Kind != hostclaim.KindServing || o.Claim.Op.Exec == "" { |
| 55 | t.Fatalf("create incomplete: %+v", o) |
| 56 | } |
| 57 | if len(o.Claim.Touched.Regions) == 0 || o.Claim.Touched.Regions[0] != park.HomeRegion { |
| 58 | t.Fatal("lost engine region", o.Claim.Touched) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func TestPlan13HomeRecoveryStatus(t *testing.T) { |
| 63 | for _, region := range []string{"uk", park.HomeRegion} { |
| 64 | t.Run(region, func(t *testing.T) { |
| 65 | fx, token, url := moveDoorWithClaim(t, "plan13-home-status") |
| 66 | readMoveAnswer(t, http.MethodPost, url, token, `{"region":"uk","cut":"manual"}`, http.StatusAccepted) |
| 67 | fx.srv.cfg.HomeRegion = "uk" |
| 68 | now := time.Now().UTC() |
| 69 | setMove(t, fx, func(rec *provision.Record) { |
| 70 | rec.Region = "" |
| 71 | rec.Move.To = "" |
| 72 | rec.Move.State = provision.MoveCommitted |
| 73 | rec.Move.CutPhase = provision.CutFlipped |
| 74 | rec.Move.FlippedAt = &now |
| 75 | rec.Move.CommittedAt = &now |
| 76 | }) |
| 77 | rewriteClaim(t, fx, "acme.go.tito.io", func(c *hostclaim.Claim) { |
| 78 | c.Op.Kind = hostclaim.KindRelocating |
| 79 | c.Op.Phase = "pinning" |
| 80 | c.Op.Exec = "none" |
| 81 | c.Op.Participants = nil |
| 82 | c.Op.Intent = map[string]string{"region": region, "source_box": "uk-box", "home_region": "uk"} |
| 83 | }) |
| 84 | a, out := readMoveAnswer(t, http.MethodGet, url+"?detail=1", token, "", http.StatusOK) |
| 85 | if a.State != provision.MoveRelocating || waitingFor(a) != "" { |
| 86 | t.Fatalf("home has a live box: %s", out) |
| 87 | } |
| 88 | }) |
| 89 | } |
| 90 | } |
| 91 | |