@@ -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,23 +78,112 @@ 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}
103+
104+ func TestLoadProfileByNameAndClusterID (t * testing.T ) {
105+ testCases := []struct {
106+ name string
107+ profile string
108+ configFileEnv string
109+ homeDirOverride string
110+ expectedHost string
111+ expectedClusterID string
112+ }{
113+ {
114+ name : "cluster profile" ,
115+ profile : "cluster-profile" ,
116+ configFileEnv : "./testdata/.databrickscfg" ,
117+ expectedHost : "https://www.host2.com" ,
118+ expectedClusterID : "cluster-from-config" ,
119+ },
120+ {
121+ name : "profile from home directory (existing)" ,
122+ profile : "cluster-profile" ,
123+ homeDirOverride : "testdata" ,
124+ expectedHost : "https://www.host2.com" ,
125+ expectedClusterID : "cluster-from-config" ,
126+ },
127+ {
128+ name : "profile does not exist" ,
129+ profile : "no-profile" ,
130+ configFileEnv : "./testdata/.databrickscfg" ,
131+ expectedHost : "" ,
132+ expectedClusterID : "" ,
133+ },
134+ {
135+ name : "account profile" ,
136+ profile : "account-profile" ,
137+ configFileEnv : "./testdata/.databrickscfg" ,
138+ expectedHost : "https://accounts.cloud.databricks.com" ,
139+ expectedClusterID : "" ,
140+ },
141+ {
142+ name : "config doesn't exist" ,
143+ profile : "any-profile" ,
144+ configFileEnv : "./nonexistent/.databrickscfg" ,
145+ expectedHost : "" ,
146+ expectedClusterID : "" ,
147+ },
148+ {
149+ name : "profile from home directory (non-existent)" ,
150+ profile : "any-profile" ,
151+ homeDirOverride : "nonexistent" ,
152+ expectedHost : "" ,
153+ expectedClusterID : "" ,
154+ },
155+ {
156+ name : "invalid profile (missing host)" ,
157+ profile : "invalid-profile" ,
158+ configFileEnv : "./testdata/.databrickscfg" ,
159+ expectedHost : "" ,
160+ expectedClusterID : "" ,
161+ },
162+ }
163+
164+ for _ , tc := range testCases {
165+ t .Run (tc .name , func (t * testing.T ) {
166+ ctx := context .Background ()
167+
168+ if tc .configFileEnv != "" {
169+ t .Setenv ("DATABRICKS_CONFIG_FILE" , tc .configFileEnv )
170+ } else if tc .homeDirOverride != "" {
171+ // Use default ~/.databrickscfg
172+ ctx = env .WithUserHomeDir (ctx , tc .homeDirOverride )
173+ }
174+
175+ profile , err := loadProfileByName (ctx , tc .profile , profile .DefaultProfiler )
176+ require .NoError (t , err )
177+
178+ if tc .expectedHost == "" {
179+ assert .Nil (t , profile , "Test case '%s' failed: expected nil profile but got non-nil profile" , tc .name )
180+ } else {
181+ assert .NotNil (t , profile , "Test case '%s' failed: expected profile but got nil" , tc .name )
182+ assert .Equal (t , tc .expectedHost , profile .Host ,
183+ "Test case '%s' failed: expected host '%s', but got '%s'" , tc .name , tc .expectedHost , profile .Host )
184+ assert .Equal (t , tc .expectedClusterID , profile .ClusterID ,
185+ "Test case '%s' failed: expected cluster ID '%s', but got '%s'" , tc .name , tc .expectedClusterID , profile .ClusterID )
186+ }
187+ })
188+ }
189+ }
0 commit comments