Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions modules/api/pkg/resource/pod/pod_test.go
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,
},
}
Comment on lines +52 to +82

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

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.


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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The current error and result checking logic using (err != nil) != tt.expectedError is a bit dense and can be hard to follow. It's generally better to have separate, explicit checks for expected errors versus unexpected errors, and for the results. This makes the test's intent clearer and easier to debug if it fails.

            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)
			}

})
}
}