| 1 | package hostops |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "github.com/carlosframework/platform/internal/hostclaim" |
| 6 | "github.com/carlosframework/platform/internal/park" |
| 7 | "github.com/carlosframework/platform/internal/provision" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestPlan13FenceRecoveryHomeAndNamed(t *testing.T) { |
| 13 | for _, which := range []string{"source-home", "target-home", "named"} { |
| 14 | t.Run(which, func(t *testing.T) { |
| 15 | w := newC7World(t, func(c *hostclaim.Claim, r *provision.Record, _ *AppAdmission) { |
| 16 | c.Op.Kind, c.Op.Phase, c.Op.Exec = hostclaim.KindLanding, hostclaim.PhaseProbing, "source" |
| 17 | c.Op.Intent["source"], c.Op.Intent["target"], c.Op.Intent["to"], c.Op.Intent["target_enrollment"] = "target", "source", "ie", strings.Repeat("a", 32) |
| 18 | c.Op.Participants = []string{"target", "source"} |
| 19 | c.Obligations = nil |
| 20 | c.Op.Journal = map[string]string{hostclaim.JournalSourceStamp: "1", hostclaim.JournalSrcGen: "src"} |
| 21 | r.Region = "uk" |
| 22 | r.Move.From, r.Move.To, r.Move.State = "uk", "ie", provision.MoveRollingBack |
| 23 | if which == "source-home" { |
| 24 | r.Region = "" |
| 25 | r.Move.From = "" |
| 26 | c.Op.Intent["home_region"] = "uk" |
| 27 | } |
| 28 | if which == "target-home" { |
| 29 | r.Move.To = "" |
| 30 | c.Op.Intent["to"] = park.HomeRegion |
| 31 | c.Op.Intent["home_region"] = "ie" |
| 32 | } |
| 33 | at := r.CreatedAt |
| 34 | key := "control/box-fences/" + strings.Repeat("a", 32) + ".json" |
| 35 | r.Move.Rollback = &provision.MoveRollback{ID: "rb_abcdefghijklmnopqrstuvwxyz", Reason: provision.RollbackTargetFenced, Evidence: key, RequestedAt: &at, TargetPhase: provision.RbTargetLost, LossRef: key, LostAt: &at, SourcePhase: provision.RbSourceWaiting} |
| 36 | }) |
| 37 | if which == "source-home" { |
| 38 | w.authority.home = "uk" |
| 39 | w.control.homeRegion = "uk" |
| 40 | } |
| 41 | if which == "target-home" { |
| 42 | w.authority.home = "ie" |
| 43 | w.control.homeRegion = "ie" |
| 44 | } |
| 45 | run := func() error { |
| 46 | return w.control.RunRecovery(t.Context(), func(ctx context.Context) error { |
| 47 | return runMoveRecovery(ctx, w.claim(t), func(ctx context.Context, g *MoveRecoveryGrant) error { |
| 48 | _, err := g.ReleaseFencedTarget(ctx, w.ref) |
| 49 | return err |
| 50 | }, w.authority) |
| 51 | }) |
| 52 | } |
| 53 | if err := run(); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | c := w.claim(t).Claim |
| 57 | if c.Op.Phase != hostclaim.PhaseReleased { |
| 58 | t.Fatal(c.Op) |
| 59 | } |
| 60 | want := "ie" |
| 61 | if which == "target-home" { |
| 62 | want = park.HomeRegion |
| 63 | } |
| 64 | for _, ob := range c.Obligations { |
| 65 | if ob.Region != want { |
| 66 | t.Fatalf("%+v", ob) |
| 67 | } |
| 68 | } |
| 69 | if err := run(); err != nil { |
| 70 | t.Fatal("record completion", err) |
| 71 | } |
| 72 | rec, err := provision.GetRecord(t.Context(), w.raw, "bab", "app", w.host) |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | if which == "source-home" && (rec.Region != "" || rec.Move.From != "") { |
| 77 | t.Fatal("source placement rewritten") |
| 78 | } |
| 79 | if which == "target-home" && rec.Move.To != "" { |
| 80 | t.Fatal("target placement rewritten") |
| 81 | } |
| 82 | if which == "target-home" { |
| 83 | o := w.claim(t) |
| 84 | for _, ob := range o.Claim.Obligations { |
| 85 | var e error |
| 86 | o, e = hostclaim.Update(t.Context(), w.raw, o, hostclaim.CompleteObligation(ob.Op, ob.ID)) |
| 87 | if e != nil { |
| 88 | t.Fatal(e) |
| 89 | } |
| 90 | } |
| 91 | o, err = hostclaim.Update(t.Context(), w.raw, o, func(c *hostclaim.Claim) error { |
| 92 | c.Obligations = append(c.Obligations, hostclaim.Obligation{ID: "retained", Op: c.Op.ID, Kind: "park-generation", Region: "ie", Owner: "region:ie", Generation: "old", Retirement: "retirement"}) |
| 93 | return nil |
| 94 | }) |
| 95 | if err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | stamp := func() error { |
| 99 | return w.control.RunRecovery(t.Context(), func(ctx context.Context) error { |
| 100 | return runMoveRecovery(ctx, w.claim(t), func(ctx context.Context, g *MoveRecoveryGrant) error { return g.StampLostMoveTargetParked(ctx) }, w.authority) |
| 101 | }) |
| 102 | } |
| 103 | if err := stamp(); err == nil { |
| 104 | t.Fatal("parked stamp ignored retained-name chore") |
| 105 | } |
| 106 | rec, err = provision.GetRecord(t.Context(), w.raw, "bab", "app", w.host) |
| 107 | if err != nil || rec.Move.Rollback.TargetParkedAt != nil { |
| 108 | t.Fatal("premature parked stamp", err) |
| 109 | } |
| 110 | if _, err = hostclaim.Update(t.Context(), w.raw, o, hostclaim.CompleteObligation(o.Claim.Op.ID, "retained")); err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | if err := stamp(); err != nil { |
| 114 | t.Fatal("completed target refused", err) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | }) |
| 119 | } |
| 120 | } |
| 121 | |