helpers_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. Copyright 2018 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 polymorphichelpers
  14. import (
  15. "fmt"
  16. "sort"
  17. "testing"
  18. "time"
  19. corev1 "k8s.io/api/core/v1"
  20. apiequality "k8s.io/apimachinery/pkg/api/equality"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/labels"
  23. "k8s.io/apimachinery/pkg/watch"
  24. fakeexternal "k8s.io/client-go/kubernetes/fake"
  25. testcore "k8s.io/client-go/testing"
  26. "k8s.io/kubernetes/pkg/kubectl/util/podutils"
  27. )
  28. func TestGetFirstPod(t *testing.T) {
  29. labelSet := map[string]string{"test": "selector"}
  30. tests := []struct {
  31. name string
  32. podList *corev1.PodList
  33. watching []watch.Event
  34. sortBy func([]*corev1.Pod) sort.Interface
  35. expected *corev1.Pod
  36. expectedNum int
  37. expectedErr bool
  38. }{
  39. {
  40. name: "kubectl logs - two ready pods",
  41. podList: newPodList(2, -1, -1, labelSet),
  42. sortBy: func(pods []*corev1.Pod) sort.Interface { return podutils.ByLogging(pods) },
  43. expected: &corev1.Pod{
  44. ObjectMeta: metav1.ObjectMeta{
  45. Name: "pod-1",
  46. Namespace: metav1.NamespaceDefault,
  47. CreationTimestamp: metav1.Date(2016, time.April, 1, 1, 0, 0, 0, time.UTC),
  48. Labels: map[string]string{"test": "selector"},
  49. },
  50. Status: corev1.PodStatus{
  51. Conditions: []corev1.PodCondition{
  52. {
  53. Status: corev1.ConditionTrue,
  54. Type: corev1.PodReady,
  55. },
  56. },
  57. },
  58. },
  59. expectedNum: 2,
  60. },
  61. {
  62. name: "kubectl logs - one unhealthy, one healthy",
  63. podList: newPodList(2, -1, 1, labelSet),
  64. sortBy: func(pods []*corev1.Pod) sort.Interface { return podutils.ByLogging(pods) },
  65. expected: &corev1.Pod{
  66. ObjectMeta: metav1.ObjectMeta{
  67. Name: "pod-2",
  68. Namespace: metav1.NamespaceDefault,
  69. CreationTimestamp: metav1.Date(2016, time.April, 1, 1, 0, 1, 0, time.UTC),
  70. Labels: map[string]string{"test": "selector"},
  71. },
  72. Status: corev1.PodStatus{
  73. Conditions: []corev1.PodCondition{
  74. {
  75. Status: corev1.ConditionTrue,
  76. Type: corev1.PodReady,
  77. },
  78. },
  79. ContainerStatuses: []corev1.ContainerStatus{{RestartCount: 5}},
  80. },
  81. },
  82. expectedNum: 2,
  83. },
  84. {
  85. name: "kubectl attach - two ready pods",
  86. podList: newPodList(2, -1, -1, labelSet),
  87. sortBy: func(pods []*corev1.Pod) sort.Interface { return sort.Reverse(podutils.ActivePods(pods)) },
  88. expected: &corev1.Pod{
  89. ObjectMeta: metav1.ObjectMeta{
  90. Name: "pod-1",
  91. Namespace: metav1.NamespaceDefault,
  92. CreationTimestamp: metav1.Date(2016, time.April, 1, 1, 0, 0, 0, time.UTC),
  93. Labels: map[string]string{"test": "selector"},
  94. },
  95. Status: corev1.PodStatus{
  96. Conditions: []corev1.PodCondition{
  97. {
  98. Status: corev1.ConditionTrue,
  99. Type: corev1.PodReady,
  100. },
  101. },
  102. },
  103. },
  104. expectedNum: 2,
  105. },
  106. {
  107. name: "kubectl attach - wait for ready pod",
  108. podList: newPodList(1, 1, -1, labelSet),
  109. watching: []watch.Event{
  110. {
  111. Type: watch.Modified,
  112. Object: &corev1.Pod{
  113. ObjectMeta: metav1.ObjectMeta{
  114. Name: "pod-1",
  115. Namespace: metav1.NamespaceDefault,
  116. CreationTimestamp: metav1.Date(2016, time.April, 1, 1, 0, 0, 0, time.UTC),
  117. Labels: map[string]string{"test": "selector"},
  118. },
  119. Status: corev1.PodStatus{
  120. Conditions: []corev1.PodCondition{
  121. {
  122. Status: corev1.ConditionTrue,
  123. Type: corev1.PodReady,
  124. },
  125. },
  126. },
  127. },
  128. },
  129. },
  130. sortBy: func(pods []*corev1.Pod) sort.Interface { return sort.Reverse(podutils.ActivePods(pods)) },
  131. expected: &corev1.Pod{
  132. ObjectMeta: metav1.ObjectMeta{
  133. Name: "pod-1",
  134. Namespace: metav1.NamespaceDefault,
  135. CreationTimestamp: metav1.Date(2016, time.April, 1, 1, 0, 0, 0, time.UTC),
  136. Labels: map[string]string{"test": "selector"},
  137. },
  138. Status: corev1.PodStatus{
  139. Conditions: []corev1.PodCondition{
  140. {
  141. Status: corev1.ConditionTrue,
  142. Type: corev1.PodReady,
  143. },
  144. },
  145. },
  146. },
  147. expectedNum: 1,
  148. },
  149. }
  150. for i := range tests {
  151. test := tests[i]
  152. fake := fakeexternal.NewSimpleClientset(test.podList)
  153. if len(test.watching) > 0 {
  154. watcher := watch.NewFake()
  155. for _, event := range test.watching {
  156. switch event.Type {
  157. case watch.Added:
  158. go watcher.Add(event.Object)
  159. case watch.Modified:
  160. go watcher.Modify(event.Object)
  161. }
  162. }
  163. fake.PrependWatchReactor("pods", testcore.DefaultWatchReactor(watcher, nil))
  164. }
  165. selector := labels.Set(labelSet).AsSelector()
  166. pod, numPods, err := GetFirstPod(fake.CoreV1(), metav1.NamespaceDefault, selector.String(), 1*time.Minute, test.sortBy)
  167. pod.Spec.SecurityContext = nil
  168. if !test.expectedErr && err != nil {
  169. t.Errorf("%s: unexpected error: %v", test.name, err)
  170. continue
  171. }
  172. if test.expectedErr && err == nil {
  173. t.Errorf("%s: expected an error", test.name)
  174. continue
  175. }
  176. if test.expectedNum != numPods {
  177. t.Errorf("%s: expected %d pods, got %d", test.name, test.expectedNum, numPods)
  178. continue
  179. }
  180. if !apiequality.Semantic.DeepEqual(test.expected, pod) {
  181. t.Errorf("%s:\nexpected pod:\n%#v\ngot:\n%#v\n\n", test.name, test.expected, pod)
  182. }
  183. }
  184. }
  185. func newPodList(count, isUnready, isUnhealthy int, labels map[string]string) *corev1.PodList {
  186. pods := []corev1.Pod{}
  187. for i := 0; i < count; i++ {
  188. newPod := corev1.Pod{
  189. ObjectMeta: metav1.ObjectMeta{
  190. Name: fmt.Sprintf("pod-%d", i+1),
  191. Namespace: metav1.NamespaceDefault,
  192. CreationTimestamp: metav1.Date(2016, time.April, 1, 1, 0, i, 0, time.UTC),
  193. Labels: labels,
  194. },
  195. Status: corev1.PodStatus{
  196. Conditions: []corev1.PodCondition{
  197. {
  198. Status: corev1.ConditionTrue,
  199. Type: corev1.PodReady,
  200. },
  201. },
  202. },
  203. }
  204. pods = append(pods, newPod)
  205. }
  206. if isUnready > -1 && isUnready < count {
  207. pods[isUnready].Status.Conditions[0].Status = corev1.ConditionFalse
  208. }
  209. if isUnhealthy > -1 && isUnhealthy < count {
  210. pods[isUnhealthy].Status.ContainerStatuses = []corev1.ContainerStatus{{RestartCount: 5}}
  211. }
  212. return &corev1.PodList{
  213. Items: pods,
  214. }
  215. }