| new file mode 100644 |
| index 000000000..8d8839e7a |
| --- /dev/null |
| +++ b/internal/regionlabel/regionlabel_test.go |
| @@ -0,0 +1,145 @@ |
| +package regionlabel |
| + |
| +import ( |
| + "strings" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/edgesfile" |
| + "github.com/carlosframework/platform/internal/geo" |
| + "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// names is a deployment with its home in ie, a box in uk and a box in au. |
| +func names() Names { |
| + return NamesFrom(edgesfile.Snapshot{Complete: true, Files: []edgesfile.PlacementView{ |
| + {V: 1, Box: "box-ie", Placement: "", Region: "eu-west-1"}, |
| + {V: 1, Box: "box-uk", Placement: "uk", Region: "eu-west-2"}, |
| + {V: 1, Box: "box-au", Placement: "au", Region: "ap-southeast-2"}, |
| + }}, "ie") |
| +} |
| + |
| +func rec(region, pool, kind string) provision.Record { |
| + return provision.Record{Schema: 1, Host: "a.shop.example.com", App: "shop", Account: "bab", |
| + Region: region, Pool: pool, Kind: kind, Channel: "stable"} |
| +} |
| + |
| +// TestLabelTableInOrder is §3.1's table, first match wins, row by row. |
| +func TestLabelTableInOrder(t *testing.T) { |
| + t.Parallel() |
| + n := names() |
| + cases := []struct { |
| + name string |
| + got Label |
| + want string |
| + }{ |
| + {"a static record on a fleet: fleet first", ForRecord(rec("fleet/private", "shop", "static"), "ie", n), "fleet private"}, |
| + {"a pooled static record: pool", ForRecord(rec("uk", "shop", "static"), "ie", n), "pool shop"}, |
| + {"a static record with no pool: every region", ForRecord(rec("uk", "", "static"), "ie", n), "every region"}, |
| + {"a pooled socket-backed record keeps its region", ForRecord(rec("uk", "shop", ""), "ie", n), "London (uk)"}, |
| + {"the home's record", ForRecord(rec("", "", ""), "ie", n), "Dublin (ie)"}, |
| + {"no home named", ForCode("", "", n), "the home region"}, |
| + {"a code the table knows", ForCode("au", "ie", n), "Sydney (au)"}, |
| + {"the home's name as a code", ForCode("ie", "ie", n), "Dublin (ie)"}, |
| + {"a code no box publishes", ForCode("nz", "ie", n), "nz"}, |
| + {"a registry-only everywhere row", ForRoute(registry.Route{Host: "s.shop.example.com", Kind: "static", Region: "uk"}, "ie", n), "every region"}, |
| + {"a registry-only pooled everywhere row", ForRoute(registry.Route{Host: "s.shop.example.com", Kind: "static", Region: "uk", Pool: "shop"}, "ie", n), "pool shop"}, |
| + {"a registry-only socket row", ForRoute(registry.Route{Host: "e.shop.example.com", Kind: "instance", Region: "uk"}, "ie", n), "London (uk)"}, |
| + {"a registry-only fleet row", ForRoute(registry.Route{Host: "e.shop.example.com", Kind: "static", Region: "fleet/private"}, "ie", n), "fleet private"}, |
| + } |
| + for _, c := range cases { |
| + if c.got.Text != c.want { |
| + t.Errorf("%s: Text = %q, want %q (%+v)", c.name, c.got.Text, c.want, c.got) |
| + } |
| + } |
| + if l := ForCode("uk", "ie", n); l.Code != "uk" || l.Place != "UK · London" || l.Fleet != "" || l.Everywhere { |
| + t.Fatalf("ForCode(uk) = %+v", l) |
| + } |
| + if l := ForRecord(rec("uk", "shop", "static"), "ie", n); !l.Everywhere || l.Pool != "shop" || l.Code != "uk" { |
| + t.Fatalf("a pooled static record = %+v: the code and place still say where its record lives", l) |
| + } |
| + if l := ForCode("fleet/private", "ie", n); l.Fleet != "private" || l.Code != "fleet/private" { |
| + t.Fatalf("ForCode(fleet/private) = %+v", l) |
| + } |
| +} |
| + |
| +// TestForCodeWithoutAPlaceIsTheCode: Review Focus 1. Before the map is |
| +// read, or for a code no box publishes, the label is the code, never empty. |
| +func TestForCodeWithoutAPlaceIsTheCode(t *testing.T) { |
| + t.Parallel() |
| + for _, code := range []string{"uk", "au", "nz", "ie"} { |
| + if l := ForCode(code, "ie", Names{}); l.Text != code || l.Place != "" { |
| + t.Errorf("ForCode(%q) with no names = %+v, want the bare code", code, l) |
| + } |
| + } |
| + if l := ForCode("", "ie", Names{}); l.Text != "ie" { |
| + t.Errorf("the home with no names = %+v, want its name", l) |
| + } |
| +} |
| + |
| +// TestCityOverEveryPlace is §10's city rule over all of C23. |
| +func TestCityOverEveryPlace(t *testing.T) { |
| + t.Parallel() |
| + for _, code := range geo.AWSRegionCodes() { |
| + r, _ := geo.LookupAWSRegion(code) |
| + city := City(r.Place) |
| + if city == "" || strings.Contains(city, "·") || !strings.HasSuffix(r.Place, city) { |
| + t.Errorf("%s: City(%q) = %q", code, r.Place, city) |
| + } |
| + } |
| + for place, want := range map[string]string{ |
| + "UK · London": "London", "US East · N. Virginia": "N. Virginia", "Bahrain": "Bahrain", "UAE": "UAE", |
| + } { |
| + if got := City(place); got != want { |
| + t.Errorf("City(%q) = %q, want %q", place, got, want) |
| + } |
| + } |
| +} |
| + |
| +// TestNamesFromFolds: home folding, fleets and malformed codes skipped, |
| +// stale files counted, majority, ties to the lexically smaller region. |
| +func TestNamesFromFolds(t *testing.T) { |
| + t.Parallel() |
| + n := NamesFrom(edgesfile.Snapshot{Files: []edgesfile.PlacementView{ |
| + {V: 1, Box: "home-1", Placement: "", Region: "eu-west-1"}, |
| + {V: 1, Box: "home-2", Placement: "ie", Region: "eu-west-1", Fresh: false}, // the home's name folds to ""; stale still counts |
| + {V: 1, Box: "uk-1", Placement: "uk", Region: "eu-west-2"}, |
| + {V: 1, Box: "uk-2", Placement: "uk", Region: "eu-west-2"}, |
| + {V: 1, Box: "uk-3", Placement: "uk", Region: "eu-west-3"}, // outvoted |
| + {V: 1, Box: "tie-1", Placement: "tie", Region: "eu-west-3"}, |
| + {V: 1, Box: "tie-2", Placement: "tie", Region: "eu-central-1"}, // tie: eu-central-1 < eu-west-3 |
| + {V: 1, Box: "pi-1", Placement: "fleet/pi", Region: "eu-west-1"}, |
| + {V: 1, Box: "bad-1", Placement: "UK!", Region: "eu-west-2"}, |
| + }}, "ie") |
| + for code, want := range map[string]string{"ie": "Dublin (ie)", "uk": "London (uk)", "tie": "Frankfurt (tie)"} { |
| + if got := ForCode(code, "ie", n).Text; got != want { |
| + t.Errorf("%s: %q, want %q", code, got, want) |
| + } |
| + } |
| + for label, want := range map[string]string{"home-1": "", "home-2": "", "uk-3": "uk"} { |
| + if code, known := n.BoxPlacement(label); !known || code != want { |
| + t.Errorf("BoxPlacement(%s) = %q %v, want %q true", label, code, known, want) |
| + } |
| + } |
| + for _, label := range []string{"pi-1", "bad-1"} { |
| + if _, known := n.BoxPlacement(label); known { |
| + t.Errorf("%s: a fleet or malformed placement must not be in the box map", label) |
| + } |
| + } |
| +} |
| + |
| +// TestBoxPlacementKnownHomeVersusUnknown (labels 4): a home box is known |
| +// with code "", which is not the same answer as a box with no entry. |
| +func TestBoxPlacementKnownHomeVersusUnknown(t *testing.T) { |
| + t.Parallel() |
| + n := names() |
| + if code, known := n.BoxPlacement("box-ie"); !known || code != "" { |
| + t.Fatalf("home box = %q %v, want \"\" true", code, known) |
| + } |
| + if code, known := n.BoxPlacement("box-gone"); known || code != "" { |
| + t.Fatalf("unknown box = %q %v, want \"\" false", code, known) |
| + } |
| + if code, known := (Names{}).BoxPlacement("box-ie"); known || code != "" { |
| + t.Fatal("the zero Names knows no box") |
| + } |
| +} |