github.com/nevins-b/terraform@v0.3.8-0.20170215184714-bbae22007d5a/builtin/providers/datadog/resource_datadog_monitor_test.go (about) 1 package datadog 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 "testing" 8 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 "github.com/zorkian/go-datadog-api" 12 ) 13 14 func TestAccDatadogMonitor_Basic(t *testing.T) { 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testAccCheckDatadogMonitorDestroy, 19 Steps: []resource.TestStep{ 20 { 21 Config: testAccCheckDatadogMonitorConfig, 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckDatadogMonitorExists("datadog_monitor.foo"), 24 resource.TestCheckResourceAttr( 25 "datadog_monitor.foo", "name", "name for monitor foo"), 26 resource.TestCheckResourceAttr( 27 "datadog_monitor.foo", "message", "some message Notify: @hipchat-channel"), 28 resource.TestCheckResourceAttr( 29 "datadog_monitor.foo", "type", "metric alert"), 30 resource.TestCheckResourceAttr( 31 "datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2"), 32 resource.TestCheckResourceAttr( 33 "datadog_monitor.foo", "notify_no_data", "false"), 34 resource.TestCheckResourceAttr( 35 "datadog_monitor.foo", "renotify_interval", "60"), 36 resource.TestCheckResourceAttr( 37 "datadog_monitor.foo", "thresholds.warning", "1.0"), 38 resource.TestCheckResourceAttr( 39 "datadog_monitor.foo", "thresholds.critical", "2.0"), 40 resource.TestCheckResourceAttr( 41 "datadog_monitor.foo", "require_full_window", "true"), 42 resource.TestCheckResourceAttr( 43 "datadog_monitor.foo", "locked", "false"), 44 resource.TestCheckResourceAttr( 45 "datadog_monitor.foo", "tags.0", "foo:bar"), 46 resource.TestCheckResourceAttr( 47 "datadog_monitor.foo", "tags.1", "baz"), 48 ), 49 }, 50 }, 51 }) 52 } 53 54 func TestAccDatadogMonitor_BasicNoTreshold(t *testing.T) { 55 resource.Test(t, resource.TestCase{ 56 PreCheck: func() { testAccPreCheck(t) }, 57 Providers: testAccProviders, 58 CheckDestroy: testAccCheckDatadogMonitorDestroy, 59 Steps: []resource.TestStep{ 60 { 61 Config: testAccCheckDatadogMonitorConfigNoThresholds, 62 Check: resource.ComposeTestCheckFunc( 63 testAccCheckDatadogMonitorExists("datadog_monitor.foo"), 64 resource.TestCheckResourceAttr( 65 "datadog_monitor.foo", "name", "name for monitor foo"), 66 resource.TestCheckResourceAttr( 67 "datadog_monitor.foo", "message", "some message Notify: @hipchat-channel"), 68 resource.TestCheckResourceAttr( 69 "datadog_monitor.foo", "type", "metric alert"), 70 resource.TestCheckResourceAttr( 71 "datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2"), 72 resource.TestCheckResourceAttr( 73 "datadog_monitor.foo", "notify_no_data", "false"), 74 resource.TestCheckResourceAttr( 75 "datadog_monitor.foo", "renotify_interval", "60"), 76 resource.TestCheckResourceAttr( 77 "datadog_monitor.foo", "require_full_window", "true"), 78 resource.TestCheckResourceAttr( 79 "datadog_monitor.foo", "locked", "false"), 80 resource.TestCheckResourceAttr( 81 "datadog_monitor.foo", "tags.0", "foo:bar"), 82 resource.TestCheckResourceAttr( 83 "datadog_monitor.foo", "tags.1", "bar:baz"), 84 ), 85 }, 86 }, 87 }) 88 } 89 90 func TestAccDatadogMonitor_Updated(t *testing.T) { 91 resource.Test(t, resource.TestCase{ 92 PreCheck: func() { testAccPreCheck(t) }, 93 Providers: testAccProviders, 94 CheckDestroy: testAccCheckDatadogMonitorDestroy, 95 Steps: []resource.TestStep{ 96 { 97 Config: testAccCheckDatadogMonitorConfig, 98 Check: resource.ComposeTestCheckFunc( 99 testAccCheckDatadogMonitorExists("datadog_monitor.foo"), 100 resource.TestCheckResourceAttr( 101 "datadog_monitor.foo", "name", "name for monitor foo"), 102 resource.TestCheckResourceAttr( 103 "datadog_monitor.foo", "message", "some message Notify: @hipchat-channel"), 104 resource.TestCheckResourceAttr( 105 "datadog_monitor.foo", "escalation_message", "the situation has escalated @pagerduty"), 106 resource.TestCheckResourceAttr( 107 "datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2"), 108 resource.TestCheckResourceAttr( 109 "datadog_monitor.foo", "type", "metric alert"), 110 resource.TestCheckResourceAttr( 111 "datadog_monitor.foo", "notify_no_data", "false"), 112 resource.TestCheckResourceAttr( 113 "datadog_monitor.foo", "renotify_interval", "60"), 114 resource.TestCheckResourceAttr( 115 "datadog_monitor.foo", "thresholds.warning", "1.0"), 116 resource.TestCheckResourceAttr( 117 "datadog_monitor.foo", "thresholds.critical", "2.0"), 118 resource.TestCheckResourceAttr( 119 "datadog_monitor.foo", "notify_audit", "false"), 120 resource.TestCheckResourceAttr( 121 "datadog_monitor.foo", "timeout_h", "60"), 122 resource.TestCheckResourceAttr( 123 "datadog_monitor.foo", "include_tags", "true"), 124 resource.TestCheckResourceAttr( 125 "datadog_monitor.foo", "require_full_window", "true"), 126 resource.TestCheckResourceAttr( 127 "datadog_monitor.foo", "locked", "false"), 128 resource.TestCheckResourceAttr( 129 "datadog_monitor.foo", "tags.0", "foo:bar"), 130 resource.TestCheckResourceAttr( 131 "datadog_monitor.foo", "tags.1", "baz"), 132 ), 133 }, 134 { 135 Config: testAccCheckDatadogMonitorConfigUpdated, 136 Check: resource.ComposeTestCheckFunc( 137 testAccCheckDatadogMonitorExists("datadog_monitor.foo"), 138 resource.TestCheckResourceAttr( 139 "datadog_monitor.foo", "name", "name for monitor bar"), 140 resource.TestCheckResourceAttr( 141 "datadog_monitor.foo", "message", "a different message Notify: @hipchat-channel"), 142 resource.TestCheckResourceAttr( 143 "datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:bar,host:bar} by {host} > 3"), 144 resource.TestCheckResourceAttr( 145 "datadog_monitor.foo", "escalation_message", "the situation has escalated! @pagerduty"), 146 resource.TestCheckResourceAttr( 147 "datadog_monitor.foo", "type", "metric alert"), 148 resource.TestCheckResourceAttr( 149 "datadog_monitor.foo", "notify_no_data", "true"), 150 resource.TestCheckResourceAttr( 151 "datadog_monitor.foo", "no_data_timeframe", "20"), 152 resource.TestCheckResourceAttr( 153 "datadog_monitor.foo", "renotify_interval", "40"), 154 resource.TestCheckResourceAttr( 155 "datadog_monitor.foo", "thresholds.ok", "0.0"), 156 resource.TestCheckResourceAttr( 157 "datadog_monitor.foo", "thresholds.warning", "1.0"), 158 resource.TestCheckResourceAttr( 159 "datadog_monitor.foo", "thresholds.critical", "3.0"), 160 resource.TestCheckResourceAttr( 161 "datadog_monitor.foo", "notify_audit", "true"), 162 resource.TestCheckResourceAttr( 163 "datadog_monitor.foo", "timeout_h", "70"), 164 resource.TestCheckResourceAttr( 165 "datadog_monitor.foo", "include_tags", "false"), 166 resource.TestCheckResourceAttr( 167 "datadog_monitor.foo", "silenced.*", "0"), 168 resource.TestCheckResourceAttr( 169 "datadog_monitor.foo", "require_full_window", "false"), 170 resource.TestCheckResourceAttr( 171 "datadog_monitor.foo", "locked", "true"), 172 resource.TestCheckResourceAttr( 173 "datadog_monitor.foo", "tags.0", "baz:qux"), 174 resource.TestCheckResourceAttr( 175 "datadog_monitor.foo", "tags.1", "quux"), 176 ), 177 }, 178 }, 179 }) 180 } 181 182 func TestAccDatadogMonitor_TrimWhitespace(t *testing.T) { 183 resource.Test(t, resource.TestCase{ 184 PreCheck: func() { testAccPreCheck(t) }, 185 Providers: testAccProviders, 186 CheckDestroy: testAccCheckDatadogMonitorDestroy, 187 Steps: []resource.TestStep{ 188 { 189 Config: testAccCheckDatadogMonitorConfigWhitespace, 190 Check: resource.ComposeTestCheckFunc( 191 testAccCheckDatadogMonitorExists("datadog_monitor.foo"), 192 resource.TestCheckResourceAttr( 193 "datadog_monitor.foo", "name", "name for monitor foo"), 194 resource.TestCheckResourceAttr( 195 "datadog_monitor.foo", "message", "some message Notify: @hipchat-channel"), 196 resource.TestCheckResourceAttr( 197 "datadog_monitor.foo", "type", "metric alert"), 198 resource.TestCheckResourceAttr( 199 "datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2"), 200 resource.TestCheckResourceAttr( 201 "datadog_monitor.foo", "notify_no_data", "false"), 202 resource.TestCheckResourceAttr( 203 "datadog_monitor.foo", "renotify_interval", "60"), 204 resource.TestCheckResourceAttr( 205 "datadog_monitor.foo", "thresholds.ok", "0.0"), 206 resource.TestCheckResourceAttr( 207 "datadog_monitor.foo", "thresholds.warning", "1.0"), 208 resource.TestCheckResourceAttr( 209 "datadog_monitor.foo", "thresholds.critical", "2.0"), 210 ), 211 }, 212 }, 213 }) 214 } 215 216 func TestAccDatadogMonitor_Basic_float_int(t *testing.T) { 217 resource.Test(t, resource.TestCase{ 218 PreCheck: func() { testAccPreCheck(t) }, 219 Providers: testAccProviders, 220 CheckDestroy: testAccCheckDatadogMonitorDestroy, 221 Steps: []resource.TestStep{ 222 { 223 Config: testAccCheckDatadogMonitorConfig_ints, 224 Check: resource.ComposeTestCheckFunc( 225 testAccCheckDatadogMonitorExists("datadog_monitor.foo"), 226 resource.TestCheckResourceAttr( 227 "datadog_monitor.foo", "thresholds.warning", "1"), 228 resource.TestCheckResourceAttr( 229 "datadog_monitor.foo", "thresholds.critical", "2"), 230 ), 231 }, 232 233 { 234 Config: testAccCheckDatadogMonitorConfig_ints_mixed, 235 Check: resource.ComposeTestCheckFunc( 236 testAccCheckDatadogMonitorExists("datadog_monitor.foo"), 237 resource.TestCheckResourceAttr( 238 "datadog_monitor.foo", "thresholds.warning", "1.0"), 239 resource.TestCheckResourceAttr( 240 "datadog_monitor.foo", "thresholds.critical", "3.0"), 241 ), 242 }, 243 }, 244 }) 245 } 246 247 func testAccCheckDatadogMonitorDestroy(s *terraform.State) error { 248 client := testAccProvider.Meta().(*datadog.Client) 249 250 if err := destroyHelper(s, client); err != nil { 251 return err 252 } 253 return nil 254 } 255 256 func testAccCheckDatadogMonitorExists(n string) resource.TestCheckFunc { 257 return func(s *terraform.State) error { 258 client := testAccProvider.Meta().(*datadog.Client) 259 if err := existsHelper(s, client); err != nil { 260 return err 261 } 262 return nil 263 } 264 } 265 266 const testAccCheckDatadogMonitorConfig = ` 267 resource "datadog_monitor" "foo" { 268 name = "name for monitor foo" 269 type = "metric alert" 270 message = "some message Notify: @hipchat-channel" 271 escalation_message = "the situation has escalated @pagerduty" 272 273 query = "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2" 274 275 thresholds { 276 warning = "1.0" 277 critical = "2.0" 278 } 279 280 renotify_interval = 60 281 282 notify_audit = false 283 timeout_h = 60 284 include_tags = true 285 require_full_window = true 286 locked = false 287 tags = ["foo:bar", "baz"] 288 } 289 ` 290 const testAccCheckDatadogMonitorConfigNoThresholds = ` 291 resource "datadog_monitor" "foo" { 292 name = "name for monitor foo" 293 type = "metric alert" 294 message = "some message Notify: @hipchat-channel" 295 escalation_message = "the situation has escalated @pagerduty" 296 297 query = "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2" 298 299 notify_no_data = false 300 renotify_interval = 60 301 302 notify_audit = false 303 timeout_h = 60 304 include_tags = true 305 require_full_window = true 306 locked = false 307 tags = ["foo:bar", "bar:baz"] 308 } 309 ` 310 311 const testAccCheckDatadogMonitorConfig_ints = ` 312 resource "datadog_monitor" "foo" { 313 name = "name for monitor foo" 314 type = "metric alert" 315 message = "some message Notify: @hipchat-channel" 316 escalation_message = "the situation has escalated @pagerduty" 317 318 query = "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2" 319 320 thresholds { 321 warning = 1 322 critical = 2 323 } 324 325 notify_no_data = false 326 renotify_interval = 60 327 328 notify_audit = false 329 timeout_h = 60 330 include_tags = true 331 require_full_window = true 332 locked = false 333 334 tags = ["foo:bar", "baz"] 335 } 336 ` 337 338 const testAccCheckDatadogMonitorConfig_ints_mixed = ` 339 resource "datadog_monitor" "foo" { 340 name = "name for monitor foo" 341 type = "metric alert" 342 message = "some message Notify: @hipchat-channel" 343 escalation_message = "the situation has escalated @pagerduty" 344 345 query = "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 3" 346 347 thresholds { 348 warning = 1 349 critical = 3.0 350 } 351 352 notify_no_data = false 353 renotify_interval = 60 354 355 notify_audit = false 356 timeout_h = 60 357 include_tags = true 358 require_full_window = true 359 locked = false 360 361 tags = ["foo:bar", "baz"] 362 } 363 ` 364 365 const testAccCheckDatadogMonitorConfigUpdated = ` 366 resource "datadog_monitor" "foo" { 367 name = "name for monitor bar" 368 type = "metric alert" 369 message = "a different message Notify: @hipchat-channel" 370 escalation_message = "the situation has escalated @pagerduty" 371 372 query = "avg(last_1h):avg:aws.ec2.cpu{environment:bar,host:bar} by {host} > 3" 373 374 thresholds { 375 ok = "0.0" 376 warning = "1.0" 377 critical = "3.0" 378 } 379 380 notify_no_data = true 381 no_data_timeframe = 20 382 renotify_interval = 40 383 escalation_message = "the situation has escalated! @pagerduty" 384 notify_audit = true 385 timeout_h = 70 386 include_tags = false 387 require_full_window = false 388 locked = true 389 silenced { 390 "*" = 0 391 } 392 tags = ["baz:qux", "quux"] 393 } 394 ` 395 396 const testAccCheckDatadogMonitorConfigWhitespace = ` 397 resource "datadog_monitor" "foo" { 398 name = "name for monitor foo" 399 type = "metric alert" 400 message = <<EOF 401 some message Notify: @hipchat-channel 402 EOF 403 escalation_message = <<EOF 404 the situation has escalated @pagerduty 405 EOF 406 query = <<EOF 407 avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2 408 EOF 409 thresholds { 410 ok = "0.0" 411 warning = "1.0" 412 critical = "2.0" 413 } 414 415 notify_no_data = false 416 renotify_interval = 60 417 418 notify_audit = false 419 timeout_h = 60 420 include_tags = true 421 } 422 ` 423 424 func destroyHelper(s *terraform.State, client *datadog.Client) error { 425 for _, r := range s.RootModule().Resources { 426 i, _ := strconv.Atoi(r.Primary.ID) 427 if _, err := client.GetMonitor(i); err != nil { 428 if strings.Contains(err.Error(), "404 Not Found") { 429 continue 430 } 431 return fmt.Errorf("Received an error retrieving monitor %s", err) 432 } 433 return fmt.Errorf("Monitor still exists") 434 } 435 return nil 436 } 437 438 func existsHelper(s *terraform.State, client *datadog.Client) error { 439 for _, r := range s.RootModule().Resources { 440 i, _ := strconv.Atoi(r.Primary.ID) 441 if _, err := client.GetMonitor(i); err != nil { 442 return fmt.Errorf("Received an error retrieving monitor %s", err) 443 } 444 } 445 return nil 446 }