| 1 | package hostops |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "github.com/carlosframework/platform/internal/hostactors" |
| 6 | "github.com/carlosframework/platform/internal/hostclaim" |
| 7 | "github.com/carlosframework/platform/internal/park" |
| 8 | "github.com/carlosframework/platform/internal/provision" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestPlan13BeginClaimMoveHomeAndNamed(t *testing.T) { |
| 13 | for _, tc := range []struct{ name, home, from, to, target, want string }{ |
| 14 | {"out-home", "ie", "", "uk", "uk-box", "uk"}, |
| 15 | {"into-home", "uk", "ie", "", "uk-box", park.HomeRegion}, |
| 16 | {"named", "", "ie", "uk", "uk-box", "uk"}, |
| 17 | } { |
| 18 | t.Run(tc.name, func(t *testing.T) { |
| 19 | f := newMoveStartFixture(t, func(r *hostactors.Roster) { r.HomeRegion = tc.home }) |
| 20 | rec, etag, err := provision.GetRecordETag(t.Context(), f.raw, "bab", "app", f.host) |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | rec.Region = tc.from |
| 25 | raw, err := json.Marshal(rec) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | if _, err = f.raw.Store.Put(t.Context(), f.raw.recordKey, raw, etag); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | o := f.read(t) |
| 33 | // This is an existing claim, including its named touched region. |
| 34 | h, err := f.authority.RegisterMove(t.Context(), o, "canonical", moveStartID) |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | got, err := BeginClaimMove(t.Context(), f.raw, f.authority.Directory, h, o, moveStartID, "operator", tc.target, tc.to, f.now, tc.home) |
| 39 | if err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | if got.Claim.Op.Intent["to"] != tc.want || got.Claim.Touched.Regions[0] != "ie" { |
| 43 | t.Fatalf("%+v", got.Claim) |
| 44 | } |
| 45 | if got.Claim.Op.Intent["home_region"] != tc.home { |
| 46 | t.Fatal("lost home binding") |
| 47 | } |
| 48 | _ = hostclaim.KindMoving |
| 49 | }) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestPlan13MoveStartRefusesFalseHome(t *testing.T) { |
| 54 | f := newMoveStartFixture(t, func(r *hostactors.Roster) { r.HomeRegion = "ie" }) |
| 55 | o := f.read(t) |
| 56 | h, err := f.authority.RegisterMove(t.Context(), o, "canonical", moveStartID) |
| 57 | if err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | if _, err := BeginClaimMove(t.Context(), f.raw, f.authority.Directory, h, o, moveStartID, "operator", "uk-box", "", f.now, "uk"); err == nil { |
| 61 | t.Fatal("operation home overrode signed fleet home") |
| 62 | } |
| 63 | if f.read(t).ETag != o.ETag { |
| 64 | t.Fatal("refusal mutated claim") |
| 65 | } |
| 66 | } |
| 67 | |