@@ -15,7 +15,9 @@ import (
15
15
batchv1 "k8s.io/api/batch/v1"
16
16
corev1 "k8s.io/api/core/v1"
17
17
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18
+ "k8s.io/apimachinery/pkg/runtime"
18
19
"sigs.k8s.io/controller-runtime/pkg/client"
20
+ "sigs.k8s.io/controller-runtime/pkg/client/fake"
19
21
"sigs.k8s.io/controller-runtime/pkg/envtest"
20
22
)
21
23
@@ -435,3 +437,180 @@ func TestListArtifactsJobForNodes(t *testing.T) {
435
437
})
436
438
}
437
439
}
440
+
441
+ func TestGetArtifactJobForNode_HostCABundle (t * testing.T ) {
442
+ // Test with HostCABundlePath set
443
+ t .Run ("with HostCABundlePath set" , func (t * testing.T ) {
444
+ log := testr .NewWithOptions (t , testr.Options {Verbosity : 10 })
445
+ ctx := logr .NewContext (context .Background (), log )
446
+
447
+ scheme := runtime .NewScheme ()
448
+ require .NoError (t , clusterv1beta1 .AddToScheme (scheme ))
449
+ require .NoError (t , batchv1 .AddToScheme (scheme ))
450
+ require .NoError (t , corev1 .AddToScheme (scheme ))
451
+
452
+ // CA path used for testing
453
+ testCAPath := "/etc/ssl/certs/ca-certificates.crt"
454
+
455
+ // Create a minimal installation CR with RuntimeConfig.HostCABundlePath set
456
+ installation := & clusterv1beta1.Installation {
457
+ ObjectMeta : metav1.ObjectMeta {
458
+ Name : "test-installation" ,
459
+ },
460
+ Spec : clusterv1beta1.InstallationSpec {
461
+ AirGap : true ,
462
+ Artifacts : & clusterv1beta1.ArtifactsLocation {
463
+ Images : "images" ,
464
+ HelmCharts : "helm-charts" ,
465
+ EmbeddedClusterBinary : "embedded-cluster-binary" ,
466
+ EmbeddedClusterMetadata : "embedded-cluster-metadata" ,
467
+ },
468
+ RuntimeConfig : & clusterv1beta1.RuntimeConfigSpec {
469
+ HostCABundlePath : testCAPath ,
470
+ },
471
+ },
472
+ }
473
+
474
+ // Create a fake client
475
+ cli := fake .NewClientBuilder ().
476
+ WithScheme (scheme ).
477
+ WithObjects (installation ).
478
+ Build ()
479
+
480
+ // Create a test node
481
+ node := corev1.Node {
482
+ ObjectMeta : metav1.ObjectMeta {
483
+ Name : "test-node" ,
484
+ },
485
+ }
486
+
487
+ // Call the function under test
488
+ job , err := getArtifactJobForNode (
489
+ ctx , cli , installation , node ,
490
+ "local-artifact-mirror:latest" ,
491
+ "app-slug" ,
492
+ "channel-id" ,
493
+ "1.0.0" ,
494
+ )
495
+ require .NoError (t , err )
496
+
497
+ // Verify that the host CA bundle volume exists
498
+ var hostCABundleVolumeFound bool
499
+ for _ , volume := range job .Spec .Template .Spec .Volumes {
500
+ if volume .Name == "host-ca-bundle" {
501
+ hostCABundleVolumeFound = true
502
+ // Verify the volume properties
503
+ require .NotNil (t , volume .HostPath , "Host CA bundle volume should be a hostPath volume" )
504
+ assert .Equal (t , testCAPath , volume .HostPath .Path , "Host CA bundle path should match RuntimeConfig.HostCABundlePath" )
505
+ assert .Equal (t , corev1 .HostPathFileOrCreate , * volume .HostPath .Type , "Host CA bundle type should be FileOrCreate" )
506
+ break
507
+ }
508
+ }
509
+ assert .True (t , hostCABundleVolumeFound , "Host CA bundle volume should exist" )
510
+
511
+ // Verify that the volume mount exists
512
+ var hostCABundleMountFound bool
513
+ for _ , mount := range job .Spec .Template .Spec .Containers [0 ].VolumeMounts {
514
+ if mount .Name == "host-ca-bundle" {
515
+ hostCABundleMountFound = true
516
+ // Verify the mount properties
517
+ assert .Equal (t , "/certs/ca-certificates.crt" , mount .MountPath , "Host CA bundle mount path should be correct" )
518
+ break
519
+ }
520
+ }
521
+ assert .True (t , hostCABundleMountFound , "Host CA bundle mount should exist" )
522
+
523
+ // Verify that the SSL_CERT_DIR environment variable exists
524
+ var sslCertDirEnvFound bool
525
+ for _ , env := range job .Spec .Template .Spec .Containers [0 ].Env {
526
+ if env .Name == "SSL_CERT_DIR" {
527
+ sslCertDirEnvFound = true
528
+ // Verify the env var value
529
+ assert .Equal (t , "/certs" , env .Value , "SSL_CERT_DIR value should be correct" )
530
+ break
531
+ }
532
+ }
533
+ assert .True (t , sslCertDirEnvFound , "SSL_CERT_DIR environment variable should exist" )
534
+ })
535
+
536
+ // Test without HostCABundlePath set
537
+ t .Run ("without HostCABundlePath set" , func (t * testing.T ) {
538
+ log := testr .NewWithOptions (t , testr.Options {Verbosity : 10 })
539
+ ctx := logr .NewContext (context .Background (), log )
540
+
541
+ scheme := runtime .NewScheme ()
542
+ require .NoError (t , clusterv1beta1 .AddToScheme (scheme ))
543
+ require .NoError (t , batchv1 .AddToScheme (scheme ))
544
+ require .NoError (t , corev1 .AddToScheme (scheme ))
545
+
546
+ // Create a minimal installation CR without RuntimeConfig.HostCABundlePath
547
+ installation := & clusterv1beta1.Installation {
548
+ ObjectMeta : metav1.ObjectMeta {
549
+ Name : "test-installation" ,
550
+ },
551
+ Spec : clusterv1beta1.InstallationSpec {
552
+ AirGap : true ,
553
+ Artifacts : & clusterv1beta1.ArtifactsLocation {
554
+ Images : "images" ,
555
+ HelmCharts : "helm-charts" ,
556
+ EmbeddedClusterBinary : "embedded-cluster-binary" ,
557
+ EmbeddedClusterMetadata : "embedded-cluster-metadata" ,
558
+ },
559
+ // No RuntimeConfig or empty RuntimeConfig
560
+ },
561
+ }
562
+
563
+ // Create a fake client
564
+ cli := fake .NewClientBuilder ().
565
+ WithScheme (scheme ).
566
+ WithObjects (installation ).
567
+ Build ()
568
+
569
+ // Create a test node
570
+ node := corev1.Node {
571
+ ObjectMeta : metav1.ObjectMeta {
572
+ Name : "test-node" ,
573
+ },
574
+ }
575
+
576
+ // Call the function under test
577
+ job , err := getArtifactJobForNode (
578
+ ctx , cli , installation , node ,
579
+ "local-artifact-mirror:latest" ,
580
+ "app-slug" ,
581
+ "channel-id" ,
582
+ "1.0.0" ,
583
+ )
584
+ require .NoError (t , err )
585
+
586
+ // Verify that the host CA bundle volume does NOT exist
587
+ var hostCABundleVolumeFound bool
588
+ for _ , volume := range job .Spec .Template .Spec .Volumes {
589
+ if volume .Name == "host-ca-bundle" {
590
+ hostCABundleVolumeFound = true
591
+ break
592
+ }
593
+ }
594
+ assert .False (t , hostCABundleVolumeFound , "Host CA bundle volume should not exist when HostCABundlePath is not set" )
595
+
596
+ // Verify that the volume mount does NOT exist
597
+ var hostCABundleMountFound bool
598
+ for _ , mount := range job .Spec .Template .Spec .Containers [0 ].VolumeMounts {
599
+ if mount .Name == "host-ca-bundle" {
600
+ hostCABundleMountFound = true
601
+ break
602
+ }
603
+ }
604
+ assert .False (t , hostCABundleMountFound , "Host CA bundle mount should not exist when HostCABundlePath is not set" )
605
+
606
+ // Verify that the SSL_CERT_DIR environment variable does NOT exist
607
+ var sslCertDirEnvFound bool
608
+ for _ , env := range job .Spec .Template .Spec .Containers [0 ].Env {
609
+ if env .Name == "SSL_CERT_DIR" {
610
+ sslCertDirEnvFound = true
611
+ break
612
+ }
613
+ }
614
+ assert .False (t , sslCertDirEnvFound , "SSL_CERT_DIR environment variable should not exist when HostCABundlePath is not set" )
615
+ })
616
+ }
0 commit comments