|
| 1 | +/* |
| 2 | +Copyright 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 v1alpha1 |
| 18 | + |
| 19 | +import ( |
| 20 | + "strings" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/pfeifferj/karpenter-provider-ibm-cloud/pkg/apis/constants" |
| 24 | + v1 "sigs.k8s.io/karpenter/pkg/apis/v1" |
| 25 | +) |
| 26 | + |
| 27 | +func TestLabelConstants(t *testing.T) { |
| 28 | + tests := []struct { |
| 29 | + name string |
| 30 | + label string |
| 31 | + expected string |
| 32 | + }{ |
| 33 | + {"InstanceSizeLabelKey", InstanceSizeLabelKey, constants.Group + "/instance-size"}, |
| 34 | + {"InstanceFamilyLabelKey", InstanceFamilyLabelKey, constants.Group + "/instance-family"}, |
| 35 | + {"InstanceMemoryLabelKey", InstanceMemoryLabelKey, constants.Group + "/instance-memory"}, |
| 36 | + {"InstanceCPULabelKey", InstanceCPULabelKey, constants.Group + "/instance-cpu"}, |
| 37 | + } |
| 38 | + |
| 39 | + for _, tt := range tests { |
| 40 | + t.Run(tt.name, func(t *testing.T) { |
| 41 | + if tt.label != tt.expected { |
| 42 | + t.Errorf("%s = %v, want %v", tt.name, tt.label, tt.expected) |
| 43 | + } |
| 44 | + }) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestInternalLabelConstants(t *testing.T) { |
| 49 | + tests := []struct { |
| 50 | + name string |
| 51 | + label string |
| 52 | + expected string |
| 53 | + }{ |
| 54 | + {"IbmLabelKey", IbmLabelKey, "ibm.x-k8s.io/node"}, |
| 55 | + {"IbmLabelValue", IbmLabelValue, "fake"}, |
| 56 | + {"NodeViewerLabelKey", NodeViewerLabelKey, "eks-node-viewer/instance-price"}, |
| 57 | + {"IbmPartitionLabelKey", IbmPartitionLabelKey, "ibm-partition"}, |
| 58 | + } |
| 59 | + |
| 60 | + for _, tt := range tests { |
| 61 | + t.Run(tt.name, func(t *testing.T) { |
| 62 | + if tt.label != tt.expected { |
| 63 | + t.Errorf("%s = %v, want %v", tt.name, tt.label, tt.expected) |
| 64 | + } |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestInstanceLabelsHaveGroupPrefix(t *testing.T) { |
| 70 | + instanceLabels := []string{ |
| 71 | + InstanceSizeLabelKey, |
| 72 | + InstanceFamilyLabelKey, |
| 73 | + InstanceMemoryLabelKey, |
| 74 | + InstanceCPULabelKey, |
| 75 | + } |
| 76 | + |
| 77 | + for _, label := range instanceLabels { |
| 78 | + if !strings.HasPrefix(label, constants.Group) { |
| 79 | + t.Errorf("Instance label %v should start with group prefix %v", label, constants.Group) |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestLabelsNotEmpty(t *testing.T) { |
| 85 | + labels := []string{ |
| 86 | + InstanceSizeLabelKey, |
| 87 | + InstanceFamilyLabelKey, |
| 88 | + InstanceMemoryLabelKey, |
| 89 | + InstanceCPULabelKey, |
| 90 | + IbmLabelKey, |
| 91 | + IbmLabelValue, |
| 92 | + NodeViewerLabelKey, |
| 93 | + IbmPartitionLabelKey, |
| 94 | + } |
| 95 | + |
| 96 | + for _, label := range labels { |
| 97 | + if label == "" { |
| 98 | + t.Error("Label constant should not be empty") |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestInitFunctionRegistersLabels(t *testing.T) { |
| 104 | + // Test that the init function properly registers the group in RestrictedLabelDomains |
| 105 | + if !v1.RestrictedLabelDomains.Has(constants.Group) { |
| 106 | + t.Errorf("RestrictedLabelDomains should contain %v", constants.Group) |
| 107 | + } |
| 108 | + |
| 109 | + // Test that instance labels are registered in WellKnownLabels |
| 110 | + expectedWellKnownLabels := []string{ |
| 111 | + InstanceSizeLabelKey, |
| 112 | + InstanceFamilyLabelKey, |
| 113 | + InstanceCPULabelKey, |
| 114 | + InstanceMemoryLabelKey, |
| 115 | + } |
| 116 | + |
| 117 | + for _, label := range expectedWellKnownLabels { |
| 118 | + if !v1.WellKnownLabels.Has(label) { |
| 119 | + t.Errorf("WellKnownLabels should contain %v", label) |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments