|
| 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 noderesourcetopology |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + "k8s.io/apimachinery/pkg/api/resource" |
| 26 | +) |
| 27 | + |
| 28 | +func TestGetID(t *testing.T) { |
| 29 | + testCases := []struct { |
| 30 | + description string |
| 31 | + name string |
| 32 | + expectedID int |
| 33 | + expectedErr error |
| 34 | + }{ |
| 35 | + { |
| 36 | + description: "id equals 1", |
| 37 | + name: "node-1", |
| 38 | + expectedID: 1, |
| 39 | + }, |
| 40 | + { |
| 41 | + description: "id equals 10", |
| 42 | + name: "node-10", |
| 43 | + expectedID: 10, |
| 44 | + }, |
| 45 | + { |
| 46 | + description: "invalid format of name, node name without hyphen", |
| 47 | + name: "node0", |
| 48 | + expectedErr: fmt.Errorf("invalid zone format"), |
| 49 | + }, |
| 50 | + { |
| 51 | + description: "invalid format of name, zone instead of node", |
| 52 | + name: "zone-10", |
| 53 | + expectedErr: fmt.Errorf("invalid zone format"), |
| 54 | + }, |
| 55 | + { |
| 56 | + description: "invalid format of name, suffix is not an integer", |
| 57 | + name: "node-a10a", |
| 58 | + expectedErr: fmt.Errorf("invalid zone format"), |
| 59 | + }, |
| 60 | + { |
| 61 | + description: "invalid format of name, suffix is not an integer", |
| 62 | + name: "node-10a", |
| 63 | + expectedErr: fmt.Errorf("invalid zone format"), |
| 64 | + }, |
| 65 | + { |
| 66 | + description: "invalid numaID range", |
| 67 | + name: "node-10123412415115114", |
| 68 | + expectedErr: fmt.Errorf("invalid NUMA id range"), |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + for _, testCase := range testCases { |
| 73 | + t.Run(testCase.description, func(t *testing.T) { |
| 74 | + id, err := getID(testCase.name) |
| 75 | + if testCase.expectedErr == nil { |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("expected err to be nil not %v", err) |
| 78 | + } |
| 79 | + |
| 80 | + if id != testCase.expectedID { |
| 81 | + t.Fatalf("expected id to equal %d not %d", testCase.expectedID, id) |
| 82 | + } |
| 83 | + } else { |
| 84 | + fmt.Println(id) |
| 85 | + if !strings.Contains(err.Error(), testCase.expectedErr.Error()) { |
| 86 | + t.Fatalf("expected err: %v to contain %s", err, testCase.expectedErr) |
| 87 | + } |
| 88 | + } |
| 89 | + }) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func TestOnlyNonNUMAResources(t *testing.T) { |
| 94 | + numaNodes := NUMANodeList{ |
| 95 | + { |
| 96 | + NUMAID: 0, |
| 97 | + Resources: corev1.ResourceList{ |
| 98 | + corev1.ResourceCPU: *resource.NewQuantity(8, resource.DecimalSI), |
| 99 | + corev1.ResourceMemory: resource.MustParse("10Gi"), |
| 100 | + "gpu": resource.MustParse("1"), |
| 101 | + }, |
| 102 | + }, |
| 103 | + { |
| 104 | + NUMAID: 1, |
| 105 | + Resources: corev1.ResourceList{ |
| 106 | + corev1.ResourceCPU: *resource.NewQuantity(8, resource.DecimalSI), |
| 107 | + corev1.ResourceMemory: resource.MustParse("10Gi"), |
| 108 | + "nic": resource.MustParse("1"), |
| 109 | + }, |
| 110 | + }, |
| 111 | + } |
| 112 | + testCases := []struct { |
| 113 | + description string |
| 114 | + resources corev1.ResourceList |
| 115 | + expected bool |
| 116 | + }{ |
| 117 | + { |
| 118 | + description: "all resources missing in NUMANodeList", |
| 119 | + resources: corev1.ResourceList{ |
| 120 | + "resource1": resource.MustParse("1"), |
| 121 | + "resource2": resource.MustParse("1"), |
| 122 | + }, |
| 123 | + expected: true, |
| 124 | + }, |
| 125 | + { |
| 126 | + description: "resource is present in both NUMA nodes", |
| 127 | + resources: corev1.ResourceList{ |
| 128 | + corev1.ResourceCPU: resource.MustParse("1"), |
| 129 | + }, |
| 130 | + expected: false, |
| 131 | + }, |
| 132 | + { |
| 133 | + description: "more than resource is present in both NUMA nodes", |
| 134 | + resources: corev1.ResourceList{ |
| 135 | + corev1.ResourceCPU: resource.MustParse("1"), |
| 136 | + corev1.ResourceMemory: resource.MustParse("1"), |
| 137 | + }, |
| 138 | + expected: false, |
| 139 | + }, |
| 140 | + { |
| 141 | + description: "resource is present only in NUMA node 0", |
| 142 | + resources: corev1.ResourceList{ |
| 143 | + "gpu": resource.MustParse("1"), |
| 144 | + }, |
| 145 | + expected: false, |
| 146 | + }, |
| 147 | + { |
| 148 | + description: "resource is present only in NUMA node 1", |
| 149 | + resources: corev1.ResourceList{ |
| 150 | + "nic": resource.MustParse("1"), |
| 151 | + }, |
| 152 | + expected: false, |
| 153 | + }, |
| 154 | + { |
| 155 | + description: "two distinct resources from different NUMA nodes", |
| 156 | + resources: corev1.ResourceList{ |
| 157 | + "nic": resource.MustParse("1"), |
| 158 | + "gpu": resource.MustParse("1"), |
| 159 | + }, |
| 160 | + expected: false, |
| 161 | + }, |
| 162 | + } |
| 163 | + for _, testCase := range testCases { |
| 164 | + t.Run(testCase.description, func(t *testing.T) { |
| 165 | + result := onlyNonNUMAResources(numaNodes, testCase.resources) |
| 166 | + if result != testCase.expected { |
| 167 | + t.Fatalf("expected %t to equal %t", result, testCase.expected) |
| 168 | + } |
| 169 | + }) |
| 170 | + } |
| 171 | +} |
0 commit comments