Skip to content

Commit

Permalink
update testAccCheckPagerDutyEscalationPolicyTeamsFieldMatches for c…
Browse files Browse the repository at this point in the history
…orrectness
  • Loading branch information
Lorenzo Davis committed Jul 5, 2024
1 parent 3542b41 commit b996c38
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions pagerduty/resource_pagerduty_team_membership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,16 +298,14 @@ resource "pagerduty_escalation_policy" "foo" {
`, user, team, role, escalationPolicy)
}

func TestAccPagerDutyTeamMembership_basic(t *testing.T) { // from gpt and modified
func TestAccPagerDutyTeamMembership_basic(t *testing.T) {
user := fmt.Sprintf("tf-%s", acctest.RandString(5))
team1 := fmt.Sprintf("tf-%s", acctest.RandString(5))
team2 := fmt.Sprintf("tf-%s", acctest.RandString(5))
role := "manager"
escalationPolicy1 := fmt.Sprintf("tf-%s", acctest.RandString(5))
escalationPolicy2 := fmt.Sprintf("tf-%s", acctest.RandString(5))

fmt.Print("starting tests\n")
fmt.Printf("starting tests\n\n")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -329,35 +327,37 @@ func TestAccPagerDutyTeamMembership_basic(t *testing.T) { // from gpt and modifi
testAccCheckPagerDutyTeamExists("pagerduty_team.bar"),
testAccCheckPagerDutyEscalationPolicyExists("pagerduty_escalation_policy.foo"),
testAccCheckPagerDutyEscalationPolicyExists("pagerduty_escalation_policy.bar"),
testAccCheckPagerDutyEscalationPolicyTeamsFieldMatches("pagerduty_escalation_policy.foo", "foo"),
testAccCheckPagerDutyEscalationPolicyTeamsFieldMatches("pagerduty_escalation_policy.bar", "bar"),
testAccCheckPagerDutyEscalationPolicyTeamsFieldMatches("pagerduty_escalation_policy.foo", "pagerduty_team.foo"),
testAccCheckPagerDutyEscalationPolicyTeamsFieldMatches("pagerduty_escalation_policy.bar", "pagerduty_team.bar"),
),
},
},
})
}

// update this tomorrow
// the check should involve a call to the API and inspecting the state on the server
func testAccCheckPagerDutyEscalationPolicyTeamsFieldMatches(n, expectedTeam string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

teams := rs.Primary.Attributes["teams.#"]
if teams != "1" {
return fmt.Errorf("Expected teams to have 1 element, got: %s", teams)
client, _ := testAccProvider.Meta().(*Config).Client()
expectedTeamID, err := getTeamID(s, expectedTeam)
if err != nil {
return err
}

team := rs.Primary.Attributes["teams.0"]
if team != expectedTeam {
return fmt.Errorf("Expected team to be %s, got: %s", expectedTeam, team)
// get the object from remote state -- tests for prescence
remoteResource, _, err := client.EscalationPolicies.Get(rs.Primary.ID, &pagerduty.GetEscalationPolicyOptions{})
if err != nil {
return fmt.Errorf("%s\n", err)
}

return fmt.Errorf("dummy error")
// return nil
if attachedTeam := remoteResource.Teams[0].ID; attachedTeam != expectedTeamID {
return fmt.Errorf("Mismatch detected. Expected %s, and got %s\n", expectedTeamID, attachedTeam)
}
return nil
}
}

Expand All @@ -379,13 +379,13 @@ resource "pagerduty_team" "bar" {
}
resource "pagerduty_team_membership" "foo" {
user_id = data.pagerduty_user.foo.id
user_id = pagerduty_user.foo.id
team_id = pagerduty_team.foo.id
role = "%[3]v"
}
resource "pagerduty_team_membership" "bar" {
user_id = data.pagerduty_user.foo.id
user_id = pagerduty_user.foo.id
team_id = pagerduty_team.bar.id
role = "%[3]v"
}
Expand All @@ -398,7 +398,7 @@ resource "pagerduty_escalation_policy" "foo" {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = data.pagerduty_user.foo.id
id = pagerduty_user.foo.id
}
}
}
Expand All @@ -411,7 +411,7 @@ resource "pagerduty_escalation_policy" "bar" {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = data.pagerduty_user.foo.id
id = pagerduty_user.foo.id
}
}
}
Expand All @@ -436,7 +436,7 @@ resource "pagerduty_team" "bar" {
description = "bar"
}
resource "pagerduty_team_membership" "bar" {
user_id = data.pagerduty_user.foo.id
user_id = pagerduty_user.foo.id
team_id = pagerduty_team.bar.id
role = "%[3]v"
}
Expand All @@ -449,7 +449,7 @@ resource "pagerduty_escalation_policy" "foo" {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = data.pagerduty_user.foo.id
id = pagerduty_user.foo.id
}
}
}
Expand All @@ -462,9 +462,18 @@ resource "pagerduty_escalation_policy" "bar" {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = data.pagerduty_user.foo.id
id = pagerduty_user.foo.id
}
}
}
`, user, team1, role, escalationPolicy1, team2, escalationPolicy2)
}

func getTeamID(s *terraform.State, teamName string) (string, error) {
for name, r := range s.RootModule().Resources {
if name == teamName {
return r.Primary.ID, nil
}
}
return "", fmt.Errorf("Could not find team %s\n", teamName)
}

0 comments on commit b996c38

Please sign in to comment.