pod_manager_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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/kubernetes/pkg/api"
  18. podtest "k8s.io/kubernetes/pkg/kubelet/pod/testing"
  19. kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
  20. )
  21. // Stub out mirror client for testing purpose.
  22. func newTestManager() (*basicManager, *podtest.FakeMirrorClient) {
  23. fakeMirrorClient := podtest.NewFakeMirrorClient()
  24. manager := NewBasicPodManager(fakeMirrorClient).(*basicManager)
  25. return manager, fakeMirrorClient
  26. }
  27. // Tests that pods/maps are properly set after the pod update, and the basic
  28. // methods work correctly.
  29. func TestGetSetPods(t *testing.T) {
  30. mirrorPod := &api.Pod{
  31. ObjectMeta: api.ObjectMeta{
  32. UID: "987654321",
  33. Name: "bar",
  34. Namespace: "default",
  35. Annotations: map[string]string{
  36. kubetypes.ConfigSourceAnnotationKey: "api",
  37. kubetypes.ConfigMirrorAnnotationKey: "mirror",
  38. },
  39. },
  40. }
  41. staticPod := &api.Pod{
  42. ObjectMeta: api.ObjectMeta{
  43. UID: "123456789",
  44. Name: "bar",
  45. Namespace: "default",
  46. Annotations: map[string]string{kubetypes.ConfigSourceAnnotationKey: "file"},
  47. },
  48. }
  49. expectedPods := []*api.Pod{
  50. {
  51. ObjectMeta: api.ObjectMeta{
  52. UID: "999999999",
  53. Name: "taco",
  54. Namespace: "default",
  55. Annotations: map[string]string{kubetypes.ConfigSourceAnnotationKey: "api"},
  56. },
  57. },
  58. staticPod,
  59. }
  60. updates := append(expectedPods, mirrorPod)
  61. podManager, _ := newTestManager()
  62. podManager.SetPods(updates)
  63. // Tests that all regular pods are recorded correctly.
  64. actualPods := podManager.GetPods()
  65. if len(actualPods) != len(expectedPods) {
  66. t.Errorf("expected %d pods, got %d pods; expected pods %#v, got pods %#v", len(expectedPods), len(actualPods),
  67. expectedPods, actualPods)
  68. }
  69. for _, expected := range expectedPods {
  70. found := false
  71. for _, actual := range actualPods {
  72. if actual.UID == expected.UID {
  73. if !reflect.DeepEqual(&expected, &actual) {
  74. t.Errorf("pod was recorded incorrectly. expect: %#v, got: %#v", expected, actual)
  75. }
  76. found = true
  77. break
  78. }
  79. }
  80. if !found {
  81. t.Errorf("pod %q was not found in %#v", expected.UID, actualPods)
  82. }
  83. }
  84. // Tests UID translation works as expected.
  85. if uid := podManager.TranslatePodUID(mirrorPod.UID); uid != staticPod.UID {
  86. t.Errorf("unable to translate UID %q to the static POD's UID %q; %#v",
  87. mirrorPod.UID, staticPod.UID, podManager.mirrorPodByUID)
  88. }
  89. // Test the basic Get methods.
  90. actualPod, ok := podManager.GetPodByFullName("bar_default")
  91. if !ok || !reflect.DeepEqual(actualPod, staticPod) {
  92. t.Errorf("unable to get pod by full name; expected: %#v, got: %#v", staticPod, actualPod)
  93. }
  94. actualPod, ok = podManager.GetPodByName("default", "bar")
  95. if !ok || !reflect.DeepEqual(actualPod, staticPod) {
  96. t.Errorf("unable to get pod by name; expected: %#v, got: %#v", staticPod, actualPod)
  97. }
  98. }