region labels: create-time lines name the label, and app region lines print whenever answered (plan 2 task 2)
TestE2EInstancesThroughConsole's create line now ends "the home region": that console names no home, and every create is labelled (labels 9).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
10 files changed,
+238
−6
apps_region.go+10 −1apps_region_test.go+65 −0canary.go+1 −1canary_region_test.go+3 −0deploy.go+1 −1deploy_region_test.go+2 −0instances_cli.go+5 −2instances_e2e_test.go+3 −1region_copy.go+16 −0region_labels_e2e_test.go+132 −0
diff --git a/apps_region.go b/apps_region.go| index 0fc9acf60..dfb60024f 100644 |
| --- a/apps_region.go |
| +++ b/apps_region.go |
| @@ -22,7 +22,12 @@ import ( |
| // onFleet is true when the create named a fleet (--place fleet/<name>). The |
| // fleet governs where its instances run, so C13b ("--region was not applied", |
| // "this app's instances start in <home>") would be wrong: it prints nothing. |
| +// |
| +// A console that sends region_label gets RC-5 to RC-8 whenever it answered a |
| +// region and a source, one region included (region labels 9); an older |
| +// console keeps C9 to C12 under the two-region rule, byte for byte. |
| func printAppRegion(w io.Writer, res apiCreateAppResult, asked string, onFleet bool) { |
| + label := cleanPlace(res.RegionLabel) |
| switch { |
| case res.Region == "" && res.RegionSource == "": |
| if asked != "" { |
| @@ -33,8 +38,12 @@ func printAppRegion(w io.Writer, res apiCreateAppResult, asked string, onFleet b |
| case asked != "" && res.RegionSource != "chosen" && onFleet: |
| // The fleet decides placement. Say nothing. |
| case asked != "" && res.RegionSource != "chosen": |
| - fmt.Fprintln(w, regionNotAppliedSwitchOff(res.RegionName)) |
| + fmt.Fprintln(w, regionNotAppliedSwitchOff(placeOr(label, res.RegionName))) |
| + case label != "": |
| + // A labels console: whenever it answered (region labels §5 row 18). |
| + fmt.Fprintln(w, regionLabelledLine(label, res.RegionSource)) |
| case res.RegionsAvailable >= 2: |
| + // An older console: today's line under today's rule. |
| fmt.Fprintln(w, regionCreatedLine(res.RegionName, res.RegionSource)) |
| } |
| } |
diff --git a/apps_region_test.go b/apps_region_test.go| index 854dce0f6..5d6123d3e 100644 |
| --- a/apps_region_test.go |
| +++ b/apps_region_test.go |
| @@ -200,3 +200,68 @@ func TestPrintAppRegionOnFleet(t *testing.T) { |
| t.Fatalf("chosen on a fleet: %q", b.String()) |
| } |
| } |
| + |
| +// TestPrintAppRegionWithLabels: a labels console (spec rev 5 §6, labels 9): |
| +// RC-5 to RC-8 whenever it answered a region and a source, one region |
| +// included. |
| +func TestPrintAppRegionWithLabels(t *testing.T) { |
| + lab := func(source string, avail int) apiCreateAppResult { |
| + return apiCreateAppResult{Region: "uk", RegionName: "UK · London", RegionLabel: "London (uk)", RegionSource: source, RegionsAvailable: avail} |
| + } |
| + for _, c := range []struct { |
| + name string |
| + res apiCreateAppResult |
| + asked string |
| + want string |
| + }{ |
| + {"timezone", lab("timezone", 2), "", "Region: London (uk), from your timezone. Use --region to choose another.\n"}, |
| + {"ip", lab("ip", 2), "", "Region: London (uk), from your network location. Use --region to choose another.\n"}, |
| + {"default, one region", lab("default", 1), "", "Region: London (uk), the platform default. Use --region to choose another.\n"}, |
| + {"chosen", lab("chosen", 3), "uk", "Region: London (uk).\n"}, |
| + {"switch off, asked", lab("default", 1), "au", |
| + "This console is not assigning app regions yet, so --region was not applied. This app's instances start in London (uk).\n"}, |
| + // A label not fit to print is no label: today's line, today's rule. |
| + {"unprintable label", apiCreateAppResult{Region: "uk", RegionName: "UK · London", RegionLabel: "London\x1b[0m", RegionSource: "timezone", RegionsAvailable: 2}, "", |
| + "Region: UK · London (from your timezone). Use --region to choose another.\n"}, |
| + } { |
| + var b bytes.Buffer |
| + printAppRegion(&b, c.res, c.asked, false) |
| + if b.String() != c.want { |
| + t.Errorf("%s: %q, want %q", c.name, b.String(), c.want) |
| + } |
| + } |
| +} |
| + |
| +// TestPrintAppRegionOlderConsoleIsToday (Astra plan finding 6): no |
| +// region_label, so C9 to C12 in their own shape, and nothing with fewer |
| +// than two regions available, byte for byte as at the base. |
| +func TestPrintAppRegionOlderConsoleIsToday(t *testing.T) { |
| + old := func(avail int) apiCreateAppResult { |
| + return apiCreateAppResult{Region: "ie", RegionName: "Ireland · Dublin", RegionSource: "default", RegionsAvailable: avail} |
| + } |
| + for _, c := range []struct { |
| + name string |
| + res apiCreateAppResult |
| + want string |
| + }{ |
| + {"one region", old(1), ""}, |
| + {"two regions", old(2), "Region: Ireland · Dublin (the platform default). Use --region to choose another.\n"}, |
| + } { |
| + var b bytes.Buffer |
| + printAppRegion(&b, c.res, "", false) |
| + if b.String() != c.want { |
| + t.Errorf("%s: %q, want today's %q", c.name, b.String(), c.want) |
| + } |
| + } |
| +} |
| + |
| +// TestInstanceCreatedLineNamesTheFleet (spec §4; Astra plan finding 3): a |
| +// fleet create's answer carries region_label and no region_name. |
| +func TestInstanceCreatedLineNamesTheFleet(t *testing.T) { |
| + if got := instanceCreatedLine(apiInstanceRecord{Host: "a.cart.example.com", Channel: "stable", RegionLabel: "fleet private"}, ""); got != "a.cart.example.com (channel stable, fleet private)" { |
| + t.Fatalf("fleet create: %q", got) |
| + } |
| + if got := instanceCreatedLine(apiInstanceRecord{Host: "a.cart.example.com", Channel: "stable"}, ""); got != "a.cart.example.com (channel stable)" { |
| + t.Fatalf("an older console's fleet create: %q, want today's", got) |
| + } |
| +} |
diff --git a/canary.go b/canary.go| index ff8b411de..a9b078c28 100644 |
| --- a/canary.go |
| +++ b/canary.go |
| @@ -347,7 +347,7 @@ func canaryRegionCheck(res apiRegionsResult, readErr error, app, name, asked str |
| have = res.Default |
| } |
| if have != asked { |
| - return canaryPreflight{}, errors.New(canaryRegionMismatch(host, res.nameOf(have))) |
| + return canaryPreflight{}, errors.New(canaryRegionMismatch(host, res.labelOf(have))) |
| } |
| } |
| return canaryPreflight{Host: host, Rec: rec, Found: found}, nil |
diff --git a/canary_region_test.go b/canary_region_test.go| index cd9a05e93..027d589b3 100644 |
| --- a/canary_region_test.go |
| +++ b/canary_region_test.go |
| @@ -31,6 +31,8 @@ func TestCanaryRegionCheck(t *testing.T) { |
| return &d |
| } |
| plain := apiRegionsResult{Regions: names, Default: "ie", AppDefault: known(apiAppDefault{Region: "uk", Effective: "uk"})} |
| + labelled := apiRegionsResult{Default: "ie", AppDefault: known(apiAppDefault{Region: "uk", Effective: "uk"}), |
| + Regions: []apiRegionEntry{{Code: "ie", Name: "P-ie", Label: "Dublin (ie)"}, {Code: "uk", Name: "P-uk", Label: "London (uk)"}}} |
| fleet := apiRegionsResult{Regions: names, Default: "ie", AppDefault: known(apiAppDefault{Region: "uk", Effective: "fleet/acme", Reason: "fleet"})} |
| older := apiRegionsResult{Regions: names, Default: "ie"} // no app_default |
| unread := apiRegionsResult{Regions: names, Default: "ie", AppDefault: &apiAppDefault{Region: "uk", Effective: "uk", RegionKnown: true, Reason: "policy-unreadable"}} |
| @@ -57,6 +59,7 @@ func TestCanaryRegionCheck(t *testing.T) { |
| {"existing, same", plain, nil, []apiInstanceRecord{existingUK}, nil, "uk", ""}, |
| {"existing home read as its name", plain, nil, []apiInstanceRecord{existingHome}, nil, "ie", ""}, |
| {"existing, different, named by place", plain, nil, []apiInstanceRecord{existingUK}, nil, "ie", canaryRegionMismatch(host, "P-uk")}, |
| + {"existing, different, named by label", labelled, nil, []apiInstanceRecord{existingUK}, nil, "ie", canaryRegionMismatch(host, "London (uk)")}, |
| {"fleet, first run", fleet, nil, nil, nil, "uk", canaryRegionOnFleet("blog", "acme")}, |
| {"fleet, repeat", fleet, nil, []apiInstanceRecord{existingUK}, nil, "uk", canaryRegionOnFleet("blog", "acme")}, |
| {"older console, first run", older, nil, nil, nil, "uk", canaryFleetUnknown("blog")}, |
diff --git a/deploy.go b/deploy.go| index 553f7ec3f..9c4279606 100644 |
| --- a/deploy.go |
| +++ b/deploy.go |
| @@ -53,7 +53,7 @@ func deployRegionPreflight(res apiRegionsResult, readErr error, app, asked strin |
| if res.AppDefault.Region == asked { |
| return nil |
| } |
| - return errors.New(deployRegionMismatch(app, res.AppDefault.Name)) |
| + return errors.New(deployRegionMismatch(app, placeOr(res.AppDefault.Label, res.AppDefault.Name))) |
| } |
| // The deploy-only asset sentences (edge asset cache copy record). The --assets |
diff --git a/deploy_region_test.go b/deploy_region_test.go| index e1b98accb..8d7685c0b 100644 |
| --- a/deploy_region_test.go |
| +++ b/deploy_region_test.go |
| @@ -9,6 +9,7 @@ import ( |
| // without app_default (an older console) skips the check. |
| func TestDeployRegionPreflight(t *testing.T) { |
| uk := &apiAppDefault{Region: "uk", Name: "P-uk", Effective: "uk", RegionKnown: true, PlacementKnown: true} |
| + labelled := &apiAppDefault{Region: "uk", Name: "P-uk", Label: "London (uk)", Effective: "uk", RegionKnown: true, PlacementKnown: true} |
| fleet := &apiAppDefault{Region: "uk", Name: "P-uk", Effective: "fleet/acme", Reason: "fleet", RegionKnown: true, PlacementKnown: true} |
| // The console reports a home app's region as the home's own code, never |
| // "" (homeRegionOut), so naming the home equals the stored "". |
| @@ -24,6 +25,7 @@ func TestDeployRegionPreflight(t *testing.T) { |
| {"no flag", apiRegionsResult{AppDefault: uk}, boom, "", ""}, |
| {"equal", apiRegionsResult{AppDefault: uk}, nil, "uk", ""}, |
| {"different", apiRegionsResult{AppDefault: uk}, nil, "ie", deployRegionMismatch("blog", "P-uk")}, |
| + {"different, labelled", apiRegionsResult{AppDefault: labelled}, nil, "ie", deployRegionMismatch("blog", "London (uk)")}, |
| {"older console", apiRegionsResult{}, nil, "ie", ""}, |
| {"read failed", apiRegionsResult{}, boom, "ie", deployRegionUnreadable("blog", boom)}, |
| {"fleet, stored equal", apiRegionsResult{AppDefault: fleet}, nil, "uk", ""}, |
diff --git a/instances_cli.go b/instances_cli.go| index 974d65ed4..3a9ceb358 100644 |
| --- a/instances_cli.go |
| +++ b/instances_cli.go |
| @@ -377,10 +377,13 @@ func seedCopyingLine(source, host string) string { |
| // host": today's "host (channel X<env>)" byte for byte from an older |
| // console, C14 when the console names the place. |
| func instanceCreatedLine(rec apiInstanceRecord, envSuffix string) string { |
| - if rec.RegionName == "" { |
| + // The console's label when it sent one fit to print (region labels §5 |
| + // row 17), else the full place an older console sent. |
| + place := placeOr(rec.RegionLabel, rec.RegionName) |
| + if place == "" { |
| return fmt.Sprintf("%s (channel %s%s)", rec.Host, rec.Channel, envSuffix) |
| } |
| - return fmt.Sprintf("%s (%s%s)", rec.Host, instanceCreatedSuffix(rec.Channel, rec.RegionName), envSuffix) |
| + return fmt.Sprintf("%s (%s%s)", rec.Host, instanceCreatedSuffix(rec.Channel, place), envSuffix) |
| } |
| // replicaReqFromFlags builds the create body's replica slice from the CLI's |
diff --git a/instances_e2e_test.go b/instances_e2e_test.go| index 8a7720e5d..7c7f71486 100644 |
| --- a/instances_e2e_test.go |
| +++ b/instances_e2e_test.go |
| @@ -116,7 +116,9 @@ func TestE2EInstancesThroughConsole(t *testing.T) { |
| host := "titogo." + domain |
| buf.Reset() |
| runInstancesCreate([]string{"-app", "titogo", "-account", "ops", "-host", host}) |
| - if want := "created instance " + host + " (channel beta)"; !strings.Contains(buf.String(), want) { |
| + // This console names no home, so the create says "the home region" |
| + // (region labels spec §3.1, RC-2; labels 9: every create is labelled). |
| + if want := "created instance " + host + " (channel beta, the home region)"; !strings.Contains(buf.String(), want) { |
| t.Fatalf("create stdout = %q, want %q", buf.String(), want) |
| } |
| rec, err := provision.GetRecord(context.Background(), relStore, release.OpsAccount, "titogo", host) |
diff --git a/region_copy.go b/region_copy.go| index 6d02a3160..b71c8f0a4 100644 |
| --- a/region_copy.go |
| +++ b/region_copy.go |
| @@ -20,6 +20,22 @@ func regionCreatedLine(place, source string) string { |
| return fmt.Sprintf("Region: %s (the platform default). Use --region to choose another.", place) |
| } |
| +// regionLabelledLine is RC-5 to RC-8 (region labels spec rev 5 §6): C9 to |
| +// C12 for a console that sends the region's label, which carries its own |
| +// "(code)", so the source follows a comma. An older console's answers keep |
| +// regionCreatedLine. |
| +func regionLabelledLine(label, source string) string { |
| + switch source { |
| + case "timezone": |
| + return fmt.Sprintf("Region: %s, from your timezone. Use --region to choose another.", label) // RC-5 |
| + case "ip": |
| + return fmt.Sprintf("Region: %s, from your network location. Use --region to choose another.", label) // RC-6 |
| + case "chosen": |
| + return fmt.Sprintf("Region: %s.", label) // RC-7 |
| + } |
| + return fmt.Sprintf("Region: %s, the platform default. Use --region to choose another.", label) // RC-8 |
| +} |
| + |
| // regionNotRecordedOldConsole is C13a. |
| const regionNotRecordedOldConsole = "This console does not record an app region yet, so --region was not applied." |
diff --git a/region_labels_e2e_test.go b/region_labels_e2e_test.go| new file mode 100644 |
| index 000000000..ac2b560ea |
| --- /dev/null |
| +++ b/region_labels_e2e_test.go |
| @@ -0,0 +1,132 @@ |
| +package main |
| + |
| +import ( |
| + "bytes" |
| + "context" |
| + "fmt" |
| + "net/http" |
| + "net/http/httptest" |
| + "path/filepath" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/console" |
| + "github.com/carlosframework/platform/internal/edgesfile" |
| + "github.com/carlosframework/platform/internal/release" |
| + "github.com/carlosframework/platform/internal/status" |
| +) |
| + |
| +// labelsE2E is a real console (HomeRegion ie, apps domain oncarlos.test, |
| +// the ops account bab owning app shop) over a DirStore holding two boxes' |
| +// edges files, its region name map read, and a signed-in CLI. |
| +type labelsE2E struct { |
| + rel *release.DirStore |
| + cst *console.Store |
| + srv *console.Server |
| + regPath string |
| + buf *bytes.Buffer |
| +} |
| + |
| +const ( |
| + labelsAddr = "paul@keymail.dev" |
| + labelsDomain = "bab.oncarlos.test" |
| +) |
| + |
| +var ( |
| + labelsHomeBox = status.Label("ie-machine") |
| + labelsUKBox = status.Label("uk-machine") |
| +) |
| + |
| +func newLabelsE2E(t *testing.T) *labelsE2E { |
| + t.Helper() |
| + useTempCredentials(t) |
| + clearDeploymentEnv(t) |
| + tmp := t.TempDir() |
| + rel := release.NewDirStore(filepath.Join(tmp, "store")) |
| + seed, pubB64, err := release.GenerateKey() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + priv, err := release.ParseSigningKey(seed, "e2e") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + pub, err := release.ParsePublicKey(pubB64, "e2e") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + cst, err := console.Open(filepath.Join(tmp, "console.db")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { cst.Close() }) |
| + if err := cst.SetOperator(labelsAddr, true); err != nil { |
| + t.Fatal(err) |
| + } |
| + var srv *console.Server |
| + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { srv.ServeHTTP(w, r) })) |
| + t.Cleanup(ts.Close) |
| + regPath := filepath.Join(tmp, "registry.db") |
| + srv, err = console.NewServer(console.Config{ |
| + Origin: ts.URL, Keymail: "http://keymail.invalid", Registry: regPath, |
| + Releases: rel, ReleasePub: pub, Key: priv, AppScheme: "http", |
| + AppsDomain: "oncarlos.test", HomeRegion: "ie", |
| + }, cst) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + now := time.Now() |
| + if err := cst.AddMember(console.OpsAccountID, labelsAddr, console.RoleOwner, now); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := cst.CreateAppIn(console.OpsAccountID, "shop", labelsAddr, now); err != nil { |
| + t.Fatal(err) |
| + } |
| + // The boxes as they publish themselves: the home box (placement "") in |
| + // eu-west-1, and a uk box in eu-west-2, each with a dial address. |
| + for _, f := range []struct{ box, placement, aws string }{ |
| + {labelsHomeBox, "", "eu-west-1"}, {labelsUKBox, "uk", "eu-west-2"}, |
| + } { |
| + body := fmt.Sprintf(`{"v":1,"box":%q,"eip":"192.0.2.10","region":%q,"placement":%q,"hosts":{}}`, f.box, f.aws, f.placement) |
| + if _, err := rel.Put(context.Background(), edgesfile.Key(f.box), []byte(body), ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if !srv.LoadRegionNames(context.Background()) { |
| + t.Fatal("the console did not read its region names") |
| + } |
| + token, expiresAt := e2eDeviceLogin(t, ts.URL, cst, labelsAddr, "e2e@laptop") |
| + credFile, err := loadCredentials() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + credFile.put(credential{Console: ts.URL, Address: labelsAddr, Token: token, ExpiresAt: expiresAt}) |
| + if err := credFile.save(); err != nil { |
| + t.Fatal(err) |
| + } |
| + return &labelsE2E{rel: rel, cst: cst, srv: srv, regPath: regPath, buf: captureStdout(t)} |
| +} |
| + |
| +// out runs one CLI verb in process and returns what it printed. |
| +func (e *labelsE2E) out(t *testing.T, fn func([]string), args ...string) string { |
| + t.Helper() |
| + e.buf.Reset() |
| + fn(args) |
| + return e.buf.String() |
| +} |
| + |
| +// TestRegionLabelsCreateLinesThroughTheConsole: surfaces 17 and 18, and |
| +// labels 9: carlos apps create against a switch-off console prints RC-8. |
| +func TestRegionLabelsCreateLinesThroughTheConsole(t *testing.T) { |
| + e := newLabelsE2E(t) |
| + e.out(t, runInstancesEnable, "-app", "shop", "-account", "ops", "-channel", "beta") |
| + if got := e.out(t, runInstancesCreate, "-app", "shop", "-account", "ops", "-host", "uk1."+labelsDomain, "-region", "uk"); !strings.Contains(got, |
| + "created instance uk1."+labelsDomain+" (channel beta, London (uk))") { |
| + t.Fatalf("instances create:\n%s", got) |
| + } |
| + if got := e.out(t, runAppsCreate, "-app", "cart", "-account", "ops"); !strings.Contains(got, |
| + "Region: Dublin (ie), the platform default. Use --region to choose another.") { |
| + t.Fatalf("apps create on a switch-off console:\n%s", got) |
| + } |
| +} |