@@ -12,50 +12,65 @@ import (
1212 "github.com/stretchr/testify/require"
1313)
1414
15+ func loadTestProfile (t * testing.T , ctx context.Context , profileName string ) * profile.Profile {
16+ profile , err := loadProfileByName (ctx , profileName , profile .DefaultProfiler )
17+ require .NoError (t , err )
18+ require .NotNil (t , profile )
19+ return profile
20+ }
21+
1522func TestSetHostDoesNotFailWithNoDatabrickscfg (t * testing.T ) {
1623 ctx := context .Background ()
1724 ctx = env .Set (ctx , "DATABRICKS_CONFIG_FILE" , "./imaginary-file/databrickscfg" )
18- err := setHostAndAccountId (ctx , profile .DefaultProfiler , "foo" , & auth.AuthArguments {Host : "test" }, []string {})
25+
26+ // Load profile (will fail but we tolerate it)
27+ existingProfile , err := loadProfileByName (ctx , "foo" , profile .DefaultProfiler )
28+ assert .NoError (t , err )
29+
30+ err = setHostAndAccountId (ctx , existingProfile , & auth.AuthArguments {Host : "test" }, []string {})
1931 assert .NoError (t , err )
2032}
2133
2234func TestSetHost (t * testing.T ) {
23- authArguments := auth.AuthArguments {}
35+ var authArguments auth.AuthArguments
2436 t .Setenv ("DATABRICKS_CONFIG_FILE" , "./testdata/.databrickscfg" )
2537 ctx , _ := cmdio .SetupTest (context .Background ())
2638
39+ profile1 := loadTestProfile (t , ctx , "profile-1" )
40+ profile2 := loadTestProfile (t , ctx , "profile-2" )
41+
2742 // Test error when both flag and argument are provided
2843 authArguments .Host = "val from --host"
29- err := setHostAndAccountId (ctx , profile . DefaultProfiler , "profile-1" , & authArguments , []string {"val from [HOST]" })
44+ err := setHostAndAccountId (ctx , profile1 , & authArguments , []string {"val from [HOST]" })
3045 assert .EqualError (t , err , "please only provide a host as an argument or a flag, not both" )
3146
3247 // Test setting host from flag
3348 authArguments .Host = "val from --host"
34- err = setHostAndAccountId (ctx , profile . DefaultProfiler , "profile-1" , & authArguments , []string {})
49+ err = setHostAndAccountId (ctx , profile1 , & authArguments , []string {})
3550 assert .NoError (t , err )
3651 assert .Equal (t , "val from --host" , authArguments .Host )
3752
3853 // Test setting host from argument
3954 authArguments .Host = ""
40- err = setHostAndAccountId (ctx , profile . DefaultProfiler , "profile-1" , & authArguments , []string {"val from [HOST]" })
55+ err = setHostAndAccountId (ctx , profile1 , & authArguments , []string {"val from [HOST]" })
4156 assert .NoError (t , err )
4257 assert .Equal (t , "val from [HOST]" , authArguments .Host )
4358
4459 // Test setting host from profile
4560 authArguments .Host = ""
46- err = setHostAndAccountId (ctx , profile . DefaultProfiler , "profile-1" , & authArguments , []string {})
61+ err = setHostAndAccountId (ctx , profile1 , & authArguments , []string {})
4762 assert .NoError (t , err )
4863 assert .Equal (t , "https://www.host1.com" , authArguments .Host )
4964
5065 // Test setting host from profile
5166 authArguments .Host = ""
52- err = setHostAndAccountId (ctx , profile . DefaultProfiler , "profile-2" , & authArguments , []string {})
67+ err = setHostAndAccountId (ctx , profile2 , & authArguments , []string {})
5368 assert .NoError (t , err )
5469 assert .Equal (t , "https://www.host2.com" , authArguments .Host )
5570
5671 // Test host is not set. Should prompt.
5772 authArguments .Host = ""
58- err = setHostAndAccountId (ctx , profile . DefaultProfiler , "" , & authArguments , []string {})
73+ err = setHostAndAccountId (ctx , nil , & authArguments , []string {})
5974 assert .EqualError (t , err , "the command is being run in a non-interactive environment, please specify a host using --host" )
6075}
6176
@@ -64,75 +79,97 @@ func TestSetAccountId(t *testing.T) {
6479 t .Setenv ("DATABRICKS_CONFIG_FILE" , "./testdata/.databrickscfg" )
6580 ctx , _ := cmdio .SetupTest (context .Background ())
6681
82+ accountProfile := loadTestProfile (t , ctx , "account-profile" )
83+
6784 // Test setting account-id from flag
6885 authArguments .AccountID = "val from --account-id"
69- err := setHostAndAccountId (ctx , profile . DefaultProfiler , "account-profile" , & authArguments , []string {})
86+ err := setHostAndAccountId (ctx , accountProfile , & authArguments , []string {})
7087 assert .NoError (t , err )
7188 assert .Equal (t , "https://accounts.cloud.databricks.com" , authArguments .Host )
7289 assert .Equal (t , "val from --account-id" , authArguments .AccountID )
7390
7491 // Test setting account_id from profile
7592 authArguments .AccountID = ""
76- err = setHostAndAccountId (ctx , profile . DefaultProfiler , "account-profile" , & authArguments , []string {})
93+ err = setHostAndAccountId (ctx , accountProfile , & authArguments , []string {})
7794 require .NoError (t , err )
7895 assert .Equal (t , "https://accounts.cloud.databricks.com" , authArguments .Host )
7996 assert .Equal (t , "id-from-profile" , authArguments .AccountID )
8097
8198 // Neither flag nor profile account-id is set, should prompt
8299 authArguments .AccountID = ""
83100 authArguments .Host = "https://accounts.cloud.databricks.com"
84- err = setHostAndAccountId (ctx , profile . DefaultProfiler , "" , & authArguments , []string {})
101+ err = setHostAndAccountId (ctx , nil , & authArguments , []string {})
85102 assert .EqualError (t , err , "the command is being run in a non-interactive environment, please specify an account ID using --account-id" )
86103}
87104
88- func TestLoginGetClusterID (t * testing.T ) {
105+ func TestLoadProfileByNameAndClusterID (t * testing.T ) {
89106 testCases := []struct {
90- name string
91- profile string
92- configFileEnv string
93- expected string
107+ name string
108+ profile string
109+ configFileEnv string
110+ expectedHost string
111+ expectedClusterID string
94112 }{
95113 {
96- name : "cluster profile" ,
97- profile : "cluster-profile" ,
98- configFileEnv : "./testdata/.databrickscfg" ,
99- expected : "cluster-from-config" ,
114+ name : "cluster profile" ,
115+ profile : "cluster-profile" ,
116+ configFileEnv : "./testdata/.databrickscfg" ,
117+ expectedHost : "https://www.host2.com" ,
118+ expectedClusterID : "cluster-from-config" ,
100119 },
101120 {
102- name : "empty profile" ,
103- profile : "no-profile" ,
104- configFileEnv : "./testdata/.databrickscfg" ,
105- expected : "" ,
121+ name : "empty profile" ,
122+ profile : "no-profile" ,
123+ configFileEnv : "./testdata/.databrickscfg" ,
124+ expectedHost : "" ,
125+ expectedClusterID : "" ,
106126 },
107127 {
108- name : "account profile" ,
109- profile : "account-profile" ,
110- configFileEnv : "./testdata/.databrickscfg" ,
111- expected : "" ,
128+ name : "account profile" ,
129+ profile : "account-profile" ,
130+ configFileEnv : "./testdata/.databrickscfg" ,
131+ expectedHost : "https://accounts.cloud.databricks.com" ,
132+ expectedClusterID : "" ,
112133 },
113134 {
114- name : "config doesn't exist" ,
115- profile : "any-profile" ,
116- configFileEnv : "./nonexistent/.databrickscfg" ,
117- expected : "" ,
135+ name : "config doesn't exist" ,
136+ profile : "any-profile" ,
137+ configFileEnv : "./nonexistent/.databrickscfg" ,
138+ expectedHost : "" ,
139+ expectedClusterID : "" ,
118140 },
119141 {
120- name : "invalid profile (missing host)" ,
121- profile : "invalid-profile" ,
122- configFileEnv : "./testdata/.databrickscfg" ,
123- expected : "" ,
142+ name : "invalid profile (missing host)" ,
143+ profile : "invalid-profile" ,
144+ configFileEnv : "./testdata/.databrickscfg" ,
145+ expectedHost : "" ,
146+ expectedClusterID : "" ,
147+ },
148+ {
149+ name : "no config file specified" ,
150+ profile : "any-profile" ,
151+ configFileEnv : "" ,
152+ expectedHost : "" ,
153+ expectedClusterID : "" ,
124154 },
125155 }
126156
127157 for _ , tc := range testCases {
128158 t .Run (tc .name , func (t * testing.T ) {
129159 t .Setenv ("DATABRICKS_CONFIG_FILE" , tc .configFileEnv )
130160
131- clusterID , err := getClusterID (context .Background (), tc .profile )
161+ profile , err := loadProfileByName (context .Background (), tc .profile , profile . DefaultProfiler )
132162 require .NoError (t , err )
133- assert .Equal (t , tc .expected , clusterID ,
134- "Test case '%s' failed: expected cluster ID '%s', but got '%s' (profile: %s, config file: %s)" ,
135- tc .name , tc .expected , clusterID , tc .profile , tc .configFileEnv )
163+
164+ if tc .expectedHost == "" {
165+ assert .Nil (t , profile , "Test case '%s' failed: expected nil profile but got non-nil profile" , tc .name )
166+ } else {
167+ assert .NotNil (t , profile , "Test case '%s' failed: expected profile but got nil" , tc .name )
168+ assert .Equal (t , tc .expectedHost , profile .Host ,
169+ "Test case '%s' failed: expected host '%s', but got '%s'" , tc .name , tc .expectedHost , profile .Host )
170+ assert .Equal (t , tc .expectedClusterID , profile .ClusterID ,
171+ "Test case '%s' failed: expected cluster ID '%s', but got '%s'" , tc .name , tc .expectedClusterID , profile .ClusterID )
172+ }
136173 })
137174 }
138175}
0 commit comments