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"
2024-07-09 11:12:42 +00:00
"github.com/databricks/cli/libs/dyn"
"github.com/databricks/cli/libs/dyn/convert"
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" ,
2024-06-26 10:25:32 +00:00
Default : defaultVal ,
2023-05-15 09:34:05 +00:00
}
// set value for variable as an environment variable
t . Setenv ( "BUNDLE_VAR_foo" , "process-env" )
2024-07-09 11:12:42 +00:00
v , err := convert . FromTyped ( variable , dyn . NilValue )
require . NoError ( t , err )
2023-05-15 09:34:05 +00:00
2024-07-09 11:12:42 +00:00
v , err = setVariable ( context . Background ( ) , v , & variable , "foo" )
require . NoError ( t , err )
err = convert . ToTyped ( & variable , v )
require . NoError ( t , err )
2024-06-26 10:25:32 +00:00
assert . Equal ( t , variable . Value , "process-env" )
2023-05-15 09:34:05 +00:00
}
func TestSetVariableUsingDefaultValue ( t * testing . T ) {
defaultVal := "default"
variable := variable . Variable {
Description : "a test variable" ,
2024-06-26 10:25:32 +00:00
Default : defaultVal ,
2023-05-15 09:34:05 +00:00
}
2024-07-09 11:12:42 +00:00
v , err := convert . FromTyped ( variable , dyn . NilValue )
require . NoError ( t , err )
v , err = setVariable ( context . Background ( ) , v , & variable , "foo" )
require . NoError ( t , err )
err = convert . ToTyped ( & variable , v )
require . NoError ( t , err )
2024-06-26 10:25:32 +00:00
assert . Equal ( t , variable . Value , "default" )
2023-05-15 09:34:05 +00:00
}
func TestSetVariableWhenAlreadyAValueIsAssigned ( t * testing . T ) {
defaultVal := "default"
val := "assigned-value"
variable := variable . Variable {
Description : "a test variable" ,
2024-06-26 10:25:32 +00:00
Default : defaultVal ,
Value : val ,
2023-05-15 09:34:05 +00:00
}
// since a value is already assigned to the variable, it would not be overridden
// by the default value
2024-07-09 11:12:42 +00:00
v , err := convert . FromTyped ( variable , dyn . NilValue )
require . NoError ( t , err )
v , err = setVariable ( context . Background ( ) , v , & variable , "foo" )
require . NoError ( t , err )
err = convert . ToTyped ( & variable , v )
require . NoError ( t , err )
2024-06-26 10:25:32 +00:00
assert . Equal ( t , variable . Value , "assigned-value" )
2023-05-15 09:34:05 +00:00
}
func TestSetVariableEnvVarValueDoesNotOverridePresetValue ( t * testing . T ) {
defaultVal := "default"
val := "assigned-value"
variable := variable . Variable {
Description : "a test variable" ,
2024-06-26 10:25:32 +00:00
Default : defaultVal ,
Value : val ,
2023-05-15 09:34:05 +00:00
}
// 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
2024-07-09 11:12:42 +00:00
v , err := convert . FromTyped ( variable , dyn . NilValue )
require . NoError ( t , err )
v , err = setVariable ( context . Background ( ) , v , & variable , "foo" )
require . NoError ( t , err )
err = convert . ToTyped ( & variable , v )
require . NoError ( t , err )
2024-06-26 10:25:32 +00:00
assert . Equal ( t , variable . Value , "assigned-value" )
2023-05-15 09:34:05 +00:00
}
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
2024-07-09 11:12:42 +00:00
v , err := convert . FromTyped ( variable , dyn . NilValue )
require . NoError ( t , err )
_ , err = setVariable ( context . Background ( ) , v , & variable , "foo" )
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" )
2023-05-15 09:34:05 +00:00
}
func TestSetVariablesMutator ( t * testing . T ) {
defaultValForA := "default-a"
defaultValForB := "default-b"
valForC := "assigned-val-c"
2023-11-15 14:03:36 +00:00
b := & bundle . Bundle {
2023-05-15 09:34:05 +00:00
Config : config . Root {
Variables : map [ string ] * variable . Variable {
"a" : {
Description : "resolved to default value" ,
2024-06-26 10:25:32 +00:00
Default : defaultValForA ,
2023-05-15 09:34:05 +00:00
} ,
"b" : {
Description : "resolved from environment vairables" ,
2024-06-26 10:25:32 +00:00
Default : defaultValForB ,
2023-05-15 09:34:05 +00:00
} ,
"c" : {
Description : "has already been assigned a value" ,
2024-06-26 10:25:32 +00:00
Value : valForC ,
2023-05-15 09:34:05 +00:00
} ,
} ,
} ,
}
t . Setenv ( "BUNDLE_VAR_b" , "env-var-b" )
2024-03-25 14:18:47 +00:00
diags := bundle . Apply ( context . Background ( ) , b , SetVariables ( ) )
require . NoError ( t , diags . Error ( ) )
2024-06-26 10:25:32 +00:00
assert . Equal ( t , "default-a" , b . Config . Variables [ "a" ] . Value )
assert . Equal ( t , "env-var-b" , b . Config . Variables [ "b" ] . Value )
assert . Equal ( t , "assigned-val-c" , b . Config . Variables [ "c" ] . Value )
}
func TestSetComplexVariablesViaEnvVariablesIsNotAllowed ( t * testing . T ) {
defaultVal := "default"
variable := variable . Variable {
Description : "a test variable" ,
Default : defaultVal ,
Type : variable . VariableTypeComplex ,
}
// set value for variable as an environment variable
t . Setenv ( "BUNDLE_VAR_foo" , "process-env" )
2024-07-09 11:12:42 +00:00
v , err := convert . FromTyped ( variable , dyn . NilValue )
require . NoError ( t , err )
_ , err = setVariable ( context . Background ( ) , v , & variable , "foo" )
assert . ErrorContains ( t , err , "setting via environment variables (BUNDLE_VAR_foo) is not supported for complex variable foo" )
2023-05-15 09:34:05 +00:00
}