Skip to content

btf: allow unit tests to run without network access#343

Merged
mozillazg merged 1 commit intomasterfrom
improve-tests
Oct 1, 2025
Merged

btf: allow unit tests to run without network access#343
mozillazg merged 1 commit intomasterfrom
improve-tests

Conversation

@mozillazg
Copy link
Owner

@mozillazg mozillazg commented Oct 1, 2025

Summary by CodeRabbit

  • Refactor
    • Improved testability of BTF loading and remote retrieval flows via abstraction, with no change to user-facing behavior.
  • Tests
    • Added deterministic unit tests for remote BTF retrieval using stubbed dependencies to avoid real network and decompression, reducing flakiness and improving reliability.
  • Documentation
    • Clarified internal testing approach in code comments to aid maintainability.
  • Chores
    • General test setup and cleanup enhancements to ensure consistent execution across environments.

No user-facing changes.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 1, 2025

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title clearly and concisely summarizes the primary change of the pull request—enabling the BTF tests to run without network access—using concise, specific phrasing that a teammate can quickly understand without unnecessary detail.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch improve-tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
internal/btf/btf.go (1)

199-217: Use loadSpecFunc for consistency.

Line 203 directly calls loadSpec(path) instead of using the loadSpecFunc variable. This bypasses the mocking infrastructure introduced by this PR and prevents tests from fully stubbing BTF loading in loadSpecFromCandidateLocations.

Apply this diff:

-	spec, path, err := loadSpec(path)
+	spec, path, err := loadSpecFunc(path)
🧹 Nitpick comments (2)
internal/btf/btf_test.go (2)

18-39: Consider stubbing saveDataToFileFunc for full isolation.

The test successfully eliminates network dependencies, but saveDataToFileFunc remains unstubbed, meaning the test still performs file I/O. While this verifies file creation (line 82: assert.FileExists), stubbing it would provide complete isolation and faster tests.

If full isolation is desired, add:

 	origHTTPGet := httpGetFunc
 	origDecompress := decompressXzReaderFunc
 	origLoadSpec := loadSpecFunc
+	origSaveData := saveDataToFileFunc
 	httpGetFunc = func(ctx context.Context, url string) (*http.Response, error) {
 		return &http.Response{
 			StatusCode: http.StatusOK,
 			Body:       io.NopCloser(strings.NewReader("stub")),
 		}, nil
 	}
 	decompressXzReaderFunc = func(src io.Reader) ([]byte, error) {
 		return []byte("stub"), nil
 	}
+	saveDataToFileFunc = func(data []byte, path string) error {
+		// Stub: no-op or minimal file creation
+		return nil
+	}
 	loadSpecFunc = func(p string) (*ebpfbtf.Spec, string, error) {
 		return dummySpec, p, nil
 	}
 	t.Cleanup(func() {
 		httpGetFunc = origHTTPGet
 		decompressXzReaderFunc = origDecompress
 		loadSpecFunc = origLoadSpec
+		saveDataToFileFunc = origSaveData
 	})

Note: If you stub saveDataToFileFunc, you may need to adjust the assert.FileExists expectation or create the file in the stub.


89-105: Consider stubbing saveDataToFileFunc for full isolation.

Similar to Test_loadSpecFromBTFHub, this test eliminates network dependencies but saveDataToFileFunc remains unstubbed. For complete isolation and faster tests, consider stubbing it.

Apply the same pattern as suggested for Test_loadSpecFromBTFHub:

 	origHTTPGet := httpGetFunc
 	origLoadSpec := loadSpecFunc
+	origSaveData := saveDataToFileFunc
 	httpGetFunc = func(ctx context.Context, url string) (*http.Response, error) {
 		return &http.Response{
 			StatusCode: http.StatusOK,
 			Body:       io.NopCloser(strings.NewReader("stub")),
 		}, nil
 	}
+	saveDataToFileFunc = func(data []byte, path string) error {
+		return nil
+	}
 	loadSpecFunc = func(p string) (*ebpfbtf.Spec, string, error) {
 		return dummySpec, p, nil
 	}
 	t.Cleanup(func() {
 		httpGetFunc = origHTTPGet
 		loadSpecFunc = origLoadSpec
+		saveDataToFileFunc = origSaveData
 	})
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8f4c363 and 814fd9d.

📒 Files selected for processing (2)
  • internal/btf/btf.go (6 hunks)
  • internal/btf/btf_test.go (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
internal/btf/btf.go (1)
internal/log/log.go (1)
  • Infof (56-58)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: build
  • GitHub Check: unit-test
  • GitHub Check: release-test
🔇 Additional comments (2)
internal/btf/btf.go (2)

18-23: LGTM! Clean dependency injection pattern.

The function pointer variables enable test mocking without network access, directly supporting the PR objective. The naming is clear and follows Go conventions.


43-43: LGTM! Consistent use of function pointers throughout.

All call sites correctly use the function pointer variables (loadSpecFunc, httpGetFunc, decompressXzReaderFunc, saveDataToFileFunc), enabling comprehensive mocking in tests.

Also applies to: 116-116, 122-122, 128-128, 132-132, 136-136, 149-149, 155-155, 165-165, 169-169, 216-216

@coveralls
Copy link

Coverage Status

coverage: 73.149% (-0.7%) from 73.823%
when pulling 814fd9d on improve-tests
into 8f4c363 on master.

@mozillazg mozillazg merged commit c873bed into master Oct 1, 2025
63 checks passed
@mozillazg mozillazg deleted the improve-tests branch October 1, 2025 09:42
@coderabbitai coderabbitai bot mentioned this pull request Oct 1, 2025
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