|
6 | 6 | "encoding/json" |
7 | 7 | "net/http" |
8 | 8 | "net/http/httptest" |
| 9 | + "os" |
| 10 | + "path/filepath" |
9 | 11 | "testing" |
10 | 12 |
|
11 | 13 | "github.com/databricks/cli/libs/cmdio" |
@@ -65,6 +67,18 @@ func newTestCmd(ctx context.Context) *cobra.Command { |
65 | 67 | return cmd |
66 | 68 | } |
67 | 69 |
|
| 70 | +func clearConfigEnv(t *testing.T) { |
| 71 | + t.Helper() |
| 72 | + |
| 73 | + for _, attr := range config.ConfigAttributes { |
| 74 | + for _, key := range attr.EnvVars { |
| 75 | + t.Setenv(key, "") |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + t.Setenv(env.HomeEnvVar(), t.TempDir()) |
| 80 | +} |
| 81 | + |
68 | 82 | func TestCheckCLIVersion(t *testing.T) { |
69 | 83 | result := checkCLIVersion() |
70 | 84 | assert.Equal(t, "CLI Version", result.Name) |
@@ -118,6 +132,8 @@ func TestCheckConfigFileAbsentIsWarn(t *testing.T) { |
118 | 132 | } |
119 | 133 |
|
120 | 134 | func TestCheckCurrentProfileDefault(t *testing.T) { |
| 135 | + clearConfigEnv(t) |
| 136 | + |
121 | 137 | ctx := cmdio.MockDiscard(t.Context()) |
122 | 138 | cmd := newTestCmd(ctx) |
123 | 139 |
|
@@ -168,36 +184,52 @@ func TestCheckAuthSuccess(t *testing.T) { |
168 | 184 | })) |
169 | 185 | defer srv.Close() |
170 | 186 |
|
| 187 | + clearConfigEnv(t) |
171 | 188 | t.Setenv("DATABRICKS_HOST", srv.URL) |
172 | 189 | t.Setenv("DATABRICKS_TOKEN", "test-token") |
173 | | - t.Setenv("DATABRICKS_CONFIG_PROFILE", "") |
174 | | - t.Setenv("HOME", t.TempDir()) |
175 | 190 |
|
176 | 191 | ctx := cmdio.MockDiscard(t.Context()) |
177 | 192 | cmd := newTestCmd(ctx) |
178 | 193 |
|
179 | | - result, w := checkAuth(cmd) |
| 194 | + cfg, err := resolveConfig(cmd) |
| 195 | + require.NoError(t, err) |
| 196 | + |
| 197 | + result, w := checkAuth(cmd, cfg, err) |
180 | 198 | assert.Equal(t, "Authentication", result.Name) |
181 | 199 | assert.Equal(t, statusPass, result.Status) |
182 | 200 | assert.Contains(t, result.Message, "OK") |
183 | 201 | assert.NotNil(t, w) |
184 | 202 | } |
185 | 203 |
|
186 | 204 | func TestCheckAuthFailure(t *testing.T) { |
187 | | - t.Setenv("DATABRICKS_HOST", "") |
188 | | - t.Setenv("DATABRICKS_TOKEN", "") |
189 | | - t.Setenv("DATABRICKS_CONFIG_PROFILE", "") |
190 | | - t.Setenv("HOME", t.TempDir()) |
| 205 | + clearConfigEnv(t) |
191 | 206 |
|
192 | 207 | ctx := cmdio.MockDiscard(t.Context()) |
193 | 208 | cmd := newTestCmd(ctx) |
194 | 209 |
|
195 | | - result, w := checkAuth(cmd) |
| 210 | + cfg, err := resolveConfig(cmd) |
| 211 | + result, w := checkAuth(cmd, cfg, err) |
196 | 212 | assert.Equal(t, "Authentication", result.Name) |
197 | 213 | assert.Equal(t, statusFail, result.Status) |
198 | 214 | assert.Nil(t, w) |
199 | 215 | } |
200 | 216 |
|
| 217 | +func TestResolveConfigUsesCommandContextEnv(t *testing.T) { |
| 218 | + clearConfigEnv(t) |
| 219 | + t.Setenv("DATABRICKS_HOST", "https://real.example.com") |
| 220 | + t.Setenv("DATABRICKS_TOKEN", "real-token") |
| 221 | + |
| 222 | + ctx := cmdio.MockDiscard(t.Context()) |
| 223 | + ctx = env.Set(ctx, "DATABRICKS_HOST", "https://context.example.com") |
| 224 | + ctx = env.Set(ctx, "DATABRICKS_TOKEN", "context-token") |
| 225 | + cmd := newTestCmd(ctx) |
| 226 | + |
| 227 | + cfg, err := resolveConfig(cmd) |
| 228 | + require.NoError(t, err) |
| 229 | + assert.Equal(t, "https://context.example.com", cfg.Host) |
| 230 | + assert.Equal(t, "context-token", cfg.Token) |
| 231 | +} |
| 232 | + |
201 | 233 | func TestCheckIdentitySuccess(t *testing.T) { |
202 | 234 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
203 | 235 | if r.URL.Path == "/api/2.0/preview/scim/v2/Me" { |
@@ -284,12 +316,34 @@ func TestCheckNetworkWithClient(t *testing.T) { |
284 | 316 | ctx := cmdio.MockDiscard(t.Context()) |
285 | 317 | cmd := newTestCmd(ctx) |
286 | 318 |
|
287 | | - result := checkNetwork(cmd, w) |
| 319 | + result := checkNetwork(cmd, w.Config, nil, w) |
288 | 320 | assert.Equal(t, "Network", result.Name) |
289 | 321 | assert.Equal(t, statusPass, result.Status) |
290 | 322 | assert.Contains(t, result.Message, "reachable") |
291 | 323 | } |
292 | 324 |
|
| 325 | +func TestCheckNetworkConfigResolutionFailure(t *testing.T) { |
| 326 | + clearConfigEnv(t) |
| 327 | + |
| 328 | + configFile := filepath.Join(t.TempDir(), ".databrickscfg") |
| 329 | + err := os.WriteFile(configFile, []byte("[DEFAULT]\nhost = https://example.com\n"), 0o600) |
| 330 | + require.NoError(t, err) |
| 331 | + |
| 332 | + ctx := cmdio.MockDiscard(t.Context()) |
| 333 | + ctx = env.Set(ctx, "DATABRICKS_CONFIG_FILE", configFile) |
| 334 | + ctx = env.Set(ctx, "DATABRICKS_CONFIG_PROFILE", "missing") |
| 335 | + cmd := newTestCmd(ctx) |
| 336 | + |
| 337 | + cfg, err := resolveConfig(cmd) |
| 338 | + require.Error(t, err) |
| 339 | + |
| 340 | + result := checkNetwork(cmd, cfg, err, nil) |
| 341 | + assert.Equal(t, "Network", result.Name) |
| 342 | + assert.Equal(t, statusFail, result.Status) |
| 343 | + assert.Equal(t, "Cannot resolve config", result.Message) |
| 344 | + assert.Contains(t, result.Detail, "missing profile") |
| 345 | +} |
| 346 | + |
293 | 347 | func TestRenderResultsText(t *testing.T) { |
294 | 348 | results := []CheckResult{ |
295 | 349 | {Name: "Test", Status: statusPass, Message: "all good"}, |
@@ -337,11 +391,9 @@ func TestRenderResultsJSONOmitsEmptyDetail(t *testing.T) { |
337 | 391 | } |
338 | 392 |
|
339 | 393 | func TestNewCommandJSON(t *testing.T) { |
| 394 | + clearConfigEnv(t) |
| 395 | + |
340 | 396 | ctx := cmdio.MockDiscard(t.Context()) |
341 | | - ctx = env.Set(ctx, "DATABRICKS_HOST", "") |
342 | | - ctx = env.Set(ctx, "DATABRICKS_TOKEN", "") |
343 | | - ctx = env.Set(ctx, "DATABRICKS_CONFIG_PROFILE", "") |
344 | | - ctx = env.Set(ctx, "HOME", t.TempDir()) |
345 | 397 | ctx = profile.WithProfiler(ctx, &mockProfiler{ |
346 | 398 | path: "/tmp/.databrickscfg", |
347 | 399 | profiles: profile.Profiles{ |
@@ -372,11 +424,9 @@ func TestNewCommandJSON(t *testing.T) { |
372 | 424 | } |
373 | 425 |
|
374 | 426 | func TestNewCommandJSONTrailingNewline(t *testing.T) { |
| 427 | + clearConfigEnv(t) |
| 428 | + |
375 | 429 | ctx := cmdio.MockDiscard(t.Context()) |
376 | | - ctx = env.Set(ctx, "DATABRICKS_HOST", "") |
377 | | - ctx = env.Set(ctx, "DATABRICKS_TOKEN", "") |
378 | | - ctx = env.Set(ctx, "DATABRICKS_CONFIG_PROFILE", "") |
379 | | - ctx = env.Set(ctx, "HOME", t.TempDir()) |
380 | 430 | ctx = profile.WithProfiler(ctx, &mockProfiler{ |
381 | 431 | path: "/tmp/.databrickscfg", |
382 | 432 | profiles: profile.Profiles{ |
|
0 commit comments