Skip to content

Commit

Permalink
update acc test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
imjaroiswebdev committed Apr 30, 2024
1 parent 4baa0e6 commit 544c321
Showing 1 changed file with 82 additions and 19 deletions.
101 changes: 82 additions & 19 deletions pagerduty/resource_pagerduty_team_membership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pagerduty

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
Expand Down Expand Up @@ -81,7 +82,8 @@ func TestAccPagerDutyTeamMembership_WithRoleConsistentlyAssigned(t *testing.T) {
}

func TestAccPagerDutyTeamMembership_DestroyWithEscalationPolicyDependant(t *testing.T) {
user := fmt.Sprintf("tf-%s", acctest.RandString(5))
userFoo := fmt.Sprintf("tf-%s", acctest.RandString(5))
userBar := fmt.Sprintf("tf-%s", acctest.RandString(5))
team := fmt.Sprintf("tf-%s", acctest.RandString(5))
role := "manager"
escalationPolicy := fmt.Sprintf("tf-%s", acctest.RandString(5))
Expand All @@ -92,16 +94,27 @@ func TestAccPagerDutyTeamMembership_DestroyWithEscalationPolicyDependant(t *test
CheckDestroy: testAccCheckPagerDutyTeamMembershipDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyTeamMembershipDestroyWithEscalationPolicyDependant(user, team, role, escalationPolicy),
Config: testAccCheckPagerDutyTeamMembershipDestroyWithEscalationPolicyDependant(userFoo, userBar, team, role, escalationPolicy),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyTeamMembershipExists("pagerduty_team_membership.foo"),
),
},
// This test case is expected to fail because userFoo is a member of the
// escalation policy
{
Config: testAccCheckPagerDutyTeamMembershipDestroyWithEscalationPolicyDependantUpdated(user, team, role, escalationPolicy),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyTeamMembershipNoExists("pagerduty_team_membership.foo"),
),
Config: testAccCheckPagerDutyTeamMembershipDestroyWithEscalationPolicyDependantUpdated(userFoo, userBar, team, role, escalationPolicy),
ExpectError: regexp.MustCompile("User \".*\" can't be removed from Team \".*\" as they belong to an Escalation Policy on this team"),
},
// This test case is expected to pass because userFoo is being removed
// from escalation policy as remediation measure to unblock the team
// membership removal
{
Config: testAccCheckPagerDutyTeamMembershipDestroyWithEscalationPolicyDependantAfterRemediation(userFoo, userBar, team, role, escalationPolicy),
},
// This test case is expected to pass because userFoo is no longer a
// member of the escalation policy
{
Config: testAccCheckPagerDutyTeamMembershipDestroyWithEscalationPolicyDependantUpdated(userFoo, userBar, team, role, escalationPolicy),
},
},
})
Expand Down Expand Up @@ -236,26 +249,31 @@ resource "pagerduty_team_membership" "foo" {
`, user, team, role)
}

func testAccCheckPagerDutyTeamMembershipDestroyWithEscalationPolicyDependant(user, team, role, escalationPolicy string) string {
func testAccCheckPagerDutyTeamMembershipDestroyWithEscalationPolicyDependant(userFoo, userBar, team, role, escalationPolicy string) string {
return fmt.Sprintf(`
resource "pagerduty_user" "foo" {
name = "%[1]v"
email = "%[1][email protected]"
name = "%[1]s"
email = "%[1][email protected]"
}
resource "pagerduty_user" "bar" {
name = "%[2]s"
email = "%[2][email protected]"
}
resource "pagerduty_team" "foo" {
name = "%[2]v"
name = "%[3]s"
description = "foo"
}
resource "pagerduty_team_membership" "foo" {
user_id = pagerduty_user.foo.id
team_id = pagerduty_team.foo.id
role = "%[3]v"
role = "%[4]s"
}
resource "pagerduty_escalation_policy" "foo" {
name = "%s"
name = "%[5]s"
num_loops = 2
teams = [pagerduty_team.foo.id]
Expand All @@ -265,25 +283,34 @@ resource "pagerduty_escalation_policy" "foo" {
type = "user_reference"
id = pagerduty_user.foo.id
}
target {
type = "user_reference"
id = pagerduty_user.bar.id
}
}
}
`, user, team, role, escalationPolicy)
`, userFoo, userBar, team, role, escalationPolicy)
}

func testAccCheckPagerDutyTeamMembershipDestroyWithEscalationPolicyDependantUpdated(user, team, role, escalationPolicy string) string {
func testAccCheckPagerDutyTeamMembershipDestroyWithEscalationPolicyDependantUpdated(userFoo, userBar, team, role, escalationPolicy string) string {
return fmt.Sprintf(`
resource "pagerduty_user" "foo" {
name = "%[1]v"
email = "%[1][email protected]"
name = "%[1]s"
email = "%[1][email protected]"
}
resource "pagerduty_user" "bar" {
name = "%[2]s"
email = "%[2][email protected]"
}
resource "pagerduty_team" "foo" {
name = "%[2]v"
name = "%[3]s"
description = "foo"
}
resource "pagerduty_escalation_policy" "foo" {
name = "%[4]s"
name = "%[5]s"
num_loops = 2
teams = [pagerduty_team.foo.id]
Expand All @@ -293,7 +320,43 @@ resource "pagerduty_escalation_policy" "foo" {
type = "user_reference"
id = pagerduty_user.foo.id
}
target {
type = "user_reference"
id = pagerduty_user.bar.id
}
}
}
`, user, team, role, escalationPolicy)
`, userFoo, userBar, team, role, escalationPolicy)
}

func testAccCheckPagerDutyTeamMembershipDestroyWithEscalationPolicyDependantAfterRemediation(userFoo, userBar, team, role, escalationPolicy string) string {
return fmt.Sprintf(`
resource "pagerduty_user" "foo" {
name = "%[1]s"
email = "%[1][email protected]"
}
resource "pagerduty_user" "bar" {
name = "%[2]s"
email = "%[2][email protected]"
}
resource "pagerduty_team" "foo" {
name = "%[3]s"
description = "foo"
}
resource "pagerduty_escalation_policy" "foo" {
name = "%[5]s"
num_loops = 2
teams = [pagerduty_team.foo.id]
rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = pagerduty_user.bar.id
}
}
}`, userFoo, userBar, team, role, escalationPolicy)
}

0 comments on commit 544c321

Please sign in to comment.