github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/pagerduty/resource_pagerduty_schedule_test.go (about) 1 package pagerduty 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/PagerDuty/go-pagerduty" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestAccPagerDutySchedule_Basic(t *testing.T) { 13 resource.Test(t, resource.TestCase{ 14 PreCheck: func() { testAccPreCheck(t) }, 15 Providers: testAccProviders, 16 CheckDestroy: testAccCheckPagerDutyScheduleDestroy, 17 Steps: []resource.TestStep{ 18 resource.TestStep{ 19 Config: testAccCheckPagerDutyScheduleConfig, 20 Check: resource.ComposeTestCheckFunc( 21 testAccCheckPagerDutyScheduleExists("pagerduty_schedule.foo"), 22 resource.TestCheckResourceAttr( 23 "pagerduty_schedule.foo", "name", "foo"), 24 resource.TestCheckResourceAttr( 25 "pagerduty_schedule.foo", "description", "foo"), 26 resource.TestCheckResourceAttr( 27 "pagerduty_schedule.foo", "time_zone", "Europe/Berlin"), 28 resource.TestCheckResourceAttr( 29 "pagerduty_schedule.foo", "layer.#", "1"), 30 resource.TestCheckResourceAttr( 31 "pagerduty_schedule.foo", "layer.0.name", "foo"), 32 ), 33 }, 34 resource.TestStep{ 35 Config: testAccCheckPagerDutyScheduleConfigUpdated, 36 Check: resource.ComposeTestCheckFunc( 37 testAccCheckPagerDutyScheduleExists("pagerduty_schedule.foo"), 38 resource.TestCheckResourceAttr( 39 "pagerduty_schedule.foo", "name", "bar"), 40 resource.TestCheckResourceAttr( 41 "pagerduty_schedule.foo", "description", "Managed by Terraform"), 42 resource.TestCheckResourceAttr( 43 "pagerduty_schedule.foo", "time_zone", "America/New_York"), 44 resource.TestCheckResourceAttr( 45 "pagerduty_schedule.foo", "layer.#", "1"), 46 resource.TestCheckResourceAttr( 47 "pagerduty_schedule.foo", "layer.0.name", "foo"), 48 ), 49 }, 50 }, 51 }) 52 } 53 54 func TestAccPagerDutySchedule_Multi(t *testing.T) { 55 resource.Test(t, resource.TestCase{ 56 PreCheck: func() { testAccPreCheck(t) }, 57 Providers: testAccProviders, 58 CheckDestroy: testAccCheckPagerDutyScheduleDestroy, 59 Steps: []resource.TestStep{ 60 resource.TestStep{ 61 Config: testAccCheckPagerDutyScheduleConfigMulti, 62 Check: resource.ComposeTestCheckFunc( 63 testAccCheckPagerDutyScheduleExists("pagerduty_schedule.foo"), 64 resource.TestCheckResourceAttr( 65 "pagerduty_schedule.foo", "name", "foo"), 66 resource.TestCheckResourceAttr( 67 "pagerduty_schedule.foo", "description", "foo"), 68 resource.TestCheckResourceAttr( 69 "pagerduty_schedule.foo", "time_zone", "America/New_York"), 70 resource.TestCheckResourceAttr( 71 "pagerduty_schedule.foo", "layer.#", "3"), 72 resource.TestCheckResourceAttr( 73 "pagerduty_schedule.foo", "layer.0.name", "foo"), 74 resource.TestCheckResourceAttr( 75 "pagerduty_schedule.foo", "layer.1.name", "bar"), 76 resource.TestCheckResourceAttr( 77 "pagerduty_schedule.foo", "layer.2.name", "foobar"), 78 ), 79 }, 80 }, 81 }) 82 } 83 84 func testAccCheckPagerDutyScheduleDestroy(s *terraform.State) error { 85 client := testAccProvider.Meta().(*pagerduty.Client) 86 for _, r := range s.RootModule().Resources { 87 if r.Type != "pagerduty_schedule" { 88 continue 89 } 90 91 _, err := client.GetSchedule(r.Primary.ID, pagerduty.GetScheduleOptions{}) 92 93 if err == nil { 94 return fmt.Errorf("Schedule still exists") 95 } 96 97 } 98 return nil 99 } 100 101 func testAccCheckPagerDutyScheduleExists(n string) resource.TestCheckFunc { 102 return func(s *terraform.State) error { 103 rs, ok := s.RootModule().Resources[n] 104 if !ok { 105 return fmt.Errorf("Not found: %s", n) 106 } 107 if rs.Primary.ID == "" { 108 return fmt.Errorf("No Schedule ID is set") 109 } 110 111 client := testAccProvider.Meta().(*pagerduty.Client) 112 113 found, err := client.GetSchedule(rs.Primary.ID, pagerduty.GetScheduleOptions{}) 114 if err != nil { 115 return err 116 } 117 118 if found.ID != rs.Primary.ID { 119 return fmt.Errorf("Schedule not found: %v - %v", rs.Primary.ID, found) 120 } 121 122 return nil 123 } 124 } 125 126 const testAccCheckPagerDutyScheduleConfig = ` 127 resource "pagerduty_user" "foo" { 128 name = "foo" 129 email = "foo@bar.com" 130 } 131 132 resource "pagerduty_schedule" "foo" { 133 name = "foo" 134 135 time_zone = "Europe/Berlin" 136 description = "foo" 137 138 layer { 139 name = "foo" 140 start = "2015-11-06T20:00:00-05:00" 141 rotation_virtual_start = "2015-11-06T20:00:00-05:00" 142 rotation_turn_length_seconds = 86400 143 users = ["${pagerduty_user.foo.id}"] 144 145 restriction { 146 type = "daily_restriction" 147 start_time_of_day = "08:00:00" 148 duration_seconds = 32101 149 } 150 } 151 } 152 ` 153 154 const testAccCheckPagerDutyScheduleConfigUpdated = ` 155 resource "pagerduty_user" "foo" { 156 name = "foo" 157 email = "foo@bar.com" 158 } 159 160 resource "pagerduty_schedule" "foo" { 161 name = "bar" 162 163 time_zone = "America/New_York" 164 165 layer { 166 name = "foo" 167 start = "2015-11-06T20:00:00-05:00" 168 rotation_virtual_start = "2015-11-06T20:00:00-05:00" 169 rotation_turn_length_seconds = 86400 170 users = ["${pagerduty_user.foo.id}"] 171 172 restriction { 173 type = "daily_restriction" 174 start_time_of_day = "08:00:00" 175 duration_seconds = 32101 176 } 177 } 178 } 179 ` 180 181 const testAccCheckPagerDutyScheduleConfigMulti = ` 182 resource "pagerduty_user" "foo" { 183 name = "foo" 184 email = "foo@bar.com" 185 } 186 187 resource "pagerduty_schedule" "foo" { 188 name = "foo" 189 190 time_zone = "America/New_York" 191 description = "foo" 192 193 layer { 194 name = "foo" 195 start = "2015-11-06T20:00:00-05:00" 196 rotation_virtual_start = "2015-11-06T20:00:00-05:00" 197 rotation_turn_length_seconds = 86400 198 users = ["${pagerduty_user.foo.id}"] 199 200 restriction { 201 type = "daily_restriction" 202 start_time_of_day = "08:00:00" 203 duration_seconds = 32101 204 } 205 } 206 207 layer { 208 name = "bar" 209 start = "2015-11-06T20:00:00-05:00" 210 rotation_virtual_start = "2015-11-06T20:00:00-05:00" 211 rotation_turn_length_seconds = 86400 212 users = ["${pagerduty_user.foo.id}"] 213 214 restriction { 215 type = "daily_restriction" 216 start_time_of_day = "08:00:00" 217 duration_seconds = 32101 218 } 219 } 220 221 layer { 222 name = "foobar" 223 start = "2015-11-06T20:00:00-05:00" 224 rotation_virtual_start = "2015-11-06T20:00:00-05:00" 225 rotation_turn_length_seconds = 86400 226 users = ["${pagerduty_user.foo.id}"] 227 228 restriction { 229 type = "daily_restriction" 230 start_time_of_day = "08:00:00" 231 duration_seconds = 32101 232 } 233 } 234 } 235 `