k8s.io/kubernetes@v1.29.3/pkg/scheduler/internal/cache/debugger/comparer_test.go (about) 1 /* 2 Copyright 2018 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 debugger 18 19 import ( 20 "testing" 21 22 "github.com/google/go-cmp/cmp" 23 24 "k8s.io/api/core/v1" 25 "k8s.io/apimachinery/pkg/types" 26 "k8s.io/kubernetes/pkg/scheduler/framework" 27 ) 28 29 func TestCompareNodes(t *testing.T) { 30 tests := []struct { 31 name string 32 actual []string 33 cached []string 34 missing []string 35 redundant []string 36 }{ 37 { 38 name: "redundant cached value", 39 actual: []string{"foo", "bar"}, 40 cached: []string{"bar", "foo", "foobar"}, 41 missing: []string{}, 42 redundant: []string{"foobar"}, 43 }, 44 { 45 name: "missing cached value", 46 actual: []string{"foo", "bar", "foobar"}, 47 cached: []string{"bar", "foo"}, 48 missing: []string{"foobar"}, 49 redundant: []string{}, 50 }, 51 { 52 name: "proper cache set", 53 actual: []string{"foo", "bar", "foobar"}, 54 cached: []string{"bar", "foobar", "foo"}, 55 missing: []string{}, 56 redundant: []string{}, 57 }, 58 } 59 60 for _, test := range tests { 61 t.Run(test.name, func(t *testing.T) { 62 testCompareNodes(test.actual, test.cached, test.missing, test.redundant, t) 63 }) 64 } 65 } 66 67 func testCompareNodes(actual, cached, missing, redundant []string, t *testing.T) { 68 compare := CacheComparer{} 69 nodes := []*v1.Node{} 70 for _, nodeName := range actual { 71 node := &v1.Node{} 72 node.Name = nodeName 73 nodes = append(nodes, node) 74 } 75 76 nodeInfo := make(map[string]*framework.NodeInfo) 77 for _, nodeName := range cached { 78 nodeInfo[nodeName] = &framework.NodeInfo{} 79 } 80 81 m, r := compare.CompareNodes(nodes, nodeInfo) 82 83 if diff := cmp.Diff(missing, m); diff != "" { 84 t.Errorf("Unexpected missing (-want, +got):\n%s", diff) 85 } 86 87 if diff := cmp.Diff(redundant, r); diff != "" { 88 t.Errorf("Unexpected redundant (-want, +got):\n%s", diff) 89 } 90 } 91 92 func TestComparePods(t *testing.T) { 93 tests := []struct { 94 name string 95 actual []string 96 cached []string 97 queued []string 98 missing []string 99 redundant []string 100 }{ 101 { 102 name: "redundant cached value", 103 actual: []string{"foo", "bar"}, 104 cached: []string{"bar", "foo", "foobar"}, 105 queued: []string{}, 106 missing: []string{}, 107 redundant: []string{"foobar"}, 108 }, 109 { 110 name: "redundant and queued values", 111 actual: []string{"foo", "bar"}, 112 cached: []string{"foo", "foobar"}, 113 queued: []string{"bar"}, 114 missing: []string{}, 115 redundant: []string{"foobar"}, 116 }, 117 { 118 name: "missing cached value", 119 actual: []string{"foo", "bar", "foobar"}, 120 cached: []string{"bar", "foo"}, 121 queued: []string{}, 122 missing: []string{"foobar"}, 123 redundant: []string{}, 124 }, 125 { 126 name: "missing and queued values", 127 actual: []string{"foo", "bar", "foobar"}, 128 cached: []string{"foo"}, 129 queued: []string{"bar"}, 130 missing: []string{"foobar"}, 131 redundant: []string{}, 132 }, 133 { 134 name: "correct cache set", 135 actual: []string{"foo", "bar", "foobar"}, 136 cached: []string{"bar", "foobar", "foo"}, 137 queued: []string{}, 138 missing: []string{}, 139 redundant: []string{}, 140 }, 141 { 142 name: "queued cache value", 143 actual: []string{"foo", "bar", "foobar"}, 144 cached: []string{"foobar", "foo"}, 145 queued: []string{"bar"}, 146 missing: []string{}, 147 redundant: []string{}, 148 }, 149 } 150 151 for _, test := range tests { 152 t.Run(test.name, func(t *testing.T) { 153 testComparePods(test.actual, test.cached, test.queued, test.missing, test.redundant, t) 154 }) 155 } 156 } 157 158 func testComparePods(actual, cached, queued, missing, redundant []string, t *testing.T) { 159 compare := CacheComparer{} 160 pods := []*v1.Pod{} 161 for _, uid := range actual { 162 pod := &v1.Pod{} 163 pod.UID = types.UID(uid) 164 pods = append(pods, pod) 165 } 166 167 queuedPods := []*v1.Pod{} 168 for _, uid := range queued { 169 pod := &v1.Pod{} 170 pod.UID = types.UID(uid) 171 queuedPods = append(queuedPods, pod) 172 } 173 174 nodeInfo := make(map[string]*framework.NodeInfo) 175 for _, uid := range cached { 176 pod := &v1.Pod{} 177 pod.UID = types.UID(uid) 178 pod.Namespace = "ns" 179 pod.Name = uid 180 181 nodeInfo[uid] = framework.NewNodeInfo(pod) 182 } 183 184 m, r := compare.ComparePods(pods, queuedPods, nodeInfo) 185 186 if diff := cmp.Diff(missing, m); diff != "" { 187 t.Errorf("Unexpected missing (-want, +got):\n%s", diff) 188 } 189 190 if diff := cmp.Diff(redundant, r); diff != "" { 191 t.Errorf("Unexpected redundant (-want, +got):\n%s", diff) 192 } 193 }