github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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_BasicWeek(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: testAccCheckPagerDutyScheduleConfigWeek, 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", "Europe/Berlin"), 70 resource.TestCheckResourceAttr( 71 "pagerduty_schedule.foo", "layer.#", "1"), 72 resource.TestCheckResourceAttr( 73 "pagerduty_schedule.foo", "layer.0.name", "foo"), 74 resource.TestCheckResourceAttr( 75 "pagerduty_schedule.foo", "layer.0.restriction.0.start_day_of_week", "1"), 76 ), 77 }, 78 resource.TestStep{ 79 Config: testAccCheckPagerDutyScheduleConfigWeekUpdated, 80 Check: resource.ComposeTestCheckFunc( 81 testAccCheckPagerDutyScheduleExists("pagerduty_schedule.foo"), 82 resource.TestCheckResourceAttr( 83 "pagerduty_schedule.foo", "name", "bar"), 84 resource.TestCheckResourceAttr( 85 "pagerduty_schedule.foo", "description", "Managed by Terraform"), 86 resource.TestCheckResourceAttr( 87 "pagerduty_schedule.foo", "time_zone", "America/New_York"), 88 resource.TestCheckResourceAttr( 89 "pagerduty_schedule.foo", "layer.#", "1"), 90 resource.TestCheckResourceAttr( 91 "pagerduty_schedule.foo", "layer.0.name", "foo"), 92 resource.TestCheckResourceAttr( 93 "pagerduty_schedule.foo", "layer.0.restriction.0.start_day_of_week", "5"), 94 ), 95 }, 96 }, 97 }) 98 } 99 100 func TestAccPagerDutySchedule_Multi(t *testing.T) { 101 resource.Test(t, resource.TestCase{ 102 PreCheck: func() { testAccPreCheck(t) }, 103 Providers: testAccProviders, 104 CheckDestroy: testAccCheckPagerDutyScheduleDestroy, 105 Steps: []resource.TestStep{ 106 resource.TestStep{ 107 Config: testAccCheckPagerDutyScheduleConfigMulti, 108 Check: resource.ComposeTestCheckFunc( 109 testAccCheckPagerDutyScheduleExists("pagerduty_schedule.foo"), 110 resource.TestCheckResourceAttr( 111 "pagerduty_schedule.foo", "name", "foo"), 112 resource.TestCheckResourceAttr( 113 "pagerduty_schedule.foo", "description", "foo"), 114 resource.TestCheckResourceAttr( 115 "pagerduty_schedule.foo", "time_zone", "America/New_York"), 116 117 resource.TestCheckResourceAttr( 118 "pagerduty_schedule.foo", "layer.#", "3"), 119 resource.TestCheckResourceAttr( 120 "pagerduty_schedule.foo", "layer.0.name", "foo"), 121 resource.TestCheckResourceAttr( 122 "pagerduty_schedule.foo", "layer.0.restriction.#", "1"), 123 resource.TestCheckResourceAttr( 124 "pagerduty_schedule.foo", "layer.0.restriction.0.duration_seconds", "32101"), 125 resource.TestCheckResourceAttr( 126 "pagerduty_schedule.foo", "layer.0.restriction.0.start_time_of_day", "08:00:00"), 127 resource.TestCheckResourceAttr( 128 "pagerduty_schedule.foo", "layer.0.rotation_turn_length_seconds", "86400"), 129 resource.TestCheckResourceAttr( 130 "pagerduty_schedule.foo", "layer.0.rotation_virtual_start", "2015-11-06T20:00:00-05:00"), 131 resource.TestCheckResourceAttr( 132 "pagerduty_schedule.foo", "layer.0.users.#", "1"), 133 134 resource.TestCheckResourceAttr( 135 "pagerduty_schedule.foo", "layer.1.name", "bar"), 136 resource.TestCheckResourceAttr( 137 "pagerduty_schedule.foo", "layer.1.restriction.#", "1"), 138 resource.TestCheckResourceAttr( 139 "pagerduty_schedule.foo", "layer.1.restriction.0.duration_seconds", "32101"), 140 resource.TestCheckResourceAttr( 141 "pagerduty_schedule.foo", "layer.1.restriction.0.start_time_of_day", "08:00:00"), 142 resource.TestCheckResourceAttr( 143 "pagerduty_schedule.foo", "layer.1.restriction.0.start_day_of_week", "5"), 144 resource.TestCheckResourceAttr( 145 "pagerduty_schedule.foo", "layer.1.rotation_turn_length_seconds", "86400"), 146 resource.TestCheckResourceAttr( 147 "pagerduty_schedule.foo", "layer.1.rotation_virtual_start", "2015-11-06T20:00:00-05:00"), 148 resource.TestCheckResourceAttr( 149 "pagerduty_schedule.foo", "layer.1.users.#", "1"), 150 151 resource.TestCheckResourceAttr( 152 "pagerduty_schedule.foo", "layer.2.name", "foobar"), 153 resource.TestCheckResourceAttr( 154 "pagerduty_schedule.foo", "layer.2.restriction.#", "1"), 155 resource.TestCheckResourceAttr( 156 "pagerduty_schedule.foo", "layer.2.restriction.0.duration_seconds", "32101"), 157 resource.TestCheckResourceAttr( 158 "pagerduty_schedule.foo", "layer.2.restriction.0.start_time_of_day", "08:00:00"), 159 resource.TestCheckResourceAttr( 160 "pagerduty_schedule.foo", "layer.2.restriction.0.start_day_of_week", "1"), 161 resource.TestCheckResourceAttr( 162 "pagerduty_schedule.foo", "layer.2.rotation_turn_length_seconds", "86400"), 163 resource.TestCheckResourceAttr( 164 "pagerduty_schedule.foo", "layer.2.rotation_virtual_start", "2015-11-06T20:00:00-05:00"), 165 resource.TestCheckResourceAttr( 166 "pagerduty_schedule.foo", "layer.2.users.#", "1"), 167 ), 168 }, 169 }, 170 }) 171 } 172 173 func testAccCheckPagerDutyScheduleDestroy(s *terraform.State) error { 174 client := testAccProvider.Meta().(*pagerduty.Client) 175 for _, r := range s.RootModule().Resources { 176 if r.Type != "pagerduty_schedule" { 177 continue 178 } 179 180 _, err := client.GetSchedule(r.Primary.ID, pagerduty.GetScheduleOptions{}) 181 182 if err == nil { 183 return fmt.Errorf("Schedule still exists") 184 } 185 186 } 187 return nil 188 } 189 190 func testAccCheckPagerDutyScheduleExists(n string) resource.TestCheckFunc { 191 return func(s *terraform.State) error { 192 rs, ok := s.RootModule().Resources[n] 193 if !ok { 194 return fmt.Errorf("Not found: %s", n) 195 } 196 if rs.Primary.ID == "" { 197 return fmt.Errorf("No Schedule ID is set") 198 } 199 200 client := testAccProvider.Meta().(*pagerduty.Client) 201 202 found, err := client.GetSchedule(rs.Primary.ID, pagerduty.GetScheduleOptions{}) 203 if err != nil { 204 return err 205 } 206 207 if found.ID != rs.Primary.ID { 208 return fmt.Errorf("Schedule not found: %v - %v", rs.Primary.ID, found) 209 } 210 211 return nil 212 } 213 } 214 215 const testAccCheckPagerDutyScheduleConfig = ` 216 resource "pagerduty_user" "foo" { 217 name = "foo" 218 email = "foo@bar.com" 219 } 220 221 resource "pagerduty_schedule" "foo" { 222 name = "foo" 223 224 time_zone = "Europe/Berlin" 225 description = "foo" 226 227 layer { 228 name = "foo" 229 start = "2015-11-06T20:00:00-05:00" 230 rotation_virtual_start = "2015-11-06T20:00:00-05:00" 231 rotation_turn_length_seconds = 86400 232 users = ["${pagerduty_user.foo.id}"] 233 234 restriction { 235 type = "daily_restriction" 236 start_time_of_day = "08:00:00" 237 duration_seconds = 32101 238 } 239 } 240 } 241 ` 242 243 const testAccCheckPagerDutyScheduleConfigUpdated = ` 244 resource "pagerduty_user" "foo" { 245 name = "foo" 246 email = "foo@bar.com" 247 } 248 249 resource "pagerduty_schedule" "foo" { 250 name = "bar" 251 252 time_zone = "America/New_York" 253 254 layer { 255 name = "foo" 256 start = "2015-11-06T20:00:00-05:00" 257 rotation_virtual_start = "2015-11-06T20:00:00-05:00" 258 rotation_turn_length_seconds = 86400 259 users = ["${pagerduty_user.foo.id}"] 260 261 restriction { 262 type = "daily_restriction" 263 start_time_of_day = "08:00:00" 264 duration_seconds = 32101 265 } 266 } 267 } 268 ` 269 270 const testAccCheckPagerDutyScheduleConfigWeek = ` 271 resource "pagerduty_user" "foo" { 272 name = "foo" 273 email = "foo@bar.com" 274 } 275 276 resource "pagerduty_schedule" "foo" { 277 name = "foo" 278 279 time_zone = "Europe/Berlin" 280 description = "foo" 281 282 layer { 283 name = "foo" 284 start = "2015-11-06T20:00:00-05:00" 285 rotation_virtual_start = "2015-11-06T20:00:00-05:00" 286 rotation_turn_length_seconds = 86400 287 users = ["${pagerduty_user.foo.id}"] 288 289 restriction { 290 type = "weekly_restriction" 291 start_time_of_day = "08:00:00" 292 start_day_of_week = 1 293 duration_seconds = 32101 294 } 295 } 296 } 297 ` 298 299 const testAccCheckPagerDutyScheduleConfigWeekUpdated = ` 300 resource "pagerduty_user" "foo" { 301 name = "foo" 302 email = "foo@bar.com" 303 } 304 305 resource "pagerduty_schedule" "foo" { 306 name = "bar" 307 308 time_zone = "America/New_York" 309 310 layer { 311 name = "foo" 312 start = "2015-11-06T20:00:00-05:00" 313 rotation_virtual_start = "2015-11-06T20:00:00-05:00" 314 rotation_turn_length_seconds = 86400 315 users = ["${pagerduty_user.foo.id}"] 316 317 restriction { 318 type = "weekly_restriction" 319 start_time_of_day = "08:00:00" 320 start_day_of_week = 5 321 duration_seconds = 32101 322 } 323 } 324 } 325 ` 326 327 const testAccCheckPagerDutyScheduleConfigMulti = ` 328 resource "pagerduty_user" "foo" { 329 name = "foo" 330 email = "foo@bar.com" 331 } 332 333 resource "pagerduty_schedule" "foo" { 334 name = "foo" 335 336 time_zone = "America/New_York" 337 description = "foo" 338 339 layer { 340 name = "foo" 341 start = "2015-11-06T20:00:00-05:00" 342 rotation_virtual_start = "2015-11-06T20:00:00-05:00" 343 rotation_turn_length_seconds = 86400 344 users = ["${pagerduty_user.foo.id}"] 345 346 restriction { 347 type = "daily_restriction" 348 start_time_of_day = "08:00:00" 349 duration_seconds = 32101 350 } 351 } 352 353 layer { 354 name = "bar" 355 start = "2015-11-06T20:00:00-05:00" 356 rotation_virtual_start = "2015-11-06T20:00:00-05:00" 357 rotation_turn_length_seconds = 86400 358 users = ["${pagerduty_user.foo.id}"] 359 360 restriction { 361 type = "weekly_restriction" 362 start_time_of_day = "08:00:00" 363 start_day_of_week = 5 364 duration_seconds = 32101 365 } 366 } 367 368 layer { 369 name = "foobar" 370 start = "2015-11-06T20:00:00-05:00" 371 rotation_virtual_start = "2015-11-06T20:00:00-05:00" 372 rotation_turn_length_seconds = 86400 373 users = ["${pagerduty_user.foo.id}"] 374 375 restriction { 376 type = "weekly_restriction" 377 start_time_of_day = "08:00:00" 378 start_day_of_week = 1 379 duration_seconds = 32101 380 } 381 } 382 } 383 `