package hostops import ( "context" "github.com/carlosframework/platform/internal/hostclaim" "github.com/carlosframework/platform/internal/park" "github.com/carlosframework/platform/internal/provision" "testing" ) func TestPlan13PreFlipHomeConversion(t *testing.T) { for _, direction := range []string{"source-home", "target-home", "named"} { for _, route := range []string{"grant", "held"} { t.Run(direction+"/"+route, func(t *testing.T) { w := newC7World(t, func(c *hostclaim.Claim, r *provision.Record, _ *AppAdmission) { if direction == "source-home" { r.Region, r.Move.From = "", "" c.Op.Intent["home_region"] = "ie" } if direction == "target-home" { r.Move.To = "" c.Op.Intent["to"] = park.HomeRegion c.Op.Intent["home_region"] = "uk" } }) if direction == "source-home" { w.control.homeRegion = "ie" } if direction == "target-home" { w.control.homeRegion = "uk" } run := func() error { if route == "held" { return RecoverTerminatedMovingHost(t.Context(), w.productionData(t, w.raw), w.mh, w.authority, w.host, w.ref, w.now) } return w.control.RunRecovery(t.Context(), func(ctx context.Context) error { return runMoveRecovery(ctx, w.claim(t), func(ctx context.Context, g *MoveRecoveryGrant) error { return g.Convert(ctx, w.ref) }, w.authority) }) } for i := 0; i < 3; i++ { if err := run(); err != nil { t.Fatalf("pass %d: %v", i, err) } } c := w.claim(t).Claim if c.Op.Kind != hostclaim.KindRelocating || c.Op.Intent["region"] != "ie" { t.Fatalf("claim: %+v", c.Op) } r, err := provision.GetRecord(t.Context(), w.raw, "bab", "app", w.host) if err != nil { t.Fatal(err) } from, to := "ie", "uk" if direction == "source-home" { from = "" } if direction == "target-home" { to = "" } if r.Region != from || r.Move.From != from || r.Move.To != to || r.Move.Relocation.Region != from { t.Fatalf("rewritten record: %+v", r) } tag := w.claim(t).ETag rtag := w.recordTag(t) if err := run(); err != nil { t.Fatal(err) } if tag != w.claim(t).ETag || rtag != w.recordTag(t) { t.Fatal("replay wrote") } }) } } } func TestPlan13PreFlipUnboundHomeRefuses(t *testing.T) { w := newC7World(t, func(c *hostclaim.Claim, r *provision.Record, _ *AppAdmission) { r.Region, r.Move.From = "", "" }) tag := w.recordTag(t) if err := RecoverTerminatedMovingHost(t.Context(), w.productionData(t, w.raw), w.mh, w.authority, w.host, w.ref, w.now); err == nil { t.Fatal("unbound home accepted") } if tag != w.recordTag(t) { t.Fatal("refusal wrote") } }