-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from splunk/VOAPI-574-acceptance-tests
Added acceptance tests
- Loading branch information
Showing
17 changed files
with
513 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ terraform.tfstate | |
terraform.tfstate.backup | ||
*.log | ||
.DS_Store | ||
.envs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Define the first tf-configured user, 'John Dane' | ||
resource "victorops_user" "test_user" { | ||
first_name = "{{.User.FirstName}}" | ||
last_name = "{{.User.LastName}}" | ||
user_name = "{{.User.Username}}" | ||
email = "{{.User.Email}}" | ||
is_admin = true | ||
replacement_user = "{{.Replacement}}" | ||
} | ||
|
||
// Create a new team | ||
resource "victorops_team" "test_team" { | ||
name = "{{.TeamName}}" | ||
} | ||
|
||
// Assigning an existing user to a team | ||
resource "victorops_team_membership" "test_membership" { | ||
team_id = victorops_team.test_team.id | ||
user_name = victorops_user.test_user.user_name | ||
replacement_user = victorops_user.test_user.replacement_user | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
resource "victorops_team" "test_team" { | ||
name = "{{.TeamName}}" | ||
} | ||
|
||
resource "victorops_escalation_policy" "test_policy" { | ||
name = "{{.PolicyName}}" | ||
team_id = victorops_team.test_team.id | ||
step { | ||
timeout = 60 | ||
entries = [ | ||
{ | ||
type = "rotationGroup" | ||
slug = "{{.RotationSlug}}" | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
|
||
resource "victorops_team" "test_team_1" { | ||
name = "{{.TeamName}}" | ||
} | ||
|
||
resource "victorops_escalation_policy" "test_policy_2" { | ||
name = "{{.PolicyName}}" | ||
team_id = victorops_team.test_team_1.id | ||
step { | ||
timeout = 60 | ||
entries = [] | ||
} | ||
} | ||
|
||
// Create routing keys to push alerts to our escalation policies | ||
resource "victorops_routing_key" "test_key" { | ||
name = "{{.RoutingKeyName}}" | ||
targets = [victorops_escalation_policy.test_policy_2.id] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
resource "victorops_team" "test_team" { | ||
name = "{{.}}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
resource "victorops_user" "test_user" { | ||
first_name = "{{.User.FirstName}}" | ||
last_name = "{{.User.LastName}}" | ||
user_name = "{{.User.Username}}" | ||
email = "{{.User.Email}}" | ||
is_admin = true | ||
replacement_user = "{{.Replacement}}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,86 @@ | ||
package victorops | ||
|
||
// todo: placeholder for acceptance tests | ||
import ( | ||
"fmt" | ||
"github.com/bxcodec/faker/v3" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
"github.com/victorops/go-victorops/victorops" | ||
"os" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
type MembershipData struct { | ||
User victorops.User | ||
TeamName string | ||
Replacement string | ||
} | ||
|
||
func TestAccCreateTeamMembership(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping testing in short mode") | ||
} | ||
|
||
tfMembershipResourceName := "victorops_team_membership.test_membership" | ||
membership := createNewMembershipModel() | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(t) | ||
}, | ||
Providers: testAccProviders, | ||
CheckDestroy: testMembershipDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: createMembershipResource(membership), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccTeamMembershipExists(tfMembershipResourceName), | ||
resource.TestCheckResourceAttr(tfMembershipResourceName, "user_name", membership.User.Username), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func createNewMembershipModel() MembershipData { | ||
replacementUser := os.Getenv("VO_REPLACEMENT_USERNAME") | ||
|
||
return MembershipData{ | ||
User: victorops.User{ | ||
FirstName: faker.FirstName(), | ||
LastName: faker.LastName(), | ||
Username: strings.ToLower(faker.Username()), | ||
Email: faker.Email(), | ||
Admin: true, | ||
}, | ||
TeamName: faker.Word(), | ||
Replacement: replacementUser, | ||
} | ||
} | ||
|
||
func createMembershipResource(md MembershipData) string { | ||
return getTestTemplate("test_membership.tf", md) | ||
} | ||
|
||
func testAccTeamMembershipExists(resource string) resource.TestCheckFunc { | ||
return func(state *terraform.State) error { | ||
rs, ok := state.RootModule().Resources[resource] | ||
if !ok { | ||
return fmt.Errorf("not found: %s", resource) | ||
} | ||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("no record ID is set") | ||
} | ||
teamSlug := rs.Primary.ID | ||
apiClient := testAccProvider.Meta().(Config).VictorOpsClient | ||
_, _, err := apiClient.GetTeam(teamSlug) | ||
if err != nil { | ||
return fmt.Errorf("error fetching item with resource %s. %s", resource, err) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func testMembershipDestroy(s *terraform.State) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,77 @@ | ||
package victorops | ||
|
||
// todo: placeholder for acceptance tests | ||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
func TestAccTeamCreate(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping testing in short mode") | ||
} | ||
|
||
teamName := "DevOps" | ||
tfResourceName := "victorops_team.test_team" | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(t) | ||
}, | ||
Providers: testAccProviders, | ||
CheckDestroy: testTeamDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: createTeamResource(teamName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccTeamExists(tfResourceName), | ||
resource.TestCheckResourceAttr(tfResourceName, "name", teamName), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func createTeamResource(s string) string { | ||
return getTestTemplate("test_team.tf", s) | ||
} | ||
|
||
func testAccTeamExists(resource string) resource.TestCheckFunc { | ||
return func(state *terraform.State) error { | ||
rs, ok := state.RootModule().Resources[resource] | ||
if !ok { | ||
return fmt.Errorf("not found: %s", resource) | ||
} | ||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("no record ID is set") | ||
} | ||
teamSlug := rs.Primary.ID | ||
apiClient := testAccProvider.Meta().(Config).VictorOpsClient | ||
_, _, err := apiClient.GetTeam(teamSlug) | ||
if err != nil { | ||
return fmt.Errorf("error fetching item with resource %s. %s", resource, err) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func testTeamDestroy(s *terraform.State) error { | ||
|
||
apiClient := testAccProvider.Meta().(Config).VictorOpsClient | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "victorops_team" { | ||
continue | ||
} | ||
|
||
_, response, _ := apiClient.GetTeam(rs.Primary.ID) | ||
notFoundErr := "{\"error\":\"No team 'team-.+' found\"}" | ||
expectedErr := regexp.MustCompile(notFoundErr) | ||
if !expectedErr.Match([]byte(response.ResponseBody)) { | ||
return fmt.Errorf("expected %s, got %s", notFoundErr, response.ResponseBody) | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.