github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/pagerduty/data_source_pagerduty_user_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 TestAccDataSourcePagerDutyUser_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: testAccDataSourcePagerDutyUserConfig(rName), 20 Check: resource.ComposeTestCheckFunc( 21 testAccDataSourcePagerDutyUser("pagerduty_user.test", "data.pagerduty_user.by_email"), 22 ), 23 }, 24 }, 25 }) 26 } 27 28 func testAccDataSourcePagerDutyUser(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 user ID from PagerDuty") 39 } 40 41 testAtts := []string{"id", "name", "email"} 42 43 for _, att := range testAtts { 44 if a[att] != srcA[att] { 45 return fmt.Errorf("Expected the user %s to be: %s, but got: %s", att, srcA[att], a[att]) 46 } 47 } 48 49 return nil 50 } 51 } 52 53 func testAccDataSourcePagerDutyUserConfig(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 data "pagerduty_user" "by_email" { 61 email = "${pagerduty_user.test.email}" 62 } 63 `, rName) 64 }