github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/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 resource.TestStep{ 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.ok", "0"), 38 resource.TestCheckResourceAttr( 39 "datadog_monitor.foo", "thresholds.warning", "1"), 40 resource.TestCheckResourceAttr( 41 "datadog_monitor.foo", "thresholds.critical", "2"), 42 ), 43 }, 44 }, 45 }) 46 } 47 48 func TestAccDatadogMonitor_Updated(t *testing.T) { 49 resource.Test(t, resource.TestCase{ 50 PreCheck: func() { testAccPreCheck(t) }, 51 Providers: testAccProviders, 52 CheckDestroy: testAccCheckDatadogMonitorDestroy, 53 Steps: []resource.TestStep{ 54 resource.TestStep{ 55 Config: testAccCheckDatadogMonitorConfig, 56 Check: resource.ComposeTestCheckFunc( 57 testAccCheckDatadogMonitorExists("datadog_monitor.foo"), 58 resource.TestCheckResourceAttr( 59 "datadog_monitor.foo", "name", "name for monitor foo"), 60 resource.TestCheckResourceAttr( 61 "datadog_monitor.foo", "message", "some message Notify: @hipchat-channel"), 62 resource.TestCheckResourceAttr( 63 "datadog_monitor.foo", "escalation_message", "the situation has escalated @pagerduty"), 64 resource.TestCheckResourceAttr( 65 "datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2"), 66 resource.TestCheckResourceAttr( 67 "datadog_monitor.foo", "type", "metric alert"), 68 resource.TestCheckResourceAttr( 69 "datadog_monitor.foo", "notify_no_data", "false"), 70 resource.TestCheckResourceAttr( 71 "datadog_monitor.foo", "renotify_interval", "60"), 72 resource.TestCheckResourceAttr( 73 "datadog_monitor.foo", "thresholds.ok", "0"), 74 resource.TestCheckResourceAttr( 75 "datadog_monitor.foo", "thresholds.warning", "1"), 76 resource.TestCheckResourceAttr( 77 "datadog_monitor.foo", "thresholds.critical", "2"), 78 resource.TestCheckResourceAttr( 79 "datadog_monitor.foo", "notify_audit", "false"), 80 resource.TestCheckResourceAttr( 81 "datadog_monitor.foo", "timeout_h", "60"), 82 resource.TestCheckResourceAttr( 83 "datadog_monitor.foo", "include_tags", "true"), 84 ), 85 }, 86 resource.TestStep{ 87 Config: testAccCheckDatadogMonitorConfigUpdated, 88 Check: resource.ComposeTestCheckFunc( 89 testAccCheckDatadogMonitorExists("datadog_monitor.foo"), 90 resource.TestCheckResourceAttr( 91 "datadog_monitor.foo", "name", "name for monitor bar"), 92 resource.TestCheckResourceAttr( 93 "datadog_monitor.foo", "message", "a different message Notify: @hipchat-channel"), 94 resource.TestCheckResourceAttr( 95 "datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:bar,host:bar} by {host} > 3"), 96 resource.TestCheckResourceAttr( 97 "datadog_monitor.foo", "escalation_message", "the situation has escalated! @pagerduty"), 98 resource.TestCheckResourceAttr( 99 "datadog_monitor.foo", "type", "metric alert"), 100 resource.TestCheckResourceAttr( 101 "datadog_monitor.foo", "notify_no_data", "true"), 102 resource.TestCheckResourceAttr( 103 "datadog_monitor.foo", "renotify_interval", "40"), 104 resource.TestCheckResourceAttr( 105 "datadog_monitor.foo", "thresholds.ok", "0"), 106 resource.TestCheckResourceAttr( 107 "datadog_monitor.foo", "thresholds.warning", "1"), 108 resource.TestCheckResourceAttr( 109 "datadog_monitor.foo", "thresholds.critical", "3"), 110 resource.TestCheckResourceAttr( 111 "datadog_monitor.foo", "notify_audit", "true"), 112 resource.TestCheckResourceAttr( 113 "datadog_monitor.foo", "timeout_h", "70"), 114 resource.TestCheckResourceAttr( 115 "datadog_monitor.foo", "include_tags", "false"), 116 resource.TestCheckResourceAttr( 117 "datadog_monitor.foo", "silenced.*", "0"), 118 ), 119 }, 120 }, 121 }) 122 } 123 124 func TestAccDatadogMonitor_TrimWhitespace(t *testing.T) { 125 resource.Test(t, resource.TestCase{ 126 PreCheck: func() { testAccPreCheck(t) }, 127 Providers: testAccProviders, 128 CheckDestroy: testAccCheckDatadogMonitorDestroy, 129 Steps: []resource.TestStep{ 130 resource.TestStep{ 131 Config: testAccCheckDatadogMonitorConfigWhitespace, 132 Check: resource.ComposeTestCheckFunc( 133 testAccCheckDatadogMonitorExists("datadog_monitor.foo"), 134 resource.TestCheckResourceAttr( 135 "datadog_monitor.foo", "name", "name for monitor foo"), 136 resource.TestCheckResourceAttr( 137 "datadog_monitor.foo", "message", "some message Notify: @hipchat-channel"), 138 resource.TestCheckResourceAttr( 139 "datadog_monitor.foo", "type", "metric alert"), 140 resource.TestCheckResourceAttr( 141 "datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2"), 142 resource.TestCheckResourceAttr( 143 "datadog_monitor.foo", "notify_no_data", "false"), 144 resource.TestCheckResourceAttr( 145 "datadog_monitor.foo", "renotify_interval", "60"), 146 resource.TestCheckResourceAttr( 147 "datadog_monitor.foo", "thresholds.ok", "0"), 148 resource.TestCheckResourceAttr( 149 "datadog_monitor.foo", "thresholds.warning", "1"), 150 resource.TestCheckResourceAttr( 151 "datadog_monitor.foo", "thresholds.critical", "2"), 152 ), 153 }, 154 }, 155 }) 156 } 157 158 func testAccCheckDatadogMonitorDestroy(s *terraform.State) error { 159 client := testAccProvider.Meta().(*datadog.Client) 160 161 if err := destroyHelper(s, client); err != nil { 162 return err 163 } 164 return nil 165 } 166 167 func testAccCheckDatadogMonitorExists(n string) resource.TestCheckFunc { 168 return func(s *terraform.State) error { 169 client := testAccProvider.Meta().(*datadog.Client) 170 if err := existsHelper(s, client); err != nil { 171 return err 172 } 173 return nil 174 } 175 } 176 177 const testAccCheckDatadogMonitorConfig = ` 178 resource "datadog_monitor" "foo" { 179 name = "name for monitor foo" 180 type = "metric alert" 181 message = "some message Notify: @hipchat-channel" 182 escalation_message = "the situation has escalated @pagerduty" 183 184 query = "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2" 185 186 thresholds { 187 ok = 0 188 warning = 1 189 critical = 2 190 } 191 192 notify_no_data = false 193 renotify_interval = 60 194 195 notify_audit = false 196 timeout_h = 60 197 include_tags = true 198 } 199 ` 200 201 const testAccCheckDatadogMonitorConfigUpdated = ` 202 resource "datadog_monitor" "foo" { 203 name = "name for monitor bar" 204 type = "metric alert" 205 message = "a different message Notify: @hipchat-channel" 206 escalation_message = "the situation has escalated @pagerduty" 207 208 query = "avg(last_1h):avg:aws.ec2.cpu{environment:bar,host:bar} by {host} > 3" 209 210 thresholds { 211 ok = 0 212 warning = 1 213 critical = 3 214 } 215 216 notify_no_data = true 217 renotify_interval = 40 218 escalation_message = "the situation has escalated! @pagerduty" 219 notify_audit = true 220 timeout_h = 70 221 include_tags = false 222 silenced { 223 "*" = 0 224 } 225 } 226 ` 227 228 const testAccCheckDatadogMonitorConfigWhitespace = ` 229 resource "datadog_monitor" "foo" { 230 name = "name for monitor foo" 231 type = "metric alert" 232 message = <<EOF 233 some message Notify: @hipchat-channel 234 EOF 235 escalation_message = <<EOF 236 the situation has escalated @pagerduty 237 EOF 238 query = <<EOF 239 avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2 240 EOF 241 thresholds { 242 ok = 0 243 warning = 1 244 critical = 2 245 } 246 247 notify_no_data = false 248 renotify_interval = 60 249 250 notify_audit = false 251 timeout_h = 60 252 include_tags = true 253 } 254 ` 255 256 func destroyHelper(s *terraform.State, client *datadog.Client) error { 257 for _, r := range s.RootModule().Resources { 258 i, _ := strconv.Atoi(r.Primary.ID) 259 if _, err := client.GetMonitor(i); err != nil { 260 if strings.Contains(err.Error(), "404 Not Found") { 261 continue 262 } 263 return fmt.Errorf("Received an error retrieving monitor %s", err) 264 } 265 return fmt.Errorf("Monitor still exists") 266 } 267 return nil 268 } 269 270 func existsHelper(s *terraform.State, client *datadog.Client) error { 271 for _, r := range s.RootModule().Resources { 272 i, _ := strconv.Atoi(r.Primary.ID) 273 if _, err := client.GetMonitor(i); err != nil { 274 return fmt.Errorf("Received an error retrieving monitor %s", err) 275 } 276 } 277 return nil 278 }