k8s.io/kubernetes@v1.29.3/pkg/registry/autoscaling/horizontalpodautoscaler/strategy_test.go (about)

     1  /*
     2  Copyright 2015 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 horizontalpodautoscaler
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	utilfeature "k8s.io/apiserver/pkg/util/feature"
    24  	featuregatetesting "k8s.io/component-base/featuregate/testing"
    25  	"k8s.io/kubernetes/pkg/apis/autoscaling"
    26  	"k8s.io/kubernetes/pkg/apis/core"
    27  	"k8s.io/kubernetes/pkg/features"
    28  	"k8s.io/utils/pointer"
    29  )
    30  
    31  func makeTestContainerMetricsHPA(hasContainerMetric bool) *autoscaling.HorizontalPodAutoscaler {
    32  	testHPA := &autoscaling.HorizontalPodAutoscaler{
    33  		Spec: autoscaling.HorizontalPodAutoscalerSpec{
    34  			Metrics: []autoscaling.MetricSpec{},
    35  		},
    36  	}
    37  	if hasContainerMetric {
    38  		testHPA.Spec.Metrics = append(testHPA.Spec.Metrics, autoscaling.MetricSpec{
    39  			Type: autoscaling.ContainerResourceMetricSourceType,
    40  			ContainerResource: &autoscaling.ContainerResourceMetricSource{
    41  				Name:      core.ResourceCPU,
    42  				Container: "test-container",
    43  				Target: autoscaling.MetricTarget{
    44  					Type:               autoscaling.UtilizationMetricType,
    45  					AverageUtilization: pointer.Int32Ptr(30),
    46  				},
    47  			},
    48  		})
    49  	}
    50  	return testHPA
    51  }
    52  
    53  func TestCreateWithFeatureEnabled(t *testing.T) {
    54  	defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.HPAContainerMetrics, true)()
    55  	testHPA := makeTestContainerMetricsHPA(true)
    56  	Strategy.PrepareForCreate(context.Background(), testHPA)
    57  	if testHPA.Spec.Metrics[0].ContainerResource == nil {
    58  		t.Errorf("container metrics was set to nil")
    59  	}
    60  }
    61  
    62  func TestCreateWithFeatureDisabled(t *testing.T) {
    63  	defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.HPAContainerMetrics, false)()
    64  	testHPA := makeTestContainerMetricsHPA(true)
    65  	Strategy.PrepareForCreate(context.Background(), testHPA)
    66  	if testHPA.Spec.Metrics[0].ContainerResource != nil {
    67  		t.Errorf("container metrics is not nil")
    68  	}
    69  }
    70  
    71  func TestAutoscalerStatusStrategy_PrepareForUpdate(t *testing.T) {
    72  	for _, tc := range []struct {
    73  		name           string
    74  		featureEnabled bool
    75  		old            bool
    76  		expectedNew    bool
    77  	}{
    78  		{
    79  			name:           "feature disabled with existing container metrics",
    80  			featureEnabled: false,
    81  			old:            true,
    82  			expectedNew:    true,
    83  		},
    84  		{
    85  			name:           "feature enabled with no container metrics",
    86  			featureEnabled: true,
    87  			old:            false,
    88  			expectedNew:    true,
    89  		},
    90  		{
    91  			name:           "feature enabled with existing container metrics",
    92  			featureEnabled: true,
    93  			old:            true,
    94  			expectedNew:    true,
    95  		},
    96  	} {
    97  		t.Run(tc.name, func(t *testing.T) {
    98  			defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.HPAContainerMetrics, tc.featureEnabled)()
    99  			oldHPA := makeTestContainerMetricsHPA(tc.old)
   100  			newHPA := makeTestContainerMetricsHPA(true)
   101  			Strategy.PrepareForUpdate(context.Background(), newHPA, oldHPA)
   102  			if tc.expectedNew && newHPA.Spec.Metrics[0].ContainerResource == nil {
   103  				t.Errorf("container metric source is nil")
   104  			}
   105  			if !tc.expectedNew && newHPA.Spec.Metrics[0].ContainerResource != nil {
   106  				t.Errorf("container metric source is not nil")
   107  			}
   108  		})
   109  	}
   110  }