...

Source file src/k8s.io/kubernetes/pkg/apis/autoscaling/v1/conversion_test.go

Documentation: k8s.io/kubernetes/pkg/apis/autoscaling/v1

     1  /*
     2  Copyright 2017 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 v1
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	autoscalingv1 "k8s.io/api/autoscaling/v1"
    24  	"k8s.io/apimachinery/pkg/api/resource"
    25  	"k8s.io/apimachinery/pkg/conversion"
    26  	"k8s.io/kubernetes/pkg/apis/autoscaling"
    27  	api "k8s.io/kubernetes/pkg/apis/core"
    28  	utilpointer "k8s.io/utils/pointer"
    29  )
    30  
    31  // Test for #101370
    32  func TestConvert_autoscaling_HorizontalPodAutoscalerSpec_To_v1_HorizontalPodAutoscalerSpec(t *testing.T) {
    33  
    34  	type args struct {
    35  		in        *autoscaling.HorizontalPodAutoscalerSpec
    36  		out       *autoscalingv1.HorizontalPodAutoscalerSpec
    37  		expectOut *autoscalingv1.HorizontalPodAutoscalerSpec
    38  		s         conversion.Scope
    39  	}
    40  	tests := []struct {
    41  		name    string
    42  		args    args
    43  		wantErr bool
    44  	}{
    45  		{
    46  			"TestConversionWithCPUAverageValueAndUtilizationBoth1",
    47  			args{
    48  				in: &autoscaling.HorizontalPodAutoscalerSpec{
    49  					MinReplicas: utilpointer.Int32(1),
    50  					MaxReplicas: 3,
    51  					Metrics: []autoscaling.MetricSpec{
    52  						{
    53  							Type: autoscaling.ResourceMetricSourceType,
    54  							Resource: &autoscaling.ResourceMetricSource{
    55  								Name: api.ResourceCPU,
    56  								Target: autoscaling.MetricTarget{
    57  									Type:         autoscaling.AverageValueMetricType,
    58  									AverageValue: resource.NewMilliQuantity(300, resource.DecimalSI),
    59  								},
    60  							},
    61  						},
    62  						{
    63  							Type: autoscaling.ResourceMetricSourceType,
    64  							Resource: &autoscaling.ResourceMetricSource{
    65  								Name: api.ResourceCPU,
    66  								Target: autoscaling.MetricTarget{
    67  									Type:               autoscaling.UtilizationMetricType,
    68  									AverageUtilization: utilpointer.Int32(70),
    69  								},
    70  							},
    71  						},
    72  					},
    73  				},
    74  				out: &autoscalingv1.HorizontalPodAutoscalerSpec{},
    75  				expectOut: &autoscalingv1.HorizontalPodAutoscalerSpec{
    76  					MinReplicas:                    utilpointer.Int32(1),
    77  					MaxReplicas:                    3,
    78  					TargetCPUUtilizationPercentage: utilpointer.Int32(70),
    79  				},
    80  				s: nil,
    81  			},
    82  			false,
    83  		},
    84  		{
    85  			"TestConversionWithCPUAverageValueAndUtilizationBoth2",
    86  			args{
    87  				in: &autoscaling.HorizontalPodAutoscalerSpec{
    88  					MinReplicas: utilpointer.Int32(1),
    89  					MaxReplicas: 3,
    90  					Metrics: []autoscaling.MetricSpec{
    91  						{
    92  							Type: autoscaling.ResourceMetricSourceType,
    93  							Resource: &autoscaling.ResourceMetricSource{
    94  								Name: api.ResourceCPU,
    95  								Target: autoscaling.MetricTarget{
    96  									Type:         autoscaling.AverageValueMetricType,
    97  									AverageValue: resource.NewMilliQuantity(300, resource.DecimalSI),
    98  								},
    99  							},
   100  						},
   101  						{
   102  							Type: autoscaling.ResourceMetricSourceType,
   103  							Resource: &autoscaling.ResourceMetricSource{
   104  								Name: api.ResourceCPU,
   105  								Target: autoscaling.MetricTarget{
   106  									Type:               autoscaling.UtilizationMetricType,
   107  									AverageUtilization: utilpointer.Int32(70),
   108  								},
   109  							},
   110  						},
   111  					},
   112  				},
   113  				out: &autoscalingv1.HorizontalPodAutoscalerSpec{
   114  					MinReplicas:                    utilpointer.Int32(2),
   115  					MaxReplicas:                    4,
   116  					TargetCPUUtilizationPercentage: utilpointer.Int32(60),
   117  				},
   118  				expectOut: &autoscalingv1.HorizontalPodAutoscalerSpec{
   119  					MinReplicas:                    utilpointer.Int32(1),
   120  					MaxReplicas:                    3,
   121  					TargetCPUUtilizationPercentage: utilpointer.Int32(70),
   122  				},
   123  				s: nil,
   124  			},
   125  			false,
   126  		},
   127  		{
   128  			"TestConversionWithoutMetrics",
   129  			args{
   130  				in: &autoscaling.HorizontalPodAutoscalerSpec{
   131  					MinReplicas: utilpointer.Int32(1),
   132  					MaxReplicas: 3,
   133  					Metrics:     []autoscaling.MetricSpec{},
   134  				},
   135  				out: &autoscalingv1.HorizontalPodAutoscalerSpec{
   136  					MinReplicas:                    utilpointer.Int32(1),
   137  					MaxReplicas:                    4,
   138  					TargetCPUUtilizationPercentage: utilpointer.Int32(60),
   139  				},
   140  				expectOut: &autoscalingv1.HorizontalPodAutoscalerSpec{
   141  					MinReplicas:                    utilpointer.Int32(1),
   142  					MaxReplicas:                    3,
   143  					TargetCPUUtilizationPercentage: utilpointer.Int32(60),
   144  				},
   145  				s: nil,
   146  			},
   147  			false,
   148  		},
   149  		{
   150  			"TestConversionWithCPUUtilizationOnly",
   151  			args{
   152  				in: &autoscaling.HorizontalPodAutoscalerSpec{
   153  					MinReplicas: utilpointer.Int32(1),
   154  					MaxReplicas: 3,
   155  					Metrics: []autoscaling.MetricSpec{
   156  						{
   157  							Type: autoscaling.ResourceMetricSourceType,
   158  							Resource: &autoscaling.ResourceMetricSource{
   159  								Name: api.ResourceCPU,
   160  								Target: autoscaling.MetricTarget{
   161  									Type:               autoscaling.UtilizationMetricType,
   162  									AverageUtilization: utilpointer.Int32(60),
   163  								},
   164  							},
   165  						},
   166  					},
   167  				},
   168  				out: &autoscalingv1.HorizontalPodAutoscalerSpec{},
   169  				expectOut: &autoscalingv1.HorizontalPodAutoscalerSpec{
   170  					MinReplicas:                    utilpointer.Int32(1),
   171  					MaxReplicas:                    3,
   172  					TargetCPUUtilizationPercentage: utilpointer.Int32(60),
   173  				},
   174  				s: nil,
   175  			},
   176  			false,
   177  		},
   178  	}
   179  	for _, tt := range tests {
   180  		t.Run(tt.name, func(t *testing.T) {
   181  			if err := Convert_autoscaling_HorizontalPodAutoscalerSpec_To_v1_HorizontalPodAutoscalerSpec(tt.args.in, tt.args.out, tt.args.s); (err != nil) != tt.wantErr {
   182  				t.Errorf("Convert_autoscaling_HorizontalPodAutoscalerSpec_To_v1_HorizontalPodAutoscalerSpec() error = %v, wantErr %v", err, tt.wantErr)
   183  			}
   184  
   185  			assert.Equal(t, tt.args.expectOut, tt.args.out)
   186  		})
   187  	}
   188  }
   189  

View as plain text