Skip to content

Commit

Permalink
test: Update all tests to account for the random suffixes in their as…
Browse files Browse the repository at this point in the history
…sertions AND fix the 'destroy' part of these tests by moving the defer block upto the top
  • Loading branch information
bhargavms committed Jan 11, 2024
1 parent 7abb564 commit c4e9e19
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 49 deletions.
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
13 changes: 9 additions & 4 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 All @@ -45,7 +45,12 @@ func createPagerdutyService(t *testing.T, workingDir string, runID string) (stri
}

func destroyPagerdutyService(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 verifyPagerdutyService(t *testing.T, serviceId string, serviceName string) {
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 c4e9e19

Please sign in to comment.