@@ -22,28 +22,19 @@ import (
2222
2323 "github.com/stretchr/testify/assert"
2424 corev1 "k8s.io/api/core/v1"
25+ apierrors "k8s.io/apimachinery/pkg/api/errors"
2526 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627 "k8s.io/apimachinery/pkg/labels"
28+ "k8s.io/apimachinery/pkg/runtime/schema"
2729 listersv1 "k8s.io/client-go/listers/core/v1"
2830)
2931
30- // Helper function to create a pod with owner reference
31- func createPodWithOwner (name , namespace , sandboxName string , phase corev1.PodPhase , podIP string ) * corev1.Pod {
32- controller := true
32+ // Helper function to create a pod.
33+ func createPod (name , namespace string , phase corev1.PodPhase , podIP string ) * corev1.Pod {
3334 return & corev1.Pod {
3435 ObjectMeta : metav1.ObjectMeta {
3536 Name : name ,
3637 Namespace : namespace ,
37- Labels : map [string ]string {
38- SandboxNameLabelKey : sandboxName ,
39- },
40- OwnerReferences : []metav1.OwnerReference {
41- {
42- Kind : "Sandbox" ,
43- Name : sandboxName ,
44- Controller : & controller ,
45- },
46- },
4738 },
4839 Status : corev1.PodStatus {
4940 Phase : phase ,
@@ -52,6 +43,14 @@ func createPodWithOwner(name, namespace, sandboxName string, phase corev1.PodPha
5243 }
5344}
5445
46+ func createPodWithLabel (name , namespace , sandboxName string , phase corev1.PodPhase , podIP string ) * corev1.Pod {
47+ pod := createPod (name , namespace , phase , podIP )
48+ pod .Labels = map [string ]string {
49+ SandboxNameLabelKey : sandboxName ,
50+ }
51+ return pod
52+ }
53+
5554// mockPodNamespaceLister is a mock implementation of listersv1.PodNamespaceLister
5655type mockPodNamespaceLister struct {
5756 pods []* corev1.Pod
@@ -76,7 +75,7 @@ func (m *mockPodNamespaceLister) Get(name string) (*corev1.Pod, error) {
7675 return pod , nil
7776 }
7877 }
79- return nil , nil
78+ return nil , apierrors . NewNotFound (schema. GroupResource { Resource : "pods" }, name )
8079}
8180
8281// mockPodLister is a mock implementation of listersv1.PodLister
@@ -118,7 +117,7 @@ func (m *mockPodLister) addPod(pod *corev1.Pod) {
118117// TestGetSandboxPodIP_Success verifies GetSandboxPodIP returns IP when pod is present and valid
119118func TestGetSandboxPodIP_Success (t * testing.T ) {
120119 // Setup: Create a mock pod lister with a valid running pod
121- pod := createPodWithOwner ("test-pod" , "test-namespace" , "test-sandbox " , corev1 .PodRunning , "10.0.0.1" )
120+ pod := createPod ("test-pod" , "test-namespace" , corev1 .PodRunning , "10.0.0.1" )
122121 mockPodLister := newMockPodLister ()
123122 mockPodLister .addPod (pod )
124123
@@ -127,43 +126,55 @@ func TestGetSandboxPodIP_Success(t *testing.T) {
127126 }
128127
129128 // Execute
130- ip , err := client .GetSandboxPodIP (context .Background (), "test-namespace" , "test-sandbox" , "" )
129+ ip , err := client .GetSandboxPodIP (context .Background (), "test-namespace" , "test-sandbox" , "test-pod " )
131130
132131 // Verify
133132 assert .NoError (t , err , "Expected no error for valid pod" )
134133 assert .Equal (t , "10.0.0.1" , ip , "Expected IP to match pod IP" )
135134}
136135
137- // TestGetSandboxPodIP_PodNotFound verifies GetSandboxPodIP returns error when pod is not found
136+ func TestGetSandboxPodIP_MissingPodName (t * testing.T ) {
137+ client := & K8sClient {
138+ podLister : newMockPodLister (),
139+ }
140+
141+ ip , err := client .GetSandboxPodIP (context .Background (), "test-namespace" , "test-sandbox" , "" )
142+
143+ assert .Error (t , err , "Expected error when pod name is empty" )
144+ assert .Empty (t , ip , "Expected empty IP when error occurs" )
145+ assert .Contains (t , err .Error (), "sandbox test-sandbox has no pod name annotation" )
146+ }
147+
148+ // TestGetSandboxPodIP_PodNotFound verifies GetSandboxPodIP returns error when annotated pod is not found.
138149func TestGetSandboxPodIP_PodNotFound (t * testing.T ) {
139- // Setup: Create a mock pod lister with pod that has wrong label
140150 mockPodLister := newMockPodLister ()
141- pod := & corev1.Pod {
142- ObjectMeta : metav1.ObjectMeta {
143- Name : "test-pod" ,
144- Namespace : "test-namespace" ,
145- Labels : map [string ]string {
146- SandboxNameLabelKey : "other-sandbox" ,
147- },
148- },
149- Status : corev1.PodStatus {
150- Phase : corev1 .PodRunning ,
151- PodIP : "10.0.0.1" ,
152- },
153- }
154- mockPodLister .addPod (pod )
155151
156152 client := & K8sClient {
157153 podLister : mockPodLister ,
158154 }
159155
160156 // Execute
161- ip , err := client .GetSandboxPodIP (context .Background (), "test-namespace" , "test-sandbox" , "" )
157+ ip , err := client .GetSandboxPodIP (context .Background (), "test-namespace" , "test-sandbox" , "missing-pod " )
162158
163159 // Verify
164160 assert .Error (t , err , "Expected error when pod not found" )
165161 assert .Empty (t , ip , "Expected empty IP when error occurs" )
166- assert .Contains (t , err .Error (), "no pod found for sandbox test-sandbox" , "Error message should indicate pod not found" )
162+ assert .Contains (t , err .Error (), "failed to get sandbox pod test-namespace/missing-pod" , "Error message should indicate pod not found" )
163+ }
164+
165+ func TestGetSandboxPodIP_DoesNotFallbackToLabelSelector (t * testing.T ) {
166+ mockPodLister := newMockPodLister ()
167+ mockPodLister .addPod (createPodWithLabel ("label-matched-pod" , "test-namespace" , "test-sandbox" , corev1 .PodRunning , "10.0.0.1" ))
168+
169+ client := & K8sClient {
170+ podLister : mockPodLister ,
171+ }
172+
173+ ip , err := client .GetSandboxPodIP (context .Background (), "test-namespace" , "test-sandbox" , "missing-pod" )
174+
175+ assert .Error (t , err , "Expected error when annotated pod is missing" )
176+ assert .Empty (t , ip , "Expected empty IP when error occurs" )
177+ assert .Contains (t , err .Error (), "missing-pod" )
167178}
168179
169180// TestGetSandboxPodIP_InvalidPodStatus verifies GetSandboxPodIP returns error when pod status is invalid
@@ -192,7 +203,7 @@ func TestGetSandboxPodIP_InvalidPodStatus(t *testing.T) {
192203 for _ , tc := range testCases {
193204 t .Run (tc .name , func (t * testing.T ) {
194205 // Setup: Create a mock pod lister with invalid pod status
195- pod := createPodWithOwner ("test-pod" , "test-namespace" , "test-sandbox " , tc .phase , tc .podIP )
206+ pod := createPod ("test-pod" , "test-namespace" , tc .phase , tc .podIP )
196207 mockPodLister := newMockPodLister ()
197208 mockPodLister .addPod (pod )
198209
@@ -201,7 +212,7 @@ func TestGetSandboxPodIP_InvalidPodStatus(t *testing.T) {
201212 }
202213
203214 // Execute
204- ip , err := client .GetSandboxPodIP (context .Background (), "test-namespace" , "test-sandbox" , "" )
215+ ip , err := client .GetSandboxPodIP (context .Background (), "test-namespace" , "test-sandbox" , "test-pod " )
205216
206217 // Verify
207218 assert .Error (t , err , "Expected error for invalid pod status" )
0 commit comments