github.com/IBM-Cloud/terraform@v0.6.4-0.20170726051544-8872b87621df/builtin/providers/pagerduty/resource_pagerduty_escalation_policy_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/acctest" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccPagerDutyEscalationPolicy_Basic(t *testing.T) { 14 username := fmt.Sprintf("tf-%s", acctest.RandString(5)) 15 email := fmt.Sprintf("%s@foo.com", username) 16 escalationPolicy := fmt.Sprintf("tf-%s", acctest.RandString(5)) 17 escalationPolicyUpdated := fmt.Sprintf("tf-%s", acctest.RandString(5)) 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testAccCheckPagerDutyEscalationPolicyDestroy, 23 Steps: []resource.TestStep{ 24 { 25 Config: testAccCheckPagerDutyEscalationPolicyConfig(username, email, escalationPolicy), 26 Check: resource.ComposeTestCheckFunc( 27 testAccCheckPagerDutyEscalationPolicyExists("pagerduty_escalation_policy.foo"), 28 resource.TestCheckResourceAttr( 29 "pagerduty_escalation_policy.foo", "name", escalationPolicy), 30 resource.TestCheckResourceAttr( 31 "pagerduty_escalation_policy.foo", "description", "foo"), 32 resource.TestCheckResourceAttr( 33 "pagerduty_escalation_policy.foo", "num_loops", "1"), 34 resource.TestCheckResourceAttr( 35 "pagerduty_escalation_policy.foo", "rule.#", "1"), 36 resource.TestCheckResourceAttr( 37 "pagerduty_escalation_policy.foo", "rule.0.escalation_delay_in_minutes", "10"), 38 ), 39 }, 40 41 { 42 Config: testAccCheckPagerDutyEscalationPolicyConfigUpdated(username, email, escalationPolicyUpdated), 43 Check: resource.ComposeTestCheckFunc( 44 testAccCheckPagerDutyEscalationPolicyExists("pagerduty_escalation_policy.foo"), 45 resource.TestCheckResourceAttr( 46 "pagerduty_escalation_policy.foo", "name", escalationPolicyUpdated), 47 resource.TestCheckResourceAttr( 48 "pagerduty_escalation_policy.foo", "description", "bar"), 49 resource.TestCheckResourceAttr( 50 "pagerduty_escalation_policy.foo", "num_loops", "2"), 51 resource.TestCheckResourceAttr( 52 "pagerduty_escalation_policy.foo", "rule.#", "2"), 53 resource.TestCheckResourceAttr( 54 "pagerduty_escalation_policy.foo", "rule.0.escalation_delay_in_minutes", "10"), 55 resource.TestCheckResourceAttr( 56 "pagerduty_escalation_policy.foo", "rule.1.escalation_delay_in_minutes", "20"), 57 ), 58 }, 59 }, 60 }) 61 } 62 63 func TestAccPagerDutyEscalationPolicyWithTeams_Basic(t *testing.T) { 64 username := fmt.Sprintf("tf-%s", acctest.RandString(5)) 65 email := fmt.Sprintf("%s@foo.com", username) 66 team := fmt.Sprintf("tf-%s", acctest.RandString(5)) 67 escalationPolicy := fmt.Sprintf("tf-%s", acctest.RandString(5)) 68 escalationPolicyUpdated := fmt.Sprintf("tf-%s", acctest.RandString(5)) 69 70 resource.Test(t, resource.TestCase{ 71 PreCheck: func() { testAccPreCheck(t) }, 72 Providers: testAccProviders, 73 CheckDestroy: testAccCheckPagerDutyEscalationPolicyDestroy, 74 Steps: []resource.TestStep{ 75 { 76 Config: testAccCheckPagerDutyEscalationPolicyWithTeamsConfig(username, email, team, escalationPolicy), 77 Check: resource.ComposeTestCheckFunc( 78 testAccCheckPagerDutyEscalationPolicyExists("pagerduty_escalation_policy.foo"), 79 resource.TestCheckResourceAttr( 80 "pagerduty_escalation_policy.foo", "name", escalationPolicy), 81 resource.TestCheckResourceAttr( 82 "pagerduty_escalation_policy.foo", "description", "foo"), 83 resource.TestCheckResourceAttr( 84 "pagerduty_escalation_policy.foo", "num_loops", "1"), 85 resource.TestCheckResourceAttr( 86 "pagerduty_escalation_policy.foo", "rule.#", "1"), 87 resource.TestCheckResourceAttr( 88 "pagerduty_escalation_policy.foo", "rule.0.escalation_delay_in_minutes", "10"), 89 resource.TestCheckResourceAttr( 90 "pagerduty_escalation_policy.foo", "teams.#", "1"), 91 ), 92 }, 93 { 94 Config: testAccCheckPagerDutyEscalationPolicyWithTeamsConfigUpdated(username, email, team, escalationPolicyUpdated), 95 Check: resource.ComposeTestCheckFunc( 96 testAccCheckPagerDutyEscalationPolicyExists("pagerduty_escalation_policy.foo"), 97 resource.TestCheckResourceAttr( 98 "pagerduty_escalation_policy.foo", "name", escalationPolicyUpdated), 99 resource.TestCheckResourceAttr( 100 "pagerduty_escalation_policy.foo", "description", "bar"), 101 resource.TestCheckResourceAttr( 102 "pagerduty_escalation_policy.foo", "num_loops", "2"), 103 resource.TestCheckResourceAttr( 104 "pagerduty_escalation_policy.foo", "rule.#", "2"), 105 resource.TestCheckResourceAttr( 106 "pagerduty_escalation_policy.foo", "rule.0.escalation_delay_in_minutes", "10"), 107 resource.TestCheckResourceAttr( 108 "pagerduty_escalation_policy.foo", "rule.1.escalation_delay_in_minutes", "20"), 109 resource.TestCheckResourceAttr( 110 "pagerduty_escalation_policy.foo", "teams.#", "0"), 111 ), 112 }, 113 }, 114 }) 115 } 116 117 func testAccCheckPagerDutyEscalationPolicyDestroy(s *terraform.State) error { 118 client := testAccProvider.Meta().(*pagerduty.Client) 119 for _, r := range s.RootModule().Resources { 120 if r.Type != "pagerduty_escalation_policy" { 121 continue 122 } 123 124 _, err := client.GetEscalationPolicy(r.Primary.ID, &pagerduty.GetEscalationPolicyOptions{}) 125 126 if err == nil { 127 return fmt.Errorf("Escalation Policy still exists") 128 } 129 130 } 131 return nil 132 } 133 134 func testAccCheckPagerDutyEscalationPolicyExists(n string) resource.TestCheckFunc { 135 return func(s *terraform.State) error { 136 rs, ok := s.RootModule().Resources[n] 137 if !ok { 138 return fmt.Errorf("Not found: %s", n) 139 } 140 if rs.Primary.ID == "" { 141 return fmt.Errorf("No Escalation Policy ID is set") 142 } 143 144 client := testAccProvider.Meta().(*pagerduty.Client) 145 146 found, err := client.GetEscalationPolicy(rs.Primary.ID, &pagerduty.GetEscalationPolicyOptions{}) 147 if err != nil { 148 return err 149 } 150 151 if found.ID != rs.Primary.ID { 152 return fmt.Errorf("Escalation policy not found: %v - %v", rs.Primary.ID, found) 153 } 154 155 return nil 156 } 157 } 158 159 func testAccCheckPagerDutyEscalationPolicyConfig(name, email, escalationPolicy string) string { 160 return fmt.Sprintf(` 161 resource "pagerduty_user" "foo" { 162 name = "%s" 163 email = "%s" 164 color = "green" 165 role = "user" 166 job_title = "foo" 167 description = "foo" 168 } 169 170 resource "pagerduty_escalation_policy" "foo" { 171 name = "%s" 172 description = "foo" 173 num_loops = 1 174 175 rule { 176 escalation_delay_in_minutes = 10 177 178 target { 179 type = "user_reference" 180 id = "${pagerduty_user.foo.id}" 181 } 182 } 183 } 184 `, name, email, escalationPolicy) 185 } 186 187 func testAccCheckPagerDutyEscalationPolicyConfigUpdated(name, email, escalationPolicy string) string { 188 return fmt.Sprintf(` 189 resource "pagerduty_user" "foo" { 190 name = "%s" 191 email = "%s" 192 color = "green" 193 role = "user" 194 job_title = "foo" 195 description = "foo" 196 } 197 198 resource "pagerduty_escalation_policy" "foo" { 199 name = "%s" 200 description = "bar" 201 num_loops = 2 202 203 rule { 204 escalation_delay_in_minutes = 10 205 206 target { 207 type = "user_reference" 208 id = "${pagerduty_user.foo.id}" 209 } 210 } 211 212 rule { 213 escalation_delay_in_minutes = 20 214 215 target { 216 type = "user_reference" 217 id = "${pagerduty_user.foo.id}" 218 } 219 } 220 } 221 `, name, email, escalationPolicy) 222 } 223 224 func testAccCheckPagerDutyEscalationPolicyWithTeamsConfig(name, email, team, escalationPolicy string) string { 225 return fmt.Sprintf(` 226 resource "pagerduty_user" "foo" { 227 name = "%s" 228 email = "%s" 229 color = "green" 230 role = "user" 231 job_title = "foo" 232 description = "foo" 233 } 234 235 resource "pagerduty_team" "foo" { 236 name = "%s" 237 description = "foo" 238 } 239 240 resource "pagerduty_escalation_policy" "foo" { 241 name = "%s" 242 description = "foo" 243 num_loops = 1 244 teams = ["${pagerduty_team.foo.id}"] 245 246 rule { 247 escalation_delay_in_minutes = 10 248 249 target { 250 type = "user_reference" 251 id = "${pagerduty_user.foo.id}" 252 } 253 } 254 } 255 `, name, email, team, escalationPolicy) 256 } 257 258 func testAccCheckPagerDutyEscalationPolicyWithTeamsConfigUpdated(name, email, team, escalationPolicy string) string { 259 return fmt.Sprintf(` 260 resource "pagerduty_user" "foo" { 261 name = "%s" 262 email = "%s" 263 color = "green" 264 role = "user" 265 job_title = "foo" 266 description = "foo" 267 } 268 269 resource "pagerduty_team" "foo" { 270 name = "%s" 271 description = "foo" 272 } 273 274 resource "pagerduty_escalation_policy" "foo" { 275 name = "%s" 276 description = "bar" 277 num_loops = 2 278 279 rule { 280 escalation_delay_in_minutes = 10 281 282 target { 283 type = "user_reference" 284 id = "${pagerduty_user.foo.id}" 285 } 286 } 287 288 rule { 289 escalation_delay_in_minutes = 20 290 291 target { 292 type = "user_reference" 293 id = "${pagerduty_user.foo.id}" 294 } 295 } 296 } 297 `, name, email, team, escalationPolicy) 298 }