Commit 72cf6d4
authored
refactor(tests): Replace assert with require for guard assertions (#46913)
### What does this PR do?
Replaces `if !assert.XXX(...) { return }` guard patterns with `require.XXX(...)` across test files.
The testify PR stretchr/testify#1481 (merged June 2024, included in testify v1.11.1) made `require` work safely with `*assert.CollectT` inside `EventuallyWithT` callbacks. `CollectT.FailNow()` calls `runtime.Goexit()` which exits the callback goroutine — this is caught by the `EventuallyWithT` retry loop, so the test continues retrying as expected.
**Changes across 31 test files (~115 replacements):**
- `test/new-e2e/tests/containers/` — `base_test.go`, `ecs_test.go`, `k8s_test.go`
- `test/new-e2e/tests/` — `discovery/`, `agent-runtimes/`, `cspm/`, `cws/`, `npm/`, `netpath/`, `remote-config/`, `windows/`, `agent-health/`, `agent-log-pipelines/`
- `pkg/network/tracer/`, `pkg/dyninst/`, `pkg/util/`, `comp/core/telemetry/`, etc.
- Also removed outdated `// Can be replaced by require... once PR #1481 is merged` comments
### Important: `require` must not be used inside raw goroutines
While `require` is safe inside `EventuallyWithT` callbacks (thanks to `CollectT.FailNow()` integration), it must **not** be used inside raw `go func()` blocks or HTTP handler callbacks.
[`t.FailNow()`](https://pkg.go.dev/testing#T.FailNow) calls `runtime.Goexit()` to stop the current goroutine. The [official Go `testing` documentation](https://pkg.go.dev/testing#T.FailNow) states:
> FailNow must be called from the goroutine running the test or benchmark function, not from other goroutines created during the test.
`EventuallyWithT` is a special case: testify [runs the condition via `go checkCond()`](https://github.com/stretchr/testify/blob/v1.11.1/assert/assertions.go#L2108), but `CollectT` implements its own `FailNow()` that safely exits the callback goroutine and lets the retry loop continue.
**Not converted** (intentionally left as `assert`):
- Assertions inside raw `go func()` blocks (e.g., `pkg/util/winutil/eventlog/` tests)
- Callbacks/predicates returning `bool` or `error` (not test flow control)
- Bodies that only log debug info without returning
### Motivation
Using `assert` as a guard is error-prone: if someone forgets the `if !... { return }` wrapper, the test continues and panics when accessing nil/empty results (e.g., `metrics[len(metrics)-1]` on an empty slice). `require` makes the "stop on failure" intent explicit and eliminates the boilerplate.
### Describe how you validated your changes
- Verified `require` works correctly inside `EventuallyWithT` callbacks (via `CollectT.FailNow()` → `runtime.Goexit()` caught by retry loop)
- Confirmed raw `go func()` call sites were left unchanged
- These are mechanical replacements that preserve the exact same test semantics — `require` stops execution on failure, which is exactly what the `if !assert... { return }` pattern was doing manually
### Additional Notes
The remaining `if !assert` occurrences across other files were reviewed and intentionally kept because they serve different purposes (returning values from helper functions, setting flags, logging diagnostics without stopping, running inside raw goroutines, etc.).
Co-authored-by: nicolas.schweitzer <nicolas.schweitzer@datadoghq.com>1 parent d1a622c commit 72cf6d4
30 files changed
Lines changed: 119 additions & 335 deletions
File tree
- comp/core
- telemetry/telemetryimpl
- workloadmeta/collectors/internal/process
- pkg
- collector
- check/stats
- corechecks/gpu/integrationtests
- compliance/dbconfig
- dyninst
- procsubscribe/procscan
- fleet/installer/setup/config
- network/tracer
- trace/api
- util
- executable
- winutil/eventlog/subscription
- test/new-e2e/tests
- agent-health
- agent-log-pipelines
- linux-log/file-tailing
- windows-log/file-tailing
- agent-runtimes/infra
- containers
- cspm
- cws
- netpath/network-path-integration
- npm
- remote-config
- windows/install-test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
44 | | - | |
45 | | - | |
46 | | - | |
| 45 | + | |
47 | 46 | | |
48 | | - | |
49 | | - | |
50 | | - | |
| 47 | + | |
51 | 48 | | |
52 | 49 | | |
53 | | - | |
54 | | - | |
55 | | - | |
| 50 | + | |
56 | 51 | | |
57 | 52 | | |
58 | 53 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
28 | | - | |
29 | | - | |
| 27 | + | |
30 | 28 | | |
31 | | - | |
32 | | - | |
33 | | - | |
| 29 | + | |
34 | 30 | | |
35 | 31 | | |
36 | 32 | | |
| |||
Lines changed: 5 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
| |||
692 | 693 | | |
693 | 694 | | |
694 | 695 | | |
695 | | - | |
696 | | - | |
697 | | - | |
| 696 | + | |
| 697 | + | |
698 | 698 | | |
699 | 699 | | |
700 | 700 | | |
| |||
704 | 704 | | |
705 | 705 | | |
706 | 706 | | |
707 | | - | |
708 | | - | |
709 | | - | |
| 707 | + | |
| 708 | + | |
710 | 709 | | |
711 | 710 | | |
712 | 711 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
95 | 96 | | |
96 | 97 | | |
97 | 98 | | |
98 | | - | |
99 | | - | |
100 | | - | |
| 99 | + | |
101 | 100 | | |
102 | 101 | | |
103 | 102 | | |
| |||
Lines changed: 2 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
85 | | - | |
86 | | - | |
87 | | - | |
| 85 | + | |
| 86 | + | |
88 | 87 | | |
89 | 88 | | |
90 | 89 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| |||
599 | 600 | | |
600 | 601 | | |
601 | 602 | | |
602 | | - | |
603 | | - | |
604 | | - | |
| 603 | + | |
605 | 604 | | |
606 | 605 | | |
607 | 606 | | |
| |||
665 | 664 | | |
666 | 665 | | |
667 | 666 | | |
668 | | - | |
669 | | - | |
670 | | - | |
| 667 | + | |
671 | 668 | | |
672 | 669 | | |
673 | 670 | | |
| |||
698 | 695 | | |
699 | 696 | | |
700 | 697 | | |
701 | | - | |
702 | | - | |
703 | | - | |
| 698 | + | |
704 | 699 | | |
705 | 700 | | |
706 | 701 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
306 | 306 | | |
307 | 307 | | |
308 | 308 | | |
309 | | - | |
| 309 | + | |
310 | 310 | | |
311 | 311 | | |
312 | 312 | | |
313 | 313 | | |
314 | | - | |
315 | | - | |
316 | | - | |
| 314 | + | |
317 | 315 | | |
318 | 316 | | |
319 | 317 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
177 | 177 | | |
178 | 178 | | |
179 | 179 | | |
180 | | - | |
181 | | - | |
182 | | - | |
| 180 | + | |
183 | 181 | | |
184 | 182 | | |
185 | 183 | | |
| |||
Lines changed: 6 additions & 17 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
23 | 22 | | |
24 | 23 | | |
25 | 24 | | |
| |||
95 | 94 | | |
96 | 95 | | |
97 | 96 | | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
| 97 | + | |
| 98 | + | |
103 | 99 | | |
104 | 100 | | |
105 | | - | |
106 | | - | |
107 | | - | |
| 101 | + | |
108 | 102 | | |
109 | 103 | | |
110 | | - | |
111 | | - | |
112 | | - | |
| 104 | + | |
113 | 105 | | |
114 | | - | |
| 106 | + | |
115 | 107 | | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
| 108 | + | |
120 | 109 | | |
121 | 110 | | |
122 | 111 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| |||
362 | 363 | | |
363 | 364 | | |
364 | 365 | | |
365 | | - | |
366 | | - | |
367 | | - | |
| 366 | + | |
368 | 367 | | |
369 | 368 | | |
370 | 369 | | |
| |||
425 | 424 | | |
426 | 425 | | |
427 | 426 | | |
428 | | - | |
429 | | - | |
430 | | - | |
| 427 | + | |
431 | 428 | | |
432 | 429 | | |
433 | | - | |
434 | | - | |
435 | | - | |
| 430 | + | |
436 | 431 | | |
437 | 432 | | |
438 | 433 | | |
| |||
0 commit comments