|
1 | 1 | package controllers |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "github.com/lwolf/konsumerator/pkg/helpers/tests" |
| 5 | + appsv1 "k8s.io/api/apps/v1" |
4 | 6 | "testing" |
5 | 7 | "time" |
6 | 8 |
|
@@ -138,8 +140,6 @@ func TestUpdateStatusAnnotations(t *testing.T) { |
138 | 140 |
|
139 | 141 | } |
140 | 142 |
|
141 | | -func TestResourceRequirementsDiff(t *testing.T) {} |
142 | | -func TestResourceRequirementsSum(t *testing.T) {} |
143 | 143 | func TestResourceListSum(t *testing.T) { |
144 | 144 | testCases := map[string]struct { |
145 | 145 | a corev1.ResourceList |
@@ -189,4 +189,147 @@ func isResourceListEqual(a, b corev1.ResourceList) bool { |
189 | 189 | return true |
190 | 190 | } |
191 | 191 |
|
192 | | -func TestResourceListDiff(t *testing.T) {} |
| 192 | +func TestCalculateFallbackFromPolicy(t *testing.T) { |
| 193 | + testCases := map[string]struct { |
| 194 | + strategy konsumeratorv1.FallbackStrategy |
| 195 | + containerName []string |
| 196 | + policy *konsumeratorv1.ResourcePolicy |
| 197 | + expFallback map[string]*corev1.ResourceRequirements |
| 198 | + }{ |
| 199 | + "should return min allowed by containerName in the map": { |
| 200 | + strategy: konsumeratorv1.FallbackStrategyMin, |
| 201 | + containerName: []string{"test"}, |
| 202 | + policy: &konsumeratorv1.ResourcePolicy{ContainerPolicies: []konsumeratorv1.ContainerResourcePolicy{ |
| 203 | + tests.NewContainerResourcePolicy("test", "100m", "100M", "2", "150M"), |
| 204 | + }}, |
| 205 | + expFallback: map[string]*corev1.ResourceRequirements{"test": tests.NewResourceRequirements("100m", "100M", "100m", "100M")}, |
| 206 | + }, |
| 207 | + "should return max allowed by containerName in the map": { |
| 208 | + strategy: konsumeratorv1.FallbackStrategyMax, |
| 209 | + containerName: []string{"test"}, |
| 210 | + policy: &konsumeratorv1.ResourcePolicy{ContainerPolicies: []konsumeratorv1.ContainerResourcePolicy{ |
| 211 | + tests.NewContainerResourcePolicy("test", "100m", "100M", "2", "150M"), |
| 212 | + }}, |
| 213 | + expFallback: map[string]*corev1.ResourceRequirements{"test": tests.NewResourceRequirements("2", "150M", "2", "150M")}, |
| 214 | + }, |
| 215 | + "multi container setup should return correct values": { |
| 216 | + strategy: konsumeratorv1.FallbackStrategyMin, |
| 217 | + containerName: []string{"test", "test2"}, |
| 218 | + policy: &konsumeratorv1.ResourcePolicy{ContainerPolicies: []konsumeratorv1.ContainerResourcePolicy{ |
| 219 | + tests.NewContainerResourcePolicy("test", "100m", "100M", "2", "150M"), |
| 220 | + tests.NewContainerResourcePolicy("test2", "1", "1G", "5", "2G"), |
| 221 | + }}, |
| 222 | + expFallback: map[string]*corev1.ResourceRequirements{ |
| 223 | + "test": tests.NewResourceRequirements("100m", "100M", "100m", "100M"), |
| 224 | + "test2": tests.NewResourceRequirements("1", "1G", "1", "1G"), |
| 225 | + }, |
| 226 | + }, |
| 227 | + "should return nil if no such policy exists": { |
| 228 | + strategy: konsumeratorv1.FallbackStrategyMax, |
| 229 | + containerName: []string{"test"}, |
| 230 | + policy: nil, |
| 231 | + expFallback: nil, |
| 232 | + }, |
| 233 | + } |
| 234 | + for testName, tc := range testCases { |
| 235 | + t.Run(testName, func(t *testing.T) { |
| 236 | + fallback := calculateFallbackFromPolicy(tc.policy, tc.strategy) |
| 237 | + if tc.expFallback == nil && fallback != nil { |
| 238 | + t.Fatalf("Fallback is expected to be nil, got %v", fallback) |
| 239 | + } |
| 240 | + if tc.expFallback != nil { |
| 241 | + for _, containerName := range tc.containerName { |
| 242 | + if helpers.CmpResourceList(fallback[containerName].Requests, tc.expFallback[containerName].Requests) != 0 { |
| 243 | + t.Errorf("Fallback results mismatch. want %v, got %v", tc.expFallback[containerName].Requests, fallback[containerName].Requests) |
| 244 | + } |
| 245 | + if helpers.CmpResourceList(fallback[containerName].Limits, tc.expFallback[containerName].Limits) != 0 { |
| 246 | + t.Errorf("Fallback results mismatch. want %v, got %v", tc.expFallback[containerName].Requests, fallback[containerName].Requests) |
| 247 | + } |
| 248 | + } |
| 249 | + } |
| 250 | + }) |
| 251 | + } |
| 252 | +} |
| 253 | + |
| 254 | +func TestCalculateFallbackFromRunningInstances(t *testing.T) { |
| 255 | + instances := []appsv1.Deployment{ |
| 256 | + { |
| 257 | + Spec: appsv1.DeploymentSpec{ |
| 258 | + Template: corev1.PodTemplateSpec{ |
| 259 | + Spec: corev1.PodSpec{ |
| 260 | + Containers: []corev1.Container{ |
| 261 | + {Name: "busybox", Resources: *tests.NewResourceRequirements("100m", "100M", "100m", "100M")}, |
| 262 | + {Name: "test", Resources: *tests.NewResourceRequirements("600m", "600M", "600m", "600M")}, |
| 263 | + }}, |
| 264 | + }, |
| 265 | + }, |
| 266 | + }, |
| 267 | + { |
| 268 | + Spec: appsv1.DeploymentSpec{ |
| 269 | + Template: corev1.PodTemplateSpec{ |
| 270 | + Spec: corev1.PodSpec{ |
| 271 | + Containers: []corev1.Container{ |
| 272 | + {Name: "busybox", Resources: *tests.NewResourceRequirements("150m", "150M", "1", "100G")}, |
| 273 | + {Name: "test", Resources: *tests.NewResourceRequirements("50m", "50M", "2", "200G")}, |
| 274 | + }}, |
| 275 | + }, |
| 276 | + }, |
| 277 | + }, |
| 278 | + { |
| 279 | + Spec: appsv1.DeploymentSpec{ |
| 280 | + Template: corev1.PodTemplateSpec{ |
| 281 | + Spec: corev1.PodSpec{ |
| 282 | + Containers: []corev1.Container{ |
| 283 | + {Name: "busybox", Resources: *tests.NewResourceRequirements("200m", "200M", "300m", "300M")}, |
| 284 | + {Name: "test", Resources: *tests.NewResourceRequirements("100m", "100M", "300m", "300M")}, |
| 285 | + }}, |
| 286 | + }, |
| 287 | + }, |
| 288 | + }, |
| 289 | + } |
| 290 | + |
| 291 | + testCases := map[string]struct { |
| 292 | + strategy konsumeratorv1.FallbackStrategy |
| 293 | + containerName []string |
| 294 | + instances []appsv1.Deployment |
| 295 | + expFallback map[string]*corev1.ResourceRequirements |
| 296 | + }{ |
| 297 | + "min": { |
| 298 | + strategy: konsumeratorv1.FallbackStrategyMin, |
| 299 | + containerName: []string{"busybox", "test"}, |
| 300 | + instances: instances, |
| 301 | + expFallback: map[string]*corev1.ResourceRequirements{ |
| 302 | + "busybox": tests.NewResourceRequirements("100m", "100M", "100m", "100M"), |
| 303 | + "test": tests.NewResourceRequirements("50m", "50M", "2", "200G"), |
| 304 | + }, |
| 305 | + }, |
| 306 | + "max": { |
| 307 | + strategy: konsumeratorv1.FallbackStrategyMax, |
| 308 | + containerName: []string{"busybox", "test"}, |
| 309 | + instances: instances, |
| 310 | + expFallback: map[string]*corev1.ResourceRequirements{ |
| 311 | + "busybox": tests.NewResourceRequirements("200m", "200M", "300m", "300M"), |
| 312 | + "test": tests.NewResourceRequirements("600m", "600M", "600m", "600M"), |
| 313 | + }, |
| 314 | + }, |
| 315 | + } |
| 316 | + for testName, tc := range testCases { |
| 317 | + t.Run(testName, func(t *testing.T) { |
| 318 | + fallback := calculateFallbackFromRunningInstances(tc.instances, tc.strategy) |
| 319 | + if tc.expFallback == nil && fallback != nil { |
| 320 | + t.Fatalf("Fallback is expected to be nil, got %v", fallback) |
| 321 | + } |
| 322 | + if tc.expFallback != nil { |
| 323 | + for _, containerName := range tc.containerName { |
| 324 | + if helpers.CmpResourceList(fallback[containerName].Requests, tc.expFallback[containerName].Requests) != 0 { |
| 325 | + t.Errorf("Fallback results mismatch. want %v, got %v", tc.expFallback[containerName].Requests, fallback[containerName].Requests) |
| 326 | + } |
| 327 | + if helpers.CmpResourceList(fallback[containerName].Limits, tc.expFallback[containerName].Limits) != 0 { |
| 328 | + t.Errorf("Fallback results mismatch. want %v, got %v", tc.expFallback[containerName].Requests, fallback[containerName].Requests) |
| 329 | + } |
| 330 | + } |
| 331 | + } |
| 332 | + |
| 333 | + }) |
| 334 | + } |
| 335 | +} |
0 commit comments