...

Source file src/k8s.io/kubernetes/pkg/apis/flowcontrol/v1beta1/defaults_test.go

Documentation: k8s.io/kubernetes/pkg/apis/flowcontrol/v1beta1

     1  /*
     2  Copyright 2022 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 v1beta1
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  
    25  	flowcontrolv1beta1 "k8s.io/api/flowcontrol/v1beta1"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  	"k8s.io/utils/pointer"
    28  )
    29  
    30  func TestDefaultWithPriorityLevelConfiguration(t *testing.T) {
    31  	tests := []struct {
    32  		name     string
    33  		original runtime.Object
    34  		expected runtime.Object
    35  	}{
    36  		{
    37  			name: "Defaulting for Exempt",
    38  			original: &flowcontrolv1beta1.PriorityLevelConfiguration{
    39  				Spec: flowcontrolv1beta1.PriorityLevelConfigurationSpec{
    40  					Type:   flowcontrolv1beta1.PriorityLevelEnablementExempt,
    41  					Exempt: &flowcontrolv1beta1.ExemptPriorityLevelConfiguration{},
    42  				},
    43  			},
    44  			expected: &flowcontrolv1beta1.PriorityLevelConfiguration{
    45  				Spec: flowcontrolv1beta1.PriorityLevelConfigurationSpec{
    46  					Type: flowcontrolv1beta1.PriorityLevelEnablementExempt,
    47  					Exempt: &flowcontrolv1beta1.ExemptPriorityLevelConfiguration{
    48  						NominalConcurrencyShares: pointer.Int32(0),
    49  						LendablePercent:          pointer.Int32(0),
    50  					},
    51  				},
    52  			},
    53  		},
    54  		{
    55  			name: "LendablePercent is not specified, should default to zero",
    56  			original: &flowcontrolv1beta1.PriorityLevelConfiguration{
    57  				Spec: flowcontrolv1beta1.PriorityLevelConfigurationSpec{
    58  					Type: flowcontrolv1beta1.PriorityLevelEnablementLimited,
    59  					Limited: &flowcontrolv1beta1.LimitedPriorityLevelConfiguration{
    60  						AssuredConcurrencyShares: 5,
    61  						LimitResponse: flowcontrolv1beta1.LimitResponse{
    62  							Type: flowcontrolv1beta1.LimitResponseTypeReject,
    63  						},
    64  					},
    65  				},
    66  			},
    67  			expected: &flowcontrolv1beta1.PriorityLevelConfiguration{
    68  				Spec: flowcontrolv1beta1.PriorityLevelConfigurationSpec{
    69  					Type: flowcontrolv1beta1.PriorityLevelEnablementLimited,
    70  					Limited: &flowcontrolv1beta1.LimitedPriorityLevelConfiguration{
    71  						AssuredConcurrencyShares: 5,
    72  						LendablePercent:          pointer.Int32(0),
    73  						LimitResponse: flowcontrolv1beta1.LimitResponse{
    74  							Type: flowcontrolv1beta1.LimitResponseTypeReject,
    75  						},
    76  					},
    77  				},
    78  			},
    79  		},
    80  	}
    81  
    82  	scheme := runtime.NewScheme()
    83  	if err := AddToScheme(scheme); err != nil {
    84  		t.Fatalf("Failed to add to scheme: %v", err)
    85  	}
    86  
    87  	for _, test := range tests {
    88  		t.Run(test.name, func(t *testing.T) {
    89  			original := test.original
    90  			expected := test.expected
    91  
    92  			scheme.Default(original)
    93  			if !reflect.DeepEqual(expected, original) {
    94  				t.Errorf("Expected defaulting to work - diff: %s", cmp.Diff(expected, original))
    95  			}
    96  		})
    97  	}
    98  }
    99  

View as plain text