2023-05-15 09:34:05 +00:00
package mutator
import (
"context"
"testing"
2023-05-16 16:35:39 +00:00
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"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 TestSetVariableFromProcessEnvVar ( t * testing . T ) {
defaultVal := "default"
variable := variable . Variable {
Description : "a test variable" ,
Default : & defaultVal ,
}
// set value for variable as an environment variable
t . Setenv ( "BUNDLE_VAR_foo" , "process-env" )
2023-09-11 08:18:43 +00:00
err := setVariable ( context . Background ( ) , & variable , "foo" )
2023-05-15 09:34:05 +00:00
require . NoError ( t , err )
assert . Equal ( t , * variable . Value , "process-env" )
}
func TestSetVariableUsingDefaultValue ( t * testing . T ) {
defaultVal := "default"
variable := variable . Variable {
Description : "a test variable" ,
Default : & defaultVal ,
}
2023-09-11 08:18:43 +00:00
err := setVariable ( context . Background ( ) , & variable , "foo" )
2023-05-15 09:34:05 +00:00
require . NoError ( t , err )
assert . Equal ( t , * variable . Value , "default" )
}
func TestSetVariableWhenAlreadyAValueIsAssigned ( t * testing . T ) {
defaultVal := "default"
val := "assigned-value"
variable := variable . Variable {
Description : "a test variable" ,
Default : & defaultVal ,
Value : & val ,
}
// since a value is already assigned to the variable, it would not be overridden
// by the default value
2023-09-11 08:18:43 +00:00
err := setVariable ( context . Background ( ) , & variable , "foo" )
2023-05-15 09:34:05 +00:00
require . NoError ( t , err )
assert . Equal ( t , * variable . Value , "assigned-value" )
}
func TestSetVariableEnvVarValueDoesNotOverridePresetValue ( t * testing . T ) {
defaultVal := "default"
val := "assigned-value"
variable := variable . Variable {
Description : "a test variable" ,
Default : & defaultVal ,
Value : & val ,
}
// set value for variable as an environment variable
t . Setenv ( "BUNDLE_VAR_foo" , "process-env" )
// since a value is already assigned to the variable, it would not be overridden
// by the value from environment
2023-09-11 08:18:43 +00:00
err := setVariable ( context . Background ( ) , & variable , "foo" )
2023-05-15 09:34:05 +00:00
require . NoError ( t , err )
assert . Equal ( t , * variable . Value , "assigned-value" )
}
func TestSetVariablesErrorsIfAValueCouldNotBeResolved ( t * testing . T ) {
variable := variable . Variable {
Description : "a test variable with no default" ,
}
// fails because we could not resolve a value for the variable
2023-09-11 08:18:43 +00:00
err := setVariable ( context . Background ( ) , & variable , "foo" )
2023-05-15 09:34:05 +00:00
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" )
}
func TestSetVariablesMutator ( t * testing . T ) {
defaultValForA := "default-a"
defaultValForB := "default-b"
valForC := "assigned-val-c"
bundle := & bundle . Bundle {
Config : config . Root {
Variables : map [ string ] * variable . Variable {
"a" : {
Description : "resolved to default value" ,
Default : & defaultValForA ,
} ,
"b" : {
Description : "resolved from environment vairables" ,
Default : & defaultValForB ,
} ,
"c" : {
Description : "has already been assigned a value" ,
Value : & valForC ,
} ,
} ,
} ,
}
t . Setenv ( "BUNDLE_VAR_b" , "env-var-b" )
2023-05-24 12:45:19 +00:00
err := SetVariables ( ) . Apply ( context . Background ( ) , bundle )
2023-05-15 09:34:05 +00:00
require . NoError ( t , err )
assert . Equal ( t , "default-a" , * bundle . Config . Variables [ "a" ] . Value )
assert . Equal ( t , "env-var-b" , * bundle . Config . Variables [ "b" ] . Value )
assert . Equal ( t , "assigned-val-c" , * bundle . Config . Variables [ "c" ] . Value )
}