From d6ea6d5bf44b49cffd001aa5bef3705bcbf0ac8f Mon Sep 17 00:00:00 2001 From: Zahiar Ahmed <310030+zahiar@users.noreply.github.com> Date: Fri, 26 Nov 2021 03:17:28 +0000 Subject: [PATCH] GH-49 Add tests to cover importing of resources into Terraform (#50) --- ...ource_bitbucket_branch_restriction_test.go | 97 +++++++++++++++++++ ...esource_bitbucket_default_reviewer_test.go | 9 ++ .../resource_bitbucket_deploy_key_test.go | 21 ++++ ...esource_bitbucket_group_permission_test.go | 11 +++ bitbucket/resource_bitbucket_group_test.go | 76 ++++++++++++--- bitbucket/resource_bitbucket_project_test.go | 9 ++ .../resource_bitbucket_repository_test.go | 9 ++ bitbucket/resource_bitbucket_webhook_test.go | 11 +++ 8 files changed, 229 insertions(+), 14 deletions(-) diff --git a/bitbucket/resource_bitbucket_branch_restriction_test.go b/bitbucket/resource_bitbucket_branch_restriction_test.go index f9c3215..a4e7c73 100644 --- a/bitbucket/resource_bitbucket_branch_restriction_test.go +++ b/bitbucket/resource_bitbucket_branch_restriction_test.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "github.com/stretchr/testify/assert" ) @@ -61,6 +62,16 @@ func TestAccBitbucketBranchRestrictionResource_basic(t *testing.T) { resource.TestCheckResourceAttrSet("bitbucket_branch_restriction.testacc", "id"), ), }, + { + ResourceName: "bitbucket_branch_restriction.testacc", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: func(state *terraform.State) (string, error) { + resources := state.Modules[0].Resources + branchRestResourceAttr := resources["bitbucket_branch_restriction.testacc"].Primary.Attributes + return fmt.Sprintf("%s/%s/%s", workspaceSlug, repoName, branchRestResourceAttr["id"]), nil + }, + }, }, }) } @@ -116,6 +127,16 @@ func TestAccBitbucketBranchRestrictionResource_withKindAndValueCombination(t *te resource.TestCheckResourceAttrSet("bitbucket_branch_restriction.testacc", "id"), ), }, + { + ResourceName: "bitbucket_branch_restriction.testacc", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: func(state *terraform.State) (string, error) { + resources := state.Modules[0].Resources + branchRestResourceAttr := resources["bitbucket_branch_restriction.testacc"].Primary.Attributes + return fmt.Sprintf("%s/%s/%s", workspaceSlug, repoName, branchRestResourceAttr["id"]), nil + }, + }, }, }) } @@ -173,6 +194,28 @@ func TestAccBitbucketBranchRestrictionResource_withUsers(t *testing.T) { resource.TestCheckResourceAttrSet("bitbucket_branch_restriction.testacc", "id"), ), }, + { + ResourceName: "bitbucket_branch_restriction.testacc", + ImportState: true, + ImportStateVerify: false, + ImportStateIdFunc: func(state *terraform.State) (string, error) { + resources := state.Modules[0].Resources + branchRestResourceAttr := resources["bitbucket_branch_restriction.testacc"].Primary.Attributes + return fmt.Sprintf("%s/%s/%s", workspaceSlug, repoName, branchRestResourceAttr["id"]), nil + }, + ImportStateCheck: func(states []*terraform.InstanceState) error { + state := states[0] + assert.Equal(t, workspaceSlug, state.Attributes["workspace"]) + assert.Equal(t, repoName, state.Attributes["repository"]) + assert.Equal(t, branchRestrictionPattern, state.Attributes["pattern"]) + assert.Equal(t, branchRestrictionKind, state.Attributes["kind"]) + assert.Equal(t, "0", state.Attributes["value"]) + + assert.NotEmpty(t, state.Attributes["id"]) + + return nil + }, + }, }, }) } @@ -244,6 +287,28 @@ func TestAccBitbucketBranchRestrictionResource_withGroups(t *testing.T) { resource.TestCheckResourceAttrSet("bitbucket_branch_restriction.testacc", "id"), ), }, + { + ResourceName: "bitbucket_branch_restriction.testacc", + ImportState: true, + ImportStateVerify: false, + ImportStateIdFunc: func(state *terraform.State) (string, error) { + resources := state.Modules[0].Resources + branchRestResourceAttr := resources["bitbucket_branch_restriction.testacc"].Primary.Attributes + return fmt.Sprintf("%s/%s/%s", workspaceSlug, repoName, branchRestResourceAttr["id"]), nil + }, + ImportStateCheck: func(states []*terraform.InstanceState) error { + state := states[0] + assert.Equal(t, workspaceSlug, state.Attributes["workspace"]) + assert.Equal(t, repoName, state.Attributes["repository"]) + assert.Equal(t, branchRestrictionPattern, state.Attributes["pattern"]) + assert.Equal(t, branchRestrictionKind, state.Attributes["kind"]) + assert.Equal(t, "0", state.Attributes["value"]) + + assert.NotEmpty(t, state.Attributes["id"]) + + return nil + }, + }, }, }) } @@ -318,6 +383,28 @@ func TestAccBitbucketBranchRestrictionResource_withUsersAndGroups(t *testing.T) resource.TestCheckResourceAttrSet("bitbucket_branch_restriction.testacc", "id"), ), }, + { + ResourceName: "bitbucket_branch_restriction.testacc", + ImportState: true, + ImportStateVerify: false, + ImportStateIdFunc: func(state *terraform.State) (string, error) { + resources := state.Modules[0].Resources + branchRestResourceAttr := resources["bitbucket_branch_restriction.testacc"].Primary.Attributes + return fmt.Sprintf("%s/%s/%s", workspaceSlug, repoName, branchRestResourceAttr["id"]), nil + }, + ImportStateCheck: func(states []*terraform.InstanceState) error { + state := states[0] + assert.Equal(t, workspaceSlug, state.Attributes["workspace"]) + assert.Equal(t, repoName, state.Attributes["repository"]) + assert.Equal(t, branchRestrictionPattern, state.Attributes["pattern"]) + assert.Equal(t, branchRestrictionKind, state.Attributes["kind"]) + assert.Equal(t, "0", state.Attributes["value"]) + + assert.NotEmpty(t, state.Attributes["id"]) + + return nil + }, + }, }, }) } @@ -373,6 +460,16 @@ func TestAccBitbucketBranchRestrictionResource_withEmptyUsersAndEmptyGroups(t *t resource.TestCheckResourceAttrSet("bitbucket_branch_restriction.testacc", "id"), ), }, + { + ResourceName: "bitbucket_branch_restriction.testacc", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: func(state *terraform.State) (string, error) { + resources := state.Modules[0].Resources + branchRestResourceAttr := resources["bitbucket_branch_restriction.testacc"].Primary.Attributes + return fmt.Sprintf("%s/%s/%s", workspaceSlug, repoName, branchRestResourceAttr["id"]), nil + }, + }, }, }) } diff --git a/bitbucket/resource_bitbucket_default_reviewer_test.go b/bitbucket/resource_bitbucket_default_reviewer_test.go index 32fc254..4a4f0f8 100644 --- a/bitbucket/resource_bitbucket_default_reviewer_test.go +++ b/bitbucket/resource_bitbucket_default_reviewer_test.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "github.com/stretchr/testify/assert" ) @@ -61,6 +62,14 @@ func TestAccBitbucketDefaultReviewerResource_basic(t *testing.T) { resource.TestCheckResourceAttrSet("bitbucket_default_reviewer.testacc", "user"), ), }, + { + ResourceName: "bitbucket_default_reviewer.testacc", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: func(state *terraform.State) (string, error) { + return fmt.Sprintf("%s/%s/%s", workspaceSlug, repoName, user.Uuid), nil + }, + }, }, }) } diff --git a/bitbucket/resource_bitbucket_deploy_key_test.go b/bitbucket/resource_bitbucket_deploy_key_test.go index 952f0d2..c73fb80 100644 --- a/bitbucket/resource_bitbucket_deploy_key_test.go +++ b/bitbucket/resource_bitbucket_deploy_key_test.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) func TestAccBitbucketDeployKeyResource_basic(t *testing.T) { @@ -55,6 +56,16 @@ func TestAccBitbucketDeployKeyResource_basic(t *testing.T) { resource.TestCheckResourceAttrSet("bitbucket_deploy_key.testacc", "id"), ), }, + { + ResourceName: "bitbucket_deploy_key.testacc", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: func(state *terraform.State) (string, error) { + resources := state.Modules[0].Resources + depKeyResourceAttr := resources["bitbucket_deploy_key.testacc"].Primary.Attributes + return fmt.Sprintf("%s/%s/%s", workspaceSlug, repoName, depKeyResourceAttr["id"]), nil + }, + }, }, }) } @@ -104,6 +115,16 @@ func TestAccBitbucketDeployKeyResource_keyWithComment(t *testing.T) { resource.TestCheckResourceAttrSet("bitbucket_deploy_key.testacc", "id"), ), }, + { + ResourceName: "bitbucket_deploy_key.testacc", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: func(state *terraform.State) (string, error) { + resources := state.Modules[0].Resources + depKeyResourceAttr := resources["bitbucket_deploy_key.testacc"].Primary.Attributes + return fmt.Sprintf("%s/%s/%s", workspaceSlug, repoName, depKeyResourceAttr["id"]), nil + }, + }, }, }) } diff --git a/bitbucket/resource_bitbucket_group_permission_test.go b/bitbucket/resource_bitbucket_group_permission_test.go index 5fe6557..ec22b14 100644 --- a/bitbucket/resource_bitbucket_group_permission_test.go +++ b/bitbucket/resource_bitbucket_group_permission_test.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "github.com/stretchr/testify/assert" ) @@ -62,6 +63,16 @@ func TestAccBitbucketGroupPermissionResource_basic(t *testing.T) { resource.TestCheckResourceAttrSet("bitbucket_group_permission.testacc", "workspace"), ), }, + { + ResourceName: "bitbucket_group_permission.testacc", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: func(state *terraform.State) (string, error) { + resources := state.Modules[0].Resources + workspaceResourceAttr := resources["data.bitbucket_workspace.testacc"].Primary.Attributes + return fmt.Sprintf("%s/%s/%s", workspaceResourceAttr["uuid"], repoName, groupName), nil + }, + }, }, }) } diff --git a/bitbucket/resource_bitbucket_group_test.go b/bitbucket/resource_bitbucket_group_test.go index 7874f0d..cdca47a 100644 --- a/bitbucket/resource_bitbucket_group_test.go +++ b/bitbucket/resource_bitbucket_group_test.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "github.com/stretchr/testify/assert" ) @@ -25,20 +26,31 @@ func TestAccBitbucketGroupResource_basic(t *testing.T) { } resource "bitbucket_group" "testacc" { - workspace = data.bitbucket_workspace.testacc.id + workspace = data.bitbucket_workspace.testacc.uuid name = "%s" auto_add = true permission = "read" }`, workspaceSlug, groupName), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("bitbucket_group.testacc", "workspace", workspaceSlug), resource.TestCheckResourceAttr("bitbucket_group.testacc", "name", groupName), resource.TestCheckResourceAttr("bitbucket_group.testacc", "auto_add", "true"), resource.TestCheckResourceAttr("bitbucket_group.testacc", "permission", "read"), resource.TestCheckResourceAttr("bitbucket_group.testacc", "slug", groupName), + + resource.TestCheckResourceAttrSet("bitbucket_group.testacc", "workspace"), resource.TestCheckResourceAttrSet("bitbucket_group.testacc", "id"), ), }, + { + ResourceName: "bitbucket_group.testacc", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: func(state *terraform.State) (string, error) { + resources := state.Modules[0].Resources + workspaceResourceAttr := resources["data.bitbucket_workspace.testacc"].Primary.Attributes + return fmt.Sprintf("%s/%s", workspaceResourceAttr["uuid"], groupName), nil + }, + }, }, }) } @@ -59,17 +71,18 @@ func TestAccBitbucketGroupResource_changeName(t *testing.T) { } resource "bitbucket_group" "testacc" { - workspace = data.bitbucket_workspace.testacc.id + workspace = data.bitbucket_workspace.testacc.uuid name = "%s" auto_add = true permission = "read" }`, workspaceSlug, groupName), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("bitbucket_group.testacc", "workspace", workspaceSlug), resource.TestCheckResourceAttr("bitbucket_group.testacc", "name", groupName), resource.TestCheckResourceAttr("bitbucket_group.testacc", "auto_add", "true"), resource.TestCheckResourceAttr("bitbucket_group.testacc", "permission", "read"), resource.TestCheckResourceAttr("bitbucket_group.testacc", "slug", groupName), + + resource.TestCheckResourceAttrSet("bitbucket_group.testacc", "workspace"), resource.TestCheckResourceAttrSet("bitbucket_group.testacc", "id"), ), }, @@ -80,20 +93,31 @@ func TestAccBitbucketGroupResource_changeName(t *testing.T) { } resource "bitbucket_group" "testacc" { - workspace = data.bitbucket_workspace.testacc.id + workspace = data.bitbucket_workspace.testacc.uuid name = "%s" auto_add = true permission = "read" }`, workspaceSlug, newGroupName), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("bitbucket_group.testacc", "workspace", workspaceSlug), resource.TestCheckResourceAttr("bitbucket_group.testacc", "name", newGroupName), resource.TestCheckResourceAttr("bitbucket_group.testacc", "auto_add", "true"), resource.TestCheckResourceAttr("bitbucket_group.testacc", "permission", "read"), resource.TestCheckResourceAttr("bitbucket_group.testacc", "slug", newGroupName), + + resource.TestCheckResourceAttrSet("bitbucket_group.testacc", "workspace"), resource.TestCheckResourceAttrSet("bitbucket_group.testacc", "id"), ), }, + { + ResourceName: "bitbucket_group.testacc", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: func(state *terraform.State) (string, error) { + resources := state.Modules[0].Resources + workspaceResourceAttr := resources["data.bitbucket_workspace.testacc"].Primary.Attributes + return fmt.Sprintf("%s/%s", workspaceResourceAttr["uuid"], newGroupName), nil + }, + }, }, }) } @@ -113,17 +137,18 @@ func TestAccBitbucketGroupResource_changeProperties(t *testing.T) { } resource "bitbucket_group" "testacc" { - workspace = data.bitbucket_workspace.testacc.id + workspace = data.bitbucket_workspace.testacc.uuid name = "%s" auto_add = true permission = "read" }`, workspaceSlug, groupName), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("bitbucket_group.testacc", "workspace", workspaceSlug), resource.TestCheckResourceAttr("bitbucket_group.testacc", "name", groupName), resource.TestCheckResourceAttr("bitbucket_group.testacc", "auto_add", "true"), resource.TestCheckResourceAttr("bitbucket_group.testacc", "permission", "read"), resource.TestCheckResourceAttr("bitbucket_group.testacc", "slug", groupName), + + resource.TestCheckResourceAttrSet("bitbucket_group.testacc", "workspace"), resource.TestCheckResourceAttrSet("bitbucket_group.testacc", "id"), ), }, @@ -134,20 +159,31 @@ func TestAccBitbucketGroupResource_changeProperties(t *testing.T) { } resource "bitbucket_group" "testacc" { - workspace = data.bitbucket_workspace.testacc.id + workspace = data.bitbucket_workspace.testacc.uuid name = "%s" auto_add = false permission = "write" }`, workspaceSlug, groupName), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("bitbucket_group.testacc", "workspace", workspaceSlug), resource.TestCheckResourceAttr("bitbucket_group.testacc", "name", groupName), resource.TestCheckResourceAttr("bitbucket_group.testacc", "auto_add", "false"), resource.TestCheckResourceAttr("bitbucket_group.testacc", "permission", "write"), resource.TestCheckResourceAttr("bitbucket_group.testacc", "slug", groupName), + + resource.TestCheckResourceAttrSet("bitbucket_group.testacc", "workspace"), resource.TestCheckResourceAttrSet("bitbucket_group.testacc", "id"), ), }, + { + ResourceName: "bitbucket_group.testacc", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: func(state *terraform.State) (string, error) { + resources := state.Modules[0].Resources + workspaceResourceAttr := resources["data.bitbucket_workspace.testacc"].Primary.Attributes + return fmt.Sprintf("%s/%s", workspaceResourceAttr["uuid"], groupName), nil + }, + }, }, }) } @@ -167,15 +203,16 @@ func TestAccBitbucketGroupResource_withoutProperties(t *testing.T) { } resource "bitbucket_group" "testacc" { - workspace = data.bitbucket_workspace.testacc.id + workspace = data.bitbucket_workspace.testacc.uuid name = "%s" }`, workspaceSlug, groupName), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("bitbucket_group.testacc", "workspace", workspaceSlug), resource.TestCheckResourceAttr("bitbucket_group.testacc", "name", groupName), resource.TestCheckResourceAttr("bitbucket_group.testacc", "auto_add", "false"), resource.TestCheckResourceAttr("bitbucket_group.testacc", "permission", ""), resource.TestCheckResourceAttr("bitbucket_group.testacc", "slug", groupName), + + resource.TestCheckResourceAttrSet("bitbucket_group.testacc", "workspace"), resource.TestCheckResourceAttrSet("bitbucket_group.testacc", "id"), ), }, @@ -186,20 +223,31 @@ func TestAccBitbucketGroupResource_withoutProperties(t *testing.T) { } resource "bitbucket_group" "testacc" { - workspace = data.bitbucket_workspace.testacc.id + workspace = data.bitbucket_workspace.testacc.uuid name = "%s" auto_add = true permission = "write" }`, workspaceSlug, groupName), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("bitbucket_group.testacc", "workspace", workspaceSlug), resource.TestCheckResourceAttr("bitbucket_group.testacc", "name", groupName), resource.TestCheckResourceAttr("bitbucket_group.testacc", "auto_add", "true"), resource.TestCheckResourceAttr("bitbucket_group.testacc", "permission", "write"), resource.TestCheckResourceAttr("bitbucket_group.testacc", "slug", groupName), + + resource.TestCheckResourceAttrSet("bitbucket_group.testacc", "workspace"), resource.TestCheckResourceAttrSet("bitbucket_group.testacc", "id"), ), }, + { + ResourceName: "bitbucket_group.testacc", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: func(state *terraform.State) (string, error) { + resources := state.Modules[0].Resources + workspaceResourceAttr := resources["data.bitbucket_workspace.testacc"].Primary.Attributes + return fmt.Sprintf("%s/%s", workspaceResourceAttr["uuid"], groupName), nil + }, + }, }, }) } diff --git a/bitbucket/resource_bitbucket_project_test.go b/bitbucket/resource_bitbucket_project_test.go index 30604e7..aade947 100644 --- a/bitbucket/resource_bitbucket_project_test.go +++ b/bitbucket/resource_bitbucket_project_test.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "github.com/stretchr/testify/assert" ) @@ -63,6 +64,14 @@ func TestAccBitbucketProjectResource_basic(t *testing.T) { resource.TestCheckResourceAttrSet("bitbucket_project.testacc", "id"), ), }, + { + ResourceName: "bitbucket_project.testacc", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: func(state *terraform.State) (string, error) { + return fmt.Sprintf("%s/%s", workspaceSlug, projectKey), nil + }, + }, }, }) } diff --git a/bitbucket/resource_bitbucket_repository_test.go b/bitbucket/resource_bitbucket_repository_test.go index b47732f..d85fb73 100644 --- a/bitbucket/resource_bitbucket_repository_test.go +++ b/bitbucket/resource_bitbucket_repository_test.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "github.com/stretchr/testify/assert" ) @@ -79,6 +80,14 @@ func TestAccBitbucketRepositoryResource_basic(t *testing.T) { resource.TestCheckResourceAttrSet("bitbucket_repository.testacc", "id"), ), }, + { + ResourceName: "bitbucket_repository.testacc", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: func(state *terraform.State) (string, error) { + return fmt.Sprintf("%s/%s", workspaceSlug, repoName), nil + }, + }, }, }) } diff --git a/bitbucket/resource_bitbucket_webhook_test.go b/bitbucket/resource_bitbucket_webhook_test.go index 23dc5de..3aac6e5 100644 --- a/bitbucket/resource_bitbucket_webhook_test.go +++ b/bitbucket/resource_bitbucket_webhook_test.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "github.com/stretchr/testify/assert" ) @@ -101,6 +102,16 @@ func TestAccBitbucketWebhookResource_basic(t *testing.T) { resource.TestCheckResourceAttrSet("bitbucket_webhook.testacc", "id"), ), }, + { + ResourceName: "bitbucket_webhook.testacc", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: func(state *terraform.State) (string, error) { + resources := state.Modules[0].Resources + webhookResourceAttr := resources["bitbucket_webhook.testacc"].Primary.Attributes + return fmt.Sprintf("%s/%s/%s", workspaceSlug, repoName, webhookResourceAttr["id"]), nil + }, + }, }, }) }