66
77 "github.com/databricks/cli/libs/auth"
88 "github.com/databricks/cli/libs/cmdio"
9- "github.com/databricks/cli/libs/databrickscfg/profile"
109 "github.com/databricks/cli/libs/env"
1110 "github.com/stretchr/testify/assert"
1211 "github.com/stretchr/testify/require"
@@ -15,7 +14,12 @@ import (
1514func TestSetHostDoesNotFailWithNoDatabrickscfg (t * testing.T ) {
1615 ctx := context .Background ()
1716 ctx = env .Set (ctx , "DATABRICKS_CONFIG_FILE" , "./imaginary-file/databrickscfg" )
18- err := setHostAndAccountId (ctx , profile .DefaultProfiler , "foo" , & auth.AuthArguments {Host : "test" }, []string {})
17+
18+ // Load profile (will fail but we tolerate it)
19+ existingProfile , err := loadProfileByName (ctx , "foo" )
20+ assert .NoError (t , err )
21+
22+ err = setHostAndAccountId (ctx , existingProfile , & auth.AuthArguments {Host : "test" }, []string {})
1923 assert .NoError (t , err )
2024}
2125
@@ -24,38 +28,48 @@ func TestSetHost(t *testing.T) {
2428 t .Setenv ("DATABRICKS_CONFIG_FILE" , "./testdata/.databrickscfg" )
2529 ctx , _ := cmdio .SetupTest (context .Background ())
2630
31+ // Load profile-1
32+ profile1 , err := loadProfileByName (ctx , "profile-1" )
33+ require .NoError (t , err )
34+ require .NotNil (t , profile1 )
35+
36+ // Load profile-2
37+ profile2 , err := loadProfileByName (ctx , "profile-2" )
38+ require .NoError (t , err )
39+ require .NotNil (t , profile2 )
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,100 @@ func TestSetAccountId(t *testing.T) {
6478 t .Setenv ("DATABRICKS_CONFIG_FILE" , "./testdata/.databrickscfg" )
6579 ctx , _ := cmdio .SetupTest (context .Background ())
6680
81+ // Load account-profile
82+ accountProfile , err := loadProfileByName (ctx , "account-profile" )
83+ require .NoError (t , err )
84+ require .NotNil (t , accountProfile )
85+
6786 // Test setting account-id from flag
6887 authArguments .AccountID = "val from --account-id"
69- err : = setHostAndAccountId (ctx , profile . DefaultProfiler , "account-profile" , & authArguments , []string {})
88+ err = setHostAndAccountId (ctx , accountProfile , & authArguments , []string {})
7089 assert .NoError (t , err )
7190 assert .Equal (t , "https://accounts.cloud.databricks.com" , authArguments .Host )
7291 assert .Equal (t , "val from --account-id" , authArguments .AccountID )
7392
7493 // Test setting account_id from profile
7594 authArguments .AccountID = ""
76- err = setHostAndAccountId (ctx , profile . DefaultProfiler , "account-profile" , & authArguments , []string {})
95+ err = setHostAndAccountId (ctx , accountProfile , & authArguments , []string {})
7796 require .NoError (t , err )
7897 assert .Equal (t , "https://accounts.cloud.databricks.com" , authArguments .Host )
7998 assert .Equal (t , "id-from-profile" , authArguments .AccountID )
8099
81100 // Neither flag nor profile account-id is set, should prompt
82101 authArguments .AccountID = ""
83102 authArguments .Host = "https://accounts.cloud.databricks.com"
84- err = setHostAndAccountId (ctx , profile . DefaultProfiler , "" , & authArguments , []string {})
103+ err = setHostAndAccountId (ctx , nil , & authArguments , []string {})
85104 assert .EqualError (t , err , "the command is being run in a non-interactive environment, please specify an account ID using --account-id" )
86105}
87106
88107func TestLoginGetClusterID (t * testing.T ) {
89108 testCases := []struct {
90- name string
91- profile string
92- configFileEnv string
93- expected string
109+ name string
110+ profile string
111+ configFileEnv string
112+ expectedHost string
113+ expectedClusterID string
94114 }{
95115 {
96- name : "cluster profile" ,
97- profile : "cluster-profile" ,
98- configFileEnv : "./testdata/.databrickscfg" ,
99- expected : "cluster-from-config" ,
116+ name : "cluster profile" ,
117+ profile : "cluster-profile" ,
118+ configFileEnv : "./testdata/.databrickscfg" ,
119+ expectedHost : "https://www.host2.com" ,
120+ expectedClusterID : "cluster-from-config" ,
100121 },
101122 {
102- name : "empty profile" ,
103- profile : "no-profile" ,
104- configFileEnv : "./testdata/.databrickscfg" ,
105- expected : "" ,
123+ name : "empty profile" ,
124+ profile : "no-profile" ,
125+ configFileEnv : "./testdata/.databrickscfg" ,
126+ expectedHost : "" ,
127+ expectedClusterID : "" ,
106128 },
107129 {
108- name : "account profile" ,
109- profile : "account-profile" ,
110- configFileEnv : "./testdata/.databrickscfg" ,
111- expected : "" ,
130+ name : "account profile" ,
131+ profile : "account-profile" ,
132+ configFileEnv : "./testdata/.databrickscfg" ,
133+ expectedHost : "https://accounts.cloud.databricks.com" ,
134+ expectedClusterID : "" ,
112135 },
113136 {
114- name : "config doesn't exist" ,
115- profile : "any-profile" ,
116- configFileEnv : "./nonexistent/.databrickscfg" ,
117- expected : "" ,
137+ name : "config doesn't exist" ,
138+ profile : "any-profile" ,
139+ configFileEnv : "./nonexistent/.databrickscfg" ,
140+ expectedHost : "" ,
141+ expectedClusterID : "" ,
118142 },
119143 {
120- name : "invalid profile (missing host)" ,
121- profile : "invalid-profile" ,
122- configFileEnv : "./testdata/.databrickscfg" ,
123- expected : "" ,
144+ name : "invalid profile (missing host)" ,
145+ profile : "invalid-profile" ,
146+ configFileEnv : "./testdata/.databrickscfg" ,
147+ expectedHost : "" ,
148+ expectedClusterID : "" ,
149+ },
150+ {
151+ name : "~/.databrickscfg does not exist" ,
152+ profile : "any-profile" ,
153+ configFileEnv : "" ,
154+ expectedHost : "" ,
155+ expectedClusterID : "" ,
124156 },
125157 }
126158
127159 for _ , tc := range testCases {
128160 t .Run (tc .name , func (t * testing.T ) {
129161 t .Setenv ("DATABRICKS_CONFIG_FILE" , tc .configFileEnv )
130162
131- clusterID , err := getClusterID (context .Background (), tc .profile )
163+ profile , err := loadProfileByName (context .Background (), tc .profile )
132164 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 )
165+
166+ if tc .expectedHost == "" {
167+ assert .Nil (t , profile , "Test case '%s' failed: expected nil profile but got non-nil profile" , tc .name )
168+ } else {
169+ assert .NotNil (t , profile , "Test case '%s' failed: expected profile but got nil" , tc .name )
170+ assert .Equal (t , tc .expectedHost , profile .Host ,
171+ "Test case '%s' failed: expected host '%s', but got '%s'" , tc .name , tc .expectedHost , profile .Host )
172+ assert .Equal (t , tc .expectedClusterID , profile .ClusterID ,
173+ "Test case '%s' failed: expected cluster ID '%s', but got '%s'" , tc .name , tc .expectedClusterID , profile .ClusterID )
174+ }
136175 })
137176 }
138177}
0 commit comments