Skip to content

Commit

Permalink
Fill in the can-force-delete unavailable unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
JarrettSpiker committed Oct 26, 2022
1 parent 170accb commit 939b529
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 52 deletions.
2 changes: 1 addition & 1 deletion tfe/client_mock_workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (m *mockWorkspaces) ReadByID(ctx context.Context, workspaceID string) (*tfe
return workspace, nil
}
}
return nil, fmt.Errorf("no workspace found with id %s", workspaceID)
return nil, tfe.ErrResourceNotFound
}

func (m *mockWorkspaces) Update(ctx context.Context, organization, workspace string, options tfe.WorkspaceUpdateOptions) (*tfe.Workspace, error) {
Expand Down
80 changes: 29 additions & 51 deletions tfe/resource_tfe_workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1812,7 +1812,7 @@ func TestAccTFEWorkspace_paginatedRemoteStateConsumers(t *testing.T) {
})
}

func TestAccTFEWorkspace_deleteWithForceDeleteSettingDisabled(t *testing.T) {
func TestAccTFEWorkspace_delete_forceDeleteSettingDisabled(t *testing.T) {
workspace := &tfe.Workspace{}
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
tfeClient, err := getClientUsingEnv()
Expand Down Expand Up @@ -1862,7 +1862,7 @@ func TestAccTFEWorkspace_deleteWithForceDeleteSettingDisabled(t *testing.T) {
})
}

func TestAccTFEWorkspace_deleteWithForceDeleteSettingEnabled(t *testing.T) {
func TestAccTFEWorkspace_delete_forceDeleteSettingEnabled(t *testing.T) {
workspace := &tfe.Workspace{}
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
tfeClient, err := getClientUsingEnv()
Expand Down Expand Up @@ -1898,45 +1898,11 @@ func TestAccTFEWorkspace_deleteWithForceDeleteSettingEnabled(t *testing.T) {
})
}

func TestAccTFEWorkspace_deleteWithoutPermissionAndSettingDisabled(t *testing.T) {
workspace := &tfe.Workspace{}
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
tfeClient, err := getClientUsingEnv()
if err != nil {
t.Fatal(err)
}
func TestTFEWorkspace_delete_withoutCanForceDeletePermission(t *testing.T) {
// This test checks that workspace deletion works as expected when communicating with TFE servers which do not send
// the CanForceDelete workspace permission. To simulate this we use the mock workspaces client and call the
// workspace resource delete function directly, rather than use the usual resource.Test runner

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFEWorkspaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFEWorkspace_basic(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEWorkspaceExists(
"tfe_workspace.foobar", workspace),
testAccCheckTFEWorkspaceAttributes(workspace),
),
},
{
PreConfig: func() {
_, err := tfeClient.Workspaces.Lock(ctx, workspace.ID, tfe.WorkspaceLockOptions{})
if err != nil {
t.Fatal(err)
}
},
Config: testAccTFEWorkspace_basicDeleted(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEWorkspaceDestroy,
),
},
},
})

}

func TestAccTFEWorkspace_deleteWithoutPermissionAndSettingEnabled(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
orgName := fmt.Sprintf("test-orgnaization-%d", rInt)

Expand All @@ -1950,15 +1916,7 @@ func TestAccTFEWorkspace_deleteWithoutPermissionAndSettingEnabled(t *testing.T)
}
workspace.Permissions.CanForceDelete = nil

rd := &schema.ResourceData{
//schema: &Schema{
// "force_delete": {
// Type: schema.TypeBool,
// Optional: true,
// Default: false,
// },
//},
}
rd := resourceTFEWorkspace().TestResourceData()
rd.SetId(workspace.ID)
err = rd.Set("force_delete", false)
if err != nil {
Expand All @@ -1967,8 +1925,28 @@ func TestAccTFEWorkspace_deleteWithoutPermissionAndSettingEnabled(t *testing.T)

err = resourceTFEWorkspaceDelete(rd, client)

if !strings.Contains(err.Error(), "substring") {
t.Fatalf("Expected error containins %s but got %s", "substring", err.Error())
if err == nil {
t.Fatalf("Expected an error deleting workspace with CanForceDelete=nil and force_delete=true")
}
expectedErrSubstring := "workspace must be force deleted by setting force_delete=true"
if !strings.Contains(err.Error(), expectedErrSubstring) {
t.Fatalf("Expected error containins %s but got %s", expectedErrSubstring, err.Error())
}

// now attempt with force_delete=true and confirm that it successfully removes the workspace
err = rd.Set("force_delete", true)
if err != nil {
t.Fatalf("Unexpected err creating configuration state %v", err)
}

err = resourceTFEWorkspaceDelete(rd, client)
if err != nil {
t.Fatalf("Unexpected err deleting mock workspace %v", err)
}

workspace, err = client.Workspaces.ReadByID(ctx, workspace.ID)
if err != tfe.ErrResourceNotFound {
t.Fatalf("Expected workspace %s to have been deleted", workspace.ID)
}
}

Expand Down

0 comments on commit 939b529

Please sign in to comment.