|
| 1 | +/* |
| 2 | +Copyright 2025 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 metrics |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/sirupsen/logrus" |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "sigs.k8s.io/external-dns/internal/testutils" |
| 25 | +) |
| 26 | + |
| 27 | +func TestNewLabels(t *testing.T) { |
| 28 | + tests := []struct { |
| 29 | + name string |
| 30 | + labelNames []string |
| 31 | + expectedLabelNames []string |
| 32 | + }{ |
| 33 | + { |
| 34 | + name: "NewLabels initializes Values with labelNames", |
| 35 | + labelNames: []string{"label1", "label2"}, |
| 36 | + expectedLabelNames: []string{"label1", "label2"}, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "NewLabels sets labelNames as lower-case", |
| 40 | + labelNames: []string{"LABEL1", "label2"}, |
| 41 | + expectedLabelNames: []string{"label1", "label2"}, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + for _, tt := range tests { |
| 46 | + t.Run(tt.name, func(t *testing.T) { |
| 47 | + labels := NewLabels(tt.labelNames) |
| 48 | + keys := labels.GetKeysInOrder() |
| 49 | + |
| 50 | + assert.Equal(t, tt.expectedLabelNames, keys) |
| 51 | + }) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestLabelsWithOptions(t *testing.T) { |
| 56 | + tests := []struct { |
| 57 | + name string |
| 58 | + labelNames []string |
| 59 | + options []LabelOption |
| 60 | + expectedValuesMap map[string]string |
| 61 | + }{ |
| 62 | + { |
| 63 | + name: "WithOptions sets label values", |
| 64 | + labelNames: []string{"label1", "label2"}, |
| 65 | + options: []LabelOption{ |
| 66 | + WithLabel("label1", "alpha"), |
| 67 | + WithLabel("label2", "beta"), |
| 68 | + }, |
| 69 | + expectedValuesMap: map[string]string{ |
| 70 | + "label1": "alpha", |
| 71 | + "label2": "beta", |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + for _, tt := range tests { |
| 77 | + t.Run(tt.name, func(t *testing.T) { |
| 78 | + labels := NewLabels(tt.labelNames) |
| 79 | + labels.WithOptions(tt.options...) |
| 80 | + |
| 81 | + assert.Equal(t, tt.expectedValuesMap, labels.values) |
| 82 | + }) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestLabelsWithLabel(t *testing.T) { |
| 87 | + tests := []struct { |
| 88 | + name string |
| 89 | + labelNames []string |
| 90 | + labelName string |
| 91 | + labelValue string |
| 92 | + expectedLabels *Labels |
| 93 | + expectedErrorLog string |
| 94 | + }{ |
| 95 | + { |
| 96 | + name: "WithLabel sets label and value", |
| 97 | + labelNames: []string{"label1"}, |
| 98 | + labelName: "label1", |
| 99 | + labelValue: "alpha", |
| 100 | + expectedLabels: &Labels{ |
| 101 | + values: map[string]string{ |
| 102 | + "label1": "alpha", |
| 103 | + }}, |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "WithLabel sets labelName to lowercase", |
| 107 | + labelNames: []string{"label1"}, |
| 108 | + labelName: "LABEL1", |
| 109 | + labelValue: "alpha", |
| 110 | + expectedLabels: &Labels{ |
| 111 | + values: map[string]string{ |
| 112 | + "label1": "alpha", |
| 113 | + }}, |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "WithLabel errors if label doesn't exist", |
| 117 | + labelNames: []string{"label1"}, |
| 118 | + labelName: "notreal", |
| 119 | + labelValue: "", |
| 120 | + expectedErrorLog: "Attempting to set a value for a label that doesn't exist! 'notreal' does not exist!", |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + for _, tt := range tests { |
| 125 | + t.Run(tt.name, func(t *testing.T) { |
| 126 | + hook := testutils.LogsUnderTestWithLogLevel(logrus.WarnLevel, t) |
| 127 | + |
| 128 | + labels := NewLabels(tt.labelNames) |
| 129 | + labels.WithOptions(WithLabel(tt.labelName, tt.labelValue)) |
| 130 | + |
| 131 | + if tt.expectedLabels != nil { |
| 132 | + assert.Equal(t, tt.expectedLabels, labels) |
| 133 | + } |
| 134 | + |
| 135 | + if tt.expectedErrorLog != "" { |
| 136 | + testutils.TestHelperLogContains(tt.expectedErrorLog, hook, t) |
| 137 | + } |
| 138 | + }) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func TestLabelsGetKeysInOrder(t *testing.T) { |
| 143 | + tests := []struct { |
| 144 | + name string |
| 145 | + labels *Labels |
| 146 | + expectedKeysInOrder []string |
| 147 | + }{ |
| 148 | + { |
| 149 | + "GetKeysInOrder returns keys in alphabetical order", |
| 150 | + NewLabels([]string{"label2", "label1"}), |
| 151 | + []string{"label1", "label2"}, |
| 152 | + }, |
| 153 | + } |
| 154 | + |
| 155 | + for _, tt := range tests { |
| 156 | + t.Run(tt.name, func(t *testing.T) { |
| 157 | + orderedKeys := tt.labels.GetKeysInOrder() |
| 158 | + |
| 159 | + assert.Equal(t, tt.expectedKeysInOrder, orderedKeys) |
| 160 | + }) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +func TestLabelsGetValuesOrderedByKey(t *testing.T) { |
| 165 | + tests := []struct { |
| 166 | + name string |
| 167 | + labels *Labels |
| 168 | + labelOptions []LabelOption |
| 169 | + expectedValuesInOrder []string |
| 170 | + }{ |
| 171 | + { |
| 172 | + "GetKeysInOrder returns keys in alphabetical order", |
| 173 | + NewLabels([]string{"label1", "label2"}), |
| 174 | + []LabelOption{ |
| 175 | + WithLabel("label2", "beta"), |
| 176 | + WithLabel("label1", "alpha"), |
| 177 | + }, |
| 178 | + []string{"alpha", "beta"}, |
| 179 | + }, |
| 180 | + } |
| 181 | + |
| 182 | + for _, tt := range tests { |
| 183 | + t.Run(tt.name, func(t *testing.T) { |
| 184 | + tt.labels.WithOptions(tt.labelOptions...) |
| 185 | + |
| 186 | + orderedValues := tt.labels.GetValuesOrderedByKey() |
| 187 | + |
| 188 | + assert.Equal(t, tt.expectedValuesInOrder, orderedValues) |
| 189 | + }) |
| 190 | + } |
| 191 | +} |
0 commit comments