|
| 1 | +/* |
| 2 | +Copyright 2025 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 provreq |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + apiv1 "k8s.io/api/core/v1" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + v1 "k8s.io/autoscaler/cluster-autoscaler/apis/provisioningrequest/autoscaling.x-k8s.io/v1" |
| 26 | + "k8s.io/autoscaler/cluster-autoscaler/provisioningrequest/pods" |
| 27 | + testutils "k8s.io/autoscaler/cluster-autoscaler/utils/test" |
| 28 | +) |
| 29 | + |
| 30 | +func TestProvisioningRequestScaleUpEnforcer(t *testing.T) { |
| 31 | + prPod1 := testutils.BuildTestPod("pr-pod-1", 500, 10) |
| 32 | + prPod1.Annotations[v1.ProvisioningRequestPodAnnotationKey] = "pr-class" |
| 33 | + |
| 34 | + prPod2 := testutils.BuildTestPod("pr-pod-2", 500, 10) |
| 35 | + prPod2.Annotations[pods.DeprecatedProvisioningRequestPodAnnotationKey] = "pr-class-2" |
| 36 | + |
| 37 | + pod1 := testutils.BuildTestPod("pod-1", 500, 10) |
| 38 | + pod2 := testutils.BuildTestPod("pod-2", 500, 10) |
| 39 | + |
| 40 | + testCases := map[string]struct { |
| 41 | + unschedulablePods []*apiv1.Pod |
| 42 | + want bool |
| 43 | + }{ |
| 44 | + "Any pod with ProvisioningRequest annotation key forces scale up": { |
| 45 | + unschedulablePods: []*corev1.Pod{prPod1, pod1}, |
| 46 | + want: true, |
| 47 | + }, |
| 48 | + "Any pod with ProvisioningRequest deprecated annotation key forces scale up": { |
| 49 | + unschedulablePods: []*corev1.Pod{prPod2, pod1}, |
| 50 | + want: true, |
| 51 | + }, |
| 52 | + "Pod without ProvisioningRequest annotation key don't force scale up": { |
| 53 | + unschedulablePods: []*corev1.Pod{pod1, pod2}, |
| 54 | + want: false, |
| 55 | + }, |
| 56 | + "No pods don't force scale up": { |
| 57 | + unschedulablePods: []*corev1.Pod{}, |
| 58 | + want: false, |
| 59 | + }, |
| 60 | + } |
| 61 | + for _, test := range testCases { |
| 62 | + scaleUpEnforcer := NewProvisioningRequestScaleUpEnforcer() |
| 63 | + got := scaleUpEnforcer.ShouldForceScaleUp(test.unschedulablePods) |
| 64 | + assert.Equal(t, got, test.want) |
| 65 | + } |
| 66 | +} |
0 commit comments