k8s.io/kubernetes@v1.29.3/pkg/kubelet/util/sliceutils/sliceutils_test.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package sliceutils 18 19 import ( 20 "testing" 21 "time" 22 23 v1 "k8s.io/api/core/v1" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" 26 ) 27 28 func buildPodsByCreationTime() PodsByCreationTime { 29 return []*v1.Pod{ 30 { 31 ObjectMeta: metav1.ObjectMeta{ 32 Name: "foo1", 33 Namespace: v1.NamespaceDefault, 34 CreationTimestamp: metav1.Time{ 35 Time: time.Now(), 36 }, 37 }, 38 }, 39 { 40 ObjectMeta: metav1.ObjectMeta{ 41 Name: "foo2", 42 Namespace: v1.NamespaceDefault, 43 CreationTimestamp: metav1.Time{ 44 Time: time.Now().Add(time.Hour * 1), 45 }, 46 }, 47 }, 48 { 49 ObjectMeta: metav1.ObjectMeta{ 50 Name: "foo3", 51 Namespace: v1.NamespaceDefault, 52 CreationTimestamp: metav1.Time{ 53 Time: time.Now().Add(time.Hour * 2), 54 }, 55 }, 56 }, 57 } 58 } 59 60 func TestPodsByCreationTimeLen(t *testing.T) { 61 fooTests := []struct { 62 pods PodsByCreationTime 63 el int 64 }{ 65 {[]*v1.Pod{}, 0}, 66 {buildPodsByCreationTime(), 3}, 67 {[]*v1.Pod{nil, {}}, 2}, 68 {nil, 0}, 69 } 70 71 for _, fooTest := range fooTests { 72 r := fooTest.pods.Len() 73 if r != fooTest.el { 74 t.Errorf("returned %d but expected %d for the len of PodsByCreationTime=%s", r, fooTest.el, fooTest.pods) 75 } 76 } 77 } 78 79 func TestPodsByCreationTimeSwap(t *testing.T) { 80 fooTests := []struct { 81 pods PodsByCreationTime 82 i int 83 j int 84 }{ 85 {buildPodsByCreationTime(), 0, 1}, 86 {buildPodsByCreationTime(), 2, 1}, 87 } 88 89 for _, fooTest := range fooTests { 90 fooi := fooTest.pods[fooTest.i] 91 fooj := fooTest.pods[fooTest.j] 92 fooTest.pods.Swap(fooTest.i, fooTest.j) 93 if fooi.GetName() != fooTest.pods[fooTest.j].GetName() || fooj.GetName() != fooTest.pods[fooTest.i].GetName() { 94 t.Errorf("failed to swap for %v", fooTest) 95 } 96 } 97 } 98 99 func TestPodsByCreationTimeLess(t *testing.T) { 100 fooTests := []struct { 101 pods PodsByCreationTime 102 i int 103 j int 104 er bool 105 }{ 106 // ascending order 107 {buildPodsByCreationTime(), 0, 2, true}, 108 {buildPodsByCreationTime(), 1, 0, false}, 109 } 110 111 for _, fooTest := range fooTests { 112 r := fooTest.pods.Less(fooTest.i, fooTest.j) 113 if r != fooTest.er { 114 t.Errorf("returned %t but expected %t for the foo=%s", r, fooTest.er, fooTest.pods) 115 } 116 } 117 } 118 119 func buildByImageSize() ByImageSize { 120 return []kubecontainer.Image{ 121 { 122 ID: "1", 123 RepoTags: []string{"foo-tag11", "foo-tag12"}, 124 RepoDigests: []string{"foo-rd11", "foo-rd12"}, 125 Size: 1, 126 }, 127 { 128 ID: "2", 129 RepoTags: []string{"foo-tag21", "foo-tag22"}, 130 RepoDigests: []string{"foo-rd21", "foo-rd22"}, 131 Size: 2, 132 }, 133 { 134 ID: "3", 135 RepoTags: []string{"foo-tag31", "foo-tag32"}, 136 RepoDigests: []string{"foo-rd31", "foo-rd32"}, 137 Size: 3, 138 }, 139 { 140 ID: "4", 141 RepoTags: []string{"foo-tag41", "foo-tag42"}, 142 RepoDigests: []string{"foo-rd41", "foo-rd42"}, 143 Size: 3, 144 }, 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(), 4}, 155 {nil, 0}, 156 } 157 158 for _, fooTest := range fooTests { 159 r := fooTest.images.Len() 160 if r != fooTest.el { 161 t.Errorf("returned %d but expected %d for the len of ByImageSize=%v", r, fooTest.el, fooTest.images) 162 } 163 } 164 } 165 166 func TestByImageSizeSwap(t *testing.T) { 167 fooTests := []struct { 168 images ByImageSize 169 i int 170 j int 171 }{ 172 {buildByImageSize(), 0, 1}, 173 {buildByImageSize(), 2, 1}, 174 } 175 176 for _, fooTest := range fooTests { 177 fooi := fooTest.images[fooTest.i] 178 fooj := fooTest.images[fooTest.j] 179 fooTest.images.Swap(fooTest.i, fooTest.j) 180 if fooi.ID != fooTest.images[fooTest.j].ID || fooj.ID != fooTest.images[fooTest.i].ID { 181 t.Errorf("failed to swap for %v", fooTest) 182 } 183 } 184 } 185 186 func TestByImageSizeLess(t *testing.T) { 187 fooTests := []struct { 188 images ByImageSize 189 i int 190 j int 191 er bool 192 }{ 193 // descending order 194 {buildByImageSize(), 0, 2, false}, 195 {buildByImageSize(), 1, 0, true}, 196 {buildByImageSize(), 3, 2, true}, 197 } 198 199 for _, fooTest := range fooTests { 200 r := fooTest.images.Less(fooTest.i, fooTest.j) 201 if r != fooTest.er { 202 t.Errorf("returned %t but expected %t for the foo=%v", r, fooTest.er, fooTest.images) 203 } 204 } 205 }