...

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

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

     1  /*
     2  Copyright 2023 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  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  
    25  	flowcontrolv1 "k8s.io/api/flowcontrol/v1"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  	"k8s.io/utils/ptr"
    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: &flowcontrolv1.PriorityLevelConfiguration{
    39  				Spec: flowcontrolv1.PriorityLevelConfigurationSpec{
    40  					Type:   flowcontrolv1.PriorityLevelEnablementExempt,
    41  					Exempt: &flowcontrolv1.ExemptPriorityLevelConfiguration{},
    42  				},
    43  			},
    44  			expected: &flowcontrolv1.PriorityLevelConfiguration{
    45  				Spec: flowcontrolv1.PriorityLevelConfigurationSpec{
    46  					Type: flowcontrolv1.PriorityLevelEnablementExempt,
    47  					Exempt: &flowcontrolv1.ExemptPriorityLevelConfiguration{
    48  						NominalConcurrencyShares: ptr.To(int32(0)),
    49  						LendablePercent:          ptr.To(int32(0)),
    50  					},
    51  				},
    52  			},
    53  		},
    54  		{
    55  			name: "LendablePercent is not specified in Limited, should default to zero",
    56  			original: &flowcontrolv1.PriorityLevelConfiguration{
    57  				Spec: flowcontrolv1.PriorityLevelConfigurationSpec{
    58  					Type: flowcontrolv1.PriorityLevelEnablementLimited,
    59  					Limited: &flowcontrolv1.LimitedPriorityLevelConfiguration{
    60  						NominalConcurrencyShares: ptr.To(int32(5)),
    61  						LimitResponse: flowcontrolv1.LimitResponse{
    62  							Type: flowcontrolv1.LimitResponseTypeReject,
    63  						},
    64  					},
    65  				},
    66  			},
    67  			expected: &flowcontrolv1.PriorityLevelConfiguration{
    68  				Spec: flowcontrolv1.PriorityLevelConfigurationSpec{
    69  					Type: flowcontrolv1.PriorityLevelEnablementLimited,
    70  					Limited: &flowcontrolv1.LimitedPriorityLevelConfiguration{
    71  						NominalConcurrencyShares: ptr.To(int32(5)),
    72  						LendablePercent:          ptr.To(int32(0)),
    73  						LimitResponse: flowcontrolv1.LimitResponse{
    74  							Type: flowcontrolv1.LimitResponseTypeReject,
    75  						},
    76  					},
    77  				},
    78  			},
    79  		},
    80  		{
    81  			name: "NominalConcurrencyShares is not specified in Limited, should default to 30",
    82  			original: &flowcontrolv1.PriorityLevelConfiguration{
    83  				Spec: flowcontrolv1.PriorityLevelConfigurationSpec{
    84  					Type: flowcontrolv1.PriorityLevelEnablementLimited,
    85  					Limited: &flowcontrolv1.LimitedPriorityLevelConfiguration{
    86  						NominalConcurrencyShares: nil,
    87  						LimitResponse: flowcontrolv1.LimitResponse{
    88  							Type: flowcontrolv1.LimitResponseTypeReject,
    89  						},
    90  					},
    91  				},
    92  			},
    93  			expected: &flowcontrolv1.PriorityLevelConfiguration{
    94  				Spec: flowcontrolv1.PriorityLevelConfigurationSpec{
    95  					Type: flowcontrolv1.PriorityLevelEnablementLimited,
    96  					Limited: &flowcontrolv1.LimitedPriorityLevelConfiguration{
    97  						NominalConcurrencyShares: ptr.To(int32(PriorityLevelConfigurationDefaultNominalConcurrencyShares)),
    98  						LendablePercent:          ptr.To(int32(0)),
    99  						LimitResponse: flowcontrolv1.LimitResponse{
   100  							Type: flowcontrolv1.LimitResponseTypeReject,
   101  						},
   102  					},
   103  				},
   104  			},
   105  		},
   106  		{
   107  			name: "NominalConcurrencyShares is set to zero in Limited, no defaulting should be applied",
   108  			original: &flowcontrolv1.PriorityLevelConfiguration{
   109  				Spec: flowcontrolv1.PriorityLevelConfigurationSpec{
   110  					Type: flowcontrolv1.PriorityLevelEnablementLimited,
   111  					Limited: &flowcontrolv1.LimitedPriorityLevelConfiguration{
   112  						NominalConcurrencyShares: ptr.To(int32(0)),
   113  						LimitResponse: flowcontrolv1.LimitResponse{
   114  							Type: flowcontrolv1.LimitResponseTypeReject,
   115  						},
   116  					},
   117  				},
   118  			},
   119  			expected: &flowcontrolv1.PriorityLevelConfiguration{
   120  				Spec: flowcontrolv1.PriorityLevelConfigurationSpec{
   121  					Type: flowcontrolv1.PriorityLevelEnablementLimited,
   122  					Limited: &flowcontrolv1.LimitedPriorityLevelConfiguration{
   123  						NominalConcurrencyShares: ptr.To(int32(0)),
   124  						LendablePercent:          ptr.To(int32(0)),
   125  						LimitResponse: flowcontrolv1.LimitResponse{
   126  							Type: flowcontrolv1.LimitResponseTypeReject,
   127  						},
   128  					},
   129  				},
   130  			},
   131  		},
   132  	}
   133  
   134  	scheme := runtime.NewScheme()
   135  	if err := AddToScheme(scheme); err != nil {
   136  		t.Fatalf("Failed to add to scheme: %v", err)
   137  	}
   138  
   139  	for _, test := range tests {
   140  		t.Run(test.name, func(t *testing.T) {
   141  			original := test.original
   142  			expected := test.expected
   143  
   144  			scheme.Default(original)
   145  			if !reflect.DeepEqual(expected, original) {
   146  				t.Errorf("Expected defaulting to work - diff: %s", cmp.Diff(expected, original))
   147  			}
   148  		})
   149  	}
   150  }
   151  

View as plain text