github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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  	rName := acctest.RandString(5)
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:  func() { testAccPreCheck(t) },
    16  		Providers: testAccProviders,
    17  		Steps: []resource.TestStep{
    18  			resource.TestStep{
    19  				Config: testAccDataSourcePagerDutyEscalationPolicyConfig(rName),
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testAccDataSourcePagerDutyEscalationPolicy("pagerduty_escalation_policy.test", "data.pagerduty_escalation_policy.by_name"),
    22  				),
    23  			},
    24  		},
    25  	})
    26  }
    27  
    28  func testAccDataSourcePagerDutyEscalationPolicy(src, n string) resource.TestCheckFunc {
    29  	return func(s *terraform.State) error {
    30  
    31  		srcR := s.RootModule().Resources[src]
    32  		srcA := srcR.Primary.Attributes
    33  
    34  		r := s.RootModule().Resources[n]
    35  		a := r.Primary.Attributes
    36  
    37  		if a["id"] == "" {
    38  			return fmt.Errorf("Expected to get a escalation policy ID from PagerDuty")
    39  		}
    40  
    41  		testAtts := []string{"id", "name"}
    42  
    43  		for _, att := range testAtts {
    44  			if a[att] != srcA[att] {
    45  				return fmt.Errorf("Expected the escalation policy %s to be: %s, but got: %s", att, srcA[att], a[att])
    46  			}
    47  		}
    48  
    49  		return nil
    50  	}
    51  }
    52  
    53  func testAccDataSourcePagerDutyEscalationPolicyConfig(rName string) string {
    54  	return fmt.Sprintf(`
    55  resource "pagerduty_user" "test" {
    56    name  = "TF User %[1]s"
    57    email = "tf.%[1]s@example.com"
    58  }
    59  
    60  resource "pagerduty_escalation_policy" "test" {
    61    name        = "TF Escalation Policy %[1]v"
    62    num_loops   = 2
    63  
    64    rule {
    65      escalation_delay_in_minutes = 10
    66  
    67      target {
    68        type = "user_reference"
    69        id   = "${pagerduty_user.test.id}"
    70      }
    71    }
    72  }
    73  
    74  data "pagerduty_escalation_policy" "by_name" {
    75    name = "${pagerduty_escalation_policy.test.name}"
    76  }
    77  `, rName)
    78  }