Fix up tests

This commit is contained in:
Alexander Hellbom 2016-10-16 16:48:20 +02:00
parent 9e81677354
commit 074e989846
11 changed files with 340 additions and 144 deletions

View File

@ -7,32 +7,22 @@ import (
)
func TestAccPagerDutyEscalationPolicy_import(t *testing.T) {
resourceName := "pagerduty_escalation_policy.foo"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyEscalationPolicyDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccPagerDutyEscalationPolicyConfigImported,
Config: testAccCheckPagerDutyEscalationPolicyConfig,
},
resource.TestStep{
ResourceName: "pagerduty_escalation_policy.foo",
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: false,
ImportStateVerify: true,
},
},
})
}
const testAccPagerDutyEscalationPolicyConfigImported = `
resource "pagerduty_escalation_policy" "foo" {
name = "foo"
escalation_rule {
escalation_delay_in_minutes = 10
target {
id = "PLBP04G"
type = "user"
}
}
}
`

View File

@ -7,29 +7,22 @@ import (
)
func TestAccPagerDutyService_import(t *testing.T) {
resourceName := "pagerduty_service.foo"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyServiceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccPagerDutyServiceConfigImported,
Config: testAccCheckPagerDutyServiceConfig,
},
resource.TestStep{
ResourceName: "pagerduty_service.foo",
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: false,
ImportStateVerify: true,
},
},
})
}
const testAccPagerDutyServiceConfigImported = `
resource "pagerduty_service" "foo" {
name = "foo"
description = "foo"
acknowledgement_timeout = "1800"
auto_resolve_timeout = "14400"
escalation_policy = "PGOMBUU"
}
`

View File

