github.com/verrazzano/verrazzano-monitoring-operator@v0.0.30/pkg/config/components_test.go (about) 1 // Copyright (C) 2020, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package config 5 6 import ( 7 "fmt" 8 "os" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 "go.uber.org/zap" 13 ) 14 15 func TestNoImages(t *testing.T) { 16 unsetEnvVars(t, AllComponentDetails) 17 err := InitComponentDetails() 18 assert.Error(t, err) 19 } 20 21 func TestAllImages(t *testing.T) { 22 createEnvVars(t, AllComponentDetails) 23 testImages(t, AllComponentDetails, nil) 24 } 25 26 func TestOptionalImages(t *testing.T) { 27 var components = []*ComponentDetails{} 28 var optionalComponents = []*ComponentDetails{} 29 30 // separate the optional components 31 for _, component := range AllComponentDetails { 32 if component.Optional { 33 optionalComponents = append(optionalComponents, component) 34 } else { 35 components = append(components, component) 36 } 37 } 38 createEnvVars(t, components) 39 unsetEnvVars(t, optionalComponents) 40 testImages(t, components, optionalComponents) 41 } 42 43 func createEnvVars(t *testing.T, components []*ComponentDetails) { 44 // Create environment variable for each component 45 for _, component := range components { 46 if len(component.EnvName) > 0 { 47 zap.S().Infof("Setting environment variable %s", component.EnvName) 48 err := os.Setenv(component.EnvName, "TEST") 49 assert.Nil(t, err, fmt.Sprintf("setting environment variable %s", component.EnvName)) 50 } 51 } 52 } 53 54 func unsetEnvVars(t *testing.T, components []*ComponentDetails) { 55 // Unset variable for each component 56 for _, component := range components { 57 if len(component.EnvName) > 0 { 58 zap.S().Infof("Unsetting environment variable %s", component.EnvName) 59 err := os.Unsetenv(component.EnvName) 60 assert.Nil(t, err, fmt.Sprintf("unsetting environment variable %s", component.EnvName)) 61 } 62 } 63 } 64 65 func testImages(t *testing.T, components []*ComponentDetails, disabledComponents []*ComponentDetails) { 66 err := os.Setenv(eswaitTargetVersionEnv, "es.TEST") 67 assert.Nil(t, err, fmt.Sprintf("setting environment variable %s", eswaitTargetVersionEnv)) 68 69 err = InitComponentDetails() 70 assert.Nil(t, err, "Expected initComponentDetails to succeed") 71 72 // Test the image names were set as expected 73 for _, component := range components { 74 if len(component.EnvName) > 0 { 75 assert.Equal(t, "TEST", component.Image, fmt.Sprintf("checking image name field for %s", component.Name)) 76 } 77 } 78 // Test the disabled status is set as expected 79 for _, component := range disabledComponents { 80 assert.True(t, component.Disabled, fmt.Sprintf("checking disabled status for %s", component.Name)) 81 } 82 }