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
2023-08-17 15:22:32 +00:00
func TestVariablesTargetsBlockOverride ( t * testing . T ) {
2023-05-15 12:07:18 +00:00
b := load ( t , "./variables/env_overrides" )
2023-05-24 12:45:19 +00:00
err := bundle . Apply ( context . Background ( ) , b , bundle . Seq (
2023-08-17 15:22:32 +00:00
mutator . SelectTarget ( "env-with-single-variable-override" ) ,
2023-05-15 12:07:18 +00:00
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 )
}
2023-08-17 15:22:32 +00:00
func TestVariablesTargetsBlockOverrideForMultipleVariables ( t * testing . T ) {
2023-05-15 12:07:18 +00:00
b := load ( t , "./variables/env_overrides" )
2023-05-24 12:45:19 +00:00
err := bundle . Apply ( context . Background ( ) , b , bundle . Seq (
2023-08-17 15:22:32 +00:00
mutator . SelectTarget ( "env-with-two-variable-overrides" ) ,
2023-05-15 12:07:18 +00:00
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 )
}
2023-08-17 15:22:32 +00:00
func TestVariablesTargetsBlockOverrideWithProcessEnvVars ( t * testing . T ) {
2023-05-15 12:07:18 +00:00
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-08-17 15:22:32 +00:00
mutator . SelectTarget ( "env-with-two-variable-overrides" ) ,
2023-05-15 12:07:18 +00:00
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 )
}
2023-08-17 15:22:32 +00:00
func TestVariablesTargetsBlockOverrideWithMissingVariables ( t * testing . T ) {
2023-05-15 12:07:18 +00:00
b := load ( t , "./variables/env_overrides" )
2023-05-24 12:45:19 +00:00
err := bundle . Apply ( context . Background ( ) , b , bundle . Seq (
2023-08-17 15:22:32 +00:00
mutator . SelectTarget ( "env-missing-a-required-variable-assignment" ) ,
2023-05-15 12:07:18 +00:00
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" )
}
2023-08-17 15:22:32 +00:00
func TestVariablesTargetsBlockOverrideWithUndefinedVariables ( t * testing . T ) {
2023-05-15 12:07:18 +00:00
b := load ( t , "./variables/env_overrides" )
2023-05-24 12:45:19 +00:00
err := bundle . Apply ( context . Background ( ) , b , bundle . Seq (
2023-08-17 15:22:32 +00:00
mutator . SelectTarget ( "env-using-an-undefined-variable" ) ,
2023-05-15 12:07:18 +00:00
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" )
}
2023-11-08 11:01:14 +00:00
func TestVariablesWithoutDefinition ( t * testing . T ) {
t . Setenv ( "BUNDLE_VAR_a" , "foo" )
t . Setenv ( "BUNDLE_VAR_b" , "bar" )
b := load ( t , "./variables/without_definition" )
err := bundle . Apply ( context . Background ( ) , b , mutator . SetVariables ( ) )
require . NoError ( t , err )
require . True ( t , b . Config . Variables [ "a" ] . HasValue ( ) )
require . True ( t , b . Config . Variables [ "b" ] . HasValue ( ) )
assert . Equal ( t , "foo" , * b . Config . Variables [ "a" ] . Value )
assert . Equal ( t , "bar" , * b . Config . Variables [ "b" ] . Value )
}
2024-01-04 21:04:42 +00:00
func TestVariablesWithTargetLookupOverrides ( t * testing . T ) {
b := load ( t , "./variables/env_overrides" )
err := bundle . Apply ( context . Background ( ) , b , bundle . Seq (
mutator . SelectTarget ( "env-overrides-lookup" ) ,
mutator . SetVariables ( ) ,
interpolation . Interpolate (
interpolation . IncludeLookupsInPath ( variable . VariableReferencePrefix ) ,
) ) )
require . NoError ( t , err )
assert . Equal ( t , "cluster: some-test-cluster" , b . Config . Variables [ "d" ] . Lookup . String ( ) )
2024-01-05 10:50:53 +00:00
assert . Equal ( t , "instance-pool: some-test-instance-pool" , b . Config . Variables [ "e" ] . Lookup . String ( ) )
2024-01-04 21:04:42 +00:00
}