|
9 | 9 | "os" |
10 | 10 | "path/filepath" |
11 | 11 | "reflect" |
| 12 | + "slices" |
12 | 13 | "strings" |
13 | 14 | "sync/atomic" |
14 | 15 | "testing" |
@@ -303,6 +304,96 @@ func TestE2EMaxDurationSavesResumeState(t *testing.T) { |
303 | 304 | } |
304 | 305 | } |
305 | 306 |
|
| 307 | +// TestE2EResumeDiffOntoOutputFile covers #297: with -diff and -o on one |
| 308 | +// file, a run that stops and saves a resume state leaves the file alone, so |
| 309 | +// the resumed run reports the same diff and exit code as an uninterrupted |
| 310 | +// one, and then writes the full results. |
| 311 | +func TestE2EResumeDiffOntoOutputFile(t *testing.T) { |
| 312 | + t.Parallel() |
| 313 | + dir := t.TempDir() |
| 314 | + const n = 120 |
| 315 | + var words strings.Builder |
| 316 | + for i := 0; i < n; i++ { |
| 317 | + fmt.Fprintf(&words, "host%d\n", i) |
| 318 | + } |
| 319 | + base := []string{"-simulate", "-seed", "3", "-hit-rate", "50", "-progress=false", "-t", "2", "-format", "jsonl", |
| 320 | + "-w", writeFile(t, "wl.txt", words.String())} |
| 321 | + run := func(extra ...string) (int, string) { |
| 322 | + return runCLIMerged(t, append(append(slices.Clone(base), extra...), "example.com")...) |
| 323 | + } |
| 324 | + names := func(path string) []string { |
| 325 | + t.Helper() |
| 326 | + data, err := os.ReadFile(path) |
| 327 | + if err != nil { |
| 328 | + t.Fatal(err) |
| 329 | + } |
| 330 | + results, _ := jsonlRecords(t, string(data)) |
| 331 | + var out []string |
| 332 | + for _, r := range results { |
| 333 | + out = append(out, r.Subdomain) |
| 334 | + } |
| 335 | + slices.Sort(out) |
| 336 | + return out |
| 337 | + } |
| 338 | + diffOf := func(path string) output.DiffSummary { |
| 339 | + t.Helper() |
| 340 | + var summary output.Summary |
| 341 | + if data, err := os.ReadFile(path); err != nil || json.Unmarshal(data, &summary) != nil || summary.Diff == nil { |
| 342 | + t.Fatalf("reading the diff summary in %s: %v", path, err) |
| 343 | + } |
| 344 | + d := *summary.Diff |
| 345 | + d.Previous = "" |
| 346 | + return d |
| 347 | + } |
| 348 | + |
| 349 | + full := filepath.Join(dir, "full.jsonl") |
| 350 | + if code, out := run("-o", full); code != 0 { |
| 351 | + t.Fatalf("baseline run: exit %d\n%s", code, out) |
| 352 | + } |
| 353 | + all := names(full) |
| 354 | + if len(all) < 10 { |
| 355 | + t.Fatalf("the simulation found %d names, want at least 10", len(all)) |
| 356 | + } |
| 357 | + // The baseline lacks one name the scan finds and has one it cannot find. |
| 358 | + baseline := strings.Join(all[1:], "\n") + "\ngone.example.com\n" |
| 359 | + |
| 360 | + // The uninterrupted run. |
| 361 | + stats := filepath.Join(dir, "stats.json") |
| 362 | + wantCode, out := run("-diff", writeFile(t, "prev.txt", baseline), "-o", filepath.Join(dir, "cur.jsonl"), "-stats", stats) |
| 363 | + want := diffOf(stats) |
| 364 | + if wantCode != 4 || want.Added != 1 { |
| 365 | + t.Fatalf("uninterrupted run: exit %d, diff %+v; want exit 4 with 1 added\n%s", wantCode, want, out) |
| 366 | + } |
| 367 | + |
| 368 | + // -rate 50 with 120 words needs about 2.4 s, so a 1 s budget stops the run. |
| 369 | + results, state := writeFile(t, "results.txt", baseline), filepath.Join(dir, "state.json") |
| 370 | + code, out := run("-diff", results, "-o", results, "-stats", stats, "-rate", "50", "-max-duration", "1s", "-state", state) |
| 371 | + if _, err := os.Stat(state); err != nil { |
| 372 | + t.Fatalf("the stopped run (exit %d) saved no resume state: %v\n%s", code, err, out) |
| 373 | + } |
| 374 | + if data, err := os.ReadFile(results); err != nil || string(data) != baseline { |
| 375 | + t.Fatalf("the stopped run changed its -diff baseline (err %v):\n%s", err, data) |
| 376 | + } |
| 377 | + for i := 0; ; i++ { |
| 378 | + if i == 20 { |
| 379 | + t.Fatal("the scan did not finish in 20 resumed runs") |
| 380 | + } |
| 381 | + code, out = runCLIMerged(t, "-resume", state) |
| 382 | + if _, err := os.Stat(state); err != nil { |
| 383 | + break // the run that finished removed its state |
| 384 | + } |
| 385 | + if data, err := os.ReadFile(results); err != nil || string(data) != baseline { |
| 386 | + t.Fatalf("resumed run %d changed its -diff baseline (err %v):\n%s", i, err, data) |
| 387 | + } |
| 388 | + } |
| 389 | + if got := diffOf(stats); code != wantCode || got != want { |
| 390 | + t.Errorf("resumed run: exit %d, diff %+v; want exit %d, diff %+v as uninterrupted\n%s", code, got, wantCode, want, out) |
| 391 | + } |
| 392 | + if got := names(results); !slices.Equal(got, all) { |
| 393 | + t.Errorf("-o holds %d names after the resumed run, want all %d", len(got), len(all)) |
| 394 | + } |
| 395 | +} |
| 396 | + |
306 | 397 | // TestE2EAbortSavesResumeState covers #294: a scan the reliability guard |
307 | 398 | // aborts fails as before, but saves a resume state, and the resumed run |
308 | 399 | // tests the rest once the resolver answers again. |
|
0 commit comments