| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | // codex plan-R1 2: a READ failure stops deploy; only a successful response |
| 9 | // without app_default (an older console) skips the check. |
| 10 | func TestDeployRegionPreflight(t *testing.T) { |
| 11 | uk := &apiAppDefault{Region: "uk", Name: "P-uk", Effective: "uk", RegionKnown: true, PlacementKnown: true} |
| 12 | fleet := &apiAppDefault{Region: "uk", Name: "P-uk", Effective: "fleet/acme", Reason: "fleet", RegionKnown: true, PlacementKnown: true} |
| 13 | // The console reports a home app's region as the home's own code, never |
| 14 | // "" (homeRegionOut), so naming the home equals the stored "". |
| 15 | home := &apiAppDefault{Region: "ie", Name: "P-ie", Effective: "ie", RegionKnown: true, PlacementKnown: true} |
| 16 | boom := errors.New("500 from console") |
| 17 | cases := []struct { |
| 18 | name string |
| 19 | res apiRegionsResult |
| 20 | err error |
| 21 | asked string |
| 22 | want string |
| 23 | }{ |
| 24 | {"no flag", apiRegionsResult{AppDefault: uk}, boom, "", ""}, |
| 25 | {"equal", apiRegionsResult{AppDefault: uk}, nil, "uk", ""}, |
| 26 | {"different", apiRegionsResult{AppDefault: uk}, nil, "ie", deployRegionMismatch("blog", "P-uk")}, |
| 27 | {"older console", apiRegionsResult{}, nil, "ie", ""}, |
| 28 | {"read failed", apiRegionsResult{}, boom, "ie", deployRegionUnreadable("blog", boom)}, |
| 29 | {"fleet, stored equal", apiRegionsResult{AppDefault: fleet}, nil, "uk", ""}, |
| 30 | {"fleet, stored different", apiRegionsResult{AppDefault: fleet}, nil, "ie", deployRegionMismatch("blog", "P-uk")}, |
| 31 | // codex plan-R2 3: a 200 whose stored region could not be read. |
| 32 | {"region unknown", apiRegionsResult{AppDefault: &apiAppDefault{RegionKnown: false, PlacementKnown: true}}, nil, "ie", deployRegionUnknown("blog")}, |
| 33 | // Controller ruling: --region <home> on a home app proceeds. |
| 34 | {"home app, home named", apiRegionsResult{Default: "ie", AppDefault: home}, nil, "ie", ""}, |
| 35 | {"home app, other named", apiRegionsResult{Default: "ie", AppDefault: home}, nil, "uk", deployRegionMismatch("blog", "P-ie")}, |
| 36 | } |
| 37 | for _, c := range cases { |
| 38 | err := deployRegionPreflight(c.res, c.err, "blog", c.asked) |
| 39 | got := "" |
| 40 | if err != nil { |
| 41 | got = err.Error() |
| 42 | } |
| 43 | if got != c.want { |
| 44 | t.Errorf("%s: %q, want %q", c.name, got, c.want) |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestAppRegionsNameOf(t *testing.T) { |
| 50 | r := apiRegionsResult{Regions: []apiRegionEntry{{Code: "uk", Name: "UK · London"}, {Code: "ie"}}} |
| 51 | for code, want := range map[string]string{"uk": "UK · London", "ie": "ie", "zz": "zz"} { |
| 52 | if got := r.nameOf(code); got != want { |
| 53 | t.Errorf("nameOf(%q) = %q, want %q", code, got, want) |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |