k8s.io/kubernetes@v1.29.3/pkg/kubelet/pod/pod_manager_test.go (about) 1 /* 2 Copyright 2015 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package pod 18 19 import ( 20 "reflect" 21 "testing" 22 23 v1 "k8s.io/api/core/v1" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/types" 26 podtest "k8s.io/kubernetes/pkg/kubelet/pod/testing" 27 kubetypes "k8s.io/kubernetes/pkg/kubelet/types" 28 ) 29 30 // Stub out mirror client for testing purpose. 31 func newTestManager() (*basicManager, *podtest.FakeMirrorClient) { 32 fakeMirrorClient := podtest.NewFakeMirrorClient() 33 manager := NewBasicPodManager().(*basicManager) 34 return manager, fakeMirrorClient 35 } 36 37 // Tests that pods/maps are properly set after the pod update, and the basic 38 // methods work correctly. 39 func TestGetSetPods(t *testing.T) { 40 mirrorPod := &v1.Pod{ 41 ObjectMeta: metav1.ObjectMeta{ 42 UID: "987654321", 43 Name: "bar", 44 Namespace: "default", 45 Annotations: map[string]string{ 46 kubetypes.ConfigSourceAnnotationKey: "api", 47 kubetypes.ConfigMirrorAnnotationKey: "mirror", 48 }, 49 }, 50 } 51 staticPod := &v1.Pod{ 52 ObjectMeta: metav1.ObjectMeta{ 53 UID: "123456789", 54 Name: "bar", 55 Namespace: "default", 56 Annotations: map[string]string{kubetypes.ConfigSourceAnnotationKey: "file"}, 57 }, 58 } 59 60 expectedPods := []*v1.Pod{ 61 { 62 ObjectMeta: metav1.ObjectMeta{ 63 UID: "999999999", 64 Name: "taco", 65 Namespace: "default", 66 Annotations: map[string]string{kubetypes.ConfigSourceAnnotationKey: "api"}, 67 }, 68 }, 69 staticPod, 70 } 71 updates := append(expectedPods, mirrorPod) 72 podManager, _ := newTestManager() 73 podManager.SetPods(updates) 74 75 // Tests that all regular pods are recorded correctly. 76 actualPods := podManager.GetPods() 77 if len(actualPods) != len(expectedPods) { 78 t.Errorf("expected %d pods, got %d pods; expected pods %#v, got pods %#v", len(expectedPods), len(actualPods), 79 expectedPods, actualPods) 80 } 81 for _, expected := range expectedPods { 82 found := false 83 for _, actual := range actualPods { 84 if actual.UID == expected.UID { 85 if !reflect.DeepEqual(&expected, &actual) { 86 t.Errorf("pod was recorded incorrectly. expect: %#v, got: %#v", expected, actual) 87 } 88 found = true 89 break 90 } 91 } 92 if !found { 93 t.Errorf("pod %q was not found in %#v", expected.UID, actualPods) 94 } 95 } 96 // Tests UID translation works as expected. Convert static pod UID for comparison only. 97 if uid := podManager.TranslatePodUID(mirrorPod.UID); uid != kubetypes.ResolvedPodUID(staticPod.UID) { 98 t.Errorf("unable to translate UID %q to the static POD's UID %q; %#v", 99 mirrorPod.UID, staticPod.UID, podManager.mirrorPodByUID) 100 } 101 102 // Test the basic Get methods. 103 actualPod, ok := podManager.GetPodByFullName("bar_default") 104 if !ok || !reflect.DeepEqual(actualPod, staticPod) { 105 t.Errorf("unable to get pod by full name; expected: %#v, got: %#v", staticPod, actualPod) 106 } 107 actualPod, ok = podManager.GetPodByName("default", "bar") 108 if !ok || !reflect.DeepEqual(actualPod, staticPod) { 109 t.Errorf("unable to get pod by name; expected: %#v, got: %#v", staticPod, actualPod) 110 } 111 112 } 113 114 func TestRemovePods(t *testing.T) { 115 mirrorPod := &v1.Pod{ 116 ObjectMeta: metav1.ObjectMeta{ 117 UID: types.UID("mirror-pod-uid"), 118 Name: "mirror-static-pod-name", 119 Namespace: metav1.NamespaceDefault, 120 Annotations: map[string]string{ 121 kubetypes.ConfigSourceAnnotationKey: "api", 122 kubetypes.ConfigMirrorAnnotationKey: "mirror", 123 }, 124 }, 125 } 126 staticPod := &v1.Pod{ 127 ObjectMeta: metav1.ObjectMeta{ 128 UID: types.UID("static-pod-uid"), 129 Name: "mirror-static-pod-name", 130 Namespace: metav1.NamespaceDefault, 131 Annotations: map[string]string{kubetypes.ConfigSourceAnnotationKey: "file"}, 132 }, 133 } 134 135 expectedPods := []*v1.Pod{ 136 { 137 ObjectMeta: metav1.ObjectMeta{ 138 UID: types.UID("extra-pod-uid"), 139 Name: "extra-pod-name", 140 Namespace: metav1.NamespaceDefault, 141 Annotations: map[string]string{kubetypes.ConfigSourceAnnotationKey: "api"}, 142 }, 143 }, 144 staticPod, 145 } 146 updates := append(expectedPods, mirrorPod) 147 podManager, _ := newTestManager() 148 podManager.SetPods(updates) 149 150 podManager.RemovePod(staticPod) 151 152 actualPods := podManager.GetPods() 153 if len(actualPods) == len(expectedPods) { 154 t.Fatalf("Run RemovePod() error, expected %d pods, got %d pods; ", len(expectedPods)-1, len(actualPods)) 155 } 156 157 _, _, orphanedMirrorPodNames := podManager.GetPodsAndMirrorPods() 158 expectedOrphanedMirrorPodNameNum := 1 159 if len(orphanedMirrorPodNames) != expectedOrphanedMirrorPodNameNum { 160 t.Fatalf("Run getOrphanedMirrorPodNames() error, expected %d orphaned mirror pods, got %d orphaned mirror pods; ", expectedOrphanedMirrorPodNameNum, len(orphanedMirrorPodNames)) 161 } 162 163 expectedOrphanedMirrorPodName := mirrorPod.Name + "_" + mirrorPod.Namespace 164 if orphanedMirrorPodNames[0] != expectedOrphanedMirrorPodName { 165 t.Fatalf("Run getOrphanedMirrorPodNames() error, expected orphaned mirror pod name : %s, got orphaned mirror pod name %s; ", expectedOrphanedMirrorPodName, orphanedMirrorPodNames[0]) 166 } 167 }