From 526904168b4c1092afdb414bb413faae78715e92 Mon Sep 17 00:00:00 2001 From: Miles Yucht Date: Mon, 8 Jan 2024 09:06:23 +0100 Subject: [PATCH] Fix TestAccServicePrinicpalHomeDeleteNotDeleted (#3075) * Fix service principal nightly test * undo comment --- internal/acceptance/service_principal_test.go | 8 ++--- internal/acceptance/user_test.go | 32 +++++++++---------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/internal/acceptance/service_principal_test.go b/internal/acceptance/service_principal_test.go index 541beeca10..63c23ef175 100644 --- a/internal/acceptance/service_principal_test.go +++ b/internal/acceptance/service_principal_test.go @@ -61,6 +61,7 @@ func TestAccServicePrincipalHomeDeleteSuccess(t *testing.T) { func TestAccServicePrinicpalHomeDeleteNotDeleted(t *testing.T) { GetEnvOrSkipTest(t, "ARM_CLIENT_ID") + var appId string workspaceLevel(t, step{ Template: ` resource "databricks_service_principal" "a" { @@ -68,9 +69,8 @@ func TestAccServicePrinicpalHomeDeleteNotDeleted(t *testing.T) { force_delete_home_dir = false }`, Check: func(s *terraform.State) error { - appId := s.RootModule().Resources["databricks_service_principal.a"].Primary.Attributes["application_id"] - os.Setenv("application_id_a", appId) - return nil + appId = s.RootModule().Resources["databricks_service_principal.a"].Primary.Attributes["application_id"] + return provisionHomeFolder(context.Background(), s, "databricks_service_principal.a", appId) }, }, step{ Template: ` @@ -84,9 +84,7 @@ func TestAccServicePrinicpalHomeDeleteNotDeleted(t *testing.T) { return err } ctx := context.Background() - appId := os.Getenv("application_id_a") _, err = w.Workspace.GetStatusByPath(ctx, fmt.Sprintf("/Users/%v", appId)) - os.Remove("application_id_a") return err }, }) diff --git a/internal/acceptance/user_test.go b/internal/acceptance/user_test.go index 0e4cc791e9..aa496d2b43 100644 --- a/internal/acceptance/user_test.go +++ b/internal/acceptance/user_test.go @@ -96,6 +96,20 @@ func TestAccUserHomeDelete(t *testing.T) { }) } +func provisionHomeFolder(ctx context.Context, s *terraform.State, tfAttribute, username string) error { + client, err := client.New(&config.Config{}) + if err != nil { + return err + } + userId := s.Modules[0].Resources[tfAttribute].Primary.ID + return client.Do(ctx, "PUT", fmt.Sprintf("/api/2.0/workspace/user/%s/homefolder", userId), nil, map[string]any{ + "user": map[string]any{ + "user_id": userId, + "username": username, + }, + }, nil) +} + func TestAccUserHomeDeleteNotDeleted(t *testing.T) { username := qa.RandomEmail() workspaceLevel(t, step{ @@ -104,23 +118,7 @@ func TestAccUserHomeDeleteNotDeleted(t *testing.T) { user_name = "` + username + `" }`, Check: func(s *terraform.State) error { - client, err := client.New(&config.Config{}) - if err != nil { - return err - } - ctx := context.Background() - // Force creation of the home folder in case it is lazy - userId := s.Modules[0].Resources["databricks_user.a"].Primary.ID - err = client.Do(ctx, "PUT", fmt.Sprintf("/api/2.0/workspace/user/%s/homefolder", userId), nil, map[string]any{ - "user": map[string]any{ - "user_id": userId, - "username": username, - }, - }, nil) - if err != nil { - return err - } - return nil + return provisionHomeFolder(context.Background(), s, "databricks_user.a", username) }, }, step{ Template: `