| new file mode 100644 |
| index 000000000..b6d0688c1 |
| --- /dev/null |
| +++ b/cmd/console/hostsused_scan_test.go |
| @@ -0,0 +1,548 @@ |
| +package main_test |
| + |
| +// scripts/hosts-used-scan-aws.sh, the hostname history's AWS adapter |
| +// (nearest-region never-reuse plan Task 11), run against a fake `aws`: this |
| +// test binary, re-executed under the name aws (TestMain). The fake pages S3 |
| +// listings two keys at a time and runs an SSM command's script with sh |
| +// against a local directory, keeping at most SSM's 24,000 characters of its |
| +// output. The happy path goes through the real verbs: `console hosts-used |
| +// roster`, the scan, then `console hosts-used activate`. |
| + |
| +import ( |
| + "bufio" |
| + "encoding/json" |
| + "errors" |
| + "fmt" |
| + "os" |
| + "os/exec" |
| + "path/filepath" |
| + "slices" |
| + "sort" |
| + "strconv" |
| + "strings" |
| + "testing" |
| + |
| + boxlayout "github.com/carlosframework/platform/internal/layout" |
| + "github.com/carlosframework/platform/internal/provision" |
| +) |
| + |
| +const scanScript = "../../scripts/hosts-used-scan-aws.sh" |
| + |
| +// fakeAWSConfig is the fake's world, in config.json under FAKE_AWS_DIR. |
| +type fakeAWSConfig struct { |
| + Buckets map[string]fakeBucket `json:"buckets"` |
| + Instances map[string]fakeInstance `json:"instances"` |
| +} |
| + |
| +type fakeBucket struct { |
| + Keys []string `json:"keys"` |
| + Fail string `json:"fail"` // every call fails with this |
| + FailPage int `json:"fail_page"` // the call for this page (1-based) fails |
| +} |
| + |
| +type fakeInstance struct { |
| + SendFail string `json:"send_fail"` // send-command fails with this |
| + Truncate int `json:"truncate"` // keep only this many characters of output |
| + GrowDir string `json:"grow_dir"` // send-commands add a file here: every one... |
| + GrowAt int `json:"grow_at"` // ...or only this instance's nth |
| +} |
| + |
| +// fakeAWS is the fake aws CLI. It logs every call's arguments, one JSON |
| +// array per line, to calls.jsonl, and refuses any operation the scan has no |
| +// business making. |
| +func fakeAWS(args []string) int { |
| + dir := os.Getenv("FAKE_AWS_DIR") |
| + if f, err := os.OpenFile(filepath.Join(dir, "calls.jsonl"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600); err == nil { |
| + b, _ := json.Marshal(args) |
| + f.Write(append(b, '\n')) |
| + f.Close() |
| + } |
| + var cfg fakeAWSConfig |
| + if b, err := os.ReadFile(filepath.Join(dir, "config.json")); err != nil || json.Unmarshal(b, &cfg) != nil { |
| + fmt.Fprintln(os.Stderr, "fake aws: no config") |
| + return 99 |
| + } |
| + opts, words := map[string]string{}, []string{} |
| + for i := 0; i < len(args); i++ { |
| + switch { |
| + case args[i] == "--no-paginate": |
| + case strings.HasPrefix(args[i], "--") && i+1 < len(args): |
| + opts[args[i]] = args[i+1] |
| + i++ |
| + default: |
| + words = append(words, args[i]) |
| + } |
| + } |
| + op := strings.Join(words, " ") |
| + switch op { |
| + case "s3api list-objects-v2": |
| + return fakeListObjects(cfg, opts) |
| + case "ssm send-command": |
| + return fakeSendCommand(dir, cfg, opts) |
| + case "ssm get-command-invocation": |
| + return fakeGetInvocation(dir, opts) |
| + } |
| + fmt.Fprintf(os.Stderr, "fake aws: unexpected call %q\n", op) |
| + return 99 |
| +} |
| + |
| +func fakeListObjects(cfg fakeAWSConfig, opts map[string]string) int { |
| + b, ok := cfg.Buckets[opts["--bucket"]] |
| + if !ok { |
| + fmt.Fprintln(os.Stderr, "An error occurred (NoSuchBucket) when calling the ListObjectsV2 operation: The specified bucket does not exist") |
| + return 254 |
| + } |
| + start := 0 |
| + if tok := opts["--continuation-token"]; tok != "" { |
| + start, _ = strconv.Atoi(strings.TrimPrefix(tok, "tok-")) |
| + } |
| + if b.Fail != "" || (b.FailPage > 0 && start/2+1 == b.FailPage) { |
| + fmt.Fprintf(os.Stderr, "An error occurred (AccessDenied) when calling the ListObjectsV2 operation: %s\n", b.Fail) |
| + return 254 |
| + } |
| + var keys []string |
| + for _, k := range b.Keys { |
| + if strings.HasPrefix(k, opts["--prefix"]) { |
| + keys = append(keys, k) |
| + } |
| + } |
| + sort.Strings(keys) |
| + end := min(start+2, len(keys)) |
| + resp := map[string]any{"IsTruncated": end < len(keys), "KeyCount": end - start} |
| + if end > start { |
| + var contents []map[string]string |
| + for _, k := range keys[start:end] { |
| + contents = append(contents, map[string]string{"Key": k}) |
| + } |
| + resp["Contents"] = contents |
| + } |
| + if end < len(keys) { |
| + resp["NextContinuationToken"] = "tok-" + strconv.Itoa(end) |
| + } |
| + json.NewEncoder(os.Stdout).Encode(resp) |
| + return 0 |
| +} |
| + |
| +func fakeSendCommand(dir string, cfg fakeAWSConfig, opts map[string]string) int { |
| + inst, ok := cfg.Instances[opts["--instance-ids"]] |
| + if !ok || inst.SendFail != "" { |
| + fmt.Fprintf(os.Stderr, "An error occurred (InvalidInstanceId) when calling the SendCommand operation: %s\n", inst.SendFail) |
| + return 254 |
| + } |
| + var params struct { |
| + Commands []string `json:"commands"` |
| + } |
| + if err := json.Unmarshal([]byte(opts["--parameters"]), ¶ms); err != nil || len(params.Commands) != 1 { |
| + fmt.Fprintln(os.Stderr, "fake aws: bad --parameters") |
| + return 99 |
| + } |
| + n := fakeCounter(dir, "commands") |
| + if mine := fakeCounter(dir, "commands-"+opts["--instance-ids"]); inst.GrowDir != "" && (inst.GrowAt == 0 || mine == inst.GrowAt) { |
| + os.WriteFile(filepath.Join(inst.GrowDir, fmt.Sprintf("grow-%d.example.com.db", n)), nil, 0o600) |
| + } |
| + var stdout, stderr strings.Builder |
| + cmd := exec.Command("sh", "-c", params.Commands[0]) |
| + cmd.Stdout, cmd.Stderr = &stdout, &stderr |
| + code, status := 0, "Success" |
| + if err := cmd.Run(); err != nil { |
| + code, status = 1, "Failed" |
| + var ee *exec.ExitError |
| + if errors.As(err, &ee) { |
| + code = ee.ExitCode() |
| + } |
| + } |
| + out := stdout.String() |
| + limit := 24000 |
| + if inst.Truncate > 0 { |
| + limit = inst.Truncate |
| + } |
| + if len(out) > limit { |
| + out = out[:limit] |
| + } |
| + id := fmt.Sprintf("cmd-%d", n) |
| + b, _ := json.Marshal(map[string]any{"Status": status, "ResponseCode": code, "StandardOutputContent": out, "StandardErrorContent": stderr.String()}) |
| + os.WriteFile(filepath.Join(dir, id+".json"), b, 0o600) |
| + fmt.Println(id) |
| + return 0 |
| +} |
| + |
| +// fakeGetInvocation answers InvocationDoesNotExist on its first call for a |
| +// command, as SSM does straight after send-command, then the result. |
| +func fakeGetInvocation(dir string, opts map[string]string) int { |
| + id := opts["--command-id"] |
| + seen := filepath.Join(dir, id+".seen") |
| + if _, err := os.Stat(seen); err != nil { |
| + os.WriteFile(seen, nil, 0o600) |
| + fmt.Fprintln(os.Stderr, "An error occurred (InvocationDoesNotExist) when calling the GetCommandInvocation operation: ") |
| + return 254 |
| + } |
| + b, err := os.ReadFile(filepath.Join(dir, id+".json")) |
| + if err != nil { |
| + fmt.Fprintln(os.Stderr, "An error occurred (InvalidCommandId) when calling the GetCommandInvocation operation: ") |
| + return 254 |
| + } |
| + os.Stdout.Write(b) |
| + return 0 |
| +} |
| + |
| +func fakeCounter(dir, name string) int { |
| + p := filepath.Join(dir, name) |
| + b, _ := os.ReadFile(p) |
| + n, _ := strconv.Atoi(strings.TrimSpace(string(b))) |
| + n++ |
| + os.WriteFile(p, []byte(strconv.Itoa(n)), 0o600) |
| + return n |
| +} |
| + |
| +// scanWorld is one test's fake AWS, its PATH and its scratch. |
| +type scanWorld struct { |
| + t *testing.T |
| + dir, fake string |
| + env []string |
| + coordsPath string |
| + cfg fakeAWSConfig |
| + coordinates map[string]map[string]string |
| +} |
| + |
| +func newScanWorld(t *testing.T) *scanWorld { |
| + t.Helper() |
| + for _, tool := range []string{"jq", "sh", "find", "gzip", "base64", "sha256sum"} { |
| + if _, err := exec.LookPath(tool); err != nil { |
| + t.Skipf("%s not installed", tool) |
| + } |
| + } |
| + exe, err := os.Executable() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + dir := t.TempDir() |
| + w := &scanWorld{t: t, dir: dir, fake: filepath.Join(dir, "fake"), |
| + cfg: fakeAWSConfig{Buckets: map[string]fakeBucket{}, Instances: map[string]fakeInstance{}}, |
| + coordinates: map[string]map[string]string{}} |
| + bin := filepath.Join(dir, "bin") |
| + for _, d := range []string{bin, w.fake} { |
| + if err := os.MkdirAll(d, 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if err := os.Symlink(exe, filepath.Join(bin, "aws")); err != nil { |
| + t.Fatal(err) |
| + } |
| + w.coordsPath = filepath.Join(dir, "coords.json") |
| + w.env = []string{"PATH=" + bin + string(os.PathListSeparator) + os.Getenv("PATH"), "FAKE_AWS_DIR=" + w.fake, |
| + "HOSTS_SCAN_POLL=0", "HOSTS_SCAN_WAIT=30", "TMPDIR=" + dir} |
| + return w |
| +} |
| + |
| +// box makes a data directory holding paths (a trailing "/" is a directory) |
| +// behind instance inst, with the box's coordinates. |
| +func (w *scanWorld) box(target, inst string, paths ...string) string { |
| + w.t.Helper() |
| + d := filepath.Join(w.dir, "data-"+inst) |
| + if err := os.MkdirAll(d, 0o755); err != nil { |
| + w.t.Fatal(err) |
| + } |
| + for _, p := range paths { |
| + full := filepath.Join(d, p) |
| + if strings.HasSuffix(p, "/") { |
| + os.MkdirAll(full, 0o755) |
| + continue |
| + } |
| + os.MkdirAll(filepath.Dir(full), 0o755) |
| + if err := os.WriteFile(full, []byte("x"), 0o600); err != nil { |
| + w.t.Fatal(err) |
| + } |
| + } |
| + w.cfg.Instances[inst] = fakeInstance{} |
| + w.coordinates[target] = map[string]string{"instance": inst, "region": "eu-west-1", "data_dir": d} |
| + return d |
| +} |
| + |
| +func (w *scanWorld) bucket(target, prefix string, keys ...string) { |
| + name := strings.TrimPrefix(target, "bucket:") |
| + w.cfg.Buckets[name] = fakeBucket{Keys: keys} |
| + w.coordinates[target] = map[string]string{"prefix": prefix, "region": "eu-west-1"} |
| +} |
| + |
| +// scan writes the fake's config and the coords, runs the script and |
| +// returns its output lines, its stderr and its exit code. |
| +func (w *scanWorld) scan(rosterPath string, extraEnv ...string) ([]provision.TargetListing, string, int) { |
| + w.t.Helper() |
| + writeJSON(w.t, filepath.Join(w.fake, "config.json"), w.cfg) |
| + writeJSON(w.t, w.coordsPath, w.coordinates) |
| + out := filepath.Join(w.dir, "scan.jsonl") |
| + os.Remove(out) |
| + cmd := exec.Command(scanScript, "--profile", "scan-test", "--targets", w.coordsPath, rosterPath, out) |
| + cmd.Env = append(append(os.Environ(), w.env...), extraEnv...) |
| + var stderr strings.Builder |
| + cmd.Stderr = &stderr |
| + err := cmd.Run() |
| + code := 0 |
| + var ee *exec.ExitError |
| + if errors.As(err, &ee) { |
| + code = ee.ExitCode() |
| + } else if err != nil { |
| + w.t.Fatalf("run the scan: %v", err) |
| + } |
| + f, err := os.Open(out) |
| + if err != nil { |
| + w.t.Fatalf("no scan output (exit %d): %v\n%s", code, err, stderr.String()) |
| + } |
| + defer f.Close() |
| + // Every line is one JSON object, as activate's reader (readListings) |
| + // decodes them: strictly, with no unknown fields. |
| + var got []provision.TargetListing |
| + sc := bufio.NewScanner(f) |
| + sc.Buffer(nil, 1<<24) |
| + for sc.Scan() { |
| + dec := json.NewDecoder(strings.NewReader(sc.Text())) |
| + dec.DisallowUnknownFields() |
| + var l provision.TargetListing |
| + if err := dec.Decode(&l); err != nil { |
| + w.t.Fatalf("line %q: %v", sc.Text(), err) |
| + } |
| + got = append(got, l) |
| + } |
| + return got, stderr.String(), code |
| +} |
| + |
| +// calls is every aws invocation the fake saw. |
| +func (w *scanWorld) calls() [][]string { |
| + w.t.Helper() |
| + b, _ := os.ReadFile(filepath.Join(w.fake, "calls.jsonl")) |
| + var out [][]string |
| + for _, line := range strings.Split(strings.TrimSpace(string(b)), "\n") { |
| + if line == "" { |
| + continue |
| + } |
| + var args []string |
| + if err := json.Unmarshal([]byte(line), &args); err != nil { |
| + w.t.Fatal(err) |
| + } |
| + out = append(out, args) |
| + } |
| + return out |
| +} |
| + |
| +func writeJSON(t *testing.T, path string, v any) { |
| + t.Helper() |
| + b, err := json.Marshal(v) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(path, b, 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +func byTarget(ls []provision.TargetListing) map[string]provision.TargetListing { |
| + out := map[string]provision.TargetListing{} |
| + for _, l := range ls { |
| + out[l.Kind+":"+l.Target] = l |
| + } |
| + return out |
| +} |
| + |
| +// TestHostsUsedScanAWSActivates: a roster taken by the real console, scanned |
| +// through paged S3 listings and paged SSM output, is accepted by the real |
| +// activate, which marks every hostname the listings name. Every aws call |
| +// names the profile, and is a listing, a send-command or a read of its |
| +// result. Before that, a scan with a failed target is refused by activate. |
| +func TestHostsUsedScanAWSActivates(t *testing.T) { |
| + bin := buildConsole(t) |
| + w := newScanWorld(t) |
| + deploy := filepath.Join(w.dir, "deploy") |
| + for box, edges := range map[string]string{ |
| + "b1": `{"v":1,"box":"b1","eip":"192.0.2.1","region":"eu-west-1","placement":"","data_bucket":"data-ie","hosts":{}}`, |
| + "b2": `{"v":1,"box":"b2","eip":"192.0.2.2","region":"eu-west-2","placement":"uk","data_bucket":"data-uk","hosts":{}}`, |
| + } { |
| + p := filepath.Join(deploy, "control", "edges", box+".json") |
| + os.MkdirAll(filepath.Dir(p), 0o755) |
| + if err := os.WriteFile(p, []byte(edges), 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + os.MkdirAll(filepath.Join(w.dir, "console"), 0o755) |
| + run := runner(t, bin, "CARLOS_DEPLOYMENT_DIR="+deploy, "CONSOLE_DB="+filepath.Join(w.dir, "console", "console.db"), |
| + "CARLOS_REGISTRY="+filepath.Join(w.dir, "registry.db")) |
| + rosterPath := filepath.Join(w.dir, "roster.json") |
| + if out, err := run("hosts-used", "roster", "-o", rosterPath); err != nil { |
| + t.Fatalf("roster: %v\n%s", err, out) |
| + } |
| + |
| + // Seven keys under the three roots, two keys a page: several pages per |
| + // prefix. other/ is outside them and never listed. |
| + ieKeys := []string{ |
| + "instances/shop.example.com/db/000001", "instances/shop.example.com/generation", "instances/blog.example.org/db/1", |
| + "moves/moved.example.net/stage", "moves/@lanes/lane-1", "moves/@offline/off.example.com/x", |
| + "seeds/seed.example.com/snap", |
| + } |
| + w.bucket("bucket:data-ie", "instances", append(slices.Clone(ieKeys), "other/not-a-root")...) |
| + // An empty prefix: replicas at the top level, the whole bucket listed. |
| + ukKeys := []string{"uk.example.com/db/1", "moves/uk-moved.example.com/x", "seeds/uk-seed.example.com/y"} |
| + w.bucket("bucket:data-uk", "", ukKeys...) |
| + var filler []string |
| + for i := range 40 { |
| + filler = append(filler, fmt.Sprintf("static/asset-%02d.css", i)) |
| + } |
| + w.box("box:b1", "i-0b1", append([]string{ |
| + "shop.example.com.db", "shop.example.com.db-wal", "registry.db", |
| + "instances/blog.example.org/app.db", ".offline-moves/moved.example.net/x", "deep/a/b/c/too-deep.example.com.db", |
| + }, filler...)...) |
| + w.box("box:b2", "i-0b2", "uk.example.com.db") |
| + |
| + // A failed target: the scan says so, and activate refuses the listing. |
| + w.cfg.Buckets["data-ie"] = fakeBucket{Keys: w.cfg.Buckets["data-ie"].Keys, Fail: "Access Denied"} |
| + if _, stderr, code := w.scan(rosterPath); code != 1 || !strings.Contains(stderr, "FAIL bucket:data-ie") { |
| + t.Fatalf("scan with a denied bucket: exit %d\n%s", code, stderr) |
| + } |
| + listings := filepath.Join(w.dir, "scan.jsonl") |
| + if out, err := run("hosts-used", "activate", "-roster", rosterPath, "-listings", listings); err == nil || |
| + !strings.Contains(out, "The listing of bucket:data-ie failed.") { |
| + t.Fatalf("activate took a failed listing: %v\n%s", err, out) |
| + } |
| + w.cfg.Buckets["data-ie"] = fakeBucket{Keys: w.cfg.Buckets["data-ie"].Keys} |
| + os.Remove(filepath.Join(w.fake, "calls.jsonl")) |
| + |
| + // 64 characters a page: the box listings take several SSM pages. |
| + got, stderr, code := w.scan(rosterPath, "HOSTS_SCAN_PAGE=64") |
| + if code != 0 { |
| + t.Fatalf("scan exit %d\n%s", code, stderr) |
| + } |
| + ls := byTarget(got) |
| + if len(got) != 4 || len(ls) != 4 { |
| + t.Fatalf("want one line for each of 4 targets, got %+v", got) |
| + } |
| + for k, l := range ls { |
| + if !l.OK || l.Error != "" { |
| + t.Fatalf("%s: %+v", k, l) |
| + } |
| + } |
| + sort.Strings(ieKeys) |
| + if ie := ls["bucket:data-ie"]; !slices.Equal(ie.Entries, ieKeys) || ie.Prefix != "instances" { |
| + t.Errorf("data-ie = %q prefix %q, want %q", ie.Entries, ie.Prefix, ieKeys) |
| + } |
| + sort.Strings(ukKeys) |
| + if uk := ls["bucket:data-uk"]; !slices.Equal(uk.Entries, ukKeys) || uk.Prefix != "" { |
| + t.Errorf("data-uk = %q prefix %q, want %q", uk.Entries, uk.Prefix, ukKeys) |
| + } |
| + b1 := ls["box:b1"].Entries |
| + for _, want := range []string{"shop.example.com.db", "instances", "instances/blog.example.org", "instances/blog.example.org/app.db", |
| + ".offline-moves/moved.example.net/x", "deep/a/b", "static/asset-39.css"} { |
| + if !slices.Contains(b1, want) { |
| + t.Errorf("box b1 is missing %q: %q", want, b1) |
| + } |
| + } |
| + for _, e := range b1 { |
| + if strings.HasPrefix(e, "/") || strings.HasPrefix(e, "./") || e == "." || strings.Count(e, "/") > 2 { |
| + t.Errorf("box b1 entry %q is not relative to depth 3", e) |
| + } |
| + } |
| + hosts, unmapped := boxlayout.HostsFromBoxListing(b1) |
| + if len(unmapped) != 0 || !slices.Contains(hosts, "shop.example.com") || !slices.Contains(hosts, "blog.example.org") { |
| + t.Errorf("box b1 maps to %q, unmapped %q", hosts, unmapped) |
| + } |
| + |
| + // Pagination happened on both sides. |
| + lists, sends := 0, 0 |
| + for _, c := range w.calls() { |
| + if len(c) < 2 || c[0] != "--profile" || c[1] != "scan-test" { |
| + t.Errorf("a call without the profile first: %q", c) |
| + } |
| + switch op := strings.Join(c[2:min(4, len(c))], " "); op { |
| + case "s3api list-objects-v2": |
| + lists++ |
| + case "ssm send-command": |
| + sends++ |
| + case "ssm get-command-invocation": |
| + default: |
| + t.Errorf("an unexpected aws call: %q", c) |
| + } |
| + } |
| + if lists < 7 || sends < 4 { |
| + t.Errorf("%d listings and %d commands: the scan did not page", lists, sends) |
| + } |
| + |
| + // The real activate accepts the scan and marks what it names. |
| + out, err := run("hosts-used", "activate", "-roster", rosterPath, "-listings", listings) |
| + if err != nil || !strings.Contains(out, "the hostname history is complete") { |
| + t.Fatalf("activate: %v\n%s", err, out) |
| + } |
| + for _, h := range []string{"shop.example.com", "blog.example.org", "moved.example.net", "off.example.com", "seed.example.com", |
| + "uk.example.com", "uk-moved.example.com", "uk-seed.example.com"} { |
| + if _, err := os.Stat(filepath.Join(deploy, "control", "hosts-used", h+".json")); err != nil { |
| + t.Errorf("%s is not marked: %v", h, err) |
| + } |
| + } |
| +} |
| + |
| +// TestHostsUsedScanAWSFailsATargetClosed: each failure gives its target one |
| +// ok:false line with the reason and no entries, the other targets are still |
| +// scanned, and the scan exits 1. A listing that changed between pages is |
| +// scanned again, up to three times. |
| +func TestHostsUsedScanAWSFailsATargetClosed(t *testing.T) { |
| + w := newScanWorld(t) |
| + w.bucket("bucket:ok", "instances", "instances/a.example.com/x", "moves/b.example.com/y", "seeds/c.example.com/z") |
| + w.bucket("bucket:denied", "instances", "instances/a.example.com/x") |
| + w.cfg.Buckets["denied"] = fakeBucket{Fail: "Access Denied"} |
| + w.bucket("bucket:page2", "instances", "instances/a.example.com/1", "instances/a.example.com/2", "instances/a.example.com/3") |
| + w.cfg.Buckets["page2"] = fakeBucket{Keys: w.cfg.Buckets["page2"].Keys, FailPage: 2} |
| + w.cfg.Buckets["noprefix"] = fakeBucket{} |
| + w.coordinates["bucket:noprefix"] = map[string]string{"region": "eu-west-1"} |
| + w.box("box:okbox", "i-0a0", "shop.example.com.db") |
| + w.box("box:unsent", "i-0a1", "shop.example.com.db") |
| + w.cfg.Instances["i-0a1"] = fakeInstance{SendFail: "Instances not in a valid state for account"} |
| + w.box("box:nodir", "i-0a2") |
| + w.coordinates["box:nodir"]["data_dir"] = filepath.Join(w.dir, "no-such-dir") |
| + w.box("box:cut", "i-0a3", "shop.example.com.db") |
| + w.cfg.Instances["i-0a3"] = fakeInstance{Truncate: 110} |
| + d := w.box("box:grows", "i-0a4", "shop.example.com.db") |
| + w.cfg.Instances["i-0a4"] = fakeInstance{GrowDir: d} |
| + d = w.box("box:grows-once", "i-0a5", "shop.example.com.db") |
| + w.cfg.Instances["i-0a5"] = fakeInstance{GrowDir: d, GrowAt: 2} |
| + w.coordinates["box:badinst"] = map[string]string{"instance": "i-0a6; rm -rf /", "region": "eu-west-1"} |
| + |
| + roster := map[string]any{"targets": []string{"bucket:ok", "bucket:denied", "bucket:page2", "bucket:noprefix", "bucket:nocoords", |
| + "box:okbox", "box:unsent", "box:nodir", "box:cut", "box:grows", "box:grows-once", "box:badinst", "box:nocoords", "pool:p1"}} |
| + rosterPath := filepath.Join(w.dir, "roster.json") |
| + writeJSON(t, rosterPath, roster) |
| + got, stderr, code := w.scan(rosterPath, "HOSTS_SCAN_PAGE=40") |
| + if code != 1 { |
| + t.Fatalf("scan exit %d, want 1\n%s", code, stderr) |
| + } |
| + ls := byTarget(got) |
| + if len(got) != 14 || len(ls) != 14 { |
| + t.Fatalf("want one line for each of 14 targets, got %d: %+v", len(got), got) |
| + } |
| + for target, want := range map[string]string{ |
| + "bucket:ok": "", |
| + "box:okbox": "", |
| + "box:grows-once": "", |
| + "bucket:denied": "AccessDenied", |
| + "bucket:page2": "AccessDenied", |
| + "bucket:noprefix": "no prefix for bucket:noprefix", |
| + "bucket:nocoords": "no prefix for bucket:nocoords", |
| + "box:unsent": "InvalidInstanceId", |
| + "box:nodir": "Failed (exit 3)", |
| + "box:cut": "cut short", |
| + "box:grows": "changed while it was paged", |
| + "box:badinst": "is not an instance id", |
| + "box:nocoords": "no instance for box:nocoords", |
| + "pool:p1": "does not know this kind of target", |
| + } { |
| + l, ok := ls[target] |
| + switch { |
| + case !ok: |
| + t.Errorf("%s: no line", target) |
| + case want == "" && (!l.OK || l.Error != "" || len(l.Entries) == 0): |
| + t.Errorf("%s: want ok with entries, got %+v", target, l) |
| + case want != "" && (l.OK || !strings.Contains(l.Error, want) || len(l.Entries) != 0): |
| + t.Errorf("%s: want ok:false with %q and no entries, got %+v", target, want, l) |
| + } |
| + } |
| + for _, c := range w.calls() { |
| + if slices.Contains(c, "i-0a6; rm -rf /") { |
| + t.Errorf("a bad instance id reached aws: %q", c) |
| + } |
| + } |
| +} |