@@ -12,50 +12,64 @@ 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+ existingProfile , err := loadProfileByName (ctx , "foo" , profile .DefaultProfiler )
27+ assert .NoError (t , err )
28+
29+ err = setHostAndAccountId (ctx , existingProfile , & auth.AuthArguments {Host : "test" }, []string {})
1930 assert .NoError (t , err )
2031}
2132
2233func TestSetHost (t * testing.T ) {
23- authArguments := auth.AuthArguments {}
34+ var authArguments auth.AuthArguments
2435 t .Setenv ("DATABRICKS_CONFIG_FILE" , "./testdata/.databrickscfg" )
2536 ctx , _ := cmdio .SetupTest (context .Background ())
2637
38+ profile1 := loadTestProfile (t , ctx , "profile-1" )
39+ profile2 := loadTestProfile (t , ctx , "profile-2" )
40+
2741 // Test error when both flag and argument are provided
2842 authArguments .Host = "val from --host"
29- err := setHostAndAccountId (ctx , profile . DefaultProfiler , "profile-1" , & authArguments , []string {"val from [HOST]" })
43+ err := setHostAndAccountId (ctx , profile1 , & authArguments , []string {"val from [HOST]" })
3044 assert .EqualError (t , err , "please only provide a host as an argument or a flag, not both" )
3145
3246 // Test setting host from flag
3347 authArguments .Host = "val from --host"
34- err = setHostAndAccountId (ctx , profile . DefaultProfiler , "profile-1" , & authArguments , []string {})
48+ err = setHostAndAccountId (ctx , profile1 , & authArguments , []string {})
3549 assert .NoError (t , err )
3650 assert .Equal (t , "val from --host" , authArguments .Host )
3751
3852 // Test setting host from argument
3953 authArguments .Host = ""
40- err = setHostAndAccountId (ctx , profile . DefaultProfiler , "profile-1" , & authArguments , []string {"val from [HOST]" })
54+ err = setHostAndAccountId (ctx , profile1 , & authArguments , []string {"val from [HOST]" })
4155 assert .NoError (t , err )
4256 assert .Equal (t , "val from [HOST]" , authArguments .Host )
4357
4458 // Test setting host from profile
4559 authArguments .Host = ""
46- err = setHostAndAccountId (ctx , profile . DefaultProfiler , "profile-1" , & authArguments , []string {})
60+ err = setHostAndAccountId (ctx , profile1 , & authArguments , []string {})
4761 assert .NoError (t , err )
4862 assert .Equal (t , "https://www.host1.com" , authArguments .Host )
4963
5064 // Test setting host from profile
5165 authArguments .Host = ""
52- err = setHostAndAccountId (ctx , profile . DefaultProfiler , "profile-2" , & authArguments , []string {})
66+ err = setHostAndAccountId (ctx , profile2 , & authArguments , []string {})
5367 assert .NoError (t , err )
5468 assert .Equal (t , "https://www.host2.com" , authArguments .Host )
5569
5670 // Test host is not set. Should prompt.
5771 authArguments .Host = ""
58- err = setHostAndAccountId (ctx , profile . DefaultProfiler , "" , & authArguments , []string {})
72+ err = setHostAndAccountId (ctx , nil , & authArguments , []string {})
5973 assert .EqualError (t , err , "the command is being run in a non-interactive environment, please specify a host using --host" )
6074}
6175
@@ -64,75 +78,97 @@ func TestSetAccountId(t *testing.T) {
6478 t .Setenv ("DATABRICKS_CONFIG_FILE" , "./testdata/.databrickscfg" )
6579 ctx , _ := cmdio .SetupTest (context .Background ())
6680
81+ accountProfile := loadTestProfile (t , ctx , "account-profile" )
82+
6783 // Test setting account-id from flag
6884 authArguments .AccountID = "val from --account-id"
69- err := setHostAndAccountId (ctx , profile . DefaultProfiler , "account-profile" , & authArguments , []string {})
85+ err := setHostAndAccountId (ctx , accountProfile , & authArguments , []string {})
7086 assert .NoError (t , err )
7187 assert .Equal (t , "https://accounts.cloud.databricks.com" , authArguments .Host )
7288 assert .Equal (t , "val from --account-id" , authArguments .AccountID )
7389
7490 // Test setting account_id from profile
7591 authArguments .AccountID = ""
76- err = setHostAndAccountId (ctx , profile . DefaultProfiler , "account-profile" , & authArguments , []string {})
92+ err = setHostAndAccountId (ctx , accountProfile , & authArguments , []string {})
7793 require .NoError (t , err )
7894 assert .Equal (t , "https://accounts.cloud.databricks.com" , authArguments .Host )
7995 assert .Equal (t , "id-from-profile" , authArguments .AccountID )
8096
8197 // Neither flag nor profile account-id is set, should prompt
8298 authArguments .AccountID = ""
8399 authArguments .Host = "https://accounts.cloud.databricks.com"
84- err = setHostAndAccountId (ctx , profile . DefaultProfiler , "" , & authArguments , []string {})
100+ err = setHostAndAccountId (ctx , nil , & authArguments , []string {})
85101 assert .EqualError (t , err , "the command is being run in a non-interactive environment, please specify an account ID using --account-id" )
86102}
87103
88- func TestLoginGetClusterID (t * testing.T ) {
104+ func TestLoadProfileByNameAndClusterID (t * testing.T ) {
89105 testCases := []struct {
90- name string
91- profile string
92- configFileEnv string
93- expected string
106+ name string
107+ profile string
108+ configFileEnv string
109+ expectedHost string
110+ expectedClusterID string
94111 }{
95112 {
96- name : "cluster profile" ,
97- profile : "cluster-profile" ,
98- configFileEnv : "./testdata/.databrickscfg" ,
99- expected : "cluster-from-config" ,
113+ name : "cluster profile" ,
114+ profile : "cluster-profile" ,
115+ configFileEnv : "./testdata/.databrickscfg" ,
116+ expectedHost : "https://www.host2.com" ,
117+ expectedClusterID : "cluster-from-config" ,
100118 },
101119 {
102- name : "empty profile" ,
103- profile : "no-profile" ,
104- configFileEnv : "./testdata/.databrickscfg" ,
105- expected : "" ,
120+ name : "empty profile" ,
121+ profile : "no-profile" ,
122+ configFileEnv : "./testdata/.databrickscfg" ,
123+ expectedHost : "" ,
124+ expectedClusterID : "" ,
106125 },
107126 {
108- name : "account profile" ,
109- profile : "account-profile" ,
110- configFileEnv : "./testdata/.databrickscfg" ,
111- expected : "" ,
127+ name : "account profile" ,
128+ profile : "account-profile" ,
129+ configFileEnv : "./testdata/.databrickscfg" ,
130+ expectedHost : "https://accounts.cloud.databricks.com" ,
131+ expectedClusterID : "" ,
112132 },
113133 {
114- name : "config doesn't exist" ,
115- profile : "any-profile" ,
116- configFileEnv : "./nonexistent/.databrickscfg" ,
117- expected : "" ,
134+ name : "config doesn't exist" ,
135+ profile : "any-profile" ,
136+ configFileEnv : "./nonexistent/.databrickscfg" ,
137+ expectedHost : "" ,
138+ expectedClusterID : "" ,
118139 },
119140 {
120- name : "invalid profile (missing host)" ,
121- profile : "invalid-profile" ,
122- configFileEnv : "./testdata/.databrickscfg" ,
123- expected : "" ,
141+ name : "invalid profile (missing host)" ,
142+ profile : "invalid-profile" ,
143+ configFileEnv : "./testdata/.databrickscfg" ,
144+ expectedHost : "" ,
145+ expectedClusterID : "" ,
146+ },
147+ {
148+ name : "no config file specified" ,
149+ profile : "any-profile" ,
150+ configFileEnv : "" ,
151+ expectedHost : "" ,
152+ expectedClusterID : "" ,
124153 },
125154 }
126155
127156 for _ , tc := range testCases {
128157 t .Run (tc .name , func (t * testing.T ) {
129158 t .Setenv ("DATABRICKS_CONFIG_FILE" , tc .configFileEnv )
130159
131- clusterID , err := getClusterID (context .Background (), tc .profile )
160+ profile , err := loadProfileByName (context .Background (), tc .profile , profile . DefaultProfiler )
132161 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 )
162+
163+ if tc .expectedHost == "" {
164+ assert .Nil (t , profile , "Test case '%s' failed: expected nil profile but got non-nil profile" , tc .name )
165+ } else {
166+ assert .NotNil (t , profile , "Test case '%s' failed: expected profile but got nil" , tc .name )
167+ assert .Equal (t , tc .expectedHost , profile .Host ,
168+ "Test case '%s' failed: expected host '%s', but got '%s'" , tc .name , tc .expectedHost , profile .Host )
169+ assert .Equal (t , tc .expectedClusterID , profile .ClusterID ,
170+ "Test case '%s' failed: expected cluster ID '%s', but got '%s'" , tc .name , tc .expectedClusterID , profile .ClusterID )
171+ }
136172 })
137173 }
138174}
0 commit comments