k8s.io/kubernetes@v1.29.3/pkg/kubelet/apis/config/validation/validation_reserved_memory_test.go (about) 1 /* 2 Copyright 2020 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 validation 18 19 import ( 20 "fmt" 21 "testing" 22 23 v1 "k8s.io/api/core/v1" 24 "k8s.io/apimachinery/pkg/api/resource" 25 kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config" 26 ) 27 28 func TestValidateReservedMemoryConfiguration(t *testing.T) { 29 testCases := []struct { 30 description string 31 kubeletConfiguration *kubeletconfig.KubeletConfiguration 32 expectedError error 33 }{ 34 { 35 description: "The kubelet configuration does not have reserved memory parameter", 36 kubeletConfiguration: &kubeletconfig.KubeletConfiguration{}, 37 expectedError: nil, 38 }, 39 { 40 description: "The kubelet configuration has valid reserved memory parameter", 41 kubeletConfiguration: &kubeletconfig.KubeletConfiguration{ 42 ReservedMemory: []kubeletconfig.MemoryReservation{ 43 { 44 NumaNode: 0, 45 Limits: v1.ResourceList{ 46 v1.ResourceMemory: *resource.NewQuantity(128, resource.DecimalSI), 47 }, 48 }, 49 }, 50 }, 51 expectedError: nil, 52 }, 53 { 54 description: "The reserved memory has duplications for the NUMA node and limit type", 55 kubeletConfiguration: &kubeletconfig.KubeletConfiguration{ 56 ReservedMemory: []kubeletconfig.MemoryReservation{ 57 { 58 NumaNode: 0, 59 Limits: v1.ResourceList{ 60 v1.ResourceMemory: *resource.NewQuantity(128, resource.DecimalSI), 61 }, 62 }, 63 { 64 NumaNode: 0, 65 Limits: v1.ResourceList{ 66 v1.ResourceMemory: *resource.NewQuantity(64, resource.DecimalSI), 67 }, 68 }, 69 }, 70 }, 71 expectedError: fmt.Errorf("invalid configuration: the reserved memory has a duplicate value for NUMA node %d and resource %q", 0, v1.ResourceMemory), 72 }, 73 { 74 description: "The reserved memory has unsupported limit type", 75 kubeletConfiguration: &kubeletconfig.KubeletConfiguration{ 76 ReservedMemory: []kubeletconfig.MemoryReservation{ 77 { 78 NumaNode: 0, 79 Limits: v1.ResourceList{ 80 "blabla": *resource.NewQuantity(128, resource.DecimalSI), 81 }, 82 }, 83 }, 84 }, 85 expectedError: fmt.Errorf("invalid configuration: the limit type %q for NUMA node %d is not supported, only [memory hugepages-<HugePageSize>] is accepted", "blabla", 0), 86 }, 87 { 88 description: "The reserved memory has limit type with zero value", 89 kubeletConfiguration: &kubeletconfig.KubeletConfiguration{ 90 ReservedMemory: []kubeletconfig.MemoryReservation{ 91 { 92 NumaNode: 0, 93 Limits: v1.ResourceList{ 94 v1.ResourceMemory: *resource.NewQuantity(0, resource.DecimalSI), 95 }, 96 }, 97 }, 98 }, 99 expectedError: fmt.Errorf("invalid configuration: reserved memory may not be zero for NUMA node %d and resource %q", 0, v1.ResourceMemory), 100 }, 101 } 102 103 for _, testCase := range testCases { 104 errors := validateReservedMemoryConfiguration(testCase.kubeletConfiguration) 105 106 if len(errors) != 0 && testCase.expectedError == nil { 107 t.Errorf("expected errors %v, got %v", errors, testCase.expectedError) 108 } 109 110 if testCase.expectedError != nil { 111 if len(errors) == 0 { 112 t.Errorf("expected error %v, got %v", testCase.expectedError, errors) 113 } 114 115 if errors[0].Error() != testCase.expectedError.Error() { 116 t.Errorf("expected error %v, got %v", testCase.expectedError, errors[0]) 117 } 118 } 119 } 120 }