sliceutils_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. Copyright 2017 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 sliceutils
  14. import (
  15. "testing"
  16. "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  19. "time"
  20. )
  21. func TestStringInSlice(t *testing.T) {
  22. fooTests := []struct {
  23. s string
  24. list []string
  25. er bool
  26. }{
  27. {"first", []string{"first", "second"}, true},
  28. {"FIRST", []string{"first", "second"}, false},
  29. {"third", []string{"first", "second"}, false},
  30. {"first", nil, false},
  31. {"", []string{"first", "second"}, false},
  32. {"", []string{"first", "second", ""}, true},
  33. {"", nil, false},
  34. }
  35. for _, fooTest := range fooTests {
  36. r := StringInSlice(fooTest.s, fooTest.list)
  37. if r != fooTest.er {
  38. t.Errorf("returned %t but expected %t for s=%s & list=%s", r, fooTest.er, fooTest.s, fooTest.list)
  39. }
  40. }
  41. }
  42. func buildPodsByCreationTime() PodsByCreationTime {
  43. return []*v1.Pod{
  44. {
  45. ObjectMeta: metav1.ObjectMeta{
  46. Name: "foo1",
  47. Namespace: v1.NamespaceDefault,
  48. CreationTimestamp: metav1.Time{
  49. Time: time.Now(),
  50. },
  51. },
  52. },
  53. {
  54. ObjectMeta: metav1.ObjectMeta{
  55. Name: "foo2",
  56. Namespace: v1.NamespaceDefault,
  57. CreationTimestamp: metav1.Time{
  58. Time: time.Now().Add(time.Hour * 1),
  59. },
  60. },
  61. },
  62. {
  63. ObjectMeta: metav1.ObjectMeta{
  64. Name: "foo3",
  65. Namespace: v1.NamespaceDefault,
  66. CreationTimestamp: metav1.Time{
  67. Time: time.Now().Add(time.Hour * 2),
  68. },
  69. },
  70. },
  71. }
  72. }
  73. func TestPodsByCreationTimeLen(t *testing.T) {
  74. fooTests := []struct {
  75. pods PodsByCreationTime
  76. el int
  77. }{
  78. {[]*v1.Pod{}, 0},
  79. {buildPodsByCreationTime(), 3},
  80. {[]*v1.Pod{nil, {}}, 2},
  81. {nil, 0},
  82. }
  83. for _, fooTest := range fooTests {
  84. r := fooTest.pods.Len()
  85. if r != fooTest.el {
  86. t.Errorf("returned %d but expected %d for the len of PodsByCreationTime=%s", r, fooTest.el, fooTest.pods)
  87. }
  88. }
  89. }
  90. func TestPodsByCreationTimeSwap(t *testing.T) {
  91. fooTests := []struct {
  92. pods PodsByCreationTime
  93. i int
  94. j int
  95. }{
  96. {buildPodsByCreationTime(), 0, 1},
  97. {buildPodsByCreationTime(), 2, 1},
  98. }
  99. for _, fooTest := range fooTests {
  100. fooi := fooTest.pods[fooTest.i]
  101. fooj := fooTest.pods[fooTest.j]
  102. fooTest.pods.Swap(fooTest.i, fooTest.j)
  103. if fooi.GetName() != fooTest.pods[fooTest.j].GetName() || fooj.GetName() != fooTest.pods[fooTest.i].GetName() {
  104. t.Errorf("failed to swap for %v", fooTest)
  105. }
  106. }
  107. }
  108. func TestPodsByCreationTimeLess(t *testing.T) {
  109. fooTests := []struct {
  110. pods PodsByCreationTime
  111. i int
  112. j int
  113. er bool
  114. }{
  115. // ascending order
  116. {buildPodsByCreationTime(), 0, 2, true},
  117. {buildPodsByCreationTime(), 1, 0, false},
  118. }
  119. for _, fooTest := range fooTests {
  120. r := fooTest.pods.Less(fooTest.i, fooTest.j)
  121. if r != fooTest.er {
  122. t.Errorf("returned %t but expected %t for the foo=%s", r, fooTest.er, fooTest.pods)
  123. }
  124. }
  125. }
  126. func buildByImageSize() ByImageSize {
  127. return []kubecontainer.Image{
  128. {
  129. ID: "1",
  130. RepoTags: []string{"foo-tag11", "foo-tag12"},
  131. RepoDigests: []string{"foo-rd11", "foo-rd12"},
  132. Size: 1,
  133. },
  134. {
  135. ID: "2",
  136. RepoTags: []string{"foo-tag21", "foo-tag22"},
  137. RepoDigests: []string{"foo-rd21", "foo-rd22"},
  138. Size: 2,
  139. },
  140. {
  141. ID: "3",
  142. RepoTags: []string{"foo-tag31", "foo-tag32"},
  143. RepoDigests: []string{"foo-rd31", "foo-rd32"},
  144. Size: 3,
  145. },
  146. }
  147. }
  148. func TestByImageSizeLen(t *testing.T) {
  149. fooTests := []struct {
  150. images ByImageSize
  151. el int
  152. }{
  153. {[]kubecontainer.Image{}, 0},
  154. {buildByImageSize(), 3},
  155. {nil, 0},
  156. }
  157. for _, fooTest := range fooTests {
  158. r := fooTest.images.Len()
  159. if r != fooTest.el {
  160. t.Errorf("returned %d but expected %d for the len of ByImageSize=%v", r, fooTest.el, fooTest.images)
  161. }
  162. }
  163. }
  164. func TestByImageSizeSwap(t *testing.T) {
  165. fooTests := []struct {
  166. images ByImageSize
  167. i int
  168. j int
  169. }{
  170. {buildByImageSize(), 0, 1},
  171. {buildByImageSize(), 2, 1},
  172. }
  173. for _, fooTest := range fooTests {
  174. fooi := fooTest.images[fooTest.i]
  175. fooj := fooTest.images[fooTest.j]
  176. fooTest.images.Swap(fooTest.i, fooTest.j)
  177. if fooi.ID != fooTest.images[fooTest.j].ID || fooj.ID != fooTest.images[fooTest.i].ID {
  178. t.Errorf("failed to swap for %v", fooTest)
  179. }
  180. }
  181. }
  182. func TestByImageSizeLess(t *testing.T) {
  183. fooTests := []struct {
  184. images ByImageSize
  185. i int
  186. j int
  187. er bool
  188. }{
  189. // descending order
  190. {buildByImageSize(), 0, 2, false},
  191. {buildByImageSize(), 1, 0, true},
  192. }
  193. for _, fooTest := range fooTests {
  194. r := fooTest.images.Less(fooTest.i, fooTest.j)
  195. if r != fooTest.er {
  196. t.Errorf("returned %t but expected %t for the foo=%v", r, fooTest.er, fooTest.images)
  197. }
  198. }
  199. }