@@ -19,14 +19,17 @@ package controllers
19
19
import (
20
20
"context"
21
21
"reflect"
22
+ "strings"
22
23
"testing"
24
+ "time"
23
25
24
26
"github.com/go-logr/logr"
25
27
"helm.sh/helm/v3/pkg/chartutil"
26
28
corev1 "k8s.io/api/core/v1"
27
29
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
28
30
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29
31
"k8s.io/apimachinery/pkg/runtime"
32
+ "sigs.k8s.io/controller-runtime/pkg/client"
30
33
"sigs.k8s.io/controller-runtime/pkg/client/fake"
31
34
"sigs.k8s.io/yaml"
32
35
@@ -278,6 +281,171 @@ invalid`,
278
281
}
279
282
}
280
283
284
+ func TestValuesReferenceValidation (t * testing.T ) {
285
+ tests := []struct {
286
+ name string
287
+ references []v2.ValuesReference
288
+ wantErr bool
289
+ }{
290
+ {
291
+ name : "valid ValuesKey" ,
292
+ references : []v2.ValuesReference {
293
+ {
294
+ Kind : "Secret" ,
295
+ Name : "values" ,
296
+ ValuesKey : "any-key_na.me" ,
297
+ },
298
+ },
299
+ wantErr : false ,
300
+ },
301
+ {
302
+ name : "valid ValuesKey: empty" ,
303
+ references : []v2.ValuesReference {
304
+ {
305
+ Kind : "Secret" ,
306
+ Name : "values" ,
307
+ ValuesKey : "" ,
308
+ },
309
+ },
310
+ wantErr : false ,
311
+ },
312
+ {
313
+ name : "valid ValuesKey: long" ,
314
+ references : []v2.ValuesReference {
315
+ {
316
+ Kind : "Secret" ,
317
+ Name : "values" ,
318
+ ValuesKey : strings .Repeat ("a" , 253 ),
319
+ },
320
+ },
321
+ wantErr : false ,
322
+ },
323
+ {
324
+ name : "invalid ValuesKey" ,
325
+ references : []v2.ValuesReference {
326
+ {
327
+ Kind : "Secret" ,
328
+ Name : "values" ,
329
+ ValuesKey : "a($&^%b" ,
330
+ },
331
+ },
332
+ wantErr : true ,
333
+ },
334
+ {
335
+ name : "invalid ValuesKey: too long" ,
336
+ references : []v2.ValuesReference {
337
+ {
338
+ Kind : "Secret" ,
339
+ Name : "values" ,
340
+ ValuesKey : strings .Repeat ("a" , 254 ),
341
+ },
342
+ },
343
+ wantErr : true ,
344
+ },
345
+ {
346
+ name : "valid target path: empty" ,
347
+ references : []v2.ValuesReference {
348
+ {
349
+ Kind : "Secret" ,
350
+ Name : "values" ,
351
+ TargetPath : "" ,
352
+ },
353
+ },
354
+ wantErr : false ,
355
+ },
356
+ {
357
+ name : "valid target path" ,
358
+ references : []v2.ValuesReference {
359
+ {
360
+ Kind : "Secret" ,
361
+ Name : "values" ,
362
+ TargetPath : "list_with.nested-values.and.index[0]" ,
363
+ },
364
+ },
365
+ wantErr : false ,
366
+ },
367
+ {
368
+ name : "valid target path: long" ,
369
+ references : []v2.ValuesReference {
370
+ {
371
+ Kind : "Secret" ,
372
+ Name : "values" ,
373
+ TargetPath : strings .Repeat ("a" , 250 ),
374
+ },
375
+ },
376
+ wantErr : false ,
377
+ },
378
+ {
379
+ name : "invalid target path: too long" ,
380
+ references : []v2.ValuesReference {
381
+ {
382
+ Kind : "Secret" ,
383
+ Name : "values" ,
384
+ TargetPath : strings .Repeat ("a" , 251 ),
385
+ },
386
+ },
387
+ wantErr : true ,
388
+ },
389
+ {
390
+ name : "invalid target path: opened index" ,
391
+ references : []v2.ValuesReference {
392
+ {
393
+ Kind : "Secret" ,
394
+ Name : "values" ,
395
+ ValuesKey : "single" ,
396
+ TargetPath : "a[" ,
397
+ },
398
+ },
399
+ wantErr : true ,
400
+ },
401
+ {
402
+ name : "invalid target path: incorrect index syntax" ,
403
+ references : []v2.ValuesReference {
404
+ {
405
+ Kind : "Secret" ,
406
+ Name : "values" ,
407
+ ValuesKey : "single" ,
408
+ TargetPath : "a]0[" ,
409
+ },
410
+ },
411
+ wantErr : true ,
412
+ },
413
+ }
414
+
415
+ for _ , tt := range tests {
416
+ t .Run (tt .name , func (t * testing.T ) {
417
+ var values * apiextensionsv1.JSON
418
+ v , _ := yaml .YAMLToJSON ([]byte ("values" ))
419
+ values = & apiextensionsv1.JSON {Raw : v }
420
+
421
+ hr := v2.HelmRelease {
422
+ ObjectMeta : metav1.ObjectMeta {
423
+ Name : "test" ,
424
+ Namespace : "default" ,
425
+ },
426
+ Spec : v2.HelmReleaseSpec {
427
+ Interval : metav1.Duration {Duration : 5 * time .Minute },
428
+ Chart : v2.HelmChartTemplate {
429
+ Spec : v2.HelmChartTemplateSpec {
430
+ SourceRef : v2.CrossNamespaceObjectReference {
431
+ Name : "something" ,
432
+ },
433
+ },
434
+ },
435
+ ValuesFrom : tt .references ,
436
+ Values : values ,
437
+ },
438
+ }
439
+
440
+ err := k8sClient .Create (context .TODO (), & hr , client .DryRunAll )
441
+ if (err != nil ) != tt .wantErr {
442
+ t .Errorf ("composeValues() error = %v, wantErr %v" , err , tt .wantErr )
443
+ return
444
+ }
445
+ })
446
+ }
447
+ }
448
+
281
449
func valuesSecret (name string , data map [string ][]byte ) * corev1.Secret {
282
450
return & corev1.Secret {
283
451
ObjectMeta : metav1.ObjectMeta {Name : name },
0 commit comments