From 152d982c9bcd6c5254a7ab70e6a2c91f8caa5c65 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 3 Mar 2025 16:58:57 +0100 Subject: [PATCH] add cases for the target flag --- .../exec/databricks-cli/basic/databricks.yml | 10 +++++ .../databricks-cli/basic/out.requests.txt | 41 +++++++++++++++++++ .../exec/databricks-cli/basic/output.txt | 24 +++++++++++ .../bundle/exec/databricks-cli/basic/script | 15 +++++++ .../bundle/exec/databricks-cli/test.toml | 2 + cmd/bundle/exec.go | 29 ++++++++++++- 6 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 acceptance/bundle/exec/databricks-cli/basic/databricks.yml create mode 100644 acceptance/bundle/exec/databricks-cli/basic/out.requests.txt create mode 100644 acceptance/bundle/exec/databricks-cli/basic/output.txt create mode 100644 acceptance/bundle/exec/databricks-cli/basic/script create mode 100644 acceptance/bundle/exec/databricks-cli/test.toml diff --git a/acceptance/bundle/exec/databricks-cli/basic/databricks.yml b/acceptance/bundle/exec/databricks-cli/basic/databricks.yml new file mode 100644 index 000000000..6b782c325 --- /dev/null +++ b/acceptance/bundle/exec/databricks-cli/basic/databricks.yml @@ -0,0 +1,10 @@ +bundle: + name: foobar + +targets: + pat: + default: true + + oauth: + workspace: + client_id: client_id diff --git a/acceptance/bundle/exec/databricks-cli/basic/out.requests.txt b/acceptance/bundle/exec/databricks-cli/basic/out.requests.txt new file mode 100644 index 000000000..10a364e97 --- /dev/null +++ b/acceptance/bundle/exec/databricks-cli/basic/out.requests.txt @@ -0,0 +1,41 @@ +{ + "headers": { + "Authorization": [ + "Bearer [DATABRICKS_TOKEN]" + ] + }, + "method": "GET", + "path": "/api/2.0/preview/scim/v2/Me" +} +{ + "headers": { + "Authorization": [ + "Bearer [DATABRICKS_TOKEN]" + ] + }, + "method": "GET", + "path": "/api/2.0/preview/scim/v2/Me" +} +{ + "method": "GET", + "path": "/oidc/.well-known/oauth-authorization-server" +} +{ + "headers": { + "Authorization": [ + "Basic Y2xpZW50X2lkOmNsaWVudC1zZWNyZXQ=" + ] + }, + "method": "POST", + "path": "/oidc/v1/token", + "raw_body": "grant_type=client_credentials\u0026scope=all-apis" +} +{ + "headers": { + "Authorization": [ + "Bearer oauth-token" + ] + }, + "method": "GET", + "path": "/api/2.0/preview/scim/v2/Me" +} diff --git a/acceptance/bundle/exec/databricks-cli/basic/output.txt b/acceptance/bundle/exec/databricks-cli/basic/output.txt new file mode 100644 index 000000000..48fcf8a61 --- /dev/null +++ b/acceptance/bundle/exec/databricks-cli/basic/output.txt @@ -0,0 +1,24 @@ + +>>> [CLI] bundle exec -- databricks current-user me +{ + "id":"[USERID]", + "userName":"[USERNAME]" +} + +>>> [CLI] bundle exec -t pat -- databricks current-user me +{ + "id":"[USERID]", + "userName":"[USERNAME]" +} + +>>> errcode [CLI] bundle exec -t pat -- databricks current-user me -t oauth +Error: cannot resolve bundle auth configuration: validate: more than one authorization method configured: oauth and pat. Config: host=[DATABRICKS_URL], token=***, client_id=client_id, databricks_cli_path=[CLI]. Env: DATABRICKS_HOST, DATABRICKS_TOKEN, DATABRICKS_CLI_PATH +Error: Command exited with code: 1 + +Exit code: 1 + +>>> [CLI] bundle exec -t oauth -- databricks current-user me +{ + "id":"[USERID]", + "userName":"[USERNAME]" +} diff --git a/acceptance/bundle/exec/databricks-cli/basic/script b/acceptance/bundle/exec/databricks-cli/basic/script new file mode 100644 index 000000000..3a3c5efe9 --- /dev/null +++ b/acceptance/bundle/exec/databricks-cli/basic/script @@ -0,0 +1,15 @@ +# Default target +trace $CLI bundle exec -- databricks current-user me + +# Explicitly select default target +trace $CLI bundle exec -t pat -- databricks current-user me + +# Conflicting targets selected. This should fail because for the child command +# pat would be configured via environment variables and oauth via the CLI resulting +# in more than one authorization method configured. +trace errcode $CLI bundle exec -t pat -- databricks current-user me -t oauth + +# Explicitly select oauth target +export DATABRICKS_TOKEN="" +export DATABRICKS_CLIENT_SECRET="client-secret" +trace $CLI bundle exec -t oauth -- databricks current-user me diff --git a/acceptance/bundle/exec/databricks-cli/test.toml b/acceptance/bundle/exec/databricks-cli/test.toml new file mode 100644 index 000000000..373138e0c --- /dev/null +++ b/acceptance/bundle/exec/databricks-cli/test.toml @@ -0,0 +1,2 @@ +RecordRequests = true +IncludeRequestHeaders = ["Authorization"] diff --git a/cmd/bundle/exec.go b/cmd/bundle/exec.go index bb6ecfc11..4faac4039 100644 --- a/cmd/bundle/exec.go +++ b/cmd/bundle/exec.go @@ -14,6 +14,27 @@ import ( // TODO: test that -- works with flags as well. +// TODO: Can bundle auth be resolved twice? What about: +// databricks bundle exec -t foo -- databricks jobs list -t bar? +// OR +// databricks bundle exec -- databricks jobs list -t bar? +// OR +// databricks bundle exec -- databricks jobs list? +// OR +// databricks bundle exec -t foo -- databricks jobs list? +// +// For the first two, undefined behavior is fine. For the latter two we need to ensure +// that the target from exec is respected. +// +// Add tests for all four of these cases. +// --> Do I need similar tests for --profile as well? +// --> Also add test for what happens with a default target? + +// TODO: Add acceptance test that flags are indeed not parsed by the exec command and +// instead are parsed by the child command. + +// # TODO: Table test casing the target permutations + func newExecCommand() *cobra.Command { execCmd := &cobra.Command{ Use: "exec", @@ -41,7 +62,13 @@ Examples: childCmd := exec.Command(args[0], args[1:]...) - childCmd.Env = auth.ProcessEnv(root.ConfigUsed(cmd.Context())) + env := auth.ProcessEnv(root.ConfigUsed(cmd.Context())) + // TODO: Test that this works correctly for all permutations. + // TODO: Do the same for profile flag. + // TODO: TODO: What happens here if a default target is resolved? When + // no targets are defined? + env = append(env, "DATABRICKS_BUNDLE_TARGET="+b.Config.Bundle.Target) + childCmd.Env = env // Execute all scripts from the bundle root directory. This behavior can // be surprising in isolation, but we do it to keep the behavior consistent