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