Skip to content

Commit

Permalink
test: fix cleaning up of tests and update assertions to account for r…
Browse files Browse the repository at this point in the history
…andom suffix
  • Loading branch information
bhargavms committed Jan 10, 2024
1 parent 8beb7bf commit e1c536a
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 52 deletions.
1 change: 1 addition & 0 deletions examples/pagerduty-user/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ This example creates a basic PagerDuty User.

| Name | Description |
|------|-------------|
| <a name="output_generated_user_name_suffix"></a> [generated\_user\_name\_suffix](#output\_generated\_user\_name\_suffix) | The generated suffix for the user's name to avoid conflicting resource creation during testing. |
| <a name="output_html_url"></a> [html\_url](#output\_html\_url) | URL at which the entity is uniquely displayed in the Web app. |
| <a name="output_id"></a> [id](#output\_id) | The ID of the user. |
| <a name="output_invitation_sent"></a> [invitation\_sent](#output\_invitation\_sent) | If true, the user has an outstanding invitation. |
Expand Down
5 changes: 5 additions & 0 deletions examples/pagerduty-user/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ output "invitation_sent" {
description = "If true, the user has an outstanding invitation."
value = module.user.invitation_sent
}

output "generated_user_name_suffix" {
value = local.random_suffix
description = "The generated suffix for the user's name to avoid conflicting resource creation during testing."
}
7 changes: 3 additions & 4 deletions test/honest_two_level_schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (

func TestHonestTwoLevelSchedule(t *testing.T) {
workingDir := test_structure.CopyTerraformFolderToTemp(t, "..", "examples/honest-two-level-schedule")
defer test_structure.RunTestStage(t, "destroy_two_level_schedule", func() {
destroyTwoLevelSchedule(t, workingDir)
})

levelOneScheduleId := ""
levelTwoScheduleId := ""
Expand All @@ -28,10 +31,6 @@ func TestHonestTwoLevelSchedule(t *testing.T) {
levelOneScheduleId, levelTwoScheduleId, teamID = createTwoLevelScheduleWithUserCount(t, workingDir, 2, teamName)
})

defer test_structure.RunTestStage(t, "destroy_two_level_schedule", func() {
destroyTwoLevelSchedule(t, workingDir)
})

test_structure.RunTestStage(t, "verify_two_level_schedule", func() {
verifyTwoLevelScheduleWithUserCount(t, levelOneScheduleId, levelTwoScheduleId, 2)
verifyTeamSetInSchedule(t, levelOneScheduleId, levelTwoScheduleId, teamID)
Expand Down
24 changes: 15 additions & 9 deletions test/pagerduty_business_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"strings"
"testing"

"github.com/PagerDuty/go-pagerduty"
Expand All @@ -12,12 +13,15 @@ import (
"github.com/stretchr/testify/assert"
)


const businessServiceExampleDir = "./examples/pagerduty-business-service"
const businessServiceDescriptionMock = "Created by Terratest"
const businessServicePointOfContactMock = "Terratest - Contact the engineers"

func TestPagerdutyBusinessService(t *testing.T) {
workingDir := test_structure.CopyTerraformFolderToTemp(t, "..", businessServiceExampleDir)
defer test_structure.RunTestStage(t, "destroy_business_service", func() {
destroyPagerdutyBusinessService(t, workingDir)
})

businessServiceID := ""
runID := generateRunId()
Expand All @@ -26,9 +30,6 @@ func TestPagerdutyBusinessService(t *testing.T) {
test_structure.RunTestStage(t, "create_business_service", func() {
businessServiceID = createPagerdutyBusinessService(t, workingDir, businessServiceName)
})
defer test_structure.RunTestStage(t, "destroy_business_service", func() {
destroyPagerdutyBusinessService(t, workingDir)
})

test_structure.RunTestStage(t, "verify_business_service", func() {
verifyPagerdutyBusinessService(t, businessServiceID, businessServiceName)
Expand All @@ -39,8 +40,8 @@ func createPagerdutyBusinessService(t *testing.T, workingDir string, businessSer
options := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
TerraformDir: workingDir,
Vars: map[string]interface{}{
"name": businessServiceName,
"description": businessServiceDescriptionMock,
"name": businessServiceName,
"description": businessServiceDescriptionMock,
"point_of_contact": businessServicePointOfContactMock,
},
})
Expand All @@ -50,7 +51,12 @@ func createPagerdutyBusinessService(t *testing.T, workingDir string, businessSer
}

func destroyPagerdutyBusinessService(t *testing.T, workingDir string) {
terraform.Destroy(t, test_structure.LoadTerraformOptions(t, workingDir))
_, err := terraform.DestroyE(t, test_structure.LoadTerraformOptions(t, workingDir))
// have to re-do destroy sometimes coz of race conditions (i.e. try to delete team while it still has associations)
// In the retry the team will get deleted properly because the associations have been deleted in previous run
if err != nil {
terraform.Destroy(t, test_structure.LoadTerraformOptions(t, workingDir))
}
}

func verifyPagerdutyBusinessService(t *testing.T, serviceID string, expectedBussinessServiceName string) {
Expand All @@ -59,6 +65,6 @@ func verifyPagerdutyBusinessService(t *testing.T, serviceID string, expectedBuss
if serviceErr != nil {
log.Println("error getting service: ", serviceErr)
}

assert.Equal(t, expectedBussinessServiceName, businessService.Name)
fmt.Printf("checking strings %s with %s", expectedBussinessServiceName, businessService.Name)
assert.True(t, strings.HasPrefix(businessService.Name, expectedBussinessServiceName))
}
14 changes: 9 additions & 5 deletions test/pagerduty_escalation_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ func TestPagerdutyEscalationPolicy(t *testing.T) {

workingDir := test_structure.CopyTerraformFolderToTemp(t, "..", "examples/pagerduty-escalation-policy")
workingDir = "../examples/pagerduty-escalation-policy"
defer test_structure.RunTestStage(t, "destroy_escalation_policy", func() {
destroyEscalationPolicy(t, workingDir)
})

log.Println("working dir for this test is: ", workingDir)

Expand All @@ -28,10 +31,6 @@ func TestPagerdutyEscalationPolicy(t *testing.T) {
log.Println("created escalation policy ID: ", escalationPolicyId)
})

defer test_structure.RunTestStage(t, "destroy_escalation_policy", func() {
destroyEscalationPolicy(t, workingDir)
})

test_structure.RunTestStage(t, "verify_escalation_policy", func() {
verifyEscalationPolicy(t, escalationPolicyId)
})
Expand All @@ -54,7 +53,12 @@ func createEscalationPolicy(t *testing.T, workingDir string, runID string) strin
}

func destroyEscalationPolicy(t *testing.T, workingDir string) {
terraform.Destroy(t, test_structure.LoadTerraformOptions(t, workingDir))
_, err := terraform.DestroyE(t, test_structure.LoadTerraformOptions(t, workingDir))
// have to re-do destroy sometimes coz of race conditions (i.e. try to delete team while it still has associations)
// In the retry the team will get deleted properly because the associations have been deleted in previous run
if err != nil {
terraform.Destroy(t, test_structure.LoadTerraformOptions(t, workingDir))
}
}

func verifyEscalationPolicy(t *testing.T, escalationPolicyId string) {
Expand Down
10 changes: 5 additions & 5 deletions test/pagerduty_schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"log"
"strings"
"testing"
"time"

Expand All @@ -27,16 +28,15 @@ func TestPagerdutySchedule(t *testing.T) {
createdScheduleId := ""
userOneId := ""
userTwoId := ""
defer test_structure.RunTestStage(t, "destroy_schedule", func() {
destroySchedule(t, scheduleWorkingDir)
})

test_structure.RunTestStage(t, "create_schedule", func() {
createdScheduleId, userOneId, userTwoId = createSchedule(t, scheduleWorkingDir, scheduleName)
assert.NotNilf(t, createdScheduleId, "created schedule ID cannot be nil")
})

defer test_structure.RunTestStage(t, "destroy_schedule", func() {
destroySchedule(t, scheduleWorkingDir)
})

test_structure.RunTestStage(t, "verify_schedule", func() {
log.Println("🔎🔎🔎 About to verify schedule ID: ", createdScheduleId)
verifySchedule(t, createdScheduleId, userOneId, userTwoId, scheduleName)
Expand Down Expand Up @@ -88,7 +88,7 @@ func verifySchedule(t *testing.T, scheduleId string, userOneId string, userTwoId
}
log.Println("Retrieved schedule from PagerDuty SDK: ", schedule)

assert.Equal(t, expectedScheduleName, schedule.Name)
assert.True(t, strings.HasPrefix(schedule.Name, expectedScheduleName))
assert.Equal(t, "Asia/Bangkok", schedule.TimeZone)

assert.Equalf(t, 1, len(schedule.ScheduleLayers), "there must be one schedule layer created")
Expand Down
7 changes: 3 additions & 4 deletions test/pagerduty_service_email_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func TestPagerdutyServiceEmailIntegration(t *testing.T) {
runID := generateRunId()
integrationEmail := fmt.Sprintf("example%s@honest-test.pagerduty.com", runID)
integrationName := fmt.Sprintf("example-pagerduty-service-integrations-email-%s", runID)
defer test_structure.RunTestStage(t, "destroy_service_integration_email", func() {
destroyPagerdutyServiceEmailIntegration(t, workingDir)
})

test_structure.RunTestStage(t, "create_service_integration_email", func() {
var err error
Expand All @@ -33,10 +36,6 @@ func TestPagerdutyServiceEmailIntegration(t *testing.T) {
}
})

defer test_structure.RunTestStage(t, "destroy_service_integration_email", func() {
destroyPagerdutyServiceEmailIntegration(t, workingDir)
})

test_structure.RunTestStage(t, "verify_service_email_integration", func() {
verifyPagerdutyServiceEmailIntegration(t, serviceId, integrationId, integrationName, integrationEmail, emailFilter)
})
Expand Down
6 changes: 3 additions & 3 deletions test/pagerduty_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ func TestPagerdutyService(t *testing.T) {

// Temporary override for local development and testing
workingDir = "../examples/pagerduty-service"
defer test_structure.RunTestStage(t, "destroy_service", func() {
destroyPagerdutyService(t, workingDir)
})

serviceId := ""
serviceName := ""
runID := generateRunId()
test_structure.RunTestStage(t, "create_service", func() {
serviceId, serviceName = createPagerdutyService(t, workingDir, runID)
})
defer test_structure.RunTestStage(t, "destroy_service", func() {
destroyPagerdutyService(t, workingDir)
})

test_structure.RunTestStage(t, "verify_service", func() {
verifyPagerdutyService(t, serviceId, serviceName)
Expand Down
18 changes: 6 additions & 12 deletions test/pagerduty_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,26 @@ import (
"github.com/stretchr/testify/assert"
)


const roleManager = "manager"
const roleEngineer = "engineer"

func TestPagerdutyTeam(t *testing.T) {

// This will verify the presence of the token as well as return it
_ = loadPagerdutyToken(t)
workingDir := "../examples/pagerduty-team"
defer test_structure.RunTestStage(t, "destroy_team", func() {
terraform.Destroy(t, test_structure.LoadTerraformOptions(t, workingDir))
})
// Value will be assigned once team is created, for verification
createdTeamId := ""
runId := generateRunId()
teamName := "terratest team " + runId
teamDescription := "This team was created by Terratest from the terraform-pagerduty repo with run ID: " + runId
teamMembers := map[string]string{}
teamMembers := map[string]string{}
test_structure.RunTestStage(t, "create_team", func() {
options := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
TerraformDir: workingDir,
Vars: map[string]interface{}{
"name": teamName,
"description": teamDescription,
"name": teamName,
"description": teamDescription,
"team_members": teamMembers,
},
})
Expand All @@ -58,10 +57,5 @@ func TestPagerdutyTeam(t *testing.T) {
}
}
assert.NotNil(t, targetTeam)

})

test_structure.RunTestStage(t, "destroy_team", func() {
terraform.Destroy(t, test_structure.LoadTerraformOptions(t, workingDir))
})
}
23 changes: 13 additions & 10 deletions test/pagerduty_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package test

import (
"context"
"fmt"
"github.com/PagerDuty/go-pagerduty"
http_helper "github.com/gruntwork-io/terratest/modules/http-helper"
"github.com/gruntwork-io/terratest/modules/terraform"
Expand All @@ -17,24 +18,24 @@ func TestPagerdutyUser(t *testing.T) {

// Working dirs
userWorkingDir := ""
generatedUserNameSuffix := ""

// For assignment later
createdUserId := ""
defer test_structure.RunTestStage(t, "destroy_user", func() {
destroyUser(t, pagerdutyApiToken, userWorkingDir)
})

test_structure.RunTestStage(t, "create_user", func() {

// NOTE: We use `read_only_user` (Stakeholder) because those licenses are available.
// We don't always have standard `user` licenses available so the test will fail
// when trying to create a user with no licenses available.
userWorkingDir, createdUserId = createUser(t, "read_only_user", pagerdutyApiToken)
})

defer test_structure.RunTestStage(t, "destroy_user", func() {
destroyUser(t, pagerdutyApiToken, userWorkingDir)
userWorkingDir, createdUserId, generatedUserNameSuffix = createUser(t, "read_only_user", pagerdutyApiToken)
})

test_structure.RunTestStage(t, "verify_user", func() {
verifyUser(t, createdUserId, "read_only_user", pagerdutyApiBaseUrl, pagerdutyApiToken)
verifyUser(t, createdUserId, "read_only_user", pagerdutyApiBaseUrl, pagerdutyApiToken, fmt.Sprintf("example-%s", generatedUserNameSuffix))
})
}

Expand Down Expand Up @@ -64,7 +65,7 @@ func TestPagerdutyUser(t *testing.T) {
// })
//}

func createUser(t *testing.T, role string, pagerdutyApiToken string) (string, string) {
func createUser(t *testing.T, role string, pagerdutyApiToken string) (string, string, string) {
//workingDir := test_structure.CopyTerraformFolderToTemp(t, "..", "modules/pagerduty-user")
workingDir := "../examples/pagerduty-user"
log.Println("createUser - workingDir is: ", workingDir)
Expand All @@ -86,16 +87,17 @@ func createUser(t *testing.T, role string, pagerdutyApiToken string) (string, st
terraform.InitAndApply(t, createUserTerraformOptions)
pagerdutyUserId := terraform.Output(t, createUserTerraformOptions, "id")
log.Println("🔢 returning PagerDuty User ID: ", pagerdutyUserId)
generatedUserNameSuffix := terraform.Output(t, createUserTerraformOptions, "generated_user_name_suffix")

return workingDir, pagerdutyUserId
return workingDir, pagerdutyUserId, generatedUserNameSuffix
}

func destroyUser(t *testing.T, pagerdutyApiToken string, workingDir string) {
terraformOptions := test_structure.LoadTerraformOptions(t, workingDir)
terraform.Destroy(t, terraformOptions)
}

func verifyUser(t *testing.T, pagerdutyUserId string, role string, pagerdutyApiBaseUrl string, pagerdutyApiToken string) {
func verifyUser(t *testing.T, pagerdutyUserId string, role string, pagerdutyApiBaseUrl string, pagerdutyApiToken string, expectedUserName string) {
pagerdutyApiOptions := http_helper.HttpDoOptions{
Method: "GET",
Url: pagerdutyApiBaseUrl + "/users/" + pagerdutyUserId,
Expand All @@ -111,7 +113,8 @@ func verifyUser(t *testing.T, pagerdutyUserId string, role string, pagerdutyApiB

// Should return 200
assert.Equalf(t, 200, status, "incorrect response code, expected 200")
assert.Containsf(t, response, "\"name\":\"example\"", "correct name not found")
fmt.Printf("json = %s", response)
assert.Containsf(t, response, fmt.Sprintf("\"name\":\"%s\"", expectedUserName), "correct name not found")
assert.Containsf(t, response, "\"email\":\"pagerduty-user-example@honestbank.com\"", "correct email not found")

client := pagerduty.NewClient(loadPagerdutyToken(t))
Expand Down

0 comments on commit e1c536a

Please sign in to comment.