github.com/databricks/cli@v0.203.0/bundle/tests/variables_test.go (about) 1 package config_tests 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/databricks/cli/bundle" 8 "github.com/databricks/cli/bundle/config/interpolation" 9 "github.com/databricks/cli/bundle/config/mutator" 10 "github.com/databricks/cli/bundle/config/variable" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestVariables(t *testing.T) { 16 t.Setenv("BUNDLE_VAR_b", "def") 17 b := load(t, "./variables/vanilla") 18 err := bundle.Apply(context.Background(), b, bundle.Seq( 19 mutator.SetVariables(), 20 interpolation.Interpolate( 21 interpolation.IncludeLookupsInPath(variable.VariableReferencePrefix), 22 ))) 23 require.NoError(t, err) 24 assert.Equal(t, "abc def", b.Config.Bundle.Name) 25 } 26 27 func TestVariablesLoadingFailsWhenRequiredVariableIsNotSpecified(t *testing.T) { 28 b := load(t, "./variables/vanilla") 29 err := bundle.Apply(context.Background(), b, bundle.Seq( 30 mutator.SetVariables(), 31 interpolation.Interpolate( 32 interpolation.IncludeLookupsInPath(variable.VariableReferencePrefix), 33 ))) 34 assert.ErrorContains(t, err, "no value assigned to required variable b. Assignment can be done through the \"--var\" flag or by setting the BUNDLE_VAR_b environment variable") 35 } 36 37 func TestVariablesEnvironmentsBlockOverride(t *testing.T) { 38 b := load(t, "./variables/env_overrides") 39 err := bundle.Apply(context.Background(), b, bundle.Seq( 40 mutator.SelectEnvironment("env-with-single-variable-override"), 41 mutator.SetVariables(), 42 interpolation.Interpolate( 43 interpolation.IncludeLookupsInPath(variable.VariableReferencePrefix), 44 ))) 45 require.NoError(t, err) 46 assert.Equal(t, "default-a dev-b", b.Config.Workspace.Profile) 47 } 48 49 func TestVariablesEnvironmentsBlockOverrideForMultipleVariables(t *testing.T) { 50 b := load(t, "./variables/env_overrides") 51 err := bundle.Apply(context.Background(), b, bundle.Seq( 52 mutator.SelectEnvironment("env-with-two-variable-overrides"), 53 mutator.SetVariables(), 54 interpolation.Interpolate( 55 interpolation.IncludeLookupsInPath(variable.VariableReferencePrefix), 56 ))) 57 require.NoError(t, err) 58 assert.Equal(t, "prod-a prod-b", b.Config.Workspace.Profile) 59 } 60 61 func TestVariablesEnvironmentsBlockOverrideWithProcessEnvVars(t *testing.T) { 62 t.Setenv("BUNDLE_VAR_b", "env-var-b") 63 b := load(t, "./variables/env_overrides") 64 err := bundle.Apply(context.Background(), b, bundle.Seq( 65 mutator.SelectEnvironment("env-with-two-variable-overrides"), 66 mutator.SetVariables(), 67 interpolation.Interpolate( 68 interpolation.IncludeLookupsInPath(variable.VariableReferencePrefix), 69 ))) 70 require.NoError(t, err) 71 assert.Equal(t, "prod-a env-var-b", b.Config.Workspace.Profile) 72 } 73 74 func TestVariablesEnvironmentsBlockOverrideWithMissingVariables(t *testing.T) { 75 b := load(t, "./variables/env_overrides") 76 err := bundle.Apply(context.Background(), b, bundle.Seq( 77 mutator.SelectEnvironment("env-missing-a-required-variable-assignment"), 78 mutator.SetVariables(), 79 interpolation.Interpolate( 80 interpolation.IncludeLookupsInPath(variable.VariableReferencePrefix), 81 ))) 82 assert.ErrorContains(t, err, "no value assigned to required variable b. Assignment can be done through the \"--var\" flag or by setting the BUNDLE_VAR_b environment variable") 83 } 84 85 func TestVariablesEnvironmentsBlockOverrideWithUndefinedVariables(t *testing.T) { 86 b := load(t, "./variables/env_overrides") 87 err := bundle.Apply(context.Background(), b, bundle.Seq( 88 mutator.SelectEnvironment("env-using-an-undefined-variable"), 89 mutator.SetVariables(), 90 interpolation.Interpolate( 91 interpolation.IncludeLookupsInPath(variable.VariableReferencePrefix), 92 ))) 93 assert.ErrorContains(t, err, "variable c is not defined but is assigned a value") 94 }