2023-05-15 09:34:05 +00:00
package config_tests
import (
"context"
"testing"
2023-05-16 16:35:39 +00:00
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config/interpolation"
"github.com/databricks/cli/bundle/config/mutator"
"github.com/databricks/cli/bundle/config/variable"
2023-05-15 09:34:05 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestVariables ( t * testing . T ) {
t . Setenv ( "BUNDLE_VAR_b" , "def" )
2023-05-15 12:07:18 +00:00
b := load ( t , "./variables/vanilla" )
2023-05-24 12:45:19 +00:00
err := bundle . Apply ( context . Background ( ) , b , bundle . Seq (
2023-05-15 09:34:05 +00:00
mutator . SetVariables ( ) ,
interpolation . Interpolate (
interpolation . IncludeLookupsInPath ( variable . VariableReferencePrefix ) ,
2023-05-24 12:45:19 +00:00
) ) )
2023-05-15 09:34:05 +00:00
require . NoError ( t , err )
assert . Equal ( t , "abc def" , b . Config . Bundle . Name )
}
func TestVariablesLoadingFailsWhenRequiredVariableIsNotSpecified ( t * testing . T ) {
2023-05-15 12:07:18 +00:00
b := load ( t , "./variables/vanilla" )
2023-05-24 12:45:19 +00:00
err := bundle . Apply ( context . Background ( ) , b , bundle . Seq (
2023-05-15 09:34:05 +00:00
mutator . SetVariables ( ) ,
interpolation . Interpolate (
interpolation . IncludeLookupsInPath ( variable . VariableReferencePrefix ) ,
2023-05-24 12:45:19 +00:00
) ) )
2023-05-15 09:34:05 +00:00
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" )
}
2023-05-15 12:07:18 +00:00
func TestVariablesEnvironmentsBlockOverride ( t * testing . T ) {
b := load ( t , "./variables/env_overrides" )
2023-05-24 12:45:19 +00:00
err := bundle . Apply ( context . Background ( ) , b , bundle . Seq (
2023-05-15 12:07:18 +00:00
mutator . SelectEnvironment ( "env-with-single-variable-override" ) ,
mutator . SetVariables ( ) ,
interpolation . Interpolate (
interpolation . IncludeLookupsInPath ( variable . VariableReferencePrefix ) ,
2023-05-24 12:45:19 +00:00
) ) )
2023-05-15 12:07:18 +00:00
require . NoError ( t , err )
assert . Equal ( t , "default-a dev-b" , b . Config . Workspace . Profile )
}
func TestVariablesEnvironmentsBlockOverrideForMultipleVariables ( t * testing . T ) {
b := load ( t , "./variables/env_overrides" )
2023-05-24 12:45:19 +00:00
err := bundle . Apply ( context . Background ( ) , b , bundle . Seq (
2023-05-15 12:07:18 +00:00
mutator . SelectEnvironment ( "env-with-two-variable-overrides" ) ,
mutator . SetVariables ( ) ,
interpolation . Interpolate (
interpolation . IncludeLookupsInPath ( variable . VariableReferencePrefix ) ,
2023-05-24 12:45:19 +00:00
) ) )
2023-05-15 12:07:18 +00:00
require . NoError ( t , err )
assert . Equal ( t , "prod-a prod-b" , b . Config . Workspace . Profile )
}
func TestVariablesEnvironmentsBlockOverrideWithProcessEnvVars ( t * testing . T ) {
t . Setenv ( "BUNDLE_VAR_b" , "env-var-b" )
b := load ( t , "./variables/env_overrides" )
2023-05-24 12:45:19 +00:00
err := bundle . Apply ( context . Background ( ) , b , bundle . Seq (
2023-05-15 12:07:18 +00:00
mutator . SelectEnvironment ( "env-with-two-variable-overrides" ) ,
mutator . SetVariables ( ) ,
interpolation . Interpolate (
interpolation . IncludeLookupsInPath ( variable . VariableReferencePrefix ) ,
2023-05-24 12:45:19 +00:00
) ) )
2023-05-15 12:07:18 +00:00
require . NoError ( t , err )
assert . Equal ( t , "prod-a env-var-b" , b . Config . Workspace . Profile )
}
func TestVariablesEnvironmentsBlockOverrideWithMissingVariables ( t * testing . T ) {
b := load ( t , "./variables/env_overrides" )
2023-05-24 12:45:19 +00:00
err := bundle . Apply ( context . Background ( ) , b , bundle . Seq (
2023-05-15 12:07:18 +00:00
mutator . SelectEnvironment ( "env-missing-a-required-variable-assignment" ) ,
mutator . SetVariables ( ) ,
interpolation . Interpolate (
interpolation . IncludeLookupsInPath ( variable . VariableReferencePrefix ) ,
2023-05-24 12:45:19 +00:00
) ) )
2023-05-15 12:07:18 +00:00
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" )
}
func TestVariablesEnvironmentsBlockOverrideWithUndefinedVariables ( t * testing . T ) {
b := load ( t , "./variables/env_overrides" )
2023-05-24 12:45:19 +00:00
err := bundle . Apply ( context . Background ( ) , b , bundle . Seq (
2023-05-15 12:07:18 +00:00
mutator . SelectEnvironment ( "env-using-an-undefined-variable" ) ,
mutator . SetVariables ( ) ,
interpolation . Interpolate (
interpolation . IncludeLookupsInPath ( variable . VariableReferencePrefix ) ,
2023-05-24 12:45:19 +00:00
) ) )
2023-05-15 12:07:18 +00:00
assert . ErrorContains ( t , err , "variable c is not defined but is assigned a value" )
}