k8s.io/kubernetes@v1.29.3/pkg/kubelet/cm/cpumanager/policy_options_test.go (about) 1 /* 2 Copyright 2021 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 cpumanager 18 19 import ( 20 "testing" 21 22 utilfeature "k8s.io/apiserver/pkg/util/feature" 23 "k8s.io/component-base/featuregate" 24 featuregatetesting "k8s.io/component-base/featuregate/testing" 25 pkgfeatures "k8s.io/kubernetes/pkg/features" 26 "k8s.io/kubernetes/pkg/kubelet/cm/cpumanager/topology" 27 "k8s.io/kubernetes/pkg/kubelet/cm/topologymanager" 28 ) 29 30 type optionAvailTest struct { 31 option string 32 featureGate featuregate.Feature 33 featureGateEnable bool 34 expectedAvailable bool 35 } 36 37 func TestPolicyDefaultsAvailable(t *testing.T) { 38 testCases := []optionAvailTest{ 39 { 40 option: "this-option-does-not-exist", 41 expectedAvailable: false, 42 }, 43 { 44 option: FullPCPUsOnlyOption, 45 expectedAvailable: true, 46 }, 47 } 48 for _, testCase := range testCases { 49 t.Run(testCase.option, func(t *testing.T) { 50 err := CheckPolicyOptionAvailable(testCase.option) 51 isEnabled := (err == nil) 52 if isEnabled != testCase.expectedAvailable { 53 t.Errorf("option %q available got=%v expected=%v", testCase.option, isEnabled, testCase.expectedAvailable) 54 } 55 }) 56 } 57 } 58 59 func TestPolicyOptionsAvailable(t *testing.T) { 60 testCases := []optionAvailTest{ 61 { 62 option: "this-option-does-not-exist", 63 featureGate: pkgfeatures.CPUManagerPolicyBetaOptions, 64 featureGateEnable: false, 65 expectedAvailable: false, 66 }, 67 { 68 option: "this-option-does-not-exist", 69 featureGate: pkgfeatures.CPUManagerPolicyBetaOptions, 70 featureGateEnable: true, 71 expectedAvailable: false, 72 }, 73 { 74 option: FullPCPUsOnlyOption, 75 featureGate: pkgfeatures.CPUManagerPolicyBetaOptions, 76 featureGateEnable: true, 77 expectedAvailable: true, 78 }, 79 { 80 option: FullPCPUsOnlyOption, 81 featureGate: pkgfeatures.CPUManagerPolicyBetaOptions, 82 featureGateEnable: false, 83 expectedAvailable: false, 84 }, 85 { 86 option: AlignBySocketOption, 87 featureGate: pkgfeatures.CPUManagerPolicyAlphaOptions, 88 featureGateEnable: true, 89 expectedAvailable: true, 90 }, 91 { 92 option: AlignBySocketOption, 93 featureGate: pkgfeatures.CPUManagerPolicyBetaOptions, 94 featureGateEnable: true, 95 expectedAvailable: false, 96 }, 97 } 98 for _, testCase := range testCases { 99 t.Run(testCase.option, func(t *testing.T) { 100 defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, testCase.featureGate, testCase.featureGateEnable)() 101 err := CheckPolicyOptionAvailable(testCase.option) 102 isEnabled := (err == nil) 103 if isEnabled != testCase.expectedAvailable { 104 t.Errorf("option %q available got=%v expected=%v", testCase.option, isEnabled, testCase.expectedAvailable) 105 } 106 }) 107 } 108 } 109 110 func TestValidateStaticPolicyOptions(t *testing.T) { 111 testCases := []struct { 112 description string 113 policyOption map[string]string 114 topology *topology.CPUTopology 115 topoMgrPolicy string 116 expectedErr bool 117 }{ 118 { 119 description: "Align by socket not enabled", 120 policyOption: map[string]string{FullPCPUsOnlyOption: "true"}, 121 topology: topoDualSocketMultiNumaPerSocketHT, 122 topoMgrPolicy: topologymanager.PolicySingleNumaNode, 123 expectedErr: false, 124 }, 125 { 126 description: "Align by socket enabled with topology manager single numa node", 127 policyOption: map[string]string{AlignBySocketOption: "true"}, 128 topology: topoDualSocketMultiNumaPerSocketHT, 129 topoMgrPolicy: topologymanager.PolicySingleNumaNode, 130 expectedErr: true, 131 }, 132 { 133 description: "Align by socket enabled with num_sockets > num_numa", 134 policyOption: map[string]string{AlignBySocketOption: "true"}, 135 topology: fakeTopoMultiSocketDualSocketPerNumaHT, 136 topoMgrPolicy: topologymanager.PolicyNone, 137 expectedErr: true, 138 }, 139 { 140 description: "Align by socket enabled: with topology manager None policy", 141 policyOption: map[string]string{AlignBySocketOption: "true"}, 142 topology: topoDualSocketMultiNumaPerSocketHT, 143 topoMgrPolicy: topologymanager.PolicyNone, 144 expectedErr: false, 145 }, 146 { 147 description: "Align by socket enabled: with topology manager best-effort policy", 148 policyOption: map[string]string{AlignBySocketOption: "true"}, 149 topology: topoDualSocketMultiNumaPerSocketHT, 150 topoMgrPolicy: topologymanager.PolicyBestEffort, 151 expectedErr: false, 152 }, 153 { 154 description: "Align by socket enabled: with topology manager restricted policy", 155 policyOption: map[string]string{AlignBySocketOption: "true"}, 156 topology: topoDualSocketMultiNumaPerSocketHT, 157 topoMgrPolicy: topologymanager.PolicyRestricted, 158 expectedErr: false, 159 }, 160 } 161 for _, testCase := range testCases { 162 t.Run(testCase.description, func(t *testing.T) { 163 topoMgrPolicy := topologymanager.NewNonePolicy() 164 if testCase.topoMgrPolicy == topologymanager.PolicySingleNumaNode { 165 topoMgrPolicy = topologymanager.NewSingleNumaNodePolicy(&topologymanager.NUMAInfo{}, topologymanager.PolicyOptions{}) 166 167 } 168 topoMgrStore := topologymanager.NewFakeManagerWithPolicy(topoMgrPolicy) 169 170 defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, pkgfeatures.CPUManagerPolicyAlphaOptions, true)() 171 policyOpt, _ := NewStaticPolicyOptions(testCase.policyOption) 172 err := ValidateStaticPolicyOptions(policyOpt, testCase.topology, topoMgrStore) 173 gotError := (err != nil) 174 if gotError != testCase.expectedErr { 175 t.Errorf("testCase %q failed, got %v expected %v", testCase.description, gotError, testCase.expectedErr) 176 } 177 }) 178 } 179 }