Skip to content

Commit

Permalink
Merge pull request #2 from splunk/VOAPI-574-acceptance-tests
Browse files Browse the repository at this point in the history
Added acceptance tests
  • Loading branch information
kevals-splunk authored Oct 2, 2020
2 parents c440b62 + 34e4c6f commit e69e3ae
Show file tree
Hide file tree
Showing 17 changed files with 513 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ terraform.tfstate
terraform.tfstate.backup
*.log
.DS_Store
.envs
4 changes: 2 additions & 2 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ sweep:
go test $(SWEEP_DIR) -v -sweep=$(SWEEP) $(SWEEPARGS) -timeout 60m

test: fmtcheck
go test $(TEST) $(TESTARGS) -timeout=120s -parallel=4
go test $(TEST) $(TESTARGS) -short -timeout=120s -parallel=4

testacc: fmtcheck
TF_ACC=1 go test $(TEST) -v -count $(TEST_COUNT) -parallel 20 $(TESTARGS) -timeout 120m
go test $(TEST) -v -count $(TEST_COUNT) -parallel 20 $(TESTARGS) -timeout 120m

fmt:
@echo "==> Fixing source code with gofmt..."
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ resource "victorops_escalation_policy" "high_severity" {
slug = "rtg-wvvhXshpvaRdn7jM"
}
]
}
}
}
// Create routing keys to push alerts to our escalation policies
Expand All @@ -119,6 +119,14 @@ $ make test

In order to run the full suite of Acceptance tests, run `make testacc`.

Acceptance tests require the following environment variables to be set.

- VO_API_ID
- VO_API_KEY
- VO_BASE_URL
-VO_REPLACEMENT_USERNAME
- the default username to replace all users when removed

```sh
$ make testacc
```
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
cloud.google.com/go v0.55.0 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/aws/aws-sdk-go v1.29.31 // indirect
github.com/bxcodec/faker/v3 v3.5.0
github.com/fatih/color v1.9.0 // indirect
github.com/hashicorp/go-getter v1.4.1 // indirect
github.com/hashicorp/go-hclog v0.12.1 // indirect
Expand Down
88 changes: 88 additions & 0 deletions go.sum

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions templates/test_membership.tf
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
}
17 changes: 17 additions & 0 deletions templates/test_policy.tf
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}}"
}
]
}
}
20 changes: 20 additions & 0 deletions templates/test_routing_key.tf
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]
}
3 changes: 3 additions & 0 deletions templates/test_team.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "victorops_team" "test_team" {
name = "{{.}}"
}
8 changes: 8 additions & 0 deletions templates/test_user.tf
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}}"
}
42 changes: 34 additions & 8 deletions victorops/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package victorops

import (
"bytes"
"fmt"
"github.com/stretchr/testify/assert"
"html/template"
"io/ioutil"
"log"
"os"
"testing"

Expand All @@ -14,11 +17,39 @@ import (

var testAccProviders map[string]terraform.ResourceProvider
var testAccProvider *schema.Provider
var tfTemplate *template.Template

func init() {
tfTemplate = template.Must(tfTemplate.ParseGlob("../templates/*.tf"))
testAccProvider = Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{
"victorops_config_test": testAccProvider,
"victorops": testAccProvider,
}
}

func getTestTemplate(f string, d interface{}) string {
buf := &bytes.Buffer{}
err := tfTemplate.ExecuteTemplate(buf, f, d)
if err != nil {
log.Fatalln(err)
log.Fatalln("Could not find template file", f)
}
return buf.String()
}

func testAccPreCheck(t *testing.T) {
if v := os.Getenv("VO_API_ID"); v == "" {
t.Fatal("VO_API_ID must be set for acceptance tests")
}
if v := os.Getenv("VO_API_KEY"); v == "" {
t.Fatal("VO_API_KEY must be set for acceptance tests")
}
if v := os.Getenv("VO_BASE_URL"); v == "" {
t.Fatal("VO_BASE_URL must be set for acceptance tests")
}
if v := os.Getenv("VO_REPLACEMENT_USERNAME"); v == "" {
t.Fatal("VO_REPLACEMENT_USERNAME must be set for acceptance tests")
}
}

Expand Down Expand Up @@ -95,9 +126,9 @@ func TestProviderConfigureFromNothing(t *testing.T) {
if configuration, err := configureTestProvider(testAccProviders["victorops_config_test"], raw); err != nil {
t.Fatalf("Expected metadata, got nil. err: %s", err.Error())
} else {
assert.Equal(t, "", configuration.APIId)
assert.Equal(t, "", configuration.APIKey)
assert.Equal(t, "https://api.victorops.com", configuration.BaseURL)
assert.Equal(t, os.Getenv("VO_API_ID"), configuration.APIId)
assert.Equal(t, os.Getenv("VO_API_KEY"), configuration.APIKey)
assert.Equal(t, os.Getenv("VO_BASE_URL"), configuration.BaseURL)
}
}

Expand All @@ -122,8 +153,3 @@ func TestProvider(t *testing.T) {
t.Fatalf("err: %s", err)
}
}

// TODO: Set-up the client connection to test-org for initiating Acceptance tests
func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
}
2 changes: 0 additions & 2 deletions victorops/resource_victorops_contact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,3 @@ func TestTypeToContactType(t *testing.T) {
assert.Equal(t, val1, call1)
assert.Equal(t, val2, call2)
}

// todo: Add acceptance tests
3 changes: 0 additions & 3 deletions victorops/resource_victorops_escalation_policy_test.go

This file was deleted.

3 changes: 0 additions & 3 deletions victorops/resource_victorops_routing_key_test.go

This file was deleted.

85 changes: 84 additions & 1 deletion victorops/resource_victorops_team_membership_test.go
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
}
76 changes: 75 additions & 1 deletion victorops/resource_victorops_team_test.go
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
}
Loading

0 comments on commit e69e3ae

Please sign in to comment.