github.com/IBM-Cloud/terraform@v0.6.4-0.20170726051544-8872b87621df/builtin/providers/pagerduty/data_source_pagerduty_escalation_policy_test.go (about)

     1  package pagerduty
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccDataSourcePagerDutyEscalationPolicy_Basic(t *testing.T) {
    13  	username := fmt.Sprintf("tf-%s", acctest.RandString(5))
    14  	email := fmt.Sprintf("%s@foo.com", username)
    15  	escalationPolicy := fmt.Sprintf("tf-%s", acctest.RandString(5))
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:  func() { testAccPreCheck(t) },
    19  		Providers: testAccProviders,
    20  		Steps: []resource.TestStep{
    21  			{
    22  				Config: testAccDataSourcePagerDutyEscalationPolicyConfig(username, email, escalationPolicy),
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccDataSourcePagerDutyEscalationPolicy("pagerduty_escalation_policy.test", "data.pagerduty_escalation_policy.by_name"),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func testAccDataSourcePagerDutyEscalationPolicy(src, n string) resource.TestCheckFunc {
    32  	return func(s *terraform.State) error {
    33  
    34  		srcR := s.RootModule().Resources[src]
    35  		srcA := srcR.Primary.Attributes
    36  
    37  		r := s.RootModule().Resources[n]
    38  		a := r.Primary.Attributes
    39  
    40  		if a["id"] == "" {
    41  			return fmt.Errorf("Expected to get a escalation policy ID from PagerDuty")
    42  		}
    43  
    44  		testAtts := []string{"id", "name"}
    45  
    46  		for _, att := range testAtts {
    47  			if a[att] != srcA[att] {
    48  				return fmt.Errorf("Expected the escalation policy %s to be: %s, but got: %s", att, srcA[att], a[att])
    49  			}
    50  		}
    51  
    52  		return nil
    53  	}
    54  }
    55  
    56  func testAccDataSourcePagerDutyEscalationPolicyConfig(username, email, escalationPolicy string) string {
    57  	return fmt.Sprintf(`
    58  resource "pagerduty_user" "test" {
    59    name  = "%s"
    60    email = "%s"
    61  }
    62  
    63  resource "pagerduty_escalation_policy" "test" {
    64    name        = "%s"
    65    num_loops   = 2
    66  
    67    rule {
    68      escalation_delay_in_minutes = 10
    69  
    70      target {
    71        type = "user_reference"
    72        id   = "${pagerduty_user.test.id}"
    73      }
    74    }
    75  }
    76  
    77  data "pagerduty_escalation_policy" "by_name" {
    78    name = "${pagerduty_escalation_policy.test.name}"
    79  }
    80  `, username, email, escalationPolicy)
    81  }