Skip to content

refactor(tests): Replace assert with require for guard assertions#46913

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 11 commits into
mainfrom
nschweitzer/use_require
Mar 4, 2026
Merged

refactor(tests): Replace assert with require for guard assertions#46913
gh-worker-dd-mergequeue-cf854d[bot] merged 11 commits into
mainfrom
nschweitzer/use_require

Conversation

@chouetz
Copy link
Copy Markdown
Member

@chouetz chouetz commented Feb 25, 2026

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() calls runtime.Goexit() to stop the current goroutine. The official Go testing documentation 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(), 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.).

@agent-platform-auto-pr
Copy link
Copy Markdown
Contributor

agent-platform-auto-pr Bot commented Feb 25, 2026

Files inventory check summary

File checks results against ancestor cfd74cd9:

Results for datadog-agent_7.78.0~devel.git.137.172c742.pipeline.99461255-1_amd64.deb:

No change detected

@agent-platform-auto-pr
Copy link
Copy Markdown
Contributor

agent-platform-auto-pr Bot commented Feb 25, 2026

Static quality checks

✅ Please find below the results from static quality gates
Comparison made with ancestor cfd74cd
📊 Static Quality Gates Dashboard
🔗 SQG Job

31 successful checks with minimal change (< 2 KiB)
Quality gate Current Size
agent_deb_amd64 744.330 MiB
agent_deb_amd64_fips 702.557 MiB
agent_heroku_amd64 310.884 MiB
agent_msi 608.200 MiB
agent_rpm_amd64 744.314 MiB
agent_rpm_amd64_fips 702.540 MiB
agent_rpm_arm64 722.367 MiB
agent_rpm_arm64_fips 683.645 MiB
agent_suse_amd64 744.314 MiB
agent_suse_amd64_fips 702.540 MiB
agent_suse_arm64 722.367 MiB
agent_suse_arm64_fips 683.645 MiB
docker_agent_amd64 804.915 MiB
docker_agent_arm64 807.762 MiB
docker_agent_jmx_amd64 995.826 MiB
docker_agent_jmx_arm64 987.455 MiB
docker_cluster_agent_amd64 203.431 MiB
docker_cluster_agent_arm64 217.835 MiB
docker_cws_instrumentation_amd64 7.135 MiB
docker_cws_instrumentation_arm64 6.689 MiB
docker_dogstatsd_amd64 38.512 MiB
docker_dogstatsd_arm64 36.812 MiB
dogstatsd_deb_amd64 29.736 MiB
dogstatsd_deb_arm64 27.893 MiB
dogstatsd_rpm_amd64 29.736 MiB
dogstatsd_suse_amd64 29.736 MiB
iot_agent_deb_amd64 42.624 MiB
iot_agent_deb_arm64 39.726 MiB
iot_agent_deb_armhf 40.455 MiB
iot_agent_rpm_amd64 42.625 MiB
iot_agent_suse_amd64 42.625 MiB
On-wire sizes (compressed)
Quality gate Change Size (prev → curr → max)
agent_deb_amd64 -11.32 KiB (0.01% reduction) 173.745 → 173.734 → 186.090
agent_deb_amd64_fips +12.62 KiB (0.01% increase) 164.741 → 164.753 → 180.330
agent_heroku_amd64 -3.25 KiB (0.00% reduction) 74.977 → 74.974 → 88.440
agent_msi -16.0 KiB (0.01% reduction) 137.793 → 137.777 → 154.470
agent_rpm_amd64 -34.6 KiB (0.02% reduction) 175.719 → 175.685 → 189.170
agent_rpm_amd64_fips -37.43 KiB (0.02% reduction) 167.588 → 167.552 → 181.060
agent_rpm_arm64 -10.82 KiB (0.01% reduction) 158.010 → 157.999 → 170.020
agent_rpm_arm64_fips +59.69 KiB (0.04% increase) 150.725 → 150.783 → 164.130
agent_suse_amd64 -34.6 KiB (0.02% reduction) 175.719 → 175.685 → 189.170
agent_suse_amd64_fips -37.43 KiB (0.02% reduction) 167.588 → 167.552 → 181.060
agent_suse_arm64 -10.82 KiB (0.01% reduction) 158.010 → 157.999 → 170.020
agent_suse_arm64_fips +59.69 KiB (0.04% increase) 150.725 → 150.783 → 164.130
docker_agent_amd64 +2.89 KiB (0.00% increase) 266.274 → 266.277 → 279.410
docker_agent_arm64 neutral 253.547 MiB → 267.960
docker_agent_jmx_amd64 neutral 334.918 MiB → 348.040
docker_agent_jmx_arm64 neutral 318.161 MiB → 332.560
docker_cluster_agent_amd64 neutral 71.198 MiB → 71.920
docker_cluster_agent_arm64 neutral 66.833 MiB → 67.220
docker_cws_instrumentation_amd64 neutral 2.995 MiB → 3.330
docker_cws_instrumentation_arm64 neutral 2.726 MiB → 3.090
docker_dogstatsd_amd64 neutral 14.904 MiB → 15.820
docker_dogstatsd_arm64 neutral 14.239 MiB → 14.830
dogstatsd_deb_amd64 neutral 7.856 MiB → 8.790
dogstatsd_deb_arm64 neutral 6.743 MiB → 7.710
dogstatsd_rpm_amd64 neutral 7.865 MiB → 8.800
dogstatsd_suse_amd64 neutral 7.865 MiB → 8.800
iot_agent_deb_amd64 neutral 11.239 MiB → 12.040
iot_agent_deb_arm64 +2.42 KiB (0.02% increase) 9.602 → 9.605 → 10.450
iot_agent_deb_armhf neutral 9.797 MiB → 10.620
iot_agent_rpm_amd64 neutral 11.256 MiB → 12.060
iot_agent_suse_amd64 neutral 11.256 MiB → 12.060

