| index b7505cd20..be4fed5d4 100644 |
| --- a/domains_cli_test.go |
| +++ b/domains_cli_test.go |
| @@ -15,6 +15,7 @@ import ( |
| "errors" |
| "net/http" |
| "net/http/httptest" |
| + "os" |
| "os/exec" |
| "path/filepath" |
| "strings" |
| @@ -1172,3 +1173,85 @@ func TestRunDomainsPlacementsAgainstAnOlderConsole(t *testing.T) { |
| t.Fatalf("proof pending points at domains verify, which has no claim to verify:\n%s", out) |
| } |
| } |
| + |
| +// TestRunDomainsPlacementsPublishedExamples (#447 whole-branch R1 P2-1): |
| +// every fenced `carlos domains placements` line in docs/site/cli.md runs as |
| +// published, through the real binary (a usage error is a log.Fatal), and |
| +// reaches the console: the read as a GET, each --set line as a PUT carrying |
| +// its whole set and exactly the capability flags it names. Go's flag parser |
| +// stops at the first non-flag, so a hostname placed before --set is a usage |
| +// error, which is what the page used to show. |
| +func TestRunDomainsPlacementsPublishedExamples(t *testing.T) { |
| + raw, err := os.ReadFile("docs/site/cli.md") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var examples [][]string |
| + inFence := false |
| + for _, line := range strings.Split(string(raw), "\n") { |
| + if strings.HasPrefix(line, "```") { |
| + inFence = !inFence |
| + continue |
| + } |
| + if inFence && strings.HasPrefix(line, "carlos domains placements ") { |
| + examples = append(examples, strings.Fields(line)[1:]) |
| + } |
| + } |
| + if len(examples) < 3 { |
| + t.Fatalf("docs/site/cli.md has %d placements examples, want the read and both writes", len(examples)) |
| + } |
| + |
| + bin := buildCarlos(t) |
| + fc := newFakeDomainsConsole(t) |
| + fc.placements = regionalPlacementsAnswer() |
| + home := t.TempDir() |
| + creds := filepath.Join(home, "credentials") |
| + writeTestCredential(t, creds, fc.ts.URL) |
| + writes := 0 |
| + for _, args := range examples { |
| + fc.lastPlacementsPath, fc.lastPlacementsPut = "", nil |
| + cmd := exec.Command(bin, args...) |
| + cmd.Env = carlosTestEnv(t, "CARLOS_CREDENTIALS="+creds, "CARLOS_DEPLOYMENT_BUCKET=", "CARLOS_DEPLOYMENT_DIR=") |
| + cmd.Dir = home |
| + out, err := cmd.CombinedOutput() |
| + if err != nil { |
| + t.Fatalf("carlos %s: %v\n%s", strings.Join(args, " "), err, out) |
| + } |
| + host := args[len(args)-1] |
| + if !strings.HasSuffix(fc.lastPlacementsPath, "/domains/"+host+"/placements") { |
| + t.Fatalf("carlos %s asked for %q, want the placements door for %s", strings.Join(args, " "), fc.lastPlacementsPath, host) |
| + } |
| + var sets int |
| + named := map[string]bool{} |
| + for _, a := range args { |
| + switch a { |
| + case "--set", "-set": |
| + sets++ |
| + case "--wildcard", "-wildcard": |
| + named["wildcard"] = true |
| + case "--catchall", "-catchall": |
| + named["catchall"] = true |
| + } |
| + } |
| + if sets == 0 { |
| + if fc.lastPlacementsPut != nil { |
| + t.Fatalf("carlos %s (a read) sent a PUT: %v", strings.Join(args, " "), fc.lastPlacementsPut) |
| + } |
| + continue |
| + } |
| + writes++ |
| + var set []map[string]string |
| + if err := json.Unmarshal(fc.lastPlacementsPut["placements"], &set); err != nil || len(set) != sets { |
| + t.Fatalf("carlos %s sent placements %s (%v), want %d entries", strings.Join(args, " "), fc.lastPlacementsPut["placements"], err, sets) |
| + } |
| + for _, key := range []string{"wildcard", "catchall"} { |
| + got, ok := fc.lastPlacementsPut[key] |
| + if ok != named[key] || (ok && string(got) != "true") { |
| + t.Fatalf("carlos %s sent %s = %s (present %v), want present %v", strings.Join(args, " "), key, got, ok, named[key]) |
| + } |
| + } |
| + } |
| + if writes < 2 { |
| + t.Fatalf("ran %d published write examples, want both", writes) |
| + } |
| +} |