@@ -26,6 +26,7 @@ import (
2626 "github.com/google/go-cmp/cmp/cmpopts"
2727 "github.com/stretchr/testify/assert"
2828 corev1 "k8s.io/api/core/v1"
29+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2930 "sigs.k8s.io/controller-runtime/pkg/client"
3031 "sigs.k8s.io/controller-runtime/pkg/client/fake"
3132 "sigs.k8s.io/yaml"
@@ -126,7 +127,7 @@ func TestProvisionAutomountResourcesInto(t *testing.T) {
126127 }
127128 // Note: this test does not allow for returning AutoMountError with isFatal: false (i.e. no retrying)
128129 // and so is not suitable for testing automount features that provision cluster resources (yet)
129- err := ProvisionAutoMountResourcesInto (podAdditions , testAPI , testNamespace , false )
130+ err := ProvisionAutoMountResourcesInto (podAdditions , testAPI , testNamespace , false , false )
130131 if tt .Output .ErrRegexp != nil {
131132 if ! assert .Error (t , err , "Expected an error but got none" ) {
132133 return
@@ -407,3 +408,220 @@ func loadTestCaseOrPanic(t *testing.T, testPath string) testCase {
407408 test .TestPath = testPath
408409 return test
409410}
411+
412+ func TestShouldNotMountSecretWithMountOnStartOnlyIfWorkspaceStarted (t * testing.T ) {
413+ testSecret := corev1.Secret {
414+ ObjectMeta : metav1.ObjectMeta {
415+ Name : "test-secret" ,
416+ Namespace : testNamespace ,
417+ Labels : map [string ]string {
418+ "controller.devfile.io/mount-to-devworkspace" : "true" ,
419+ },
420+ Annotations : map [string ]string {
421+ "controller.devfile.io/mount-as" : "file" ,
422+ "controller.devfile.io/mount-path" : "/test/path" ,
423+ "controller.devfile.io/mount-on-start-only" : "true" ,
424+ },
425+ },
426+ Data : map [string ][]byte {
427+ "data" : []byte ("test" ),
428+ },
429+ }
430+
431+ testAPI := sync.ClusterAPI {
432+ Client : fake .NewClientBuilder ().WithObjects (& testSecret ).Build (),
433+ }
434+
435+ testPodAdditions := & v1alpha1.PodAdditions {
436+ Containers : []corev1.Container {{
437+ Name : "test-container" ,
438+ Image : "test-image" ,
439+ }},
440+ }
441+
442+ // When workspace is started (isWorkspaceStarted=true), secret with mount-on-start-only should be skipped
443+ err := ProvisionAutoMountResourcesInto (testPodAdditions , testAPI , testNamespace , false , true )
444+ assert .NoError (t , err )
445+ assert .Empty (t , testPodAdditions .Volumes )
446+ assert .Empty (t , testPodAdditions .Containers [0 ].VolumeMounts )
447+ }
448+
449+ func TestMountSecretWithMountOnStartOnlyIfWorkspaceNotStarted (t * testing.T ) {
450+ testSecret := corev1.Secret {
451+ ObjectMeta : metav1.ObjectMeta {
452+ Name : "test-secret" ,
453+ Namespace : testNamespace ,
454+ Labels : map [string ]string {
455+ "controller.devfile.io/mount-to-devworkspace" : "true" ,
456+ },
457+ Annotations : map [string ]string {
458+ "controller.devfile.io/mount-as" : "file" ,
459+ "controller.devfile.io/mount-path" : "/test/path" ,
460+ "controller.devfile.io/mount-on-start-only" : "true" ,
461+ },
462+ },
463+ Data : map [string ][]byte {
464+ "data" : []byte ("test" ),
465+ },
466+ }
467+
468+ testAPI := sync.ClusterAPI {
469+ Client : fake .NewClientBuilder ().WithObjects (& testSecret ).Build (),
470+ }
471+
472+ testPodAdditions := & v1alpha1.PodAdditions {
473+ Containers : []corev1.Container {{
474+ Name : "test-container" ,
475+ Image : "test-image" ,
476+ }},
477+ }
478+
479+ // When workspace is not started (isWorkspaceStarted=false), secret with mount-on-start-only should be mounted
480+ err := ProvisionAutoMountResourcesInto (testPodAdditions , testAPI , testNamespace , false , false )
481+ assert .NoError (t , err )
482+ assert .Len (t , testPodAdditions .Volumes , 1 )
483+ assert .Len (t , testPodAdditions .Containers [0 ].VolumeMounts , 1 )
484+ assert .Equal (t , "test-secret" , testPodAdditions .Volumes [0 ].Name )
485+ }
486+
487+ func TestShouldNotMountConfigMapWithMountOnStartOnlyIfWorkspaceStarted (t * testing.T ) {
488+ testConfigMap := corev1.ConfigMap {
489+ ObjectMeta : metav1.ObjectMeta {
490+ Name : "test-cm" ,
491+ Namespace : testNamespace ,
492+ Labels : map [string ]string {
493+ "controller.devfile.io/mount-to-devworkspace" : "true" ,
494+ },
495+ Annotations : map [string ]string {
496+ "controller.devfile.io/mount-as" : "file" ,
497+ "controller.devfile.io/mount-path" : "/test/path" ,
498+ "controller.devfile.io/mount-on-start-only" : "true" ,
499+ },
500+ },
501+ Data : map [string ]string {
502+ "data" : "test" ,
503+ },
504+ }
505+
506+ testAPI := sync.ClusterAPI {
507+ Client : fake .NewClientBuilder ().WithObjects (& testConfigMap ).Build (),
508+ }
509+
510+ testPodAdditions := & v1alpha1.PodAdditions {
511+ Containers : []corev1.Container {{
512+ Name : "test-container" ,
513+ Image : "test-image" ,
514+ }},
515+ }
516+
517+ // When workspace is started (isWorkspaceStarted=true), configmap with mount-on-start-only should be skipped
518+ err := ProvisionAutoMountResourcesInto (testPodAdditions , testAPI , testNamespace , false , true )
519+ assert .NoError (t , err )
520+ assert .Empty (t , testPodAdditions .Volumes )
521+ assert .Empty (t , testPodAdditions .Containers [0 ].VolumeMounts )
522+ }
523+
524+ func TestMountConfigMapWithMountOnStartOnlyIfWorkspaceNotStarted (t * testing.T ) {
525+ testConfigMap := corev1.ConfigMap {
526+ ObjectMeta : metav1.ObjectMeta {
527+ Name : "test-cm" ,
528+ Namespace : testNamespace ,
529+ Labels : map [string ]string {
530+ "controller.devfile.io/mount-to-devworkspace" : "true" ,
531+ },
532+ Annotations : map [string ]string {
533+ "controller.devfile.io/mount-as" : "file" ,
534+ "controller.devfile.io/mount-path" : "/test/path" ,
535+ "controller.devfile.io/mount-on-start-only" : "true" ,
536+ },
537+ },
538+ Data : map [string ]string {
539+ "data" : "test" ,
540+ },
541+ }
542+
543+ testAPI := sync.ClusterAPI {
544+ Client : fake .NewClientBuilder ().WithObjects (& testConfigMap ).Build (),
545+ }
546+
547+ testPodAdditions := & v1alpha1.PodAdditions {
548+ Containers : []corev1.Container {{
549+ Name : "test-container" ,
550+ Image : "test-image" ,
551+ }},
552+ }
553+
554+ // When workspace is not started (isWorkspaceStarted=false), configmap with mount-on-start-only should be mounted
555+ err := ProvisionAutoMountResourcesInto (testPodAdditions , testAPI , testNamespace , false , false )
556+ assert .NoError (t , err )
557+ assert .Len (t , testPodAdditions .Volumes , 1 )
558+ assert .Len (t , testPodAdditions .Containers [0 ].VolumeMounts , 1 )
559+ assert .Equal (t , "test-cm" , testPodAdditions .Volumes [0 ].Name )
560+ }
561+
562+ func TestShouldNotMountPVCWithMountOnStartOnlyIfWorkspaceStarted (t * testing.T ) {
563+ testPVC := corev1.PersistentVolumeClaim {
564+ ObjectMeta : metav1.ObjectMeta {
565+ Name : "test-pvc" ,
566+ Namespace : testNamespace ,
567+ Labels : map [string ]string {
568+ "controller.devfile.io/mount-to-devworkspace" : "true" ,
569+ },
570+ Annotations : map [string ]string {
571+ "controller.devfile.io/mount-path" : "/test/path" ,
572+ "controller.devfile.io/mount-on-start-only" : "true" ,
573+ },
574+ },
575+ }
576+
577+ testAPI := sync.ClusterAPI {
578+ Client : fake .NewClientBuilder ().WithObjects (& testPVC ).Build (),
579+ }
580+
581+ testPodAdditions := & v1alpha1.PodAdditions {
582+ Containers : []corev1.Container {{
583+ Name : "test-container" ,
584+ Image : "test-image" ,
585+ }},
586+ }
587+
588+ // When workspace is started (isWorkspaceStarted=true), PVC with mount-on-start-only should be skipped
589+ err := ProvisionAutoMountResourcesInto (testPodAdditions , testAPI , testNamespace , false , true )
590+ assert .NoError (t , err )
591+ assert .Empty (t , testPodAdditions .Volumes )
592+ assert .Empty (t , testPodAdditions .Containers [0 ].VolumeMounts )
593+ }
594+
595+ func TestMountPVCWithMountOnStartOnlyIfWorkspaceNotStarted (t * testing.T ) {
596+ testPVC := corev1.PersistentVolumeClaim {
597+ ObjectMeta : metav1.ObjectMeta {
598+ Name : "test-pvc" ,
599+ Namespace : testNamespace ,
600+ Labels : map [string ]string {
601+ "controller.devfile.io/mount-to-devworkspace" : "true" ,
602+ },
603+ Annotations : map [string ]string {
604+ "controller.devfile.io/mount-path" : "/test/path" ,
605+ "controller.devfile.io/mount-on-start-only" : "true" ,
606+ },
607+ },
608+ }
609+
610+ testAPI := sync.ClusterAPI {
611+ Client : fake .NewClientBuilder ().WithObjects (& testPVC ).Build (),
612+ }
613+
614+ testPodAdditions := & v1alpha1.PodAdditions {
615+ Containers : []corev1.Container {{
616+ Name : "test-container" ,
617+ Image : "test-image" ,
618+ }},
619+ }
620+
621+ // When workspace is not started (isWorkspaceStarted=false), PVC with mount-on-start-only should be mounted
622+ err := ProvisionAutoMountResourcesInto (testPodAdditions , testAPI , testNamespace , false , false )
623+ assert .NoError (t , err )
624+ assert .Len (t , testPodAdditions .Volumes , 1 )
625+ assert .Len (t , testPodAdditions .Containers [0 ].VolumeMounts , 1 )
626+ assert .Equal (t , common .AutoMountPVCVolumeName ("test-pvc" ), testPodAdditions .Volumes [0 ].Name )
627+ }
0 commit comments