Skip to content

tests: replace most t.Fatalf calls#1883

Merged
AndrewChubatiuk merged 1 commit intomasterfrom
more-asserts
Feb 25, 2026
Merged

tests: replace most t.Fatalf calls#1883
AndrewChubatiuk merged 1 commit intomasterfrom
more-asserts

Conversation

@vrutkovs
Copy link
Collaborator

@vrutkovs vrutkovs commented Feb 24, 2026

Testify asserts produce a significantly better output


Summary by cubic

Replaced most t.Fatalf/t.Errorf in tests with testify assertions for clearer failures and cleaner test code. No functional changes; tests only.

  • Refactors
    • Switched manual checks to assert.NoError/assert.Error/assert.Equal and related helpers.
    • Replaced reflect.DeepEqual with assert.Equal and removed reflect imports.
    • Standardized validations with assert.Contains/assert.Empty/assert.NotNil/assert.Len/assert.True.
    • Used assertions to simplify control flow instead of manual if/return paths.
    • Applied across v1beta1 API and operator factory tests (build, converter, k8stools, reconcile, vmagent, vmalert, vmalertmanager, vlagent, vlsingle, vtsingle, vmauth, vmanomaly).

Written for commit e9909ed. Summary will update on new commits.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6 issues found across 29 files

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="internal/controller/operator/factory/vmagent/podscrape_test.go">

<violation number="1" location="internal/controller/operator/factory/vmagent/podscrape_test.go:32">
P2: This assert does not stop the test on error, unlike the previous return path. If generatePodScrapeConfig fails, the test continues and may panic or produce confusing failures. Use a fatal assertion or return on failure.</violation>

<violation number="2" location="internal/controller/operator/factory/vmagent/podscrape_test.go:34">
P2: This assert doesn't halt the test on marshal failure, unlike the previous early return. Consider returning on failure (or use require.NoError) to keep test failures focused on the real error.</violation>
</file>

<file name="internal/controller/operator/factory/vmalertmanager/config_test.go">

<violation number="1" location="internal/controller/operator/factory/vmalertmanager/config_test.go:58">
P3: Non-fatal assert lets the test continue after an unexpected error, which can dereference `got` and hide the real failure. Guard the assertion to return early on failure.</violation>
</file>

<file name="internal/controller/operator/factory/vmagent/nodescrape_test.go">

<violation number="1" location="internal/controller/operator/factory/vmagent/nodescrape_test.go:33">
P2: `assert.NoError` doesn’t stop the test. If `generateNodeScrapeConfig` returns an error, the test continues and uses `got`, which can cause misleading failures. Add an early return (or use require.NoError) to match the previous behavior.</violation>

<violation number="2" location="internal/controller/operator/factory/vmagent/nodescrape_test.go:35">
P2: `assert.NoError` here doesn’t stop execution, so the test can still compare `gotBytes` after a marshal failure. Add an early return (or use require.NoError) to avoid cascading failures.</violation>
</file>

<file name="internal/controller/operator/factory/vlsingle/vlogs_test.go">

<violation number="1" location="internal/controller/operator/factory/vlsingle/vlogs_test.go:33">
P2: This assertion block no longer short-circuits the test on expected/actual errors, so the test continues to build and fetch services even when an error path is triggered. Add a return for the error case (or use require.*) to avoid executing the rest of the test on failure.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment on lines +33 to 37
if o.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Feb 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This assertion block no longer short-circuits the test on expected/actual errors, so the test continues to build and fetch services even when an error path is triggered. Add a return for the error case (or use require.*) to avoid executing the rest of the test on failure.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At internal/controller/operator/factory/vlsingle/vlogs_test.go, line 33:

<comment>This assertion block no longer short-circuits the test on expected/actual errors, so the test continues to build and fetch services even when an error path is triggered. Add a return for the error case (or use require.*) to avoid executing the rest of the test on failure.</comment>

<file context>
@@ -30,9 +30,10 @@ func TestCreateOrUpdateVLogs(t *testing.T) {
-		if (err != nil) != o.wantErr {
-			t.Errorf("CreateOrUpdateVLogs() error = %v, wantErr %v", err, o.wantErr)
-			return
+		if o.wantErr {
+			assert.Error(t, err)
+		} else {
</file context>
Suggested change
if o.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
if o.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
Fix with Cubic

@vrutkovs vrutkovs force-pushed the more-asserts branch 2 times, most recently from 8c265b4 to 443e620 Compare February 24, 2026 20:53
t.Errorf("CreateOrUpdateService() error = %v, wantErr %v", err, o.wantErr)
return
if o.wantErr {
assert.Error(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just return after error check instead of placing a whole successful path into else?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is even worse.

I was attempting to find the middle ground between

		if o.wantErr {
			assert.Error(t, err)
		} else {
			assert.NoError(t, err)
		}

and

		if o.wantErr {
			assert.Error(t, err)
			return
		}
		assert.NoError(t, err)
		svc := build.Service(o.cr, o.cr.Spec.Port, nil)

Testify asserts produce a significantly better output
@AndrewChubatiuk AndrewChubatiuk merged commit e1aaf81 into master Feb 25, 2026
6 checks passed
@AndrewChubatiuk AndrewChubatiuk deleted the more-asserts branch February 25, 2026 04:36
vrutkovs added a commit that referenced this pull request Mar 3, 2026
Testify asserts produce a significantly better output
AndrewChubatiuk pushed a commit that referenced this pull request Mar 3, 2026
Testify asserts produce a significantly better output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants