apiserver_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. Copyright 2014 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 config
  14. import (
  15. "testing"
  16. "k8s.io/api/core/v1"
  17. apiequality "k8s.io/apimachinery/pkg/api/equality"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "k8s.io/apimachinery/pkg/watch"
  21. "k8s.io/client-go/tools/cache"
  22. kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
  23. )
  24. type fakePodLW struct {
  25. listResp runtime.Object
  26. watchResp watch.Interface
  27. }
  28. func (lw fakePodLW) List(options metav1.ListOptions) (runtime.Object, error) {
  29. return lw.listResp, nil
  30. }
  31. func (lw fakePodLW) Watch(options metav1.ListOptions) (watch.Interface, error) {
  32. return lw.watchResp, nil
  33. }
  34. var _ cache.ListerWatcher = fakePodLW{}
  35. func TestNewSourceApiserver_UpdatesAndMultiplePods(t *testing.T) {
  36. pod1v1 := &v1.Pod{
  37. ObjectMeta: metav1.ObjectMeta{Name: "p"},
  38. Spec: v1.PodSpec{Containers: []v1.Container{{Image: "image/one"}}}}
  39. pod1v2 := &v1.Pod{
  40. ObjectMeta: metav1.ObjectMeta{Name: "p"},
  41. Spec: v1.PodSpec{Containers: []v1.Container{{Image: "image/two"}}}}
  42. pod2 := &v1.Pod{
  43. ObjectMeta: metav1.ObjectMeta{Name: "q"},
  44. Spec: v1.PodSpec{Containers: []v1.Container{{Image: "image/blah"}}}}
  45. // Setup fake api client.
  46. fakeWatch := watch.NewFake()
  47. lw := fakePodLW{
  48. listResp: &v1.PodList{Items: []v1.Pod{*pod1v1}},
  49. watchResp: fakeWatch,
  50. }
  51. ch := make(chan interface{})
  52. newSourceApiserverFromLW(lw, ch)
  53. got, ok := <-ch
  54. if !ok {
  55. t.Errorf("Unable to read from channel when expected")
  56. }
  57. update := got.(kubetypes.PodUpdate)
  58. expected := CreatePodUpdate(kubetypes.SET, kubetypes.ApiserverSource, pod1v1)
  59. if !apiequality.Semantic.DeepEqual(expected, update) {
  60. t.Errorf("Expected %#v; Got %#v", expected, update)
  61. }
  62. // Add another pod
  63. fakeWatch.Add(pod2)
  64. got, ok = <-ch
  65. if !ok {
  66. t.Errorf("Unable to read from channel when expected")
  67. }
  68. update = got.(kubetypes.PodUpdate)
  69. // Could be sorted either of these two ways:
  70. expectedA := CreatePodUpdate(kubetypes.SET, kubetypes.ApiserverSource, pod1v1, pod2)
  71. expectedB := CreatePodUpdate(kubetypes.SET, kubetypes.ApiserverSource, pod2, pod1v1)
  72. if !apiequality.Semantic.DeepEqual(expectedA, update) && !apiequality.Semantic.DeepEqual(expectedB, update) {
  73. t.Errorf("Expected %#v or %#v, Got %#v", expectedA, expectedB, update)
  74. }
  75. // Modify pod1
  76. fakeWatch.Modify(pod1v2)
  77. got, ok = <-ch
  78. if !ok {
  79. t.Errorf("Unable to read from channel when expected")
  80. }
  81. update = got.(kubetypes.PodUpdate)
  82. expectedA = CreatePodUpdate(kubetypes.SET, kubetypes.ApiserverSource, pod1v2, pod2)
  83. expectedB = CreatePodUpdate(kubetypes.SET, kubetypes.ApiserverSource, pod2, pod1v2)
  84. if !apiequality.Semantic.DeepEqual(expectedA, update) && !apiequality.Semantic.DeepEqual(expectedB, update) {
  85. t.Errorf("Expected %#v or %#v, Got %#v", expectedA, expectedB, update)
  86. }
  87. // Delete pod1
  88. fakeWatch.Delete(pod1v2)
  89. got, ok = <-ch
  90. if !ok {
  91. t.Errorf("Unable to read from channel when expected")
  92. }
  93. update = got.(kubetypes.PodUpdate)
  94. expected = CreatePodUpdate(kubetypes.SET, kubetypes.ApiserverSource, pod2)
  95. if !apiequality.Semantic.DeepEqual(expected, update) {
  96. t.Errorf("Expected %#v, Got %#v", expected, update)
  97. }
  98. // Delete pod2
  99. fakeWatch.Delete(pod2)
  100. got, ok = <-ch
  101. if !ok {
  102. t.Errorf("Unable to read from channel when expected")
  103. }
  104. update = got.(kubetypes.PodUpdate)
  105. expected = CreatePodUpdate(kubetypes.SET, kubetypes.ApiserverSource)
  106. if !apiequality.Semantic.DeepEqual(expected, update) {
  107. t.Errorf("Expected %#v, Got %#v", expected, update)
  108. }
  109. }
  110. func TestNewSourceApiserver_TwoNamespacesSameName(t *testing.T) {
  111. pod1 := v1.Pod{
  112. ObjectMeta: metav1.ObjectMeta{Name: "p", Namespace: "one"},
  113. Spec: v1.PodSpec{Containers: []v1.Container{{Image: "image/one"}}}}
  114. pod2 := v1.Pod{
  115. ObjectMeta: metav1.ObjectMeta{Name: "p", Namespace: "two"},
  116. Spec: v1.PodSpec{Containers: []v1.Container{{Image: "image/blah"}}}}
  117. // Setup fake api client.
  118. fakeWatch := watch.NewFake()
  119. lw := fakePodLW{
  120. listResp: &v1.PodList{Items: []v1.Pod{pod1, pod2}},
  121. watchResp: fakeWatch,
  122. }
  123. ch := make(chan interface{})
  124. newSourceApiserverFromLW(lw, ch)
  125. got, ok := <-ch
  126. if !ok {
  127. t.Errorf("Unable to read from channel when expected")
  128. }
  129. update := got.(kubetypes.PodUpdate)
  130. // Make sure that we get both pods. Catches bug #2294.
  131. if !(len(update.Pods) == 2) {
  132. t.Errorf("Expected %d, Got %d", 2, len(update.Pods))
  133. }
  134. // Delete pod1
  135. fakeWatch.Delete(&pod1)
  136. got, ok = <-ch
  137. if !ok {
  138. t.Errorf("Unable to read from channel when expected")
  139. }
  140. update = got.(kubetypes.PodUpdate)
  141. if !(len(update.Pods) == 1) {
  142. t.Errorf("Expected %d, Got %d", 1, len(update.Pods))
  143. }
  144. }
  145. func TestNewSourceApiserverInitialEmptySendsEmptyPodUpdate(t *testing.T) {
  146. // Setup fake api client.
  147. fakeWatch := watch.NewFake()
  148. lw := fakePodLW{
  149. listResp: &v1.PodList{Items: []v1.Pod{}},
  150. watchResp: fakeWatch,
  151. }
  152. ch := make(chan interface{})
  153. newSourceApiserverFromLW(lw, ch)
  154. got, ok := <-ch
  155. if !ok {
  156. t.Errorf("Unable to read from channel when expected")
  157. }
  158. update := got.(kubetypes.PodUpdate)
  159. expected := CreatePodUpdate(kubetypes.SET, kubetypes.ApiserverSource)
  160. if !apiequality.Semantic.DeepEqual(expected, update) {
  161. t.Errorf("Expected %#v; Got %#v", expected, update)
  162. }
  163. }