@@ -231,3 +231,61 @@ func TestAddComponents(t *testing.T) {
231
231
})
232
232
}
233
233
}
234
+
235
+ func TestComponentLabelKey (t * testing.T ) {
236
+ tests := []struct {
237
+ description string
238
+ componentLabelKey string
239
+ name string
240
+ expectedLabel string
241
+ returnsError bool
242
+ }{
243
+ {
244
+ description : "when componentLabelKey is set then return it" ,
245
+ componentLabelKey : "my-component-label" ,
246
+ name : "my-operator" ,
247
+ expectedLabel : "my-component-label" ,
248
+ returnsError : false ,
249
+ },
250
+ {
251
+ description : "when componentLabelKey is not set and operator name is less than 63 characters then do not truncate" ,
252
+ componentLabelKey : "" ,
253
+ name : "my-operator" ,
254
+ expectedLabel : ComponentLabelKeyPrefix + "my-operator" ,
255
+ returnsError : false ,
256
+ },
257
+ {
258
+ description : "when componentLabelKey is not set and operator name is more than 63 characters truncate" ,
259
+ name : "this-is-my-operator-its-the-coolest-you-got-a-problem-with-that-come-at-me-bro" ,
260
+ expectedLabel : ComponentLabelKeyPrefix + "this-is-my-operator-its-the-coolest-you-got-a-problem-with-that" ,
261
+ returnsError : false ,
262
+ },
263
+ {
264
+ description : "when componentLabelKey is not set and operator name is more than 63 characters truncate and drop trailing illegal characters" ,
265
+ name : "this-is-my-operator-its-the-coolest-you-got-a-problem-with----...---___...---" ,
266
+ expectedLabel : ComponentLabelKeyPrefix + "this-is-my-operator-its-the-coolest-you-got-a-problem-with" ,
267
+ returnsError : false ,
268
+ },
269
+ {
270
+ description : "when componentLabelKey is not set and operator name is more than 63 characters and is made up of illegal characters then return error" ,
271
+ name : "----...---___...-------...---___...-------...---___...-------...---___...---" ,
272
+ expectedLabel : "" ,
273
+ returnsError : true ,
274
+ },
275
+ }
276
+
277
+ for _ , tt := range tests {
278
+ operator := & Operator {
279
+ Operator : & operatorsv1.Operator {
280
+ ObjectMeta : metav1.ObjectMeta {
281
+ Name : tt .name ,
282
+ },
283
+ },
284
+ componentLabelKey : tt .componentLabelKey ,
285
+ }
286
+
287
+ actualLabel , actualErr := operator .ComponentLabelKey ()
288
+ require .Equal (t , tt .returnsError , actualErr != nil , actualErr )
289
+ require .Equal (t , tt .expectedLabel , actualLabel )
290
+ }
291
+ }
0 commit comments