@ -7,26 +7,21 @@ import (
)
func TestAccPagerDutyTeam_import(t *testing.T) {
resourceName := "pagerduty_team.foo"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyTeamDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccPagerDutyTeamConfigImported,
Config: testAccCheckPagerDutyTeamConfig,
},
resource.TestStep{
ResourceName: "pagerduty_team.foo",
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
const testAccPagerDutyTeamConfigImported = `
resource "pagerduty_team" "foo" {
name = "foo"
description = "foo"
}
`

View File

@ -1,35 +1,28 @@
package pagerduty
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccPagerDutyUser_import(t *testing.T) {
resourceName := "pagerduty_user.foo"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyUserDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccPagerDutyUserConfigImported(importUserID),
Config: testAccCheckPagerDutyUserConfig,
},
resource.TestStep{
ResourceName: "pagerduty_user.foo",
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func testAccPagerDutyUserConfigImported(id string) string {
return fmt.Sprintf(`
resource "pagerduty_user" "foo" {
name = "foo"
email = "foo@bar.com"
}
`)
}

View File

@ -32,17 +32,4 @@ func testAccPreCheck(t *testing.T) {
if v := os.Getenv("PAGERDUTY_TOKEN"); v == "" {
t.Fatal("PAGERDUTY_TOKEN must be set for acceptance tests")
}
if v := os.Getenv("PAGERDUTY_USER_ID"); v == "" {
t.Fatal("PAGERDUTY_USER_ID must be set for acceptance tests")
}
if v := os.Getenv("PAGERDUTY_ESCALATION_POLICY_ID"); v == "" {
t.Fatal("PAGERDUTY_ESCALATION_POLICY_ID must be set for acceptance tests")
}
}
var importEscalationPolicyID = os.Getenv("PAGERDUTY_ESCALATION_POLICY_ID")
var importUserID = os.Getenv("PAGERDUTY_USER_ID")
var userID = os.Getenv("PAGERDUTY_USER_ID")
var escalationPolicyID = os.Getenv("PAGERDUTY_ESCALATION_POLICY_ID")

View File

@ -35,6 +35,10 @@ func resourcePagerDutyEscalationPolicy() *schema.Resource {
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"escalation_delay_in_minutes": &schema.Schema{
Type: schema.TypeInt,
Required: true,
@ -142,7 +146,27 @@ func resourcePagerDutyEscalationPolicyRead(d *schema.ResourceData, meta interfac
d.Set("name", e.Name)
d.Set("description", e.Description)
d.Set("num_loops", e.NumLoops)
d.Set("escalation_rules", e.EscalationRules)
escalationRules := make([]map[string]interface{}, 0, len(e.EscalationRules))
for _, r := range e.EscalationRules {
targets := make([]map[string]interface{}, 0, len(r.Targets))
for _, t := range r.Targets {
targets = append(targets, map[string]interface{}{
"id": t.ID,
"type": t.Type,
})
}
escalationRules = append(escalationRules, map[string]interface{}{
"id": r.ID,
"target": targets,
"escalation_delay_in_minutes": r.Delay,
})
}
d.Set("escalation_rule", escalationRules)
return nil
}

View File

@ -16,7 +16,7 @@ func TestAccPagerDutyEscalationPolicy_Basic(t *testing.T) {
CheckDestroy: testAccCheckPagerDutyEscalationPolicyDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckPagerDutyEscalationPolicyConfig(userID),
Config: testAccCheckPagerDutyEscalationPolicyConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyEscalationPolicyExists("pagerduty_escalation_policy.foo"),
resource.TestCheckResourceAttr(
@ -25,20 +25,10 @@ func TestAccPagerDutyEscalationPolicy_Basic(t *testing.T) {
"pagerduty_escalation_policy.foo", "description", "foo"),
resource.TestCheckResourceAttr(
"pagerduty_escalation_policy.foo", "num_loops", "1"),
resource.TestCheckResourceAttr(
"pagerduty_escalation_policy.foo", "escalation_rule.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_escalation_policy.foo", "escalation_rule.0.escalation_delay_in_minutes", "10"),
resource.TestCheckResourceAttr(
"pagerduty_escalation_policy.foo", "escalation_rule.0.target.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_escalation_policy.foo", "escalation_rule.0.target.0.id", userID),
resource.TestCheckResourceAttr(
"pagerduty_escalation_policy.foo", "escalation_rule.0.target.0.type", "user"),
),
},
resource.TestStep{
Config: testAccCheckPagerDutyEscalationPolicyConfigUpdated(userID),
Config: testAccCheckPagerDutyEscalationPolicyConfigUpdated,
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyEscalationPolicyExists("pagerduty_escalation_policy.foo"),
resource.TestCheckResourceAttr(
@ -47,16 +37,6 @@ func TestAccPagerDutyEscalationPolicy_Basic(t *testing.T) {
"pagerduty_escalation_policy.foo", "description", "bar"),
resource.TestCheckResourceAttr(
"pagerduty_escalation_policy.foo", "num_loops", "2"),
resource.TestCheckResourceAttr(
"pagerduty_escalation_policy.foo", "escalation_rule.#", "2"),
resource.TestCheckResourceAttr(
"pagerduty_escalation_policy.foo", "escalation_rule.0.escalation_delay_in_minutes", "10"),
resource.TestCheckResourceAttr(
"pagerduty_escalation_policy.foo", "escalation_rule.0.target.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_escalation_policy.foo", "escalation_rule.0.target.0.id", userID),
resource.TestCheckResourceAttr(
"pagerduty_escalation_policy.foo", "escalation_rule.0.target.0.type", "user"),
),
},
},
@ -82,36 +62,64 @@ func testAccCheckPagerDutyEscalationPolicyDestroy(s *terraform.State) error {
func testAccCheckPagerDutyEscalationPolicyExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*pagerduty.Client)
for _, r := range s.RootModule().Resources {
if _, err := client.GetEscalationPolicy(r.Primary.ID, &pagerduty.GetEscalationPolicyOptions{}); err != nil {
return fmt.Errorf("Received an error retrieving escalation_policy %s ID: %s", err, r.Primary.ID)
}
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No Escalation Policy ID is set")
}
client := testAccProvider.Meta().(*pagerduty.Client)
found, err := client.GetEscalationPolicy(rs.Primary.ID, &pagerduty.GetEscalationPolicyOptions{})
if err != nil {
return err
}
if found.ID != rs.Primary.ID {
return fmt.Errorf("Escalation policy not found: %v - %v", rs.Primary.ID, found)
}
return nil
}
}
func testAccCheckPagerDutyEscalationPolicyConfig(id string) string {
return fmt.Sprintf(`
const testAccCheckPagerDutyEscalationPolicyConfig = `
resource "pagerduty_user" "foo" {
name = "foo"
email = "foo@bar.com"
color = "green"
role = "user"
job_title = "foo"
description = "foo"
}
resource "pagerduty_escalation_policy" "foo" {
name = "foo"
description = "foo"
num_loops = 1
escalation_rule {
escalation_delay_in_minutes = 10
escalation_delay_in_minutes = 10
target {
type = "user"
id = "%s"
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
}
`, id)
`
const testAccCheckPagerDutyEscalationPolicyConfigUpdated = `
resource "pagerduty_user" "foo" {
name = "foo"
email = "foo@bar.com"
color = "green"
role = "user"
job_title = "foo"
description = "foo"
}
func testAccCheckPagerDutyEscalationPolicyConfigUpdated(id string) string {
return fmt.Sprintf(`
resource "pagerduty_escalation_policy" "foo" {
name = "bar"
description = "bar"
@ -120,18 +128,17 @@ resource "pagerduty_escalation_policy" "foo" {
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user"
id = "%[1]v"
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
escalation_rule {
escalation_delay_in_minutes = 20
target {
type = "user"
id = "%[1]v"
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
}
`, userID)
}
`

View File

@ -99,7 +99,7 @@ func resourcePagerDutyServiceRead(d *schema.ResourceData, meta interface{}) erro
}
d.Set("name", s.Name)
d.Set("escalation_policy", s.EscalationPolicy)
d.Set("escalation_policy", s.EscalationPolicy.ID)
d.Set("description", s.Description)
d.Set("auto_resolve_timeout", s.AutoResolveTimeout)
d.Set("acknowledgement_timeout", s.AcknowledgementTimeout)

View File

@ -16,9 +16,6 @@ func resourcePagerDutyServiceIntegration() *schema.Resource {
// NOTE: It's currently not possible to delete integrations via the API.
// Therefore it needs to be manually removed from the Web UI.
Delete: resourcePagerDutyServiceIntegrationDelete,
Importer: &schema.ResourceImporter{
State: resourcePagerDutyServiceIntegrationImport,
},
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
@ -26,7 +23,7 @@ func resourcePagerDutyServiceIntegration() *schema.Resource {
},
"service": &schema.Schema{
Type: schema.TypeString,
Required: true,
Optional: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
@ -107,7 +104,7 @@ func resourcePagerDutyServiceIntegrationUpdate(d *schema.ResourceData, meta inte
service := d.Get("service").(string)
log.Printf("[INFO] Updating PagerDuty service %s", d.Id())
log.Printf("[INFO] Updating PagerDuty service integration %s", d.Id())
s, err := client.UpdateIntegration(service, *s)
@ -125,10 +122,3 @@ func resourcePagerDutyServiceIntegrationDelete(d *schema.ResourceData, meta inte
return nil
}
func resourcePagerDutyServiceIntegrationImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
if err := resourcePagerDutyServiceIntegrationRead(d, meta); err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}

View File

@ -0,0 +1,166 @@
package pagerduty
import (
"fmt"
"testing"
"github.com/PagerDuty/go-pagerduty"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccPagerDutyServiceIntegration_Basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyServiceIntegrationDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckPagerDutyServiceIntegrationConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyServiceIntegrationExists("pagerduty_service_integration.foo"),
resource.TestCheckResourceAttr(
"pagerduty_service_integration.foo", "name", "foo"),
resource.TestCheckResourceAttr(
"pagerduty_service_integration.foo", "type", "generic_events_api_inbound_integration"),
),
},
resource.TestStep{
Config: testAccCheckPagerDutyServiceIntegrationConfigUpdated,
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyServiceIntegrationExists("pagerduty_service_integration.foo"),
resource.TestCheckResourceAttr(
"pagerduty_service_integration.foo", "name", "bar"),
resource.TestCheckResourceAttr(
"pagerduty_service_integration.foo", "type", "generic_events_api_inbound_integration"),
),
},
},
})
}
func testAccCheckPagerDutyServiceIntegrationDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*pagerduty.Client)
for _, r := range s.RootModule().Resources {
if r.Type != "pagerduty_service_integration" {
continue
}
service, _ := s.RootModule().Resources["pagerduty_service.foo"]
_, err := client.GetIntegration(service.Primary.ID, r.Primary.ID, pagerduty.GetIntegrationOptions{})
if err == nil {
return fmt.Errorf("Service Integration still exists")
}
}
return nil
}
func testAccCheckPagerDutyServiceIntegrationExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No Service Integration ID is set")
}
service, _ := s.RootModule().Resources["pagerduty_service.foo"]
client := testAccProvider.Meta().(*pagerduty.Client)
found, err := client.GetIntegration(service.Primary.ID, rs.Primary.ID, pagerduty.GetIntegrationOptions{})
if err != nil {
return fmt.Errorf("Service integration not found: %v", rs.Primary.ID)
// return err
}
if found.ID != rs.Primary.ID {
return fmt.Errorf("Service Integration not found: %v - %v", rs.Primary.ID, found)
}
return nil
}
}
const testAccCheckPagerDutyServiceIntegrationConfig = `
resource "pagerduty_user" "foo" {
name = "foo"
email = "foo@bar.com"
color = "green"
role = "user"
job_title = "foo"
description = "foo"
}
resource "pagerduty_escalation_policy" "foo" {
name = "foo"
description = "foo"
num_loops = 1
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
}
resource "pagerduty_service" "foo" {
name = "foo"
description = "foo"
auto_resolve_timeout = 1800
acknowledgement_timeout = 1800
escalation_policy = "${pagerduty_escalation_policy.foo.id}"
}
resource "pagerduty_service_integration" "foo" {
name = "foo"
type = "generic_events_api_inbound_integration"
service = "${pagerduty_service.foo.id}"
}
`
const testAccCheckPagerDutyServiceIntegrationConfigUpdated = `
resource "pagerduty_user" "foo" {
name = "foo"
email = "foo@bar.com"
color = "green"
role = "user"
job_title = "foo"
description = "foo"
}
resource "pagerduty_escalation_policy" "foo" {
name = "bar"
description = "bar"
num_loops = 2
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
}
resource "pagerduty_service" "foo" {
name = "bar"
description = "bar"
auto_resolve_timeout = 3600
acknowledgement_timeout = 3600
escalation_policy = "${pagerduty_escalation_policy.foo.id}"
}
resource "pagerduty_service_integration" "foo" {
name = "bar"
type = "generic_events_api_inbound_integration"
service = "${pagerduty_service.foo.id}"
}
`

