added unit and mock tests for export coverage#229
added unit and mock tests for export coverage#229stillalearner merged 1 commit intomigtools:mainfrom
Conversation
📝 WalkthroughWalkthroughTwo test files are significantly expanded with new fixtures, helper utilities, and comprehensive test cases. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Test Coverage ReportTotal: 21.2% Per-package coverage
Full function-level detailsPosted by CI |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@cmd/export/discover_test.go`:
- Around line 684-714: The test
TestWriteResources_createFailsWhenDestinationFileNotWritable relies on POSIX
file permissions but doesn't skip when run as root; update the test to detect
root (os.Geteuid() == 0) and call t.Skip("skipping test when run as root") at
the start of the test so it behaves like the other permission-based tests;
ensure you add the check near the top of
TestWriteResources_createFailsWhenDestinationFileNotWritable before creating
files/directories and leave references to writeResources and testLogger
unchanged.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: e5e794f6-5035-4997-8abf-3ea2fa02366b
📒 Files selected for processing (2)
cmd/export/cluster_test.gocmd/export/discover_test.go
| func TestWriteResources_createFailsWhenDestinationFileNotWritable(t *testing.T) { | ||
| log := testLogger() | ||
| tmp := t.TempDir() | ||
| resourceDir := filepath.Join(tmp, "out") | ||
| clusterDir := filepath.Join(tmp, "_cluster") | ||
| if err := os.MkdirAll(resourceDir, 0700); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := os.MkdirAll(clusterDir, 0700); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| nsObj := namespacedObj("ns", "pod-a") | ||
| nsObj.SetKind("Pod") | ||
| nsObj.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Pod"}) | ||
| path := filepath.Join(resourceDir, getFilePath(nsObj)) | ||
| if err := os.WriteFile(path, []byte("seed"), 0400); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| resources := []*groupResource{ | ||
| { | ||
| APIResource: metav1.APIResource{Name: "pods", Kind: "Pod"}, | ||
| objects: &unstructured.UnstructuredList{Items: []unstructured.Unstructured{nsObj}}, | ||
| }, | ||
| } | ||
| errs := writeResources(resources, clusterDir, resourceDir, log) | ||
| if len(errs) != 1 { | ||
| t.Fatalf("want 1 error opening/truncating read-only destination file, got %v", errs) | ||
| } | ||
| } |
There was a problem hiding this comment.
Missing root user skip for permission-based test.
This test relies on file permissions (0400) to cause a write failure, but root can write to any file regardless of permissions. Other permission-based tests in this file (e.g., lines 476-478, 540-542, 594-596) properly skip when running as root.
Proposed fix
func TestWriteResources_createFailsWhenDestinationFileNotWritable(t *testing.T) {
+ if os.Geteuid() == 0 {
+ t.Skip("root can write to any file regardless of permissions")
+ }
log := testLogger()
tmp := t.TempDir()📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| func TestWriteResources_createFailsWhenDestinationFileNotWritable(t *testing.T) { | |
| log := testLogger() | |
| tmp := t.TempDir() | |
| resourceDir := filepath.Join(tmp, "out") | |
| clusterDir := filepath.Join(tmp, "_cluster") | |
| if err := os.MkdirAll(resourceDir, 0700); err != nil { | |
| t.Fatal(err) | |
| } | |
| if err := os.MkdirAll(clusterDir, 0700); err != nil { | |
| t.Fatal(err) | |
| } | |
| nsObj := namespacedObj("ns", "pod-a") | |
| nsObj.SetKind("Pod") | |
| nsObj.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Pod"}) | |
| path := filepath.Join(resourceDir, getFilePath(nsObj)) | |
| if err := os.WriteFile(path, []byte("seed"), 0400); err != nil { | |
| t.Fatal(err) | |
| } | |
| resources := []*groupResource{ | |
| { | |
| APIResource: metav1.APIResource{Name: "pods", Kind: "Pod"}, | |
| objects: &unstructured.UnstructuredList{Items: []unstructured.Unstructured{nsObj}}, | |
| }, | |
| } | |
| errs := writeResources(resources, clusterDir, resourceDir, log) | |
| if len(errs) != 1 { | |
| t.Fatalf("want 1 error opening/truncating read-only destination file, got %v", errs) | |
| } | |
| } | |
| func TestWriteResources_createFailsWhenDestinationFileNotWritable(t *testing.T) { | |
| if os.Geteuid() == 0 { | |
| t.Skip("root can write to any file regardless of permissions") | |
| } | |
| log := testLogger() | |
| tmp := t.TempDir() | |
| resourceDir := filepath.Join(tmp, "out") | |
| clusterDir := filepath.Join(tmp, "_cluster") | |
| if err := os.MkdirAll(resourceDir, 0700); err != nil { | |
| t.Fatal(err) | |
| } | |
| if err := os.MkdirAll(clusterDir, 0700); err != nil { | |
| t.Fatal(err) | |
| } | |
| nsObj := namespacedObj("ns", "pod-a") | |
| nsObj.SetKind("Pod") | |
| nsObj.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Pod"}) | |
| path := filepath.Join(resourceDir, getFilePath(nsObj)) | |
| if err := os.WriteFile(path, []byte("seed"), 0400); err != nil { | |
| t.Fatal(err) | |
| } | |
| resources := []*groupResource{ | |
| { | |
| APIResource: metav1.APIResource{Name: "pods", Kind: "Pod"}, | |
| objects: &unstructured.UnstructuredList{Items: []unstructured.Unstructured{nsObj}}, | |
| }, | |
| } | |
| errs := writeResources(resources, clusterDir, resourceDir, log) | |
| if len(errs) != 1 { | |
| t.Fatalf("want 1 error opening/truncating read-only destination file, got %v", errs) | |
| } | |
| } |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@cmd/export/discover_test.go` around lines 684 - 714, The test
TestWriteResources_createFailsWhenDestinationFileNotWritable relies on POSIX
file permissions but doesn't skip when run as root; update the test to detect
root (os.Geteuid() == 0) and call t.Skip("skipping test when run as root") at
the start of the test so it behaves like the other permission-based tests;
ensure you add the check near the top of
TestWriteResources_createFailsWhenDestinationFileNotWritable before creating
files/directories and leave references to writeResources and testLogger
unchanged.
Summary
Adds unit and integration tests under
cmd/exportfor discovery, cluster-scoped RBAC/SCC filtering, and export I/O. Improves coverage and regression safety.Changes
discover_test.gotestDiscoverymock (discovery.DiscoveryInterface) fordiscoverPreferredResources(verbs, partial discovery, errors, post-filter empty lists).getObjects/resourceToExtract(ConfigMaps, ClusterRoles, imagestreamtags/imagetags, Forbidden/NotFound, badGroupVersion, Events, non-admitted cluster kinds, empty verbs/API lists).prepareClusterResourceDir,prepareFailuresDir,writeResources,writeErrors— happy paths and failure cases (bad paths, permissions, marshal/create errors).resourceToExtract:MethodNotSupported, generic list error, empty list (skip, no error).getObjects: label selector propagation; imagestreamtags whenGetfails afterList.iterateItemsInList/iterateItemsByGet: non-*unstructured.Unstructuredlist items.GroupVersionKind.cluster_test.gofilterRbacResourcesintegration (SA + CRB + ClusterRole, group subjects, multiple CRBs, drop unmatched cluster RBAC, wrongAPIGrouppass-through).clusterRoleToUnstructuredsets GVK foracceptClusterRole.RoleRefto SCC /system:openshift:scc:<name>, malformed filtered CRB, name mismatch;filterRbacResources+ SCC.cluster.goedge coverage:isClusterScopedResource,exportedSANamespaces, CRB/CR/SCC conversion and subject branches, NA CRB placeholder, etc.clusterRoleBindingUnstructuredThatFailsConversionfor realFromUnstructuredfailures.How to test