pod_container_deletor_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. Copyright 2016 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 kubelet
  14. import (
  15. "reflect"
  16. "testing"
  17. "time"
  18. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  19. )
  20. func TestGetContainersToDeleteInPodWithFilter(t *testing.T) {
  21. pod := kubecontainer.PodStatus{
  22. ContainerStatuses: []*kubecontainer.ContainerStatus{
  23. {
  24. ID: kubecontainer.ContainerID{Type: "test", ID: "1"},
  25. Name: "foo",
  26. CreatedAt: time.Now(),
  27. State: kubecontainer.ContainerStateExited,
  28. },
  29. {
  30. ID: kubecontainer.ContainerID{Type: "test", ID: "2"},
  31. Name: "bar",
  32. CreatedAt: time.Now().Add(time.Second),
  33. State: kubecontainer.ContainerStateExited,
  34. },
  35. {
  36. ID: kubecontainer.ContainerID{Type: "test", ID: "3"},
  37. Name: "bar",
  38. CreatedAt: time.Now().Add(2 * time.Second),
  39. State: kubecontainer.ContainerStateExited,
  40. },
  41. {
  42. ID: kubecontainer.ContainerID{Type: "test", ID: "4"},
  43. Name: "bar",
  44. CreatedAt: time.Now().Add(3 * time.Second),
  45. State: kubecontainer.ContainerStateExited,
  46. },
  47. {
  48. ID: kubecontainer.ContainerID{Type: "test", ID: "5"},
  49. Name: "bar",
  50. CreatedAt: time.Now().Add(4 * time.Second),
  51. State: kubecontainer.ContainerStateRunning,
  52. },
  53. },
  54. }
  55. testCases := []struct {
  56. containersToKeep int
  57. expectedContainersToDelete containerStatusbyCreatedList
  58. }{
  59. {
  60. 0,
  61. []*kubecontainer.ContainerStatus{pod.ContainerStatuses[3], pod.ContainerStatuses[2], pod.ContainerStatuses[1]},
  62. },
  63. {
  64. 1,
  65. []*kubecontainer.ContainerStatus{pod.ContainerStatuses[2], pod.ContainerStatuses[1]},
  66. },
  67. {
  68. 2,
  69. []*kubecontainer.ContainerStatus{pod.ContainerStatuses[1]},
  70. },
  71. }
  72. for _, test := range testCases {
  73. candidates := getContainersToDeleteInPod("4", &pod, test.containersToKeep)
  74. if !reflect.DeepEqual(candidates, test.expectedContainersToDelete) {
  75. t.Errorf("expected %v got %v", test.expectedContainersToDelete, candidates)
  76. }
  77. }
  78. }
  79. func TestGetContainersToDeleteInPod(t *testing.T) {
  80. pod := kubecontainer.PodStatus{
  81. ContainerStatuses: []*kubecontainer.ContainerStatus{
  82. {
  83. ID: kubecontainer.ContainerID{Type: "test", ID: "1"},
  84. Name: "foo",
  85. CreatedAt: time.Now(),
  86. State: kubecontainer.ContainerStateExited,
  87. },
  88. {
  89. ID: kubecontainer.ContainerID{Type: "test", ID: "2"},
  90. Name: "bar",
  91. CreatedAt: time.Now().Add(time.Second),
  92. State: kubecontainer.ContainerStateExited,
  93. },
  94. {
  95. ID: kubecontainer.ContainerID{Type: "test", ID: "3"},
  96. Name: "bar",
  97. CreatedAt: time.Now().Add(2 * time.Second),
  98. State: kubecontainer.ContainerStateExited,
  99. },
  100. {
  101. ID: kubecontainer.ContainerID{Type: "test", ID: "4"},
  102. Name: "bar",
  103. CreatedAt: time.Now().Add(3 * time.Second),
  104. State: kubecontainer.ContainerStateExited,
  105. },
  106. {
  107. ID: kubecontainer.ContainerID{Type: "test", ID: "5"},
  108. Name: "bar",
  109. CreatedAt: time.Now().Add(4 * time.Second),
  110. State: kubecontainer.ContainerStateRunning,
  111. },
  112. },
  113. }
  114. testCases := []struct {
  115. containersToKeep int
  116. expectedContainersToDelete containerStatusbyCreatedList
  117. }{
  118. {
  119. 0,
  120. []*kubecontainer.ContainerStatus{pod.ContainerStatuses[3], pod.ContainerStatuses[2], pod.ContainerStatuses[1], pod.ContainerStatuses[0]},
  121. },
  122. {
  123. 1,
  124. []*kubecontainer.ContainerStatus{pod.ContainerStatuses[2], pod.ContainerStatuses[1], pod.ContainerStatuses[0]},
  125. },
  126. {
  127. 2,
  128. []*kubecontainer.ContainerStatus{pod.ContainerStatuses[1], pod.ContainerStatuses[0]},
  129. },
  130. }
  131. for _, test := range testCases {
  132. candidates := getContainersToDeleteInPod("", &pod, test.containersToKeep)
  133. if !reflect.DeepEqual(candidates, test.expectedContainersToDelete) {
  134. t.Errorf("expected %v got %v", test.expectedContainersToDelete, candidates)
  135. }
  136. }
  137. }
  138. func TestGetContainersToDeleteInPodWithNoMatch(t *testing.T) {
  139. pod := kubecontainer.PodStatus{
  140. ContainerStatuses: []*kubecontainer.ContainerStatus{
  141. {
  142. ID: kubecontainer.ContainerID{Type: "test", ID: "1"},
  143. Name: "foo",
  144. CreatedAt: time.Now(),
  145. State: kubecontainer.ContainerStateExited,
  146. },
  147. {
  148. ID: kubecontainer.ContainerID{Type: "test", ID: "2"},
  149. Name: "bar",
  150. CreatedAt: time.Now().Add(time.Second),
  151. State: kubecontainer.ContainerStateExited,
  152. },
  153. {
  154. ID: kubecontainer.ContainerID{Type: "test", ID: "3"},
  155. Name: "bar",
  156. CreatedAt: time.Now().Add(2 * time.Second),
  157. State: kubecontainer.ContainerStateExited,
  158. },
  159. {
  160. ID: kubecontainer.ContainerID{Type: "test", ID: "4"},
  161. Name: "bar",
  162. CreatedAt: time.Now().Add(3 * time.Second),
  163. State: kubecontainer.ContainerStateExited,
  164. },
  165. {
  166. ID: kubecontainer.ContainerID{Type: "test", ID: "5"},
  167. Name: "bar",
  168. CreatedAt: time.Now().Add(4 * time.Second),
  169. State: kubecontainer.ContainerStateRunning,
  170. },
  171. },
  172. }
  173. testCases := []struct {
  174. filterID string
  175. expectedContainersToDelete containerStatusbyCreatedList
  176. }{
  177. {
  178. "abc",
  179. []*kubecontainer.ContainerStatus{},
  180. },
  181. }
  182. for _, test := range testCases {
  183. candidates := getContainersToDeleteInPod(test.filterID, &pod, len(pod.ContainerStatuses))
  184. if !reflect.DeepEqual(candidates, test.expectedContainersToDelete) {
  185. t.Errorf("expected %v got %v", test.expectedContainersToDelete, candidates)
  186. }
  187. }
  188. }