Skip to content

Commit

Permalink
GH-49 Add tests to cover importing of resources into Terraform (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
zahiar authored Nov 26, 2021
1 parent dd622f8 commit d6ea6d5
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 14 deletions.
97 changes: 97 additions & 0 deletions bitbucket/resource_bitbucket_branch_restriction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
},
},
},
})
}
Expand Down Expand Up @@ -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
},
},
},
})
}
Expand Down Expand Up @@ -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
},
},
},
})
}
Expand Down Expand Up @@ -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
},
},
},
})
}
Expand Down Expand Up @@ -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
},
},
},
})
}
Expand Down Expand Up @@ -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
},
},
},
})
}
Expand Down
9 changes: 9 additions & 0 deletions bitbucket/resource_bitbucket_default_reviewer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
},
},
},
})
}
Expand Down
21 changes: 21 additions & 0 deletions bitbucket/resource_bitbucket_deploy_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
},
},
},
})
}
Expand Down Expand Up @@ -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
},
},
},
})
}
11 changes: 11 additions & 0 deletions bitbucket/resource_bitbucket_group_permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
},
},
},
})
}
Expand Down
Loading

0 comments on commit d6ea6d5

Please sign in to comment.