@@ -4,8 +4,10 @@ package installer
44
55import (
66 "fmt"
7+ "strings"
78 "testing"
89
10+ "github.com/distribution/reference"
911 "github.com/ghodss/yaml"
1012 "github.com/google/go-cmp/cmp"
1113 "github.com/stretchr/testify/assert"
@@ -16,6 +18,7 @@ import (
1618
1719 k8sclient "github.com/netapp/trident/cli/k8s_client"
1820 mockExtendedK8sClient "github.com/netapp/trident/mocks/mock_operator/mock_controllers/mock_orchestrator/mock_installer"
21+ "github.com/netapp/trident/operator/config"
1922 netappv1 "github.com/netapp/trident/operator/crd/apis/netapp/v1"
2023 "github.com/netapp/trident/utils/version"
2124)
@@ -295,3 +298,111 @@ func TestSetInstallationParams_NodePrep(t *testing.T) {
295298 })
296299 }
297300}
301+
302+ func TestSetInstallationParams_Images (t * testing.T ) {
303+ tag := ":v1.2.3"
304+ images := map [string ]struct {
305+ image * string
306+ envval string
307+ }{
308+ config .TridentImageEnv : {
309+ & tridentImage ,
310+ "test-registry-2/" + strings .ToLower (config .TridentImageEnv ) + tag ,
311+ },
312+ config .AutosupportImageEnv : {
313+ & autosupportImage ,
314+ "test-registry-2/" + strings .ToLower (config .AutosupportImageEnv ) + tag ,
315+ },
316+ config .CSISidecarProvisionerImageEnv : {
317+ & csiSidecarProvisionerImage ,
318+ "test-registry-2/" + strings .ToLower (config .CSISidecarProvisionerImageEnv ) + tag ,
319+ },
320+ config .CSISidecarAttacherImageEnv : {
321+ & csiSidecarAttacherImage ,
322+ "test-registry-2/" + strings .ToLower (config .CSISidecarAttacherImageEnv ) + tag ,
323+ },
324+ config .CSISidecarResizerImageEnv : {
325+ & csiSidecarResizerImage ,
326+ "test-registry-2/" + strings .ToLower (config .CSISidecarResizerImageEnv ) + tag ,
327+ },
328+ config .CSISidecarSnapshotterImageEnv : {
329+ & csiSidecarSnapshotterImage ,
330+ "test-registry-2/" + strings .ToLower (config .CSISidecarSnapshotterImageEnv ) + tag ,
331+ },
332+ config .CSISidecarNodeDriverRegistrarImageEnv : {
333+ & csiSidecarNodeDriverRegistrarImage ,
334+ "test-registry-2/" + strings .ToLower (config .CSISidecarNodeDriverRegistrarImageEnv ) + tag ,
335+ },
336+ config .CSISidecarLivenessProbeImageEnv : {
337+ & csiSidecarLivenessProbeImage ,
338+ "test-registry-2/" + strings .ToLower (config .CSISidecarLivenessProbeImageEnv ) + tag ,
339+ },
340+ }
341+ tests := []struct {
342+ name string
343+ imageRegistry string
344+ setEnvironment bool
345+ }{
346+ {
347+ name : "default" ,
348+ },
349+ {
350+ name : "image registry set" ,
351+ imageRegistry : "test-registry.example.com" ,
352+ },
353+ {
354+ name : "environment set" ,
355+ setEnvironment : true ,
356+ },
357+ {
358+ name : "environment and image registry set" ,
359+ imageRegistry : "test-registry.example.com" ,
360+ setEnvironment : true ,
361+ },
362+ }
363+
364+ mockK8sClient := newMockKubeClient (t )
365+ mockK8sClient .EXPECT ().ServerVersion ().Return (& version.Version {}).AnyTimes ()
366+
367+ for _ , test := range tests {
368+ t .Run (test .name , func (t * testing.T ) {
369+ for _ , image := range images {
370+ * image .image = ""
371+ }
372+
373+ to := netappv1.TridentOrchestrator {
374+ TypeMeta : metav1.TypeMeta {},
375+ ObjectMeta : metav1.ObjectMeta {},
376+ Spec : netappv1.TridentOrchestratorSpec {},
377+ Status : netappv1.TridentOrchestratorStatus {},
378+ }
379+ installer := newTestInstaller (mockK8sClient )
380+
381+ if test .setEnvironment {
382+ for env , image := range images {
383+ t .Setenv (env , image .envval )
384+ }
385+ }
386+
387+ to .Spec .ImageRegistry = test .imageRegistry
388+ _ , _ , _ , err := installer .setInstallationParams (to , "" )
389+ assert .NoError (t , err )
390+
391+ // Check that all images are valid or empty
392+ for env , image := range images {
393+ _ , err := reference .Parse (* image .image )
394+ assert .NoError (t , err , "invalid image %s" , * image .image )
395+
396+ if test .setEnvironment {
397+ assert .Contains (t , * image .image , strings .ToLower (env ))
398+ assert .True (t , strings .HasSuffix (* image .image , tag ))
399+
400+ if test .imageRegistry != "" {
401+ assert .True (t , strings .HasPrefix (* image .image , test .imageRegistry ), "invalid image: %s" ,
402+ * image .image )
403+ }
404+ }
405+ }
406+ })
407+ }
408+ }
0 commit comments