@cit-pr-commenter-54b7da
Copy link
Copy Markdown

cit-pr-commenter-54b7da Bot commented Feb 25, 2026

Regression Detector

Regression Detector Results

Metrics dashboard
Target profiles
Run ID: b3ba31c0-efc8-44c9-a04b-d75b7435a1bb

Baseline: d1a622c
Comparison: 72cf6d4
Diff

Optimization Goals: ✅ No significant changes detected

Experiments ignored for regressions

Regressions in experiments with settings containing erratic: true are ignored.

perf experiment goal Δ mean % Δ mean % CI trials links
docker_containers_cpu % cpu utilization -0.50 [-3.63, +2.64] 1 Logs

Fine details of change detection per experiment

perf experiment goal Δ mean % Δ mean % CI trials links
quality_gate_logs % cpu utilization +2.99 [+1.48, +4.50] 1 Logs bounds checks dashboard
ddot_logs memory utilization +0.71 [+0.65, +0.77] 1 Logs
ddot_metrics_sum_cumulative memory utilization +0.57 [+0.43, +0.71] 1 Logs
quality_gate_metrics_logs memory utilization +0.41 [+0.19, +0.63] 1 Logs bounds checks dashboard
ddot_metrics_sum_cumulativetodelta_exporter memory utilization +0.14 [-0.09, +0.36] 1 Logs
quality_gate_idle memory utilization +0.10 [+0.06, +0.14] 1 Logs bounds checks dashboard
ddot_metrics_sum_delta memory utilization +0.09 [-0.08, +0.25] 1 Logs
uds_dogstatsd_20mb_12k_contexts_20_senders memory utilization +0.07 [+0.01, +0.13] 1 Logs
otlp_ingest_metrics memory utilization +0.05 [-0.10, +0.21] 1 Logs
quality_gate_idle_all_features memory utilization +0.04 [+0.01, +0.08] 1 Logs bounds checks dashboard
file_tree memory utilization +0.03 [-0.02, +0.07] 1 Logs
file_to_blackhole_500ms_latency egress throughput +0.01 [-0.37, +0.39] 1 Logs
uds_dogstatsd_to_api ingress throughput +0.00 [-0.13, +0.13] 1 Logs
uds_dogstatsd_to_api_v3 ingress throughput -0.00 [-0.13, +0.12] 1 Logs
tcp_dd_logs_filter_exclude ingress throughput -0.00 [-0.10, +0.09] 1 Logs
file_to_blackhole_0ms_latency egress throughput -0.01 [-0.49, +0.46] 1 Logs
ddot_metrics memory utilization -0.01 [-0.19, +0.17] 1 Logs
file_to_blackhole_100ms_latency egress throughput -0.04 [-0.08, +0.00] 1 Logs
file_to_blackhole_1000ms_latency egress throughput -0.06 [-0.49, +0.36] 1 Logs
tcp_syslog_to_blackhole ingress throughput -0.10 [-0.17, -0.02] 1 Logs
docker_containers_memory memory utilization -0.25 [-0.32, -0.18] 1 Logs
otlp_ingest_logs memory utilization -0.45 [-0.54, -0.35] 1 Logs
docker_containers_cpu % cpu utilization -0.50 [-3.63, +2.64] 1 Logs

Bounds Checks: ✅ Passed

