Skip to content

Commit 5fe2b94

Browse files
authored
Ensure tests are only run in the selected environment (#3476)
1 parent 3bc99f6 commit 5fe2b94

File tree

1 file changed

+29
-33
lines changed

1 file changed

+29
-33
lines changed

internal/acceptance/init_test.go

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,46 +38,21 @@ func init() {
3838

3939
func workspaceLevel(t *testing.T, steps ...step) {
4040
loadWorkspaceEnv(t)
41-
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
42-
if os.Getenv("DATABRICKS_ACCOUNT_ID") != "" {
43-
skipf(t)("Skipping workspace test on account level")
44-
}
45-
t.Parallel()
4641
run(t, steps)
4742
}
4843

4944
func accountLevel(t *testing.T, steps ...step) {
5045
loadAccountEnv(t)
51-
cfg := &config.Config{
52-
AccountID: GetEnvOrSkipTest(t, "DATABRICKS_ACCOUNT_ID"),
53-
}
54-
err := cfg.EnsureResolved()
55-
if err != nil {
56-
skipf(t)("error: %s", err)
57-
}
58-
if !cfg.IsAccountClient() {
59-
skipf(t)("Not in account env: %s/%s", cfg.AccountID, cfg.Host)
60-
}
61-
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
62-
t.Parallel()
6346
run(t, steps)
6447
}
6548

6649
func unityWorkspaceLevel(t *testing.T, steps ...step) {
6750
loadUcwsEnv(t)
68-
GetEnvOrSkipTest(t, "TEST_METASTORE_ID")
69-
if os.Getenv("DATABRICKS_ACCOUNT_ID") != "" {
70-
skipf(t)("Skipping workspace test on account level")
71-
}
72-
t.Parallel()
7351
run(t, steps)
7452
}
7553

7654
func unityAccountLevel(t *testing.T, steps ...step) {
7755
loadUcacctEnv(t)
78-
GetEnvOrSkipTest(t, "DATABRICKS_ACCOUNT_ID")
79-
GetEnvOrSkipTest(t, "TEST_METASTORE_ID")
80-
t.Parallel()
8156
run(t, steps)
8257
}
8358

@@ -161,6 +136,7 @@ func run(t *testing.T, steps []step) {
161136
if cloudEnv == "" {
162137
t.Skip("Acceptance tests skipped unless env 'CLOUD_ENV' is set")
163138
}
139+
t.Parallel()
164140
provider := provider.DatabricksProvider()
165141
cwd, err := os.Getwd()
166142
if err != nil {
@@ -361,23 +337,37 @@ func setDebugLogger() {
361337
}
362338

363339
func loadWorkspaceEnv(t *testing.T) {
364-
setDebugLogger()
365-
loadDebugEnvIfRunsFromIDE(t, "workspace")
340+
initTest(t, "workspace")
341+
if os.Getenv("DATABRICKS_ACCOUNT_ID") != "" {
342+
skipf(t)("Skipping workspace test on account level")
343+
}
366344
}
367345

368346
func loadAccountEnv(t *testing.T) {
369-
setDebugLogger()
370-
loadDebugEnvIfRunsFromIDE(t, "account")
347+
initTest(t, "account")
348+
if os.Getenv("DATABRICKS_ACCOUNT_ID") == "" {
349+
skipf(t)("Skipping account test on workspace level")
350+
}
371351
}
372352

373353
func loadUcwsEnv(t *testing.T) {
374-
setDebugLogger()
375-
loadDebugEnvIfRunsFromIDE(t, "ucws")
354+
initTest(t, "ucws")
355+
if os.Getenv("TEST_METASTORE_ID") == "" {
356+
skipf(t)("Skipping non-Unity Catalog test")
357+
}
358+
if os.Getenv("DATABRICKS_ACCOUNT_ID") != "" {
359+
skipf(t)("Skipping workspace test on account level")
360+
}
376361
}
377362

378363
func loadUcacctEnv(t *testing.T) {
379-
setDebugLogger()
380-
loadDebugEnvIfRunsFromIDE(t, "ucacct")
364+
initTest(t, "ucacct")
365+
if os.Getenv("TEST_METASTORE_ID") == "" {
366+
skipf(t)("Skipping non-Unity Catalog test")
367+
}
368+
if os.Getenv("DATABRICKS_ACCOUNT_ID") == "" {
369+
skipf(t)("Skipping account test on workspace level")
370+
}
381371
}
382372

383373
func isAws(t *testing.T) bool {
@@ -417,6 +407,12 @@ func isAuthedAsWorkspaceServicePrincipal(ctx context.Context) (bool, error) {
417407
return true, nil
418408
}
419409

410+
func initTest(t *testing.T, key string) {
411+
setDebugLogger()
412+
loadDebugEnvIfRunsFromIDE(t, key)
413+
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
414+
}
415+
420416
// loads debug environment from ~/.databricks/debug-env.json
421417
func loadDebugEnvIfRunsFromIDE(t *testing.T, key string) {
422418
if !isInDebug() {

0 commit comments

Comments
 (0)