| index b842db0a4..b7505cd20 100644 |
| --- a/domains_cli_test.go |
| +++ b/domains_cli_test.go |
| @@ -12,6 +12,7 @@ package main |
| import ( |
| "context" |
| "encoding/json" |
| + "errors" |
| "net/http" |
| "net/http/httptest" |
| "os/exec" |
| @@ -282,6 +283,12 @@ type fakeDomainsConsole struct { |
| // lastPlacementsPath records the URL it was asked for. |
| placements map[string]any |
| lastPlacementsPath string |
| + // The placements PUT: the body it was sent, raw by key so a test can |
| + // tell an absent key from a false one, and its answer. putStatus 0 is |
| + // 200 with placements; putBody, when set, is written verbatim. |
| + lastPlacementsPut map[string]json.RawMessage |
| + putStatus int |
| + putBody string |
| } |
| |
| func newFakeDomainsConsole(t *testing.T) *fakeDomainsConsole { |
| @@ -310,6 +317,18 @@ func newFakeDomainsConsole(t *testing.T) *fakeDomainsConsole { |
| case r.Method == http.MethodDelete: |
| fc.lastDetachPath = r.URL.Path |
| json.NewEncoder(w).Encode(map[string]any{"host": "x", "removed": fc.detachRemoved}) |
| + case r.Method == http.MethodPut && strings.HasSuffix(r.URL.Path, "/placements"): |
| + fc.lastPlacementsPath = r.URL.Path |
| + fc.lastPlacementsPut = nil |
| + json.NewDecoder(r.Body).Decode(&fc.lastPlacementsPut) |
| + if fc.putStatus != 0 { |
| + w.WriteHeader(fc.putStatus) |
| + } |
| + if fc.putBody != "" { |
| + w.Write([]byte(fc.putBody)) |
| + return |
| + } |
| + json.NewEncoder(w).Encode(fc.placements) |
| case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/placements"): |
| fc.lastPlacementsPath = r.URL.Path |
| json.NewEncoder(w).Encode(fc.placements) |
| @@ -731,6 +750,15 @@ func TestDomainsCLIUsageErrors(t *testing.T) { |
| {"list no app", []string{"domains", "list"}, "no deployment target configured"}, |
| {"paths no app", []string{"domains", "paths", "ti.to"}, "no deployment target configured"}, |
| {"paths no positional host", []string{"domains", "paths", "-app", "tito"}, "usage: carlos domains paths"}, |
| + {"placements no positional host", []string{"domains", "placements", "-app", "tito"}, "usage: carlos domains placements"}, |
| + // A --set value is region=instance; without the "=" it is refused |
| + // before anything is sent (RD-42). |
| + {"placements set without =", []string{"domains", "placements", "-app", "tito", "-set", "shop-uk.example.net", "shop.example.com"}, |
| + "write each --set as region=instance, like uk=shop-uk.example.net"}, |
| + // The capabilities take effect only with --set: on a read they |
| + // would be ignored, so naming one there is a usage error. |
| + {"placements wildcard without set", []string{"domains", "placements", "-app", "tito", "-wildcard", "shop.example.com"}, |
| + "usage: carlos domains placements"}, |
| } { |
| t.Run(tc.name, func(t *testing.T) { |
| out, err := run(tc.args...) |
| @@ -908,3 +936,239 @@ func TestRunDomainsPlacementsPrintsTheReason(t *testing.T) { |
| t.Fatalf("stdout tells the member to point DNS with no ready region:\n%s", out) |
| } |
| } |
| + |
| +// regionalPlacementsAnswer is a regional claim's placements body: ie ready |
| +// from annie, uk not ready with no serving box. |
| +func regionalPlacementsAnswer() map[string]any { |
| + return map[string]any{ |
| + "host": "shop.example.com", "mode": "regional", "route": "shop-ie.example.net", |
| + "placements": []map[string]any{ |
| + {"region": "ie", "target": "shop-ie.example.net", "ready": true, |
| + "boxes": []map[string]any{{"box": "annie", "eip": "203.0.113.10", "aws_region": "eu-west-1", "release": "v0.31.0", "counted": true}}}, |
| + {"region": "uk", "target": "shop-uk.example.net", "ready": false, "reason": "no-serving-box", "boxes": []any{}}, |
| + }, |
| + } |
| +} |
| + |
| +// TestRunDomainsPlacementsSendsTheWholeSet (Review Focus 3): every --set is |
| +// sent in the order given and exactly as typed, the same region twice |
| +// included (the console refuses that, RD-18, not the CLI); no capability |
| +// key is sent when no flag is named. The answer prints RD-15 and the view. |
| +func TestRunDomainsPlacementsSendsTheWholeSet(t *testing.T) { |
| + fc := domainsCLIFixture(t) |
| + fc.placements = regionalPlacementsAnswer() |
| + buf := captureStdout(t) |
| + |
| + runDomains([]string{"placements", "-app", "tito", "-account", "bab", |
| + "-set", "uk=shop-uk.example.net", "-set", "IE=Shop-IE.example.net", "-set", "uk=other.example.net", "shop.example.com"}) |
| + |
| + if fc.lastPlacementsPath != "/api/cli/apps/bab/tito/domains/shop.example.com/placements" { |
| + t.Fatalf("PUT path = %q", fc.lastPlacementsPath) |
| + } |
| + var set []map[string]string |
| + if err := json.Unmarshal(fc.lastPlacementsPut["placements"], &set); err != nil { |
| + t.Fatalf("placements %s: %v", fc.lastPlacementsPut["placements"], err) |
| + } |
| + want := []map[string]string{ |
| + {"region": "uk", "host": "shop-uk.example.net"}, |
| + {"region": "IE", "host": "Shop-IE.example.net"}, |
| + {"region": "uk", "host": "other.example.net"}, |
| + } |
| + if len(set) != len(want) { |
| + t.Fatalf("placements sent = %v, want %v", set, want) |
| + } |
| + for i := range want { |
| + if set[i]["region"] != want[i]["region"] || set[i]["host"] != want[i]["host"] || len(set[i]) != 2 { |
| + t.Fatalf("placements sent = %v, want %v", set, want) |
| + } |
| + } |
| + if _, ok := fc.lastPlacementsPut["wildcard"]; ok { |
| + t.Fatalf("body = %v, want no wildcard key", fc.lastPlacementsPut) |
| + } |
| + if _, ok := fc.lastPlacementsPut["catchall"]; ok { |
| + t.Fatalf("body = %v, want no catchall key", fc.lastPlacementsPut) |
| + } |
| + out := buf.String() |
| + for _, line := range []string{ |
| + "shop.example.com now serves from one instance in each of 2 regions.", |
| + "shop.example.com is a regional domain. each region serves it from its own instance.", |
| + } { |
| + if !strings.Contains(out, line) { |
| + t.Fatalf("stdout missing %q:\n%s", line, out) |
| + } |
| + } |
| + if strings.Index(out, "now serves") > strings.Index(out, "is a regional domain") { |
| + t.Fatalf("RD-15 comes after the view:\n%s", out) |
| + } |
| +} |
| + |
| +// TestRunDomainsPlacementsCapabilityFlagsSentOnlyWhenNamed: --wildcard and |
| +// --catchall are sent only when named, an explicit false as false, as |
| +// attach sends them. |
| +func TestRunDomainsPlacementsCapabilityFlagsSentOnlyWhenNamed(t *testing.T) { |
| + fc := domainsCLIFixture(t) |
| + fc.placements = regionalPlacementsAnswer() |
| + captureStdout(t) |
| + base := []string{"placements", "-app", "tito", "-account", "bab", "-set", "ie=shop-ie.example.net"} |
| + for _, c := range []struct { |
| + name string |
| + flags []string |
| + wildcard, catchall string // "" absent |
| + }{ |
| + {"neither", nil, "", ""}, |
| + {"wildcard", []string{"-wildcard"}, "true", ""}, |
| + {"wildcard false", []string{"-wildcard=false"}, "false", ""}, |
| + {"catchall", []string{"-catchall"}, "", "true"}, |
| + {"both", []string{"-wildcard", "-catchall"}, "true", "true"}, |
| + } { |
| + runDomains(append(append(append([]string{}, base...), c.flags...), "shop.example.com")) |
| + for key, want := range map[string]string{"wildcard": c.wildcard, "catchall": c.catchall} { |
| + got, ok := fc.lastPlacementsPut[key] |
| + if (want == "") == ok || (ok && string(got) != want) { |
| + t.Errorf("%s: %s = %s (present %v), want %q", c.name, key, got, ok, want) |
| + } |
| + } |
| + } |
| +} |
| + |
| +// TestRunDomainsPlacementsPrintsRegionalView: the read of a regional claim |
| +// prints RD-6, a row per region, the reason line of the region that is not |
| +// ready, and RD-8 because one is. |
| +func TestRunDomainsPlacementsPrintsRegionalView(t *testing.T) { |
| + fc := domainsCLIFixture(t) |
| + fc.placements = regionalPlacementsAnswer() |
| + buf := captureStdout(t) |
| + |
| + runDomains([]string{"placements", "-app", "tito", "-account", "bab", "shop.example.com"}) |
| + |
| + if fc.lastPlacementsPut != nil { |
| + t.Fatalf("a read sent a PUT: %v", fc.lastPlacementsPut) |
| + } |
| + out := buf.String() |
| + for _, want := range []string{ |
| + "shop.example.com is a regional domain. each region serves it from its own instance.", |
| + "uk: no server in this region serves shop-uk.example.net yet.", |
| + "point DNS for shop.example.com at the addresses of the ready regions, with latency-based routing.", |
| + } { |
| + if !strings.Contains(out, want) { |
| + t.Fatalf("stdout missing %q:\n%s", want, out) |
| + } |
| + } |
| + rows := map[string]string{} |
| + for _, line := range strings.Split(out, "\n") { |
| + if f := strings.Fields(line); len(f) == 4 { |
| + rows[f[0]] = strings.Join(f, " ") |
| + } |
| + } |
| + if rows["ie"] != "ie shop-ie.example.net yes 203.0.113.10" || rows["uk"] != "uk shop-uk.example.net no -" { |
| + t.Fatalf("rows = %v:\n%s", rows, out) |
| + } |
| + if strings.Contains(out, "now serves") { |
| + t.Fatalf("a read printed RD-15:\n%s", out) |
| + } |
| +} |
| + |
| +// TestAPISessionDomainPlacementsSet: the PUT's wire shape, its answer, and |
| +// the two refusals the CLI reads itself: a bare 404 (a console without the |
| +// door, Review Focus 5) is RD-16, and the 409 that carries "verify" (proof |
| +// pending, no claim written) comes back with the record to publish. |
| +func TestAPISessionDomainPlacementsSet(t *testing.T) { |
| + var ( |
| + status int |
| + body string |
| + got map[string]json.RawMessage |
| + method string |
| + ) |
| + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + method = r.Method |
| + got = nil |
| + json.NewDecoder(r.Body).Decode(&got) |
| + if status != 0 { |
| + w.WriteHeader(status) |
| + } |
| + w.Write([]byte(body)) |
| + })) |
| + defer ts.Close() |
| + s := newTestSession(ts.URL) |
| + ctx := context.Background() |
| + tru := true |
| + set := []apiPlacementIn{{Region: "ie", Host: "shop-ie.example.net"}} |
| + |
| + body = `{"host":"shop.example.com","mode":"regional","route":"shop-ie.example.net","placements":[{"region":"ie","target":"shop-ie.example.net","ready":false,"reason":"settling","boxes":[]}]}` |
| + pl, err := s.domainSetPlacements(ctx, apiAcct{Seg: "bab"}, "shop", "shop.example.com", set, &tru, nil) |
| + if err != nil || method != http.MethodPut || pl.Mode != "regional" || len(pl.Placements) != 1 || pl.Placements[0].Reason != "settling" { |
| + t.Fatalf("PUT = %s %+v, %v", method, pl, err) |
| + } |
| + if string(got["wildcard"]) != "true" || got["catchall"] != nil || string(got["placements"]) != `[{"region":"ie","host":"shop-ie.example.net"}]` { |
| + t.Fatalf("body = %v", got) |
| + } |
| + |
| + status, body = http.StatusNotFound, "404 page not found\n" |
| + if _, err := s.domainSetPlacements(ctx, apiAcct{Seg: "bab"}, "shop", "shop.example.com", set, nil, nil); err == nil || |
| + err.Error() != "this console does not support regional domains yet." { |
| + t.Fatalf("bare 404 = %v, want RD-16", err) |
| + } |
| + // A 404 the console wrote itself is its own diagnosis. |
| + status, body = http.StatusNotFound, `{"error":"no such app"}` |
| + if _, err := s.domainSetPlacements(ctx, apiAcct{Seg: "bab"}, "shop", "shop.example.com", set, nil, nil); err == nil || err.Error() != "no such app" { |
| + t.Fatalf("JSON 404 = %v, want the console's own error", err) |
| + } |
| + |
| + status, body = http.StatusConflict, `{"host":"shop.example.com","route":"shop-ie.example.net","created":false,"verified":false,`+ |
| + `"verify":{"type":"TXT","name":"_carlos-challenge.shop.example.com","value":"proof-123","ttl":300,"set":"verify"}}` |
| + _, err = s.domainSetPlacements(ctx, apiAcct{Seg: "bab"}, "shop", "shop.example.com", set, nil, nil) |
| + var proof *placementsProofError |
| + if !errors.As(err, &proof) || proof.Verify == nil || proof.Verify.Name != "_carlos-challenge.shop.example.com" || proof.Verify.Value != "proof-123" { |
| + t.Fatalf("proof 409 = %v, want the record to publish", err) |
| + } |
| + // Any other 409 is the console's sentence. |
| + status, body = http.StatusConflict, `{"error":"hostname unavailable"}` |
| + if _, err := s.domainSetPlacements(ctx, apiAcct{Seg: "bab"}, "shop", "shop.example.com", set, nil, nil); err == nil || |
| + errors.As(err, &proof) || err.Error() != "hostname unavailable" { |
| + t.Fatalf("plain 409 = %v", err) |
| + } |
| +} |
| + |
| +// TestRunDomainsPlacementsAgainstAnOlderConsole (Review Focus 5): a console |
| +// that predates the door answers the PUT with the mux's bare 404; the |
| +// member reads RD-16, not a decode error. Through the real binary, since |
| +// the verb exits. |
| +func TestRunDomainsPlacementsAgainstAnOlderConsole(t *testing.T) { |
| + bin := buildCarlos(t) |
| + fc := newFakeDomainsConsole(t) |
| + home := t.TempDir() |
| + creds := filepath.Join(home, "credentials") |
| + writeTestCredential(t, creds, fc.ts.URL) |
| + run := func() (string, error) { |
| + cmd := exec.Command(bin, "domains", "placements", "-app", "tito", "-account", "bab", |
| + "-set", "ie=shop-ie.example.net", "shop.example.com") |
| + cmd.Env = carlosTestEnv(t, "CARLOS_CREDENTIALS="+creds, "CARLOS_DEPLOYMENT_BUCKET=", "CARLOS_DEPLOYMENT_DIR=") |
| + cmd.Dir = home |
| + out, err := cmd.CombinedOutput() |
| + return string(out), err |
| + } |
| + |
| + fc.putStatus, fc.putBody = http.StatusNotFound, "404 page not found\n" |
| + out, err := run() |
| + if err == nil || !strings.Contains(out, "this console does not support regional domains yet.") { |
| + t.Fatalf("against an older console: %v\n%s", err, out) |
| + } |
| + |
| + // Proof pending: nothing was written, and the member is told which |
| + // record to publish and to run the command again. |
| + fc.putStatus = http.StatusConflict |
| + fc.putBody = `{"host":"shop.example.com","route":"shop-ie.example.net","created":false,"verified":false,` + |
| + `"verify":{"type":"TXT","name":"_carlos-challenge.shop.example.com","value":"proof-123","ttl":300,"set":"verify"}}` |
| + out, err = run() |
| + for _, want := range []string{ |
| + "prove you control shop.example.com first. add this TXT record, then run the same command again.", |
| + "Add: TXT _carlos-challenge.shop.example.com -> proof-123", |
| + } { |
| + if err == nil || !strings.Contains(out, want) { |
| + t.Fatalf("proof pending: %v, output missing %q:\n%s", err, want, out) |
| + } |
| + } |
| + if strings.Contains(out, "carlos domains verify") { |
| + t.Fatalf("proof pending points at domains verify, which has no claim to verify:\n%s", out) |
| + } |
| +} |