github.com/IBM-Cloud/terraform@v0.6.4-0.20170726051544-8872b87621df/builtin/providers/pagerduty/data_source_pagerduty_schedule_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 TestAccDataSourcePagerDutySchedule_Basic(t *testing.T) { 13 username := fmt.Sprintf("tf-%s", acctest.RandString(5)) 14 email := fmt.Sprintf("%s@foo.com", username) 15 schedule := 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: testAccDataSourcePagerDutyScheduleConfig(username, email, schedule), 23 Check: resource.ComposeTestCheckFunc( 24 testAccDataSourcePagerDutySchedule("pagerduty_schedule.test", "data.pagerduty_schedule.by_name"), 25 ), 26 }, 27 }, 28 }) 29 } 30 31 func testAccDataSourcePagerDutySchedule(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 schedule 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 schedule %s to be: %s, but got: %s", att, srcA[att], a[att]) 49 } 50 } 51 52 return nil 53 } 54 } 55 56 func testAccDataSourcePagerDutyScheduleConfig(username, email, schedule string) string { 57 return fmt.Sprintf(` 58 resource "pagerduty_user" "test" { 59 name = "%s" 60 email = "%s" 61 } 62 63 resource "pagerduty_schedule" "test" { 64 name = "%s" 65 66 time_zone = "America/New_York" 67 68 layer { 69 name = "foo" 70 start = "2015-11-06T20:00:00-05:00" 71 rotation_virtual_start = "2015-11-06T20:00:00-05:00" 72 rotation_turn_length_seconds = 86400 73 users = ["${pagerduty_user.test.id}"] 74 75 restriction { 76 type = "weekly_restriction" 77 start_time_of_day = "08:00:00" 78 start_day_of_week = 5 79 duration_seconds = 32101 80 } 81 } 82 } 83 84 data "pagerduty_schedule" "by_name" { 85 name = "${pagerduty_schedule.test.name}" 86 } 87 `, username, email, schedule) 88 }