From 24b58b7a77b21d98d5ab82c552b28dd8678a067a Mon Sep 17 00:00:00 2001 From: Shreyas Goenka <shreyas.goenka@databricks.com> Date: Mon, 30 Dec 2024 18:51:42 +0530 Subject: [PATCH 1/7] Enable debugging integration tests in VSCode --- integration/assumptions/main_test.go | 4 +- integration/bundle/main_test.go | 4 +- integration/cmd/alerts/main_test.go | 4 +- integration/cmd/api/main_test.go | 4 +- integration/cmd/auth/main_test.go | 4 +- integration/cmd/clusters/main_test.go | 4 +- integration/cmd/fs/main_test.go | 4 +- integration/cmd/jobs/main_test.go | 4 +- integration/cmd/main_test.go | 4 +- integration/cmd/repos/main_test.go | 4 +- integration/cmd/secrets/main_test.go | 4 +- .../cmd/storage_credentials/main_test.go | 4 +- integration/cmd/sync/main_test.go | 4 +- integration/cmd/version/main_test.go | 4 +- integration/cmd/workspace/main_test.go | 4 +- integration/internal/acc/debug.go | 44 ----------------- integration/internal/acc/workspace.go | 20 ++------ integration/internal/main.go | 48 ++++++++++++++++++- integration/libs/filer/main_test.go | 4 +- integration/libs/git/main_test.go | 4 +- integration/libs/locker/main_test.go | 4 +- integration/libs/tags/main_test.go | 4 +- integration/python/main_test.go | 4 +- 23 files changed, 90 insertions(+), 102 deletions(-) delete mode 100644 integration/internal/acc/debug.go diff --git a/integration/assumptions/main_test.go b/integration/assumptions/main_test.go index be27613858..e0dad445fb 100644 --- a/integration/assumptions/main_test.go +++ b/integration/assumptions/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.Main] for prerequisites for running integration tests. +// See [internal.WorkspaceMain] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.Main(m) + internal.WorkspaceMain(m) } diff --git a/integration/bundle/main_test.go b/integration/bundle/main_test.go index 1c44d0aaf6..7d2612eb21 100644 --- a/integration/bundle/main_test.go +++ b/integration/bundle/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.Main] for prerequisites for running integration tests. +// See [internal.WorkspaceMain] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.Main(m) + internal.WorkspaceMain(m) } diff --git a/integration/cmd/alerts/main_test.go b/integration/cmd/alerts/main_test.go index 6987ade02b..62f68126ef 100644 --- a/integration/cmd/alerts/main_test.go +++ b/integration/cmd/alerts/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.Main] for prerequisites for running integration tests. +// See [internal.WorkspaceMain] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.Main(m) + internal.WorkspaceMain(m) } diff --git a/integration/cmd/api/main_test.go b/integration/cmd/api/main_test.go index 70d021790d..ae40e7e8e2 100644 --- a/integration/cmd/api/main_test.go +++ b/integration/cmd/api/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.Main] for prerequisites for running integration tests. +// See [internal.WorkspaceMain] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.Main(m) + internal.WorkspaceMain(m) } diff --git a/integration/cmd/auth/main_test.go b/integration/cmd/auth/main_test.go index 97b1d740b4..8911dc8ae0 100644 --- a/integration/cmd/auth/main_test.go +++ b/integration/cmd/auth/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.Main] for prerequisites for running integration tests. +// See [internal.WorkspaceMain] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.Main(m) + internal.WorkspaceMain(m) } diff --git a/integration/cmd/clusters/main_test.go b/integration/cmd/clusters/main_test.go index ccd5660e70..97df93a42f 100644 --- a/integration/cmd/clusters/main_test.go +++ b/integration/cmd/clusters/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.Main] for prerequisites for running integration tests. +// See [internal.WorkspaceMain] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.Main(m) + internal.WorkspaceMain(m) } diff --git a/integration/cmd/fs/main_test.go b/integration/cmd/fs/main_test.go index b9402f0b29..edfb1c1859 100644 --- a/integration/cmd/fs/main_test.go +++ b/integration/cmd/fs/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.Main] for prerequisites for running integration tests. +// See [internal.WorkspaceMain] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.Main(m) + internal.WorkspaceMain(m) } diff --git a/integration/cmd/jobs/main_test.go b/integration/cmd/jobs/main_test.go index 46369a5264..bca86cf893 100644 --- a/integration/cmd/jobs/main_test.go +++ b/integration/cmd/jobs/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.Main] for prerequisites for running integration tests. +// See [internal.WorkspaceMain] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.Main(m) + internal.WorkspaceMain(m) } diff --git a/integration/cmd/main_test.go b/integration/cmd/main_test.go index a1a5586b64..3639999c85 100644 --- a/integration/cmd/main_test.go +++ b/integration/cmd/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.Main] for prerequisites for running integration tests. +// See [internal.WorkspaceMain] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.Main(m) + internal.WorkspaceMain(m) } diff --git a/integration/cmd/repos/main_test.go b/integration/cmd/repos/main_test.go index 7eaa174bce..5bfbbcb0c4 100644 --- a/integration/cmd/repos/main_test.go +++ b/integration/cmd/repos/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.Main] for prerequisites for running integration tests. +// See [internal.WorkspaceMain] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.Main(m) + internal.WorkspaceMain(m) } diff --git a/integration/cmd/secrets/main_test.go b/integration/cmd/secrets/main_test.go index a44d306711..87d7a772ab 100644 --- a/integration/cmd/secrets/main_test.go +++ b/integration/cmd/secrets/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.Main] for prerequisites for running integration tests. +// See [internal.WorkspaceMain] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.Main(m) + internal.WorkspaceMain(m) } diff --git a/integration/cmd/storage_credentials/main_test.go b/integration/cmd/storage_credentials/main_test.go index 14d00d9660..67951affce 100644 --- a/integration/cmd/storage_credentials/main_test.go +++ b/integration/cmd/storage_credentials/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.Main] for prerequisites for running integration tests. +// See [internal.WorkspaceMain] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.Main(m) + internal.WorkspaceMain(m) } diff --git a/integration/cmd/sync/main_test.go b/integration/cmd/sync/main_test.go index 8d9f3ca252..67f500d7f2 100644 --- a/integration/cmd/sync/main_test.go +++ b/integration/cmd/sync/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.Main] for prerequisites for running integration tests. +// See [internal.WorkspaceMain] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.Main(m) + internal.WorkspaceMain(m) } diff --git a/integration/cmd/version/main_test.go b/integration/cmd/version/main_test.go index 4aa5e046ac..48697cfced 100644 --- a/integration/cmd/version/main_test.go +++ b/integration/cmd/version/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.Main] for prerequisites for running integration tests. +// See [internal.WorkspaceMain] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.Main(m) + internal.WorkspaceMain(m) } diff --git a/integration/cmd/workspace/main_test.go b/integration/cmd/workspace/main_test.go index 40d140eac9..c90b528b46 100644 --- a/integration/cmd/workspace/main_test.go +++ b/integration/cmd/workspace/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.Main] for prerequisites for running integration tests. +// See [internal.WorkspaceMain] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.Main(m) + internal.WorkspaceMain(m) } diff --git a/integration/internal/acc/debug.go b/integration/internal/acc/debug.go deleted file mode 100644 index b4939881ee..0000000000 --- a/integration/internal/acc/debug.go +++ /dev/null @@ -1,44 +0,0 @@ -package acc - -import ( - "encoding/json" - "os" - "path" - "path/filepath" - "strings" - - "github.com/databricks/cli/internal/testutil" -) - -// Detects if test is run from "debug test" feature in VS Code. -func isInDebug() bool { - ex, _ := os.Executable() - return strings.HasPrefix(path.Base(ex), "__debug_bin") -} - -// Loads debug environment from ~/.databricks/debug-env.json. -func loadDebugEnvIfRunFromIDE(t testutil.TestingT, key string) { - if !isInDebug() { - return - } - home, err := os.UserHomeDir() - if err != nil { - t.Fatalf("cannot find user home: %s", err) - } - raw, err := os.ReadFile(filepath.Join(home, ".databricks/debug-env.json")) - if err != nil { - t.Fatalf("cannot load ~/.databricks/debug-env.json: %s", err) - } - var conf map[string]map[string]string - err = json.Unmarshal(raw, &conf) - if err != nil { - t.Fatalf("cannot parse ~/.databricks/debug-env.json: %s", err) - } - vars, ok := conf[key] - if !ok { - t.Fatalf("~/.databricks/debug-env.json#%s not configured", key) - } - for k, v := range vars { - os.Setenv(k, v) - } -} diff --git a/integration/internal/acc/workspace.go b/integration/internal/acc/workspace.go index 2f8a5b8e7f..08dd1dee42 100644 --- a/integration/internal/acc/workspace.go +++ b/integration/internal/acc/workspace.go @@ -21,19 +21,13 @@ type WorkspaceT struct { } func WorkspaceTest(t testutil.TestingT) (context.Context, *WorkspaceT) { - loadDebugEnvIfRunFromIDE(t, "workspace") - - t.Log(testutil.GetEnvOrSkipTest(t, "CLOUD_ENV")) - w, err := databricks.NewWorkspaceClient() require.NoError(t, err) wt := &WorkspaceT{ TestingT: t, - - W: w, - - ctx: context.Background(), + W: w, + ctx: context.Background(), } return wt.ctx, wt @@ -41,10 +35,6 @@ func WorkspaceTest(t testutil.TestingT) (context.Context, *WorkspaceT) { // Run the workspace test only on UC workspaces. func UcWorkspaceTest(t testutil.TestingT) (context.Context, *WorkspaceT) { - loadDebugEnvIfRunFromIDE(t, "workspace") - - t.Log(testutil.GetEnvOrSkipTest(t, "CLOUD_ENV")) - if os.Getenv("TEST_METASTORE_ID") == "" { t.Skipf("Skipping on non-UC workspaces") } @@ -57,10 +47,8 @@ func UcWorkspaceTest(t testutil.TestingT) (context.Context, *WorkspaceT) { wt := &WorkspaceT{ TestingT: t, - - W: w, - - ctx: context.Background(), + W: w, + ctx: context.Background(), } return wt.ctx, wt diff --git a/integration/internal/main.go b/integration/internal/main.go index 6d69dcf70e..9407ea3921 100644 --- a/integration/internal/main.go +++ b/integration/internal/main.go @@ -1,15 +1,59 @@ package internal import ( + "encoding/json" "fmt" "os" + "path" + "path/filepath" + "strings" "testing" ) -// Main is the entry point for integration tests. +// Detects if test is run from "debug test" feature in VS Code. +func isInDebug() bool { + ex, _ := os.Executable() + return strings.HasPrefix(path.Base(ex), "__debug_bin") +} + +// Loads debug environment from ~/.databricks/debug-env.json. +func loadDebugEnvIfRunFromIDE(key string) error { + if !isInDebug() { + return nil + } + home, err := os.UserHomeDir() + if err != nil { + return fmt.Errorf("cannot find user home: %s", err) + } + raw, err := os.ReadFile(filepath.Join(home, ".databricks/debug-env.json")) + if err != nil { + return fmt.Errorf("cannot load ~/.databricks/debug-env.json: %s", err) + } + var conf map[string]map[string]string + err = json.Unmarshal(raw, &conf) + if err != nil { + return fmt.Errorf("cannot parse ~/.databricks/debug-env.json: %s", err) + } + vars, ok := conf[key] + if !ok { + return fmt.Errorf("~/.databricks/debug-env.json is not configured", key) + } + for k, v := range vars { + os.Setenv(k, v) + } + return nil +} + +// WorkspaceMain is the entry point for integration tests that run against a Databricks workspace. // We use this for all integration tests defined in this subtree to ensure // they are not inadvertently executed when calling `go test ./...`. -func Main(m *testing.M) { +func WorkspaceMain(m *testing.M) { + err := loadDebugEnvIfRunFromIDE("workspace") + if err != nil { + fmt.Printf("failed to load debug env: %w\n", err) + return + } + value := os.Getenv("CLOUD_ENV") if value == "" { fmt.Println("CLOUD_ENV is not set, skipping integration tests") diff --git a/integration/libs/filer/main_test.go b/integration/libs/filer/main_test.go index ca866d9526..5e2a474d58 100644 --- a/integration/libs/filer/main_test.go +++ b/integration/libs/filer/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.Main] for prerequisites for running integration tests. +// See [internal.WorkspaceMain] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.Main(m) + internal.WorkspaceMain(m) } diff --git a/integration/libs/git/main_test.go b/integration/libs/git/main_test.go index 5d68e08512..df76df8501 100644 --- a/integration/libs/git/main_test.go +++ b/integration/libs/git/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.Main] for prerequisites for running integration tests. +// See [internal.WorkspaceMain] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.Main(m) + internal.WorkspaceMain(m) } diff --git a/integration/libs/locker/main_test.go b/integration/libs/locker/main_test.go index 33a883768d..1d0ac781d6 100644 --- a/integration/libs/locker/main_test.go +++ b/integration/libs/locker/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.Main] for prerequisites for running integration tests. +// See [internal.WorkspaceMain] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.Main(m) + internal.WorkspaceMain(m) } diff --git a/integration/libs/tags/main_test.go b/integration/libs/tags/main_test.go index 4eaf54a20f..ea800c94f0 100644 --- a/integration/libs/tags/main_test.go +++ b/integration/libs/tags/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.Main] for prerequisites for running integration tests. +// See [internal.WorkspaceMain] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.Main(m) + internal.WorkspaceMain(m) } diff --git a/integration/python/main_test.go b/integration/python/main_test.go index b35da21e1d..e768e21335 100644 --- a/integration/python/main_test.go +++ b/integration/python/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.Main] for prerequisites for running integration tests. +// See [internal.WorkspaceMain] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.Main(m) + internal.WorkspaceMain(m) } From 2424a87453c5cd41f4f448f80e4e87d2c93bf330 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka <shreyas.goenka@databricks.com> Date: Mon, 30 Dec 2024 18:54:54 +0530 Subject: [PATCH 2/7] - --- integration/internal/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration/internal/main.go b/integration/internal/main.go index 9407ea3921..36f2b47566 100644 --- a/integration/internal/main.go +++ b/integration/internal/main.go @@ -36,7 +36,7 @@ func loadDebugEnvIfRunFromIDE(key string) error { } vars, ok := conf[key] if !ok { - return fmt.Errorf("~/.databricks/debug-env.json is not configured", key) + return fmt.Errorf("%s is not configured in ~/.databricks/debug-env.json", key) } for k, v := range vars { os.Setenv(k, v) From e8aa7cc8baaa27e015eafcdc47a86b292a04cd3e Mon Sep 17 00:00:00 2001 From: Shreyas Goenka <shreyas.goenka@databricks.com> Date: Mon, 30 Dec 2024 19:04:24 +0530 Subject: [PATCH 3/7] lint --- integration/internal/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration/internal/main.go b/integration/internal/main.go index 36f2b47566..e6b151dd52 100644 --- a/integration/internal/main.go +++ b/integration/internal/main.go @@ -50,7 +50,7 @@ func loadDebugEnvIfRunFromIDE(key string) error { func WorkspaceMain(m *testing.M) { err := loadDebugEnvIfRunFromIDE("workspace") if err != nil { - fmt.Printf("failed to load debug env: %w\n", err) + fmt.Printf("failed to load debug env: %s\n", err) return } From b9a4b93690ca893e916abb97215b22db91a6d538 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka <shreyas.goenka@databricks.com> Date: Thu, 2 Jan 2025 14:51:09 +0530 Subject: [PATCH 4/7] Revert "lint" This reverts commit e8aa7cc8baaa27e015eafcdc47a86b292a04cd3e. --- integration/internal/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration/internal/main.go b/integration/internal/main.go index e6b151dd52..36f2b47566 100644 --- a/integration/internal/main.go +++ b/integration/internal/main.go @@ -50,7 +50,7 @@ func loadDebugEnvIfRunFromIDE(key string) error { func WorkspaceMain(m *testing.M) { err := loadDebugEnvIfRunFromIDE("workspace") if err != nil { - fmt.Printf("failed to load debug env: %s\n", err) + fmt.Printf("failed to load debug env: %w\n", err) return } From 88e1ba6bec3decf00e341bea15593b8706ec07f0 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka <shreyas.goenka@databricks.com> Date: Thu, 2 Jan 2025 14:51:10 +0530 Subject: [PATCH 5/7] Revert "-" This reverts commit 2424a87453c5cd41f4f448f80e4e87d2c93bf330. --- integration/internal/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration/internal/main.go b/integration/internal/main.go index 36f2b47566..9407ea3921 100644 --- a/integration/internal/main.go +++ b/integration/internal/main.go @@ -36,7 +36,7 @@ func loadDebugEnvIfRunFromIDE(key string) error { } vars, ok := conf[key] if !ok { - return fmt.Errorf("%s is not configured in ~/.databricks/debug-env.json", key) + return fmt.Errorf("~/.databricks/debug-env.json is not configured", key) } for k, v := range vars { os.Setenv(k, v) From be51d604f9a6668825b3afb16c59ac6877b73393 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka <shreyas.goenka@databricks.com> Date: Thu, 2 Jan 2025 14:51:24 +0530 Subject: [PATCH 6/7] Revert "Enable debugging integration tests in VSCode" This reverts commit 24b58b7a77b21d98d5ab82c552b28dd8678a067a. --- integration/assumptions/main_test.go | 4 +- integration/bundle/main_test.go | 4 +- integration/cmd/alerts/main_test.go | 4 +- integration/cmd/api/main_test.go | 4 +- integration/cmd/auth/main_test.go | 4 +- integration/cmd/clusters/main_test.go | 4 +- integration/cmd/fs/main_test.go | 4 +- integration/cmd/jobs/main_test.go | 4 +- integration/cmd/main_test.go | 4 +- integration/cmd/repos/main_test.go | 4 +- integration/cmd/secrets/main_test.go | 4 +- .../cmd/storage_credentials/main_test.go | 4 +- integration/cmd/sync/main_test.go | 4 +- integration/cmd/version/main_test.go | 4 +- integration/cmd/workspace/main_test.go | 4 +- integration/internal/acc/debug.go | 44 +++++++++++++++++ integration/internal/acc/workspace.go | 20 ++++++-- integration/internal/main.go | 48 +------------------ integration/libs/filer/main_test.go | 4 +- integration/libs/git/main_test.go | 4 +- integration/libs/locker/main_test.go | 4 +- integration/libs/tags/main_test.go | 4 +- integration/python/main_test.go | 4 +- 23 files changed, 102 insertions(+), 90 deletions(-) create mode 100644 integration/internal/acc/debug.go diff --git a/integration/assumptions/main_test.go b/integration/assumptions/main_test.go index e0dad445fb..be27613858 100644 --- a/integration/assumptions/main_test.go +++ b/integration/assumptions/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.WorkspaceMain] for prerequisites for running integration tests. +// See [internal.Main] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.WorkspaceMain(m) + internal.Main(m) } diff --git a/integration/bundle/main_test.go b/integration/bundle/main_test.go index 7d2612eb21..1c44d0aaf6 100644 --- a/integration/bundle/main_test.go +++ b/integration/bundle/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.WorkspaceMain] for prerequisites for running integration tests. +// See [internal.Main] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.WorkspaceMain(m) + internal.Main(m) } diff --git a/integration/cmd/alerts/main_test.go b/integration/cmd/alerts/main_test.go index 62f68126ef..6987ade02b 100644 --- a/integration/cmd/alerts/main_test.go +++ b/integration/cmd/alerts/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.WorkspaceMain] for prerequisites for running integration tests. +// See [internal.Main] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.WorkspaceMain(m) + internal.Main(m) } diff --git a/integration/cmd/api/main_test.go b/integration/cmd/api/main_test.go index ae40e7e8e2..70d021790d 100644 --- a/integration/cmd/api/main_test.go +++ b/integration/cmd/api/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.WorkspaceMain] for prerequisites for running integration tests. +// See [internal.Main] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.WorkspaceMain(m) + internal.Main(m) } diff --git a/integration/cmd/auth/main_test.go b/integration/cmd/auth/main_test.go index 8911dc8ae0..97b1d740b4 100644 --- a/integration/cmd/auth/main_test.go +++ b/integration/cmd/auth/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.WorkspaceMain] for prerequisites for running integration tests. +// See [internal.Main] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.WorkspaceMain(m) + internal.Main(m) } diff --git a/integration/cmd/clusters/main_test.go b/integration/cmd/clusters/main_test.go index 97df93a42f..ccd5660e70 100644 --- a/integration/cmd/clusters/main_test.go +++ b/integration/cmd/clusters/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.WorkspaceMain] for prerequisites for running integration tests. +// See [internal.Main] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.WorkspaceMain(m) + internal.Main(m) } diff --git a/integration/cmd/fs/main_test.go b/integration/cmd/fs/main_test.go index edfb1c1859..b9402f0b29 100644 --- a/integration/cmd/fs/main_test.go +++ b/integration/cmd/fs/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.WorkspaceMain] for prerequisites for running integration tests. +// See [internal.Main] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.WorkspaceMain(m) + internal.Main(m) } diff --git a/integration/cmd/jobs/main_test.go b/integration/cmd/jobs/main_test.go index bca86cf893..46369a5264 100644 --- a/integration/cmd/jobs/main_test.go +++ b/integration/cmd/jobs/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.WorkspaceMain] for prerequisites for running integration tests. +// See [internal.Main] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.WorkspaceMain(m) + internal.Main(m) } diff --git a/integration/cmd/main_test.go b/integration/cmd/main_test.go index 3639999c85..a1a5586b64 100644 --- a/integration/cmd/main_test.go +++ b/integration/cmd/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.WorkspaceMain] for prerequisites for running integration tests. +// See [internal.Main] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.WorkspaceMain(m) + internal.Main(m) } diff --git a/integration/cmd/repos/main_test.go b/integration/cmd/repos/main_test.go index 5bfbbcb0c4..7eaa174bce 100644 --- a/integration/cmd/repos/main_test.go +++ b/integration/cmd/repos/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.WorkspaceMain] for prerequisites for running integration tests. +// See [internal.Main] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.WorkspaceMain(m) + internal.Main(m) } diff --git a/integration/cmd/secrets/main_test.go b/integration/cmd/secrets/main_test.go index 87d7a772ab..a44d306711 100644 --- a/integration/cmd/secrets/main_test.go +++ b/integration/cmd/secrets/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.WorkspaceMain] for prerequisites for running integration tests. +// See [internal.Main] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.WorkspaceMain(m) + internal.Main(m) } diff --git a/integration/cmd/storage_credentials/main_test.go b/integration/cmd/storage_credentials/main_test.go index 67951affce..14d00d9660 100644 --- a/integration/cmd/storage_credentials/main_test.go +++ b/integration/cmd/storage_credentials/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.WorkspaceMain] for prerequisites for running integration tests. +// See [internal.Main] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.WorkspaceMain(m) + internal.Main(m) } diff --git a/integration/cmd/sync/main_test.go b/integration/cmd/sync/main_test.go index 67f500d7f2..8d9f3ca252 100644 --- a/integration/cmd/sync/main_test.go +++ b/integration/cmd/sync/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.WorkspaceMain] for prerequisites for running integration tests. +// See [internal.Main] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.WorkspaceMain(m) + internal.Main(m) } diff --git a/integration/cmd/version/main_test.go b/integration/cmd/version/main_test.go index 48697cfced..4aa5e046ac 100644 --- a/integration/cmd/version/main_test.go +++ b/integration/cmd/version/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.WorkspaceMain] for prerequisites for running integration tests. +// See [internal.Main] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.WorkspaceMain(m) + internal.Main(m) } diff --git a/integration/cmd/workspace/main_test.go b/integration/cmd/workspace/main_test.go index c90b528b46..40d140eac9 100644 --- a/integration/cmd/workspace/main_test.go +++ b/integration/cmd/workspace/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.WorkspaceMain] for prerequisites for running integration tests. +// See [internal.Main] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.WorkspaceMain(m) + internal.Main(m) } diff --git a/integration/internal/acc/debug.go b/integration/internal/acc/debug.go new file mode 100644 index 0000000000..b4939881ee --- /dev/null +++ b/integration/internal/acc/debug.go @@ -0,0 +1,44 @@ +package acc + +import ( + "encoding/json" + "os" + "path" + "path/filepath" + "strings" + + "github.com/databricks/cli/internal/testutil" +) + +// Detects if test is run from "debug test" feature in VS Code. +func isInDebug() bool { + ex, _ := os.Executable() + return strings.HasPrefix(path.Base(ex), "__debug_bin") +} + +// Loads debug environment from ~/.databricks/debug-env.json. +func loadDebugEnvIfRunFromIDE(t testutil.TestingT, key string) { + if !isInDebug() { + return + } + home, err := os.UserHomeDir() + if err != nil { + t.Fatalf("cannot find user home: %s", err) + } + raw, err := os.ReadFile(filepath.Join(home, ".databricks/debug-env.json")) + if err != nil { + t.Fatalf("cannot load ~/.databricks/debug-env.json: %s", err) + } + var conf map[string]map[string]string + err = json.Unmarshal(raw, &conf) + if err != nil { + t.Fatalf("cannot parse ~/.databricks/debug-env.json: %s", err) + } + vars, ok := conf[key] + if !ok { + t.Fatalf("~/.databricks/debug-env.json#%s not configured", key) + } + for k, v := range vars { + os.Setenv(k, v) + } +} diff --git a/integration/internal/acc/workspace.go b/integration/internal/acc/workspace.go index 08dd1dee42..2f8a5b8e7f 100644 --- a/integration/internal/acc/workspace.go +++ b/integration/internal/acc/workspace.go @@ -21,13 +21,19 @@ type WorkspaceT struct { } func WorkspaceTest(t testutil.TestingT) (context.Context, *WorkspaceT) { + loadDebugEnvIfRunFromIDE(t, "workspace") + + t.Log(testutil.GetEnvOrSkipTest(t, "CLOUD_ENV")) + w, err := databricks.NewWorkspaceClient() require.NoError(t, err) wt := &WorkspaceT{ TestingT: t, - W: w, - ctx: context.Background(), + + W: w, + + ctx: context.Background(), } return wt.ctx, wt @@ -35,6 +41,10 @@ func WorkspaceTest(t testutil.TestingT) (context.Context, *WorkspaceT) { // Run the workspace test only on UC workspaces. func UcWorkspaceTest(t testutil.TestingT) (context.Context, *WorkspaceT) { + loadDebugEnvIfRunFromIDE(t, "workspace") + + t.Log(testutil.GetEnvOrSkipTest(t, "CLOUD_ENV")) + if os.Getenv("TEST_METASTORE_ID") == "" { t.Skipf("Skipping on non-UC workspaces") } @@ -47,8 +57,10 @@ func UcWorkspaceTest(t testutil.TestingT) (context.Context, *WorkspaceT) { wt := &WorkspaceT{ TestingT: t, - W: w, - ctx: context.Background(), + + W: w, + + ctx: context.Background(), } return wt.ctx, wt diff --git a/integration/internal/main.go b/integration/internal/main.go index 9407ea3921..6d69dcf70e 100644 --- a/integration/internal/main.go +++ b/integration/internal/main.go @@ -1,59 +1,15 @@ package internal import ( - "encoding/json" "fmt" "os" - "path" - "path/filepath" - "strings" "testing" ) -// Detects if test is run from "debug test" feature in VS Code. -func isInDebug() bool { - ex, _ := os.Executable() - return strings.HasPrefix(path.Base(ex), "__debug_bin") -} - -// Loads debug environment from ~/.databricks/debug-env.json. -func loadDebugEnvIfRunFromIDE(key string) error { - if !isInDebug() { - return nil - } - home, err := os.UserHomeDir() - if err != nil { - return fmt.Errorf("cannot find user home: %s", err) - } - raw, err := os.ReadFile(filepath.Join(home, ".databricks/debug-env.json")) - if err != nil { - return fmt.Errorf("cannot load ~/.databricks/debug-env.json: %s", err) - } - var conf map[string]map[string]string - err = json.Unmarshal(raw, &conf) - if err != nil { - return fmt.Errorf("cannot parse ~/.databricks/debug-env.json: %s", err) - } - vars, ok := conf[key] - if !ok { - return fmt.Errorf("~/.databricks/debug-env.json is not configured", key) - } - for k, v := range vars { - os.Setenv(k, v) - } - return nil -} - -// WorkspaceMain is the entry point for integration tests that run against a Databricks workspace. +// Main is the entry point for integration tests. // We use this for all integration tests defined in this subtree to ensure // they are not inadvertently executed when calling `go test ./...`. -func WorkspaceMain(m *testing.M) { - err := loadDebugEnvIfRunFromIDE("workspace") - if err != nil { - fmt.Printf("failed to load debug env: %w\n", err) - return - } - +func Main(m *testing.M) { value := os.Getenv("CLOUD_ENV") if value == "" { fmt.Println("CLOUD_ENV is not set, skipping integration tests") diff --git a/integration/libs/filer/main_test.go b/integration/libs/filer/main_test.go index 5e2a474d58..ca866d9526 100644 --- a/integration/libs/filer/main_test.go +++ b/integration/libs/filer/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.WorkspaceMain] for prerequisites for running integration tests. +// See [internal.Main] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.WorkspaceMain(m) + internal.Main(m) } diff --git a/integration/libs/git/main_test.go b/integration/libs/git/main_test.go index df76df8501..5d68e08512 100644 --- a/integration/libs/git/main_test.go +++ b/integration/libs/git/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.WorkspaceMain] for prerequisites for running integration tests. +// See [internal.Main] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.WorkspaceMain(m) + internal.Main(m) } diff --git a/integration/libs/locker/main_test.go b/integration/libs/locker/main_test.go index 1d0ac781d6..33a883768d 100644 --- a/integration/libs/locker/main_test.go +++ b/integration/libs/locker/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.WorkspaceMain] for prerequisites for running integration tests. +// See [internal.Main] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.WorkspaceMain(m) + internal.Main(m) } diff --git a/integration/libs/tags/main_test.go b/integration/libs/tags/main_test.go index ea800c94f0..4eaf54a20f 100644 --- a/integration/libs/tags/main_test.go +++ b/integration/libs/tags/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.WorkspaceMain] for prerequisites for running integration tests. +// See [internal.Main] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.WorkspaceMain(m) + internal.Main(m) } diff --git a/integration/python/main_test.go b/integration/python/main_test.go index e768e21335..b35da21e1d 100644 --- a/integration/python/main_test.go +++ b/integration/python/main_test.go @@ -7,7 +7,7 @@ import ( ) // TestMain is the entrypoint executed by the test runner. -// See [internal.WorkspaceMain] for prerequisites for running integration tests. +// See [internal.Main] for prerequisites for running integration tests. func TestMain(m *testing.M) { - internal.WorkspaceMain(m) + internal.Main(m) } From 12b5071850b3a5b153b9686665600231f5ed0902 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka <shreyas.goenka@databricks.com> Date: Thu, 2 Jan 2025 14:55:08 +0530 Subject: [PATCH 7/7] address comments --- integration/internal/acc/debug.go | 4 ++-- integration/internal/main.go | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/integration/internal/acc/debug.go b/integration/internal/acc/debug.go index b4939881ee..08e385b09c 100644 --- a/integration/internal/acc/debug.go +++ b/integration/internal/acc/debug.go @@ -11,14 +11,14 @@ import ( ) // Detects if test is run from "debug test" feature in VS Code. -func isInDebug() bool { +func IsInDebug() bool { ex, _ := os.Executable() return strings.HasPrefix(path.Base(ex), "__debug_bin") } // Loads debug environment from ~/.databricks/debug-env.json. func loadDebugEnvIfRunFromIDE(t testutil.TestingT, key string) { - if !isInDebug() { + if !IsInDebug() { return } home, err := os.UserHomeDir() diff --git a/integration/internal/main.go b/integration/internal/main.go index 6d69dcf70e..6aa2a4c93a 100644 --- a/integration/internal/main.go +++ b/integration/internal/main.go @@ -4,6 +4,8 @@ import ( "fmt" "os" "testing" + + "github.com/databricks/cli/integration/internal/acc" ) // Main is the entry point for integration tests. @@ -11,7 +13,7 @@ import ( // they are not inadvertently executed when calling `go test ./...`. func Main(m *testing.M) { value := os.Getenv("CLOUD_ENV") - if value == "" { + if value == "" && !acc.IsInDebug() { fmt.Println("CLOUD_ENV is not set, skipping integration tests") return }