| 1 | package diagnostics |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | func TestStreamDrainsWarningFlood(t *testing.T) { |
| 14 | const flag = "CAGE_DIAGNOSTICS_FLOOD" |
| 15 | const count = 200000 |
| 16 | const warning = "W: Tried to start delayed item package, but failed\n" |
| 17 | if os.Getenv(flag) == "1" { |
| 18 | chunk := strings.Repeat(warning, 1000) |
| 19 | for i := 0; i < count/1000; i++ { |
| 20 | fmt.Fprint(os.Stderr, chunk) |
| 21 | } |
| 22 | fmt.Fprintln(os.Stderr, "installation completed") |
| 23 | return |
| 24 | } |
| 25 | s := New(t.TempDir()) |
| 26 | var visible strings.Builder |
| 27 | w := NewStream(s, "setup", "info", func(line string) { visible.WriteString(line) }) |
| 28 | ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) |
| 29 | defer cancel() |
| 30 | exe, err := os.Executable() |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | cmd := exec.CommandContext(ctx, exe, "-test.run=^TestStreamDrainsWarningFlood$") |
| 35 | cmd.Env = append(os.Environ(), flag+"=1") |
| 36 | cmd.Stderr = w |
| 37 | start := time.Now() |
| 38 | if err := cmd.Run(); err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | w.Close() |
| 42 | events := readEvents(t, s) |
| 43 | total := uint64(1) |
| 44 | for _, event := range events { |
| 45 | var repeats uint64 |
| 46 | if _, err := fmt.Sscanf(event.Message, "[previous output repeated %d additional times]", &repeats); err == nil { |
| 47 | total += repeats |
| 48 | } |
| 49 | } |
| 50 | if total != count || len(events) > 25 || !strings.Contains(visible.String(), "installation completed") { |
| 51 | t.Fatalf("flood not coalesced or final output lost: count=%d events=%d", total, len(events)) |
| 52 | } |
| 53 | if w.batchBytes != 0 || len(w.line) != 0 || len(visible.String()) > 4096 { |
| 54 | t.Fatal("output retained after drain") |
| 55 | } |
| 56 | t.Logf("drained %d warnings (%d bytes) in %s, %d events", count, count*len(warning), time.Since(start), len(events)) |
| 57 | } |
| 58 | |
| 59 | func TestStreamCoalescingKeepsProgressAndDistinctSecrets(t *testing.T) { |
| 60 | s := New(t.TempDir()) |
| 61 | w := s.Stream("setup", "info") |
| 62 | input := "password=one\npassword=two\nCAGE_TOOL_BYTES pkg 0 10\nCAGE_TOOL_BYTES pkg 0 10\nlast\nlast\n" |
| 63 | w.Write([]byte(input)) |
| 64 | w.Close() |
| 65 | events := readEvents(t, s) |
| 66 | if len(events) != 6 || !strings.Contains(events[5].Message, "1 additional times") { |
| 67 | t.Fatalf("unexpected events: %+v", events) |
| 68 | } |
| 69 | for _, e := range events { |
| 70 | if strings.Contains(e.Message, "one") || strings.Contains(e.Message, "two") { |
| 71 | t.Fatal("secret leaked") |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func TestStreamBatchRotationPreservesEvents(t *testing.T) { |
| 77 | s := New(t.TempDir()) |
| 78 | s.fileBytes = 1200 |
| 79 | s.maxBytes = 100000 |
| 80 | w := s.Stream("setup", "info") |
| 81 | var input strings.Builder |
| 82 | for i := 0; i < 200; i++ { |
| 83 | fmt.Fprintf(&input, "package %d installed\n", i) |
| 84 | } |
| 85 | w.Write([]byte(input.String())) |
| 86 | w.Close() |
| 87 | events := readEvents(t, s) |
| 88 | if len(events) != 200 { |
| 89 | t.Fatalf("batch rotation lost events: %d", len(events)) |
| 90 | } |
| 91 | files, _ := s.files() |
| 92 | for _, f := range files { |
| 93 | if f.Size() > s.fileBytes { |
| 94 | t.Fatalf("file exceeded rotation bound: %s %d", f.Name(), f.Size()) |
| 95 | } |
| 96 | } |
| 97 | } |