github.com/databricks/cli@v0.203.0/bundle/config/mutator/set_variables_test.go (about) 1 package mutator 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/databricks/cli/bundle" 8 "github.com/databricks/cli/bundle/config" 9 "github.com/databricks/cli/bundle/config/variable" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestSetVariableFromProcessEnvVar(t *testing.T) { 15 defaultVal := "default" 16 variable := variable.Variable{ 17 Description: "a test variable", 18 Default: &defaultVal, 19 } 20 21 // set value for variable as an environment variable 22 t.Setenv("BUNDLE_VAR_foo", "process-env") 23 24 err := setVariable(&variable, "foo") 25 require.NoError(t, err) 26 assert.Equal(t, *variable.Value, "process-env") 27 } 28 29 func TestSetVariableUsingDefaultValue(t *testing.T) { 30 defaultVal := "default" 31 variable := variable.Variable{ 32 Description: "a test variable", 33 Default: &defaultVal, 34 } 35 36 err := setVariable(&variable, "foo") 37 require.NoError(t, err) 38 assert.Equal(t, *variable.Value, "default") 39 } 40 41 func TestSetVariableWhenAlreadyAValueIsAssigned(t *testing.T) { 42 defaultVal := "default" 43 val := "assigned-value" 44 variable := variable.Variable{ 45 Description: "a test variable", 46 Default: &defaultVal, 47 Value: &val, 48 } 49 50 // since a value is already assigned to the variable, it would not be overridden 51 // by the default value 52 err := setVariable(&variable, "foo") 53 require.NoError(t, err) 54 assert.Equal(t, *variable.Value, "assigned-value") 55 } 56 57 func TestSetVariableEnvVarValueDoesNotOverridePresetValue(t *testing.T) { 58 defaultVal := "default" 59 val := "assigned-value" 60 variable := variable.Variable{ 61 Description: "a test variable", 62 Default: &defaultVal, 63 Value: &val, 64 } 65 66 // set value for variable as an environment variable 67 t.Setenv("BUNDLE_VAR_foo", "process-env") 68 69 // since a value is already assigned to the variable, it would not be overridden 70 // by the value from environment 71 err := setVariable(&variable, "foo") 72 require.NoError(t, err) 73 assert.Equal(t, *variable.Value, "assigned-value") 74 } 75 76 func TestSetVariablesErrorsIfAValueCouldNotBeResolved(t *testing.T) { 77 variable := variable.Variable{ 78 Description: "a test variable with no default", 79 } 80 81 // fails because we could not resolve a value for the variable 82 err := setVariable(&variable, "foo") 83 assert.ErrorContains(t, err, "no value assigned to required variable foo. Assignment can be done through the \"--var\" flag or by setting the BUNDLE_VAR_foo environment variable") 84 } 85 86 func TestSetVariablesMutator(t *testing.T) { 87 defaultValForA := "default-a" 88 defaultValForB := "default-b" 89 valForC := "assigned-val-c" 90 bundle := &bundle.Bundle{ 91 Config: config.Root{ 92 Variables: map[string]*variable.Variable{ 93 "a": { 94 Description: "resolved to default value", 95 Default: &defaultValForA, 96 }, 97 "b": { 98 Description: "resolved from environment vairables", 99 Default: &defaultValForB, 100 }, 101 "c": { 102 Description: "has already been assigned a value", 103 Value: &valForC, 104 }, 105 }, 106 }, 107 } 108 109 t.Setenv("BUNDLE_VAR_b", "env-var-b") 110 111 err := SetVariables().Apply(context.Background(), bundle) 112 require.NoError(t, err) 113 assert.Equal(t, "default-a", *bundle.Config.Variables["a"].Value) 114 assert.Equal(t, "env-var-b", *bundle.Config.Variables["b"].Value) 115 assert.Equal(t, "assigned-val-c", *bundle.Config.Variables["c"].Value) 116 }