pod_manager_test.go 5.6 KB

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