terraform/builtin/providers/librato/resource_librato_metric_tes...

223 lines
6.3 KiB
Go
Raw Normal View History

2017-05-17 06:18:43 +02:00
package librato
import (
"fmt"
"strings"
"testing"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/henrikhodne/go-librato/librato"
)
2017-05-18 15:37:16 +02:00
func TestAccLibratoCounterMetric(t *testing.T) {
2017-05-17 06:18:43 +02:00
var metric librato.Metric
name := fmt.Sprintf("tftest-metric-%s", acctest.RandString(10))
2017-05-18 22:13:11 +02:00
typ := "counter"
2017-05-17 06:18:43 +02:00
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLibratoMetricDestroy,
Steps: []resource.TestStep{
{
2017-05-18 22:13:11 +02:00
Config: counterMetricConfig(name, typ, fmt.Sprintf("A test %s metric", typ)),
2017-05-18 15:37:16 +02:00
Check: resource.ComposeTestCheckFunc(
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
testAccCheckLibratoMetricName(&metric, name),
2017-05-18 22:13:11 +02:00
testAccCheckLibratoMetricType(&metric, typ),
2017-05-18 15:37:16 +02:00
resource.TestCheckResourceAttr(
"librato_metric.foobar", "name", name),
),
},
{
PreConfig: sleep(t, 15),
2017-05-18 22:13:11 +02:00
Config: counterMetricConfig(name, typ, fmt.Sprintf("An updated test %s metric", typ)),
2017-05-18 15:37:16 +02:00
Check: resource.ComposeTestCheckFunc(
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
testAccCheckLibratoMetricName(&metric, name),
2017-05-18 22:13:11 +02:00
testAccCheckLibratoMetricType(&metric, typ),
2017-05-18 15:37:16 +02:00
resource.TestCheckResourceAttr(
"librato_metric.foobar", "name", name),
),
},
},
})
}
func TestAccLibratoGaugeMetric(t *testing.T) {
var metric librato.Metric
name := fmt.Sprintf("tftest-metric-%s", acctest.RandString(10))
2017-05-18 22:13:11 +02:00
typ := "gauge"
2017-05-18 15:37:16 +02:00
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLibratoMetricDestroy,
Steps: []resource.TestStep{
{
2017-05-18 22:13:11 +02:00
Config: gaugeMetricConfig(name, typ, fmt.Sprintf("A test %s metric", typ)),
2017-05-18 15:37:16 +02:00
Check: resource.ComposeTestCheckFunc(
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
testAccCheckLibratoMetricName(&metric, name),
2017-05-18 22:13:11 +02:00
testAccCheckLibratoMetricType(&metric, typ),
2017-05-18 15:37:16 +02:00
resource.TestCheckResourceAttr(
"librato_metric.foobar", "name", name),
),
},
{
PreConfig: sleep(t, 15),
2017-05-18 22:13:11 +02:00
Config: gaugeMetricConfig(name, typ, fmt.Sprintf("An updated test %s metric", typ)),
2017-05-18 15:37:16 +02:00
Check: resource.ComposeTestCheckFunc(
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
testAccCheckLibratoMetricName(&metric, name),
2017-05-18 22:13:11 +02:00
testAccCheckLibratoMetricType(&metric, typ),
2017-05-18 15:37:16 +02:00
resource.TestCheckResourceAttr(
"librato_metric.foobar", "name", name),
),
},
},
})
}
func TestAccLibratoCompositeMetric(t *testing.T) {
var metric librato.Metric
name := fmt.Sprintf("tftest-metric-%s", acctest.RandString(10))
2017-05-18 22:13:11 +02:00
typ := "composite"
2017-05-18 15:37:16 +02:00
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLibratoMetricDestroy,
Steps: []resource.TestStep{
{
2017-05-18 22:13:11 +02:00
Config: compositeMetricConfig(name, typ, fmt.Sprintf("A test %s metric", typ)),
2017-05-18 15:37:16 +02:00
Check: resource.ComposeTestCheckFunc(
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
testAccCheckLibratoMetricName(&metric, name),
2017-05-18 22:13:11 +02:00
testAccCheckLibratoMetricType(&metric, typ),
2017-05-18 15:37:16 +02:00
resource.TestCheckResourceAttr(
"librato_metric.foobar", "name", name),
),
},
{
PreConfig: sleep(t, 15),
2017-05-18 22:13:11 +02:00
Config: compositeMetricConfig(name, typ, fmt.Sprintf("An updated test %s metric", typ)),
2017-05-17 06:18:43 +02:00
Check: resource.ComposeTestCheckFunc(
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
testAccCheckLibratoMetricName(&metric, name),
2017-05-18 22:13:11 +02:00
testAccCheckLibratoMetricType(&metric, typ),
2017-05-17 06:18:43 +02:00
resource.TestCheckResourceAttr(
"librato_metric.foobar", "name", name),
),
},
},
})
}
func testAccCheckLibratoMetricDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*librato.Client)
for _, rs := range s.RootModule().Resources {
if rs.Type != "librato_metric" {
continue
}
2017-05-18 15:37:16 +02:00
fmt.Printf("*** testAccCheckLibratoMetricDestroy ID: %s\n", rs.Primary.ID)
2017-05-17 06:18:43 +02:00
_, _, err := client.Metrics.Get(rs.Primary.ID)
if err == nil {
return fmt.Errorf("Metric still exists")
}
}
return nil
}
func testAccCheckLibratoMetricName(metric *librato.Metric, name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if metric.Name == nil || *metric.Name != name {
return fmt.Errorf("Bad name: %s", *metric.Name)
}
return nil
}
}
2017-05-18 22:13:11 +02:00
func testAccCheckLibratoMetricType(metric *librato.Metric, wantType string) resource.TestCheckFunc {
2017-05-17 06:18:43 +02:00
return func(s *terraform.State) error {
2017-05-18 22:13:11 +02:00
if metric.Type == nil || *metric.Type != wantType {
2017-05-17 06:18:43 +02:00
return fmt.Errorf("Bad metric type: %s", *metric.Type)
}
return nil
}
}
func testAccCheckLibratoMetricExists(n string, metric *librato.Metric) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No Metric ID is set")
}
client := testAccProvider.Meta().(*librato.Client)
foundMetric, _, err := client.Metrics.Get(rs.Primary.ID)
if err != nil {
return err
}
if foundMetric.Name == nil || *foundMetric.Name != rs.Primary.ID {
return fmt.Errorf("Metric not found")
}
*metric = *foundMetric
return nil
}
}
2017-05-18 15:37:16 +02:00
func counterMetricConfig(name, typ, desc string) string {
return strings.TrimSpace(fmt.Sprintf(`
resource "librato_metric" "foobar" {
name = "%s"
type = "%s"
description = "%s"
attributes {
2017-05-18 22:13:11 +02:00
display_stacked = true
2017-05-18 15:37:16 +02:00
}
}`, name, typ, desc))
}
func gaugeMetricConfig(name, typ, desc string) string {
return strings.TrimSpace(fmt.Sprintf(`
resource "librato_metric" "foobar" {
name = "%s"
type = "%s"
description = "%s"
attributes {
2017-05-18 22:13:11 +02:00
display_stacked = true
2017-05-18 15:37:16 +02:00
}
}`, name, typ, desc))
}
func compositeMetricConfig(name, typ, desc string) string {
return strings.TrimSpace(fmt.Sprintf(`
resource "librato_metric" "foobar" {
name = "%s"
type = "%s"
description = "%s"
composite = "s(\"librato.cpu.percent.user\", {\"environment\" : \"prod\", \"service\": \"api\"})"
attributes {
2017-05-18 22:13:11 +02:00
display_stacked = true
2017-05-18 15:37:16 +02:00
}
}`, name, typ, desc))
}