github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/evictionmanager/rule/queue_test.go (about) 1 /* 2 Copyright 2022 The Katalyst 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 rule 18 19 import ( 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 v1 "k8s.io/api/core/v1" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/types" 26 27 pluginapi "github.com/kubewharf/katalyst-api/pkg/protocol/evictionplugin/v1alpha1" 28 ) 29 30 func makeRuledEvictPod(name, scope string) *RuledEvictPod { 31 return &RuledEvictPod{ 32 EvictPod: &pluginapi.EvictPod{ 33 Pod: &v1.Pod{ 34 ObjectMeta: metav1.ObjectMeta{ 35 Name: name, 36 Namespace: "default", 37 UID: types.UID(name), 38 }, 39 }, 40 }, 41 Scope: scope, 42 } 43 } 44 45 func TestEvictionQueue(t *testing.T) { 46 t.Parallel() 47 48 for _, tc := range []struct { 49 comment string 50 q EvictionQueue 51 52 addResults [][]string 53 popResults [][]string 54 withdrawResults [][]string 55 }{ 56 { 57 comment: "with SimpleEvictionQueue", 58 q: NewFIFOEvictionQueue(-1), 59 // 1: 1,2,3 + withdraw = false -> 1,2,3 60 // 2: 3,4,5 + withdraw = true -> 3,4,5 61 // 3: 4,5,6 + withdraw = false -> 3,4,5,6 62 // 6: 1,2,3 + withdraw = false -> 1,2,3 63 // 7: 3,4,5 + withdraw = false -> 1,2,3,4,5 64 addResults: [][]string{ 65 {"p-1", "p-2", "p-3"}, 66 {"p-3", "p-4", "p-5"}, 67 {"p-3", "p-4", "p-5", "p-6"}, 68 {"p-1", "p-2", "p-3"}, 69 {"p-1", "p-2", "p-3", "p-4", "p-5"}, 70 }, 71 // 4: 3,4,5,6 -> empty 72 // 5: empty -> empty 73 // 9: 1,3,5 -> empty 74 // 10: empty -> empty 75 popResults: [][]string{ 76 {"p-3", "p-4", "p-5", "p-6"}, 77 {}, 78 {"p-1", "p-3", "p-5"}, 79 {}, 80 }, 81 // 8: 2,4 -> 1,3,5 82 withdrawResults: [][]string{ 83 {"p-1", "p-3", "p-5"}, 84 }, 85 }, 86 { 87 comment: "with FIFOEvictionQueue limit set as 1", 88 q: NewFIFOEvictionQueue(1), 89 // 1: 1,2,3 + withdraw = false -> 1,2,3 90 // 2: 3,4,5 + withdraw = true -> 3,4,5 91 // 3: 4,5,6 + withdraw = false -> 3,4,5,6 92 // 6: 1,2,3 + withdraw = false -> 5,6,1,2,3 93 // 7: 3,4,5 + withdraw = false -> 5,6,1,2,3,4 94 addResults: [][]string{ 95 {"p-1", "p-2", "p-3"}, 96 {"p-3", "p-4", "p-5"}, 97 {"p-3", "p-4", "p-5", "p-6"}, 98 {"p-5", "p-6", "p-1", "p-2", "p-3"}, 99 {"p-5", "p-6", "p-1", "p-2", "p-3", "p-4"}, 100 }, 101 // 4: 3 -> empty 102 // 5: 4 -> empty 103 // 9: 5 -> empty 104 // 10: 6 -> empty 105 popResults: [][]string{ 106 {"p-3"}, 107 {"p-4"}, 108 {"p-5"}, 109 {"p-6"}, 110 }, 111 // 8: 2,4 -> 5,6,1,2 112 withdrawResults: [][]string{ 113 {"p-5", "p-6", "p-1", "p-3"}, 114 }, 115 }, 116 { 117 comment: "with FIFOEvictionQueue limit set as 2", 118 q: NewFIFOEvictionQueue(2), 119 // 1: 1,2,3 + withdraw = false -> 1,2,3 120 // 2: 3,4,5 + withdraw = true -> 3,4,5 121 // 3: 4,5,6 + withdraw = false -> 3,4,5,6 122 // 6: 1,2,3 + withdraw = false -> 1,2,3 123 // 7: 3,4,5 + withdraw = false -> 1,2,3,4,5 124 addResults: [][]string{ 125 {"p-1", "p-2", "p-3"}, 126 {"p-3", "p-4", "p-5"}, 127 {"p-3", "p-4", "p-5", "p-6"}, 128 {"p-1", "p-2", "p-3"}, 129 {"p-1", "p-2", "p-3", "p-4", "p-5"}, 130 }, 131 // 4: 3,4 -> empty 132 // 5: 5,6 -> empty 133 // 9: 1,3 -> empty 134 // 10: 5 -> empty 135 popResults: [][]string{ 136 {"p-3", "p-4"}, 137 {"p-5", "p-6"}, 138 {"p-1", "p-3"}, 139 {"p-5"}, 140 }, 141 // 8: 2,4 -> 1,3,5 142 withdrawResults: [][]string{ 143 {"p-1", "p-3", "p-5"}, 144 }, 145 }, 146 { 147 comment: "with FIFOEvictionQueue limit set as 2", 148 q: NewFIFOEvictionQueue(3), 149 // 1: 1,2,3 + withdraw = false -> 1,2,3 150 // 2: 3,4,5 + withdraw = true -> 3,4,5 151 // 3: 4,5,6 + withdraw = false -> 3,4,5,6 152 // 6: 1,2,3 + withdraw = false -> 1,2,3 153 // 7: 3,4,5 + withdraw = false -> 1,2,3,4,5 154 addResults: [][]string{ 155 {"p-1", "p-2", "p-3"}, 156 {"p-3", "p-4", "p-5"}, 157 {"p-3", "p-4", "p-5", "p-6"}, 158 {"p-1", "p-2", "p-3"}, 159 {"p-1", "p-2", "p-3", "p-4", "p-5"}, 160 }, 161 // 4: 3,4,5 -> empty 162 // 5: 6 -> empty 163 // 9: 1,3,5 -> empty 164 // 10: empty -> empty 165 popResults: [][]string{ 166 {"p-3", "p-4", "p-5"}, 167 {"p-6"}, 168 {"p-1", "p-3", "p-5"}, 169 {}, 170 }, 171 // 8: 2,4 -> 1,3,5 172 withdrawResults: [][]string{ 173 {"p-1", "p-3", "p-5"}, 174 }, 175 }, 176 } { 177 t.Logf("case: %v", tc.comment) 178 var rpList RuledEvictPodList 179 180 // 1: 1,2,3 + withdraw = false 181 tc.q.Add( 182 RuledEvictPodList{ 183 makeRuledEvictPod("p-1", ""), 184 makeRuledEvictPod("p-2", ""), 185 makeRuledEvictPod("p-3", ""), 186 }, 187 false, 188 ) 189 assert.Equal(t, tc.addResults[0], tc.q.List().getPodNames()) 190 191 // 2: 3,4,5 + withdraw = true 192 tc.q.Add( 193 RuledEvictPodList{ 194 makeRuledEvictPod("p-3", ""), 195 makeRuledEvictPod("p-4", ""), 196 makeRuledEvictPod("p-5", ""), 197 }, 198 true, 199 ) 200 assert.Equal(t, tc.addResults[1], tc.q.List().getPodNames()) 201 202 // 3: 4,5,6 + withdraw = false 203 tc.q.Add( 204 RuledEvictPodList{ 205 makeRuledEvictPod("p-4", ""), 206 makeRuledEvictPod("p-5", ""), 207 makeRuledEvictPod("p-6", ""), 208 }, 209 false, 210 ) 211 assert.Equal(t, tc.addResults[2], tc.q.List().getPodNames()) 212 213 // 4: pop 214 rpList = tc.q.Pop() 215 assert.Equal(t, tc.popResults[0], rpList.getPodNames()) 216 217 // 5: pop 218 rpList = tc.q.Pop() 219 assert.Equal(t, tc.popResults[1], rpList.getPodNames()) 220 221 // 6: 1,2,3 + withdraw = false 222 tc.q.Add( 223 RuledEvictPodList{ 224 makeRuledEvictPod("p-1", ""), 225 makeRuledEvictPod("p-2", ""), 226 makeRuledEvictPod("p-3", ""), 227 }, 228 false, 229 ) 230 assert.Equal(t, tc.addResults[3], tc.q.List().getPodNames()) 231 232 // 7: 3,4,5 + withdraw = false 233 tc.q.Add( 234 RuledEvictPodList{ 235 makeRuledEvictPod("p-3", ""), 236 makeRuledEvictPod("p-4", ""), 237 makeRuledEvictPod("p-5", ""), 238 }, 239 false, 240 ) 241 assert.Equal(t, tc.addResults[4], tc.q.List().getPodNames()) 242 243 // 8: 2,4 244 tc.q.Withdraw(RuledEvictPodList{ 245 makeRuledEvictPod("p-2", ""), 246 makeRuledEvictPod("p-4", ""), 247 }) 248 assert.Equal(t, tc.withdrawResults[0], tc.q.List().getPodNames()) 249 250 // 9: pop 251 rpList = tc.q.Pop() 252 assert.Equal(t, tc.popResults[2], rpList.getPodNames()) 253 254 // 10: pop 255 rpList = tc.q.Pop() 256 assert.Equal(t, tc.popResults[3], rpList.getPodNames()) 257 } 258 }