perf experiment bounds_check_name replicates_passed links
docker_containers_cpu simple_check_run 10/10
docker_containers_memory memory_usage 10/10
docker_containers_memory simple_check_run 10/10
file_to_blackhole_0ms_latency lost_bytes 10/10
file_to_blackhole_0ms_latency memory_usage 10/10
file_to_blackhole_1000ms_latency lost_bytes 10/10
file_to_blackhole_1000ms_latency memory_usage 10/10
file_to_blackhole_100ms_latency lost_bytes 10/10
file_to_blackhole_100ms_latency memory_usage 10/10
file_to_blackhole_500ms_latency lost_bytes 10/10
file_to_blackhole_500ms_latency memory_usage 10/10
quality_gate_idle intake_connections 10/10 bounds checks dashboard
quality_gate_idle memory_usage 10/10 bounds checks dashboard
quality_gate_idle_all_features intake_connections 10/10 bounds checks dashboard
quality_gate_idle_all_features memory_usage 10/10 bounds checks dashboard
quality_gate_logs intake_connections 10/10 bounds checks dashboard
quality_gate_logs lost_bytes 10/10 bounds checks dashboard
quality_gate_logs memory_usage 10/10 bounds checks dashboard
quality_gate_metrics_logs cpu_usage 10/10 bounds checks dashboard
quality_gate_metrics_logs intake_connections 10/10 bounds checks dashboard
quality_gate_metrics_logs lost_bytes 10/10 bounds checks dashboard
quality_gate_metrics_logs memory_usage 10/10 bounds checks dashboard

Explanation

Confidence level: 90.00%
Effect size tolerance: |Δ mean %| ≥ 5.00%

Performance changes are noted in the perf column of each table:

  • ✅ = significantly better comparison variant performance
  • ❌ = significantly worse comparison variant performance
  • ➖ = no significant change in performance

A regression test is an A/B test of target performance in a repeatable rig, where "performance" is measured as "comparison variant minus baseline variant" for an optimization goal (e.g., ingress throughput). Due to intrinsic variability in measuring that goal, we can only estimate its mean value for each experiment; we report uncertainty in that value as a 90.00% confidence interval denoted "Δ mean % CI".

For each experiment, we decide whether a change in performance is a "regression" -- a change worth investigating further -- if all of the following criteria are true:

  1. Its estimated |Δ mean %| ≥ 5.00%, indicating the change is big enough to merit a closer look.

  2. Its 90.00% confidence interval "Δ mean % CI" does not contain zero, indicating that if our statistical model is accurate, there is at least a 90.00% chance there is a difference in performance between baseline and comparison variants.

  3. Its configuration does not mark it "erratic".

CI Pass/Fail Decision

Passed. All Quality Gates passed.

  • quality_gate_idle, bounds check intake_connections: 10/10 replicas passed. Gate passed.
  • quality_gate_idle, bounds check memory_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_idle_all_features, bounds check intake_connections: 10/10 replicas passed. Gate passed.
  • quality_gate_idle_all_features, bounds check memory_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_logs, bounds check memory_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_logs, bounds check lost_bytes: 10/10 replicas passed. Gate passed.
  • quality_gate_logs, bounds check intake_connections: 10/10 replicas passed. Gate passed.
  • quality_gate_metrics_logs, bounds check lost_bytes: 10/10 replicas passed. Gate passed.
  • quality_gate_metrics_logs, bounds check cpu_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_metrics_logs, bounds check intake_connections: 10/10 replicas passed. Gate passed.
  • quality_gate_metrics_logs, bounds check memory_usage: 10/10 replicas passed. Gate passed.

@chouetz chouetz force-pushed the nschweitzer/use_require branch from 3cb6e00 to b84e9b1 Compare February 25, 2026 18:07
…e calls

require functions don't return a value (unlike assert which returns bool),
so they can't be used in || expressions. Split combined conditions into
separate require calls.
@chouetz chouetz force-pushed the nschweitzer/use_require branch from b84e9b1 to 93c1301 Compare February 26, 2026 08:01
- Fix gofmt import ordering in process_collector_test.go
- Remove unused getCheckNames function in process_autodiscovery_test.go
  (was only used in debug logging lines that were removed)
- Revert require.Equal back to assert.Equal in end_to_end_test.go where
  the variable t is assert.TestingT (which lacks FailNow required by require)
@chouetz chouetz force-pushed the nschweitzer/use_require branch from 93c1301 to 3d5a831 Compare February 26, 2026 09:33
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Feb 26, 2026

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@dd-octo-sts dd-octo-sts Bot added the team/ecs-experiences Issues and PRs owned by the ECS Experiences team label Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.