github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/librato/resource_librato_metric_test.go (about)

     1  package librato
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     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 TestAccLibratoMetrics(t *testing.T) {
    15  	var metric librato.Metric
    16  
    17  	name := fmt.Sprintf("tftest-metric-%s", acctest.RandString(10))
    18  	typ := "counter"
    19  	desc1 := fmt.Sprintf("A test %s metric", typ)
    20  	desc2 := fmt.Sprintf("An updated test %s metric", typ)
    21  	resource.Test(t, resource.TestCase{
    22  		PreCheck:     func() { testAccPreCheck(t) },
    23  		Providers:    testAccProviders,
    24  		CheckDestroy: testAccCheckLibratoMetricDestroy,
    25  		Steps: []resource.TestStep{
    26  			{
    27  				Config: counterMetricConfig(name, typ, desc1),
    28  				Check: resource.ComposeTestCheckFunc(
    29  					testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
    30  					testAccCheckLibratoMetricName(&metric, name),
    31  					testAccCheckLibratoMetricType(&metric, typ),
    32  					resource.TestCheckResourceAttr(
    33  						"librato_metric.foobar", "name", name),
    34  				),
    35  			},
    36  			{
    37  				PreConfig: sleep(t, 5),
    38  				Config:    counterMetricConfig(name, typ, desc2),
    39  				Check: resource.ComposeTestCheckFunc(
    40  					testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
    41  					testAccCheckLibratoMetricName(&metric, name),
    42  					testAccCheckLibratoMetricType(&metric, typ),
    43  					testAccCheckLibratoMetricDescription(&metric, desc2),
    44  					resource.TestCheckResourceAttr(
    45  						"librato_metric.foobar", "name", name),
    46  				),
    47  			},
    48  		},
    49  	})
    50  
    51  	name = fmt.Sprintf("tftest-metric-%s", acctest.RandString(10))
    52  	typ = "gauge"
    53  	desc1 = fmt.Sprintf("A test %s metric", typ)
    54  	desc2 = fmt.Sprintf("An updated test %s metric", typ)
    55  	resource.Test(t, resource.TestCase{
    56  		PreCheck:     func() { testAccPreCheck(t) },
    57  		Providers:    testAccProviders,
    58  		CheckDestroy: testAccCheckLibratoMetricDestroy,
    59  		Steps: []resource.TestStep{
    60  			{
    61  				Config: gaugeMetricConfig(name, typ, desc1),
    62  				Check: resource.ComposeTestCheckFunc(
    63  					testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
    64  					testAccCheckLibratoMetricName(&metric, name),
    65  					testAccCheckLibratoMetricType(&metric, typ),
    66  					resource.TestCheckResourceAttr(
    67  						"librato_metric.foobar", "name", name),
    68  				),
    69  			},
    70  			{
    71  				PreConfig: sleep(t, 5),
    72  				Config:    gaugeMetricConfig(name, typ, desc2),
    73  				Check: resource.ComposeTestCheckFunc(
    74  					testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
    75  					testAccCheckLibratoMetricName(&metric, name),
    76  					testAccCheckLibratoMetricType(&metric, typ),
    77  					testAccCheckLibratoMetricDescription(&metric, desc2),
    78  					resource.TestCheckResourceAttr(
    79  						"librato_metric.foobar", "name", name),
    80  				),
    81  			},
    82  		},
    83  	})
    84  
    85  	name = fmt.Sprintf("tftest-metric-%s", acctest.RandString(10))
    86  	typ = "composite"
    87  	desc1 = fmt.Sprintf("A test %s metric", typ)
    88  	desc2 = fmt.Sprintf("An updated test %s metric", typ)
    89  	resource.Test(t, resource.TestCase{
    90  		PreCheck:     func() { testAccPreCheck(t) },
    91  		Providers:    testAccProviders,
    92  		CheckDestroy: testAccCheckLibratoMetricDestroy,
    93  		Steps: []resource.TestStep{
    94  			{
    95  				Config: compositeMetricConfig(name, typ, desc1),
    96  				Check: resource.ComposeTestCheckFunc(
    97  					testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
    98  					testAccCheckLibratoMetricName(&metric, name),
    99  					testAccCheckLibratoMetricType(&metric, typ),
   100  					resource.TestCheckResourceAttr(
   101  						"librato_metric.foobar", "name", name),
   102  				),
   103  			},
   104  			{
   105  				PreConfig: sleep(t, 5),
   106  				Config:    compositeMetricConfig(name, typ, desc2),
   107  				Check: resource.ComposeTestCheckFunc(
   108  					testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
   109  					testAccCheckLibratoMetricName(&metric, name),
   110  					testAccCheckLibratoMetricType(&metric, typ),
   111  					testAccCheckLibratoMetricDescription(&metric, desc2),
   112  					resource.TestCheckResourceAttr(
   113  						"librato_metric.foobar", "name", name),
   114  				),
   115  			},
   116  		},
   117  	})
   118  }
   119  
   120  func testAccCheckLibratoMetricDestroy(s *terraform.State) error {
   121  	client := testAccProvider.Meta().(*librato.Client)
   122  
   123  	for _, rs := range s.RootModule().Resources {
   124  		if rs.Type != "librato_metric" {
   125  			continue
   126  		}
   127  
   128  		_, _, err := client.Metrics.Get(rs.Primary.ID)
   129  		if err == nil {
   130  			return fmt.Errorf("Metric still exists")
   131  		}
   132  	}
   133  
   134  	return nil
   135  }
   136  
   137  func testAccCheckLibratoMetricName(metric *librato.Metric, name string) resource.TestCheckFunc {
   138  	return func(s *terraform.State) error {
   139  		if metric.Name == nil || *metric.Name != name {
   140  			return fmt.Errorf("Bad name: %s", *metric.Name)
   141  		}
   142  
   143  		return nil
   144  	}
   145  }
   146  
   147  func testAccCheckLibratoMetricDescription(metric *librato.Metric, desc string) resource.TestCheckFunc {
   148  	return func(s *terraform.State) error {
   149  		if metric.Description == nil || *metric.Description != desc {
   150  			return fmt.Errorf("Bad description: %s", *metric.Description)
   151  		}
   152  
   153  		return nil
   154  	}
   155  }
   156  
   157  func testAccCheckLibratoMetricType(metric *librato.Metric, wantType string) resource.TestCheckFunc {
   158  	return func(s *terraform.State) error {
   159  		if metric.Type == nil || *metric.Type != wantType {
   160  			return fmt.Errorf("Bad metric type: %s", *metric.Type)
   161  		}
   162  
   163  		return nil
   164  	}
   165  }
   166  
   167  func testAccCheckLibratoMetricExists(n string, metric *librato.Metric) resource.TestCheckFunc {
   168  	return func(s *terraform.State) error {
   169  		rs, ok := s.RootModule().Resources[n]
   170  
   171  		if !ok {
   172  			return fmt.Errorf("Not found: %s", n)
   173  		}
   174  
   175  		if rs.Primary.ID == "" {
   176  			return fmt.Errorf("No Metric ID is set")
   177  		}
   178  
   179  		client := testAccProvider.Meta().(*librato.Client)
   180  
   181  		foundMetric, _, err := client.Metrics.Get(rs.Primary.ID)
   182  
   183  		if err != nil {
   184  			return err
   185  		}
   186  
   187  		if foundMetric.Name == nil || *foundMetric.Name != rs.Primary.ID {
   188  			return fmt.Errorf("Metric not found")
   189  		}
   190  
   191  		*metric = *foundMetric
   192  
   193  		return nil
   194  	}
   195  }
   196  
   197  func counterMetricConfig(name, typ, desc string) string {
   198  	return strings.TrimSpace(fmt.Sprintf(`
   199      resource "librato_metric" "foobar" {
   200          name = "%s"
   201          type = "%s"
   202          description = "%s"
   203          attributes {
   204            display_stacked = true
   205          }
   206      }`, name, typ, desc))
   207  }
   208  
   209  func gaugeMetricConfig(name, typ, desc string) string {
   210  	return strings.TrimSpace(fmt.Sprintf(`
   211      resource "librato_metric" "foobar" {
   212          name = "%s"
   213          type = "%s"
   214          description = "%s"
   215          attributes {
   216            display_stacked = true
   217          }
   218      }`, name, typ, desc))
   219  }
   220  
   221  func compositeMetricConfig(name, typ, desc string) string {
   222  	return strings.TrimSpace(fmt.Sprintf(`
   223      resource "librato_metric" "foobar" {
   224          name = "%s"
   225          type = "%s"
   226          description = "%s"
   227          composite = "s(\"librato.cpu.percent.user\", {\"environment\" : \"prod\", \"service\": \"api\"})"
   228          attributes {
   229            display_stacked = true
   230          }
   231      }`, name, typ, desc))
   232  }