2022-11-18 09:57:31 +00:00
package config
import (
"encoding/json"
"reflect"
"testing"
2023-05-16 16:35:39 +00:00
"github.com/databricks/cli/bundle/config/variable"
2022-11-18 09:57:31 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRootMarshalUnmarshal ( t * testing . T ) {
// Marshal empty
buf , err := json . Marshal ( & Root { } )
require . NoError ( t , err )
// Unmarshal empty
var root Root
err = json . Unmarshal ( buf , & root )
require . NoError ( t , err )
// Compare
assert . True ( t , reflect . DeepEqual ( Root { } , root ) )
}
func TestRootLoad ( t * testing . T ) {
2023-10-25 12:55:56 +00:00
root , err := Load ( "../tests/basic/databricks.yml" )
2022-11-18 09:57:31 +00:00
require . NoError ( t , err )
assert . Equal ( t , "basic" , root . Bundle . Name )
}
func TestRootMergeStruct ( t * testing . T ) {
root := & Root {
2023-04-12 14:17:13 +00:00
Path : "path" ,
2022-11-18 09:57:31 +00:00
Workspace : Workspace {
Host : "foo" ,
Profile : "profile" ,
} ,
}
other := & Root {
2023-04-12 14:17:13 +00:00
Path : "path" ,
2022-11-18 09:57:31 +00:00
Workspace : Workspace {
Host : "bar" ,
} ,
}
assert . NoError ( t , root . Merge ( other ) )
assert . Equal ( t , "bar" , root . Workspace . Host )
assert . Equal ( t , "profile" , root . Workspace . Profile )
}
func TestRootMergeMap ( t * testing . T ) {
root := & Root {
2023-04-12 14:17:13 +00:00
Path : "path" ,
2023-08-17 15:22:32 +00:00
Targets : map [ string ] * Target {
2022-11-18 09:57:31 +00:00
"development" : {
Workspace : & Workspace {
Host : "foo" ,
Profile : "profile" ,
} ,
} ,
} ,
}
other := & Root {
2023-04-12 14:17:13 +00:00
Path : "path" ,
2023-08-17 15:22:32 +00:00
Targets : map [ string ] * Target {
2022-11-18 09:57:31 +00:00
"development" : {
Workspace : & Workspace {
Host : "bar" ,
} ,
} ,
} ,
}
assert . NoError ( t , root . Merge ( other ) )
2023-08-17 15:22:32 +00:00
assert . Equal ( t , & Workspace { Host : "bar" , Profile : "profile" } , root . Targets [ "development" ] . Workspace )
2022-11-18 09:57:31 +00:00
}
2023-04-17 10:21:21 +00:00
func TestDuplicateIdOnLoadReturnsError ( t * testing . T ) {
2023-10-25 12:55:56 +00:00
_ , err := Load ( "./testdata/duplicate_resource_names_in_root/databricks.yml" )
2023-07-18 10:16:34 +00:00
assert . ErrorContains ( t , err , "multiple resources named foo (job at ./testdata/duplicate_resource_names_in_root/databricks.yml, pipeline at ./testdata/duplicate_resource_names_in_root/databricks.yml)" )
2023-04-17 10:21:21 +00:00
}
func TestDuplicateIdOnMergeReturnsError ( t * testing . T ) {
2023-10-25 12:55:56 +00:00
root , err := Load ( "./testdata/duplicate_resource_name_in_subconfiguration/databricks.yml" )
2023-04-17 10:21:21 +00:00
require . NoError ( t , err )
2023-10-25 12:55:56 +00:00
other , err := Load ( "./testdata/duplicate_resource_name_in_subconfiguration/resources.yml" )
2023-04-17 10:21:21 +00:00
require . NoError ( t , err )
err = root . Merge ( other )
2023-07-18 10:16:34 +00:00
assert . ErrorContains ( t , err , "multiple resources named foo (job at ./testdata/duplicate_resource_name_in_subconfiguration/databricks.yml, pipeline at ./testdata/duplicate_resource_name_in_subconfiguration/resources.yml)" )
2023-04-17 10:21:21 +00:00
}
2023-05-15 09:34:05 +00:00
func TestInitializeVariables ( t * testing . T ) {
fooDefault := "abc"
root := & Root {
Variables : map [ string ] * variable . Variable {
"foo" : {
Default : & fooDefault ,
Description : "an optional variable since default is defined" ,
} ,
"bar" : {
Description : "a required variable" ,
} ,
} ,
}
err := root . InitializeVariables ( [ ] string { "foo=123" , "bar=456" } )
assert . NoError ( t , err )
assert . Equal ( t , "123" , * ( root . Variables [ "foo" ] . Value ) )
assert . Equal ( t , "456" , * ( root . Variables [ "bar" ] . Value ) )
}
func TestInitializeVariablesWithAnEqualSignInValue ( t * testing . T ) {
root := & Root {
Variables : map [ string ] * variable . Variable {
"foo" : {
Description : "a variable called foo" ,
} ,
} ,
}
err := root . InitializeVariables ( [ ] string { "foo=123=567" } )
assert . NoError ( t , err )
assert . Equal ( t , "123=567" , * ( root . Variables [ "foo" ] . Value ) )
}
func TestInitializeVariablesInvalidFormat ( t * testing . T ) {
root := & Root {
Variables : map [ string ] * variable . Variable {
"foo" : {
Description : "a variable called foo" ,
} ,
} ,
}
err := root . InitializeVariables ( [ ] string { "foo" } )
assert . ErrorContains ( t , err , "unexpected flag value for variable assignment: foo" )
}
func TestInitializeVariablesUndefinedVariables ( t * testing . T ) {
root := & Root {
Variables : map [ string ] * variable . Variable {
"foo" : {
Description : "A required variable" ,
} ,
} ,
}
err := root . InitializeVariables ( [ ] string { "bar=567" } )
assert . ErrorContains ( t , err , "variable bar has not been defined" )
}
2023-07-12 06:51:54 +00:00
2023-08-17 15:22:32 +00:00
func TestRootMergeTargetOverridesWithMode ( t * testing . T ) {
2023-07-12 06:51:54 +00:00
root := & Root {
Bundle : Bundle { } ,
}
2023-08-17 15:22:32 +00:00
env := & Target { Mode : Development }
require . NoError ( t , root . MergeTargetOverrides ( env ) )
2023-07-12 06:51:54 +00:00
assert . Equal ( t , Development , root . Bundle . Mode )
}