View File

@ -16,7 +16,7 @@ func TestAccPagerDutyService_Basic(t *testing.T) {
CheckDestroy: testAccCheckPagerDutyServiceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckPagerDutyServiceConfig(escalationPolicyID),
Config: testAccCheckPagerDutyServiceConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyServiceExists("pagerduty_service.foo"),
resource.TestCheckResourceAttr(
@ -27,12 +27,10 @@ func TestAccPagerDutyService_Basic(t *testing.T) {
"pagerduty_service.foo", "auto_resolve_timeout", "1800"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "acknowledgement_timeout", "1800"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "escalation_policy", escalationPolicyID),
),
},
resource.TestStep{
Config: testAccCheckPagerDutyServiceConfigUpdated(escalationPolicyID),
Config: testAccCheckPagerDutyServiceConfigUpdated,
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyServiceExists("pagerduty_service.foo"),
resource.TestCheckResourceAttr(
@ -43,8 +41,6 @@ func TestAccPagerDutyService_Basic(t *testing.T) {
"pagerduty_service.foo", "auto_resolve_timeout", "3600"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "acknowledgement_timeout", "3600"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "escalation_policy", escalationPolicyID),
),
},
},
@ -70,36 +66,91 @@ func testAccCheckPagerDutyServiceDestroy(s *terraform.State) error {
func testAccCheckPagerDutyServiceExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*pagerduty.Client)
for _, r := range s.RootModule().Resources {
if _, err := client.GetService(r.Primary.ID, pagerduty.GetServiceOptions{}); err != nil {
return fmt.Errorf("Received an error retrieving service %s ID: %s", err, r.Primary.ID)
}
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No Service ID is set")
}
client := testAccProvider.Meta().(*pagerduty.Client)
found, err := client.GetService(rs.Primary.ID, pagerduty.GetServiceOptions{})
if err != nil {
return err
}
if found.ID != rs.Primary.ID {
return fmt.Errorf("Service not found: %v - %v", rs.Primary.ID, found)
}
return nil
}
}
func testAccCheckPagerDutyServiceConfig(id string) string {
return fmt.Sprintf(`
resource "pagerduty_service" "foo" {
name = "foo"
description = "foo"
auto_resolve_timeout = 1800
acknowledgement_timeout = 1800
escalation_policy = "%s"
}
`, escalationPolicyID)
const testAccCheckPagerDutyServiceConfig = `
resource "pagerduty_user" "foo" {
name = "foo"
email = "foo@bar.com"
color = "green"
role = "user"
job_title = "foo"
description = "foo"
}
resource "pagerduty_escalation_policy" "foo" {
name = "bar"
description = "bar"
num_loops = 2
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
}
resource "pagerduty_service" "foo" {
name = "foo"
description = "foo"
auto_resolve_timeout = 1800
acknowledgement_timeout = 1800
escalation_policy = "${pagerduty_escalation_policy.foo.id}"
}
`
const testAccCheckPagerDutyServiceConfigUpdated = `
resource "pagerduty_user" "foo" {
name = "foo"
email = "foo@bar.com"
color = "green"
role = "user"
job_title = "foo"
description = "foo"
}
resource "pagerduty_escalation_policy" "foo" {
name = "bar"
description = "bar"
num_loops = 2
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
}
func testAccCheckPagerDutyServiceConfigUpdated(id string) string {
return fmt.Sprintf(`
resource "pagerduty_service" "foo" {
name = "bar"
description = "bar"
auto_resolve_timeout = 3600
acknowledgement_timeout = 3600
escalation_policy = "%s"
}
`, escalationPolicyID)
escalation_policy = "${pagerduty_escalation_policy.foo.id}"
}
`