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