-
Notifications
You must be signed in to change notification settings - Fork 42
test: add unit tests for pod resource #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /* | ||
| Copyright 2025 The KubeEdge Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package pod | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/client-go/kubernetes/fake" | ||
| ) | ||
|
|
||
| func TestGetPodList(t *testing.T) { | ||
| // Prepare test data | ||
| ns := "default" | ||
| pod1 := &corev1.Pod{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "pod-1", | ||
| Namespace: ns, | ||
| }, | ||
| } | ||
| pod2 := &corev1.Pod{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "pod-2", | ||
| Namespace: ns, | ||
| }, | ||
| } | ||
| pod3 := &corev1.Pod{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "pod-3", | ||
| Namespace: "other-ns", | ||
| }, | ||
| } | ||
|
|
||
| // Create a fake clientset with initial data | ||
| client := fake.NewSimpleClientset(pod1, pod2, pod3) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| namespace string | ||
| expectedCount int | ||
| expectedError bool | ||
| }{ | ||
| { | ||
| name: "get pods from default namespace", | ||
| namespace: "default", | ||
| expectedCount: 2, | ||
| expectedError: false, | ||
| }, | ||
| { | ||
| name: "get pods from other namespace", | ||
| namespace: "other-ns", | ||
| expectedCount: 1, | ||
| expectedError: false, | ||
| }, | ||
| { | ||
| name: "get pods from non-existent namespace", | ||
| namespace: "non-existent", | ||
| expectedCount: 0, | ||
| expectedError: false, | ||
| }, | ||
| { | ||
| name: "get all pods (all namespaces)", | ||
| namespace: "", | ||
| expectedCount: 3, | ||
| expectedError: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| list, err := GetPodList(client, tt.namespace) | ||
|
|
||
| if (err != nil) != tt.expectedError { | ||
| t.Errorf("GetPodList() error = %v, expectedError %v", err, tt.expectedError) | ||
| return | ||
| } | ||
|
|
||
| if list == nil { | ||
| if tt.expectedCount != 0 { | ||
| t.Errorf("GetPodList() returned nil list, expected count %d", tt.expectedCount) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if len(list.Items) != tt.expectedCount { | ||
| t.Errorf("GetPodList() returned %d pods, expected %d", len(list.Items), tt.expectedCount) | ||
| } | ||
|
Comment on lines
+88
to
+102
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current error and result checking logic using if tt.expectedError {
if err == nil {
t.Errorf("GetPodList() expected an error, but got none")
}
return
}
if err != nil {
t.Errorf("GetPodList() returned unexpected error: %v", err)
return
}
if list == nil {
t.Errorf("GetPodList() returned nil list without an error")
return
}
if len(list.Items) != tt.expectedCount {
t.Errorf("GetPodList() returned %d pods, expected %d", len(list.Items), tt.expectedCount)
} |
||
| }) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test suite is great for covering the success scenarios. To make it more robust, consider adding a test case that verifies the error handling logic in
GetPodList. Currently, if the Kubernetes client returns an error, it's not being tested.You could add a test case to your table like this:
{ name: "client returns error", namespace: "default", expectedCount: 0, expectedError: true, },To make this test pass, you'll need to configure the fake client to return an error. This is typically done using a reactor. Since the client is created once for all tests, you might need to refactor the test to create a client per subtest or use a separate test function for the error case.