github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/kubelet/config/kubeletconfig_test.go (about) 1 /* 2 Copyright 2022 The Katalyst 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 config 18 19 import ( 20 "fmt" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 "k8s.io/apimachinery/pkg/api/resource" 25 kubeletconfigv1beta1 "k8s.io/kubelet/config/v1beta1" 26 "k8s.io/kubernetes/pkg/features" 27 28 "github.com/kubewharf/katalyst-api/pkg/consts" 29 ) 30 31 func TestCheckFeatureGateEnable(t *testing.T) { 32 t.Parallel() 33 34 tests := []struct { 35 name string 36 conf *kubeletconfigv1beta1.KubeletConfiguration 37 features []string 38 enabled bool 39 err error 40 }{ 41 { 42 name: "nil configration", 43 err: fmt.Errorf("nil KubeletConfiguration"), 44 }, 45 { 46 name: "partial enabled", 47 conf: &kubeletconfigv1beta1.KubeletConfiguration{ 48 FeatureGates: map[string]bool{ 49 "a": true, 50 }, 51 }, 52 features: []string{"a", "b"}, 53 enabled: false, 54 err: nil, 55 }, 56 { 57 name: "total enabled", 58 conf: &kubeletconfigv1beta1.KubeletConfiguration{ 59 FeatureGates: map[string]bool{ 60 "a": true, 61 "b": true, 62 "c": false, 63 }, 64 }, 65 features: []string{"a", "b"}, 66 enabled: true, 67 err: nil, 68 }, 69 } 70 71 for _, tt := range tests { 72 tt := tt 73 t.Run(tt.name, func(t *testing.T) { 74 t.Parallel() 75 76 ok, err := CheckFeatureGateEnable(tt.conf, tt.features...) 77 if tt.err == nil { 78 assert.Equal(t, tt.enabled, ok) 79 } else { 80 assert.NotNil(t, err) 81 } 82 }) 83 } 84 } 85 86 func TestGetReservedQuantity(t *testing.T) { 87 t.Parallel() 88 89 tests := []struct { 90 name string 91 conf *kubeletconfigv1beta1.KubeletConfiguration 92 resourceName string 93 resourceQuantity resource.Quantity 94 valid bool 95 err error 96 }{ 97 { 98 name: "nil configration", 99 err: fmt.Errorf("nil KubeletConfiguration"), 100 }, 101 { 102 name: "resource both exists", 103 conf: &kubeletconfigv1beta1.KubeletConfiguration{ 104 KubeReserved: map[string]string{ 105 "cpu": "1024m", 106 }, 107 SystemReserved: map[string]string{ 108 "cpu": "102m", 109 }, 110 }, 111 resourceName: "cpu", 112 resourceQuantity: resource.MustParse("1126m"), 113 valid: true, 114 err: nil, 115 }, 116 { 117 name: "resource only-one exists", 118 conf: &kubeletconfigv1beta1.KubeletConfiguration{ 119 KubeReserved: map[string]string{ 120 "cpu": "1024m", 121 }, 122 }, 123 resourceName: "cpu", 124 resourceQuantity: resource.MustParse("1024m"), 125 valid: true, 126 err: nil, 127 }, 128 { 129 name: "resource not exists", 130 conf: &kubeletconfigv1beta1.KubeletConfiguration{ 131 KubeReserved: map[string]string{ 132 "cpu": "1024m", 133 }, 134 SystemReserved: map[string]string{ 135 "cpu": "102m", 136 }, 137 }, 138 resourceName: "memory", 139 valid: false, 140 err: nil, 141 }, 142 } 143 144 for _, tt := range tests { 145 tt := tt 146 t.Run(tt.name, func(t *testing.T) { 147 t.Parallel() 148 149 q, ok, err := GetReservedQuantity(tt.conf, tt.resourceName) 150 if tt.err == nil { 151 assert.Equal(t, tt.valid, ok) 152 assert.Equal(t, tt.resourceQuantity.Value(), q.Value()) 153 } else { 154 assert.NotNil(t, err) 155 } 156 }) 157 } 158 } 159 160 func TestGetInTreeProviderPolicies(t *testing.T) { 161 t.Parallel() 162 163 tests := []struct { 164 name string 165 conf *kubeletconfigv1beta1.KubeletConfiguration 166 res map[string]string 167 err error 168 }{ 169 { 170 name: "nil configration", 171 err: fmt.Errorf("nil KubeletConfiguration"), 172 }, 173 { 174 name: "cpu only", 175 conf: &kubeletconfigv1beta1.KubeletConfiguration{ 176 FeatureGates: map[string]bool{ 177 string(features.CPUManager): true, 178 }, 179 CPUManagerPolicy: "test-cpu-policy", 180 }, 181 res: map[string]string{ 182 consts.KCNRAnnotationCPUManager: "test-cpu-policy", 183 consts.KCNRAnnotationMemoryManager: string(consts.MemoryManagerOff), 184 }, 185 err: nil, 186 }, 187 { 188 name: "all policies", 189 conf: &kubeletconfigv1beta1.KubeletConfiguration{ 190 FeatureGates: map[string]bool{ 191 string(features.CPUManager): true, 192 string(features.MemoryManager): true, 193 }, 194 CPUManagerPolicy: "test-cpu-policy", 195 MemoryManagerPolicy: "test-memory-policy", 196 }, 197 res: map[string]string{ 198 consts.KCNRAnnotationCPUManager: "test-cpu-policy", 199 consts.KCNRAnnotationMemoryManager: "test-memory-policy", 200 }, 201 err: nil, 202 }, 203 } 204 205 for _, tt := range tests { 206 tt := tt 207 t.Run(tt.name, func(t *testing.T) { 208 t.Parallel() 209 210 res, err := GetInTreeProviderPolicies(tt.conf) 211 if tt.err == nil { 212 assert.Equal(t, tt.res, res) 213 } else { 214 assert.NotNil(t, err) 215 } 216 }) 217 } 218 }