github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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_Basic(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_basic(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_Full(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_full(name),
    47  				Check: resource.ComposeTestCheckFunc(
    48  					testAccCheckLibratoAlertExists("librato_alert.foobar", &alert),
    49  					testAccCheckLibratoAlertName(&alert, name),
    50  					resource.TestCheckResourceAttr(
    51  						"librato_alert.foobar", "name", name),
    52  					resource.TestCheckResourceAttr(
    53  						"librato_alert.foobar", "condition.836525194.metric_name", "librato.cpu.percent.idle"),
    54  					resource.TestCheckResourceAttr(
    55  						"librato_alert.foobar", "condition.836525194.threshold", "10"),
    56  					resource.TestCheckResourceAttr(
    57  						"librato_alert.foobar", "condition.836525194.duration", "600"),
    58  				),
    59  			},
    60  		},
    61  	})
    62  }
    63  
    64  func TestAccLibratoAlert_Updated(t *testing.T) {
    65  	var alert librato.Alert
    66  	name := acctest.RandString(10)
    67  
    68  	resource.Test(t, resource.TestCase{
    69  		PreCheck:     func() { testAccPreCheck(t) },
    70  		Providers:    testAccProviders,
    71  		CheckDestroy: testAccCheckLibratoAlertDestroy,
    72  		Steps: []resource.TestStep{
    73  			{
    74  				Config: testAccCheckLibratoAlertConfig_basic(name),
    75  				Check: resource.ComposeTestCheckFunc(
    76  					testAccCheckLibratoAlertExists("librato_alert.foobar", &alert),
    77  					testAccCheckLibratoAlertDescription(&alert, "A Test Alert"),
    78  					resource.TestCheckResourceAttr(
    79  						"librato_alert.foobar", "name", name),
    80  				),
    81  			},
    82  			{
    83  				Config: testAccCheckLibratoAlertConfig_new_value(name),
    84  				Check: resource.ComposeTestCheckFunc(
    85  					testAccCheckLibratoAlertExists("librato_alert.foobar", &alert),
    86  					testAccCheckLibratoAlertDescription(&alert, "A modified Test Alert"),
    87  					resource.TestCheckResourceAttr(
    88  						"librato_alert.foobar", "description", "A modified Test Alert"),
    89  				),
    90  			},
    91  		},
    92  	})
    93  }
    94  
    95  func TestAccLibratoAlert_FullUpdate(t *testing.T) {
    96  	var alert librato.Alert
    97  	name := acctest.RandString(10)
    98  
    99  	resource.Test(t, resource.TestCase{
   100  		PreCheck:     func() { testAccPreCheck(t) },
   101  		Providers:    testAccProviders,
   102  		CheckDestroy: testAccCheckLibratoAlertDestroy,
   103  		Steps: []resource.TestStep{
   104  			{
   105  				Config: testAccCheckLibratoAlertConfig_full_update(name),
   106  				Check: resource.ComposeTestCheckFunc(
   107  					testAccCheckLibratoAlertExists("librato_alert.foobar", &alert),
   108  					testAccCheckLibratoAlertName(&alert, name),
   109  					resource.TestCheckResourceAttr(
   110  						"librato_alert.foobar", "name", name),
   111  					resource.TestCheckResourceAttr(
   112  						"librato_alert.foobar", "rearm_seconds", "1200"),
   113  					resource.TestCheckResourceAttr(
   114  						"librato_alert.foobar", "condition.2524844643.metric_name", "librato.cpu.percent.idle"),
   115  					resource.TestCheckResourceAttr(
   116  						"librato_alert.foobar", "condition.2524844643.threshold", "10"),
   117  					resource.TestCheckResourceAttr(
   118  						"librato_alert.foobar", "condition.2524844643.duration", "60"),
   119  				),
   120  			},
   121  		},
   122  	})
   123  }
   124  
   125  func testAccCheckLibratoAlertDestroy(s *terraform.State) error {
   126  	client := testAccProvider.Meta().(*librato.Client)
   127  
   128  	for _, rs := range s.RootModule().Resources {
   129  		if rs.Type != "librato_alert" {
   130  			continue
   131  		}
   132  
   133  		id, err := strconv.ParseUint(rs.Primary.ID, 10, 0)
   134  		if err != nil {
   135  			return fmt.Errorf("ID not a number")
   136  		}
   137  
   138  		_, _, err = client.Alerts.Get(uint(id))
   139  
   140  		if err == nil {
   141  			return fmt.Errorf("Alert still exists")
   142  		}
   143  	}
   144  
   145  	return nil
   146  }
   147  
   148  func testAccCheckLibratoAlertName(alert *librato.Alert, name string) resource.TestCheckFunc {
   149  	return func(s *terraform.State) error {
   150  
   151  		if alert.Name == nil || *alert.Name != name {
   152  			return fmt.Errorf("Bad name: %s", *alert.Name)
   153  		}
   154  
   155  		return nil
   156  	}
   157  }
   158  
   159  func testAccCheckLibratoAlertDescription(alert *librato.Alert, description string) resource.TestCheckFunc {
   160  	return func(s *terraform.State) error {
   161  
   162  		if alert.Description == nil || *alert.Description != description {
   163  			return fmt.Errorf("Bad description: %s", *alert.Description)
   164  		}
   165  
   166  		return nil
   167  	}
   168  }
   169  
   170  func testAccCheckLibratoAlertExists(n string, alert *librato.Alert) resource.TestCheckFunc {
   171  	return func(s *terraform.State) error {
   172  		rs, ok := s.RootModule().Resources[n]
   173  
   174  		if !ok {
   175  			return fmt.Errorf("Not found: %s", n)
   176  		}
   177  
   178  		if rs.Primary.ID == "" {
   179  			return fmt.Errorf("No Alert ID is set")
   180  		}
   181  
   182  		client := testAccProvider.Meta().(*librato.Client)
   183  
   184  		id, err := strconv.ParseUint(rs.Primary.ID, 10, 0)
   185  		if err != nil {
   186  			return fmt.Errorf("ID not a number")
   187  		}
   188  
   189  		foundAlert, _, err := client.Alerts.Get(uint(id))
   190  
   191  		if err != nil {
   192  			return err
   193  		}
   194  
   195  		if foundAlert.ID == nil || *foundAlert.ID != uint(id) {
   196  			return fmt.Errorf("Alert not found")
   197  		}
   198  
   199  		*alert = *foundAlert
   200  
   201  		return nil
   202  	}
   203  }
   204  
   205  func testAccCheckLibratoAlertConfig_basic(name string) string {
   206  	return fmt.Sprintf(`
   207  resource "librato_alert" "foobar" {
   208      name = "%s"
   209      description = "A Test Alert"
   210  }`, name)
   211  }
   212  
   213  func testAccCheckLibratoAlertConfig_new_value(name string) string {
   214  	return fmt.Sprintf(`
   215  resource "librato_alert" "foobar" {
   216      name = "%s"
   217      description = "A modified Test Alert"
   218  }`, name)
   219  }
   220  
   221  func testAccCheckLibratoAlertConfig_full(name string) string {
   222  	return fmt.Sprintf(`
   223  resource "librato_service" "foobar" {
   224      title = "Foo Bar"
   225      type = "mail"
   226      settings = <<EOF
   227  {
   228    "addresses": "admin@example.com"
   229  }
   230  EOF
   231  }
   232  
   233  resource "librato_alert" "foobar" {
   234      name = "%s"
   235      description = "A Test Alert"
   236      services = [ "${librato_service.foobar.id}" ]
   237      condition {
   238        type = "above"
   239        threshold = 10
   240        duration = 600
   241        metric_name = "librato.cpu.percent.idle"
   242      }
   243      attributes {
   244        runbook_url = "https://www.youtube.com/watch?v=oHg5SJYRHA0"
   245      }
   246      active = false
   247      rearm_seconds = 300
   248  }`, name)
   249  }
   250  
   251  func testAccCheckLibratoAlertConfig_full_update(name string) string {
   252  	return fmt.Sprintf(`
   253  resource "librato_service" "foobar" {
   254      title = "Foo Bar"
   255      type = "mail"
   256      settings = <<EOF
   257  {
   258    "addresses": "admin@example.com"
   259  }
   260  EOF
   261  }
   262  
   263  resource "librato_alert" "foobar" {
   264      name = "%s"
   265      description = "A Test Alert"
   266      services = [ "${librato_service.foobar.id}" ]
   267      condition {
   268        type = "above"
   269        threshold = 10
   270        duration = 60
   271        metric_name = "librato.cpu.percent.idle"
   272      }
   273      attributes {
   274        runbook_url = "https://www.youtube.com/watch?v=oHg5SJYRHA0"
   275      }
   276      active = false
   277      rearm_seconds = 1200
   278  }`, name)
   279  }