github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/henrikhodne/go-librato/librato"
    11  )
    12  
    13  func TestAccLibratoAlert_Basic(t *testing.T) {
    14  	var alert librato.Alert
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckLibratoAlertDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccCheckLibratoAlertConfig_basic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckLibratoAlertExists("librato_alert.foobar", &alert),
    25  					testAccCheckLibratoAlertName(&alert, "FooBar"),
    26  					resource.TestCheckResourceAttr(
    27  						"librato_alert.foobar", "name", "FooBar"),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func TestAccLibratoAlert_Full(t *testing.T) {
    35  	var alert librato.Alert
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:     func() { testAccPreCheck(t) },
    39  		Providers:    testAccProviders,
    40  		CheckDestroy: testAccCheckLibratoAlertDestroy,
    41  		Steps: []resource.TestStep{
    42  			resource.TestStep{
    43  				Config: testAccCheckLibratoAlertConfig_full,
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testAccCheckLibratoAlertExists("librato_alert.foobar", &alert),
    46  					testAccCheckLibratoAlertName(&alert, "FooBar"),
    47  					resource.TestCheckResourceAttr(
    48  						"librato_alert.foobar", "name", "FooBar"),
    49  				),
    50  			},
    51  		},
    52  	})
    53  }
    54  
    55  func TestAccLibratoAlert_Updated(t *testing.T) {
    56  	var alert librato.Alert
    57  
    58  	resource.Test(t, resource.TestCase{
    59  		PreCheck:     func() { testAccPreCheck(t) },
    60  		Providers:    testAccProviders,
    61  		CheckDestroy: testAccCheckLibratoAlertDestroy,
    62  		Steps: []resource.TestStep{
    63  			resource.TestStep{
    64  				Config: testAccCheckLibratoAlertConfig_basic,
    65  				Check: resource.ComposeTestCheckFunc(
    66  					testAccCheckLibratoAlertExists("librato_alert.foobar", &alert),
    67  					testAccCheckLibratoAlertName(&alert, "FooBar"),
    68  					resource.TestCheckResourceAttr(
    69  						"librato_alert.foobar", "name", "FooBar"),
    70  				),
    71  			},
    72  			resource.TestStep{
    73  				Config: testAccCheckLibratoAlertConfig_new_value,
    74  				Check: resource.ComposeTestCheckFunc(
    75  					testAccCheckLibratoAlertExists("librato_alert.foobar", &alert),
    76  					testAccCheckLibratoAlertName(&alert, "BarBaz"),
    77  					resource.TestCheckResourceAttr(
    78  						"librato_alert.foobar", "name", "BarBaz"),
    79  				),
    80  			},
    81  		},
    82  	})
    83  }
    84  
    85  func testAccCheckLibratoAlertDestroy(s *terraform.State) error {
    86  	client := testAccProvider.Meta().(*librato.Client)
    87  
    88  	for _, rs := range s.RootModule().Resources {
    89  		if rs.Type != "librato_alert" {
    90  			continue
    91  		}
    92  
    93  		id, err := strconv.ParseUint(rs.Primary.ID, 10, 0)
    94  		if err != nil {
    95  			return fmt.Errorf("ID not a number")
    96  		}
    97  
    98  		_, _, err = client.Alerts.Get(uint(id))
    99  
   100  		if err == nil {
   101  			return fmt.Errorf("Alert still exists")
   102  		}
   103  	}
   104  
   105  	return nil
   106  }
   107  
   108  func testAccCheckLibratoAlertName(alert *librato.Alert, name string) resource.TestCheckFunc {
   109  	return func(s *terraform.State) error {
   110  
   111  		if alert.Name == nil || *alert.Name != name {
   112  			return fmt.Errorf("Bad name: %s", *alert.Name)
   113  		}
   114  
   115  		return nil
   116  	}
   117  }
   118  
   119  func testAccCheckLibratoAlertExists(n string, alert *librato.Alert) resource.TestCheckFunc {
   120  	return func(s *terraform.State) error {
   121  		rs, ok := s.RootModule().Resources[n]
   122  
   123  		if !ok {
   124  			return fmt.Errorf("Not found: %s", n)
   125  		}
   126  
   127  		if rs.Primary.ID == "" {
   128  			return fmt.Errorf("No Alert ID is set")
   129  		}
   130  
   131  		client := testAccProvider.Meta().(*librato.Client)
   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  		foundAlert, _, err := client.Alerts.Get(uint(id))
   139  
   140  		if err != nil {
   141  			return err
   142  		}
   143  
   144  		if foundAlert.ID == nil || *foundAlert.ID != uint(id) {
   145  			return fmt.Errorf("Alert not found")
   146  		}
   147  
   148  		*alert = *foundAlert
   149  
   150  		return nil
   151  	}
   152  }
   153  
   154  const testAccCheckLibratoAlertConfig_basic = `
   155  resource "librato_alert" "foobar" {
   156      name = "FooBar"
   157      description = "A Test Alert"
   158  }`
   159  
   160  const testAccCheckLibratoAlertConfig_new_value = `
   161  resource "librato_alert" "foobar" {
   162      name = "BarBaz"
   163      description = "A Test Alert"
   164  }`
   165  
   166  const testAccCheckLibratoAlertConfig_full = `
   167  resource "librato_service" "foobar" {
   168      title = "Foo Bar"
   169      type = "mail"
   170      settings = <<EOF
   171  {
   172    "addresses": "admin@example.com"
   173  }
   174  EOF
   175  }
   176  
   177  resource "librato_alert" "foobar" {
   178      name = "FooBar"
   179      description = "A Test Alert"
   180      services = [ "${librato_service.foobar.id}" ]
   181      condition {
   182        type = "above"
   183        threshold = 10
   184        metric_name = "librato.cpu.percent.idle"
   185      }
   186      attributes {
   187        runbook_url = "https://www.youtube.com/watch?v=oHg5SJYRHA0"
   188      }
   189      active = false
   190      rearm_seconds = 300
   191  }`