github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/librato/resource_librato_alert_test.go (about) 1 package librato 2 3 import ( 4 "fmt" 5 "strconv" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/acctest" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 "github.com/henrikhodne/go-librato/librato" 12 ) 13 14 func TestAccLibratoAlert_Minimal(t *testing.T) { 15 var alert librato.Alert 16 name := acctest.RandString(10) 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckLibratoAlertDestroy, 22 Steps: []resource.TestStep{ 23 { 24 Config: testAccCheckLibratoAlertConfig_minimal(name), 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckLibratoAlertExists("librato_alert.foobar", &alert), 27 testAccCheckLibratoAlertName(&alert, name), 28 resource.TestCheckResourceAttr( 29 "librato_alert.foobar", "name", name), 30 ), 31 }, 32 }, 33 }) 34 } 35 36 func TestAccLibratoAlert_Basic(t *testing.T) { 37 var alert librato.Alert 38 name := acctest.RandString(10) 39 40 resource.Test(t, resource.TestCase{ 41 PreCheck: func() { testAccPreCheck(t) }, 42 Providers: testAccProviders, 43 CheckDestroy: testAccCheckLibratoAlertDestroy, 44 Steps: []resource.TestStep{ 45 { 46 Config: testAccCheckLibratoAlertConfig_basic(name), 47 Check: resource.ComposeTestCheckFunc( 48 testAccCheckLibratoAlertExists("librato_alert.foobar", &alert), 49 testAccCheckLibratoAlertName(&alert, name), 50 testAccCheckLibratoAlertDescription(&alert, "A Test Alert"), 51 resource.TestCheckResourceAttr( 52 "librato_alert.foobar", "name", name), 53 ), 54 }, 55 }, 56 }) 57 } 58 59 func TestAccLibratoAlert_Full(t *testing.T) { 60 var alert librato.Alert 61 name := acctest.RandString(10) 62 63 resource.Test(t, resource.TestCase{ 64 PreCheck: func() { testAccPreCheck(t) }, 65 Providers: testAccProviders, 66 CheckDestroy: testAccCheckLibratoAlertDestroy, 67 Steps: []resource.TestStep{ 68 { 69 Config: testAccCheckLibratoAlertConfig_full(name), 70 Check: resource.ComposeTestCheckFunc( 71 testAccCheckLibratoAlertExists("librato_alert.foobar", &alert), 72 testAccCheckLibratoAlertName(&alert, name), 73 testAccCheckLibratoAlertDescription(&alert, "A Test Alert"), 74 resource.TestCheckResourceAttr( 75 "librato_alert.foobar", "name", name), 76 resource.TestCheckResourceAttr( 77 "librato_alert.foobar", "condition.836525194.metric_name", "librato.cpu.percent.idle"), 78 resource.TestCheckResourceAttr( 79 "librato_alert.foobar", "condition.836525194.type", "above"), 80 resource.TestCheckResourceAttr( 81 "librato_alert.foobar", "condition.836525194.threshold", "10"), 82 resource.TestCheckResourceAttr( 83 "librato_alert.foobar", "condition.836525194.duration", "600"), 84 ), 85 }, 86 }, 87 }) 88 } 89 90 func TestAccLibratoAlert_Updated(t *testing.T) { 91 var alert librato.Alert 92 name := acctest.RandString(10) 93 94 resource.Test(t, resource.TestCase{ 95 PreCheck: func() { testAccPreCheck(t) }, 96 Providers: testAccProviders, 97 CheckDestroy: testAccCheckLibratoAlertDestroy, 98 Steps: []resource.TestStep{ 99 { 100 Config: testAccCheckLibratoAlertConfig_basic(name), 101 Check: resource.ComposeTestCheckFunc( 102 testAccCheckLibratoAlertExists("librato_alert.foobar", &alert), 103 testAccCheckLibratoAlertDescription(&alert, "A Test Alert"), 104 resource.TestCheckResourceAttr( 105 "librato_alert.foobar", "name", name), 106 ), 107 }, 108 { 109 Config: testAccCheckLibratoAlertConfig_new_value(name), 110 Check: resource.ComposeTestCheckFunc( 111 testAccCheckLibratoAlertExists("librato_alert.foobar", &alert), 112 testAccCheckLibratoAlertDescription(&alert, "A modified Test Alert"), 113 resource.TestCheckResourceAttr( 114 "librato_alert.foobar", "description", "A modified Test Alert"), 115 ), 116 }, 117 }, 118 }) 119 } 120 121 func TestAccLibratoAlert_Rename(t *testing.T) { 122 var alert librato.Alert 123 name := acctest.RandString(10) 124 newName := acctest.RandString(10) 125 126 resource.Test(t, resource.TestCase{ 127 PreCheck: func() { testAccPreCheck(t) }, 128 Providers: testAccProviders, 129 CheckDestroy: testAccCheckLibratoAlertDestroy, 130 Steps: []resource.TestStep{ 131 { 132 Config: testAccCheckLibratoAlertConfig_basic(name), 133 Check: resource.ComposeTestCheckFunc( 134 testAccCheckLibratoAlertExists("librato_alert.foobar", &alert), 135 resource.TestCheckResourceAttr( 136 "librato_alert.foobar", "name", name), 137 ), 138 }, 139 { 140 Config: testAccCheckLibratoAlertConfig_basic(newName), 141 Check: resource.ComposeTestCheckFunc( 142 testAccCheckLibratoAlertExists("librato_alert.foobar", &alert), 143 resource.TestCheckResourceAttr( 144 "librato_alert.foobar", "name", newName), 145 ), 146 }, 147 }, 148 }) 149 } 150 151 func TestAccLibratoAlert_FullUpdate(t *testing.T) { 152 var alert librato.Alert 153 name := acctest.RandString(10) 154 155 resource.Test(t, resource.TestCase{ 156 PreCheck: func() { testAccPreCheck(t) }, 157 Providers: testAccProviders, 158 CheckDestroy: testAccCheckLibratoAlertDestroy, 159 Steps: []resource.TestStep{ 160 { 161 Config: testAccCheckLibratoAlertConfig_full_update(name), 162 Check: resource.ComposeTestCheckFunc( 163 testAccCheckLibratoAlertExists("librato_alert.foobar", &alert), 164 testAccCheckLibratoAlertName(&alert, name), 165 testAccCheckLibratoAlertDescription(&alert, "A Test Alert"), 166 resource.TestCheckResourceAttr( 167 "librato_alert.foobar", "name", name), 168 resource.TestCheckResourceAttr( 169 "librato_alert.foobar", "rearm_seconds", "1200"), 170 resource.TestCheckResourceAttr( 171 "librato_alert.foobar", "condition.2524844643.metric_name", "librato.cpu.percent.idle"), 172 resource.TestCheckResourceAttr( 173 "librato_alert.foobar", "condition.2524844643.type", "above"), 174 resource.TestCheckResourceAttr( 175 "librato_alert.foobar", "condition.2524844643.threshold", "10"), 176 resource.TestCheckResourceAttr( 177 "librato_alert.foobar", "condition.2524844643.duration", "60"), 178 ), 179 }, 180 }, 181 }) 182 } 183 184 func testAccCheckLibratoAlertDestroy(s *terraform.State) error { 185 client := testAccProvider.Meta().(*librato.Client) 186 187 for _, rs := range s.RootModule().Resources { 188 if rs.Type != "librato_alert" { 189 continue 190 } 191 192 id, err := strconv.ParseUint(rs.Primary.ID, 10, 0) 193 if err != nil { 194 return fmt.Errorf("ID not a number") 195 } 196 197 _, _, err = client.Alerts.Get(uint(id)) 198 199 if err == nil { 200 return fmt.Errorf("Alert still exists") 201 } 202 } 203 204 return nil 205 } 206 207 func testAccCheckLibratoAlertName(alert *librato.Alert, name string) resource.TestCheckFunc { 208 return func(s *terraform.State) error { 209 210 if alert.Name == nil || *alert.Name != name { 211 return fmt.Errorf("Bad name: %s", *alert.Name) 212 } 213 214 return nil 215 } 216 } 217 218 func testAccCheckLibratoAlertDescription(alert *librato.Alert, description string) resource.TestCheckFunc { 219 return func(s *terraform.State) error { 220 221 if alert.Description == nil || *alert.Description != description { 222 return fmt.Errorf("Bad description: %s", *alert.Description) 223 } 224 225 return nil 226 } 227 } 228 229 func testAccCheckLibratoAlertExists(n string, alert *librato.Alert) resource.TestCheckFunc { 230 return func(s *terraform.State) error { 231 rs, ok := s.RootModule().Resources[n] 232 233 if !ok { 234 return fmt.Errorf("Not found: %s", n) 235 } 236 237 if rs.Primary.ID == "" { 238 return fmt.Errorf("No Alert ID is set") 239 } 240 241 client := testAccProvider.Meta().(*librato.Client) 242 243 id, err := strconv.ParseUint(rs.Primary.ID, 10, 0) 244 if err != nil { 245 return fmt.Errorf("ID not a number") 246 } 247 248 foundAlert, _, err := client.Alerts.Get(uint(id)) 249 250 if err != nil { 251 return err 252 } 253 254 if foundAlert.ID == nil || *foundAlert.ID != uint(id) { 255 return fmt.Errorf("Alert not found") 256 } 257 258 *alert = *foundAlert 259 260 return nil 261 } 262 } 263 264 func testAccCheckLibratoAlertConfig_minimal(name string) string { 265 return fmt.Sprintf(` 266 resource "librato_alert" "foobar" { 267 name = "%s" 268 }`, name) 269 } 270 271 func testAccCheckLibratoAlertConfig_basic(name string) string { 272 return fmt.Sprintf(` 273 resource "librato_alert" "foobar" { 274 name = "%s" 275 description = "A Test Alert" 276 }`, name) 277 } 278 279 func testAccCheckLibratoAlertConfig_new_value(name string) string { 280 return fmt.Sprintf(` 281 resource "librato_alert" "foobar" { 282 name = "%s" 283 description = "A modified Test Alert" 284 }`, name) 285 } 286 287 func testAccCheckLibratoAlertConfig_full(name string) string { 288 return fmt.Sprintf(` 289 resource "librato_service" "foobar" { 290 title = "Foo Bar" 291 type = "mail" 292 settings = <<EOF 293 { 294 "addresses": "admin@example.com" 295 } 296 EOF 297 } 298 299 resource "librato_alert" "foobar" { 300 name = "%s" 301 description = "A Test Alert" 302 services = [ "${librato_service.foobar.id}" ] 303 condition { 304 type = "above" 305 threshold = 10 306 duration = 600 307 metric_name = "librato.cpu.percent.idle" 308 } 309 attributes { 310 runbook_url = "https://www.youtube.com/watch?v=oHg5SJYRHA0" 311 } 312 active = false 313 rearm_seconds = 300 314 }`, name) 315 } 316 317 func testAccCheckLibratoAlertConfig_full_update(name string) string { 318 return fmt.Sprintf(` 319 resource "librato_service" "foobar" { 320 title = "Foo Bar" 321 type = "mail" 322 settings = <<EOF 323 { 324 "addresses": "admin@example.com" 325 } 326 EOF 327 } 328 329 resource "librato_alert" "foobar" { 330 name = "%s" 331 description = "A Test Alert" 332 services = [ "${librato_service.foobar.id}" ] 333 condition { 334 type = "above" 335 threshold = 10 336 duration = 60 337 metric_name = "librato.cpu.percent.idle" 338 } 339 attributes { 340 runbook_url = "https://www.youtube.com/watch?v=oHg5SJYRHA0" 341 } 342 active = false 343 rearm_seconds = 1200 344 }`, name) 345 }