Skip to content

Commit

Permalink
provider/librato: Randomize the test case names as dangling resources…
Browse files Browse the repository at this point in the history
… were causing failures
  • Loading branch information
stack72 committed Sep 22, 2016
1 parent 40f4dd5 commit a2748d0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 38 deletions.
71 changes: 42 additions & 29 deletions resource_librato_alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,28 @@ import (
"strconv"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/henrikhodne/go-librato/librato"
)

func TestAccLibratoAlert_Basic(t *testing.T) {
var alert librato.Alert
name := acctest.RandString(10)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLibratoAlertDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckLibratoAlertConfig_basic,
{
Config: testAccCheckLibratoAlertConfig_basic(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckLibratoAlertExists("librato_alert.foobar", &alert),
testAccCheckLibratoAlertName(&alert, "FooBar"),
testAccCheckLibratoAlertName(&alert, name),
resource.TestCheckResourceAttr(
"librato_alert.foobar", "name", "FooBar"),
"librato_alert.foobar", "name", name),
),
},
},
Expand All @@ -33,19 +35,20 @@ func TestAccLibratoAlert_Basic(t *testing.T) {

func TestAccLibratoAlert_Full(t *testing.T) {
var alert librato.Alert
name := acctest.RandString(10)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLibratoAlertDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckLibratoAlertConfig_full,
{
Config: testAccCheckLibratoAlertConfig_full(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckLibratoAlertExists("librato_alert.foobar", &alert),
testAccCheckLibratoAlertName(&alert, "FooBar"),
testAccCheckLibratoAlertName(&alert, name),
resource.TestCheckResourceAttr(
"librato_alert.foobar", "name", "FooBar"),
"librato_alert.foobar", "name", name),
resource.TestCheckResourceAttr(
"librato_alert.foobar", "condition.836525194.metric_name", "librato.cpu.percent.idle"),
resource.TestCheckResourceAttr(
Expand All @@ -60,23 +63,24 @@ func TestAccLibratoAlert_Full(t *testing.T) {

func TestAccLibratoAlert_Updated(t *testing.T) {
var alert librato.Alert
name := acctest.RandString(10)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLibratoAlertDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckLibratoAlertConfig_basic,
{
Config: testAccCheckLibratoAlertConfig_basic(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckLibratoAlertExists("librato_alert.foobar", &alert),
testAccCheckLibratoAlertDescription(&alert, "A Test Alert"),
resource.TestCheckResourceAttr(
"librato_alert.foobar", "name", "FooBar"),
"librato_alert.foobar", "name", name),
),
},
resource.TestStep{
Config: testAccCheckLibratoAlertConfig_new_value,
{
Config: testAccCheckLibratoAlertConfig_new_value(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckLibratoAlertExists("librato_alert.foobar", &alert),
testAccCheckLibratoAlertDescription(&alert, "A modified Test Alert"),
Expand All @@ -90,19 +94,20 @@ func TestAccLibratoAlert_Updated(t *testing.T) {

func TestAccLibratoAlert_FullUpdate(t *testing.T) {
var alert librato.Alert
name := acctest.RandString(10)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLibratoAlertDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckLibratoAlertConfig_full_update,
{
Config: testAccCheckLibratoAlertConfig_full_update(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckLibratoAlertExists("librato_alert.foobar", &alert),
testAccCheckLibratoAlertName(&alert, "FooBar"),
testAccCheckLibratoAlertName(&alert, name),
resource.TestCheckResourceAttr(
"librato_alert.foobar", "name", "FooBar"),
"librato_alert.foobar", "name", name),
resource.TestCheckResourceAttr(
"librato_alert.foobar", "rearm_seconds", "1200"),
resource.TestCheckResourceAttr(
Expand Down Expand Up @@ -197,19 +202,24 @@ func testAccCheckLibratoAlertExists(n string, alert *librato.Alert) resource.Tes
}
}

const testAccCheckLibratoAlertConfig_basic = `
func testAccCheckLibratoAlertConfig_basic(name string) string {
return fmt.Sprintf(`
resource "librato_alert" "foobar" {
name = "FooBar"
name = "%s"
description = "A Test Alert"
}`
}`, name)
}

const testAccCheckLibratoAlertConfig_new_value = `
func testAccCheckLibratoAlertConfig_new_value(name string) string {
return fmt.Sprintf(`
resource "librato_alert" "foobar" {
name = "FooBar"
name = "%s"
description = "A modified Test Alert"
}`
}`, name)
}

const testAccCheckLibratoAlertConfig_full = `
func testAccCheckLibratoAlertConfig_full(name string) string {
return fmt.Sprintf(`
resource "librato_service" "foobar" {
title = "Foo Bar"
type = "mail"
Expand All @@ -221,7 +231,7 @@ EOF
}
resource "librato_alert" "foobar" {
name = "FooBar"
name = "%s"
description = "A Test Alert"
services = [ "${librato_service.foobar.id}" ]
condition {
Expand All @@ -235,9 +245,11 @@ resource "librato_alert" "foobar" {
}
active = false
rearm_seconds = 300
}`
}`, name)
}

const testAccCheckLibratoAlertConfig_full_update = `
func testAccCheckLibratoAlertConfig_full_update(name string) string {
return fmt.Sprintf(`
resource "librato_service" "foobar" {
title = "Foo Bar"
type = "mail"
Expand All @@ -249,7 +261,7 @@ EOF
}
resource "librato_alert" "foobar" {
name = "FooBar"
name = "%s"
description = "A Test Alert"
services = [ "${librato_service.foobar.id}" ]
condition {
Expand All @@ -263,4 +275,5 @@ resource "librato_alert" "foobar" {
}
active = false
rearm_seconds = 1200
}`
}`, name)
}
22 changes: 13 additions & 9 deletions resource_librato_space_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,28 @@ import (
"strconv"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/henrikhodne/go-librato/librato"
)

func TestAccLibratoSpace_Basic(t *testing.T) {
var space librato.Space
name := acctest.RandString(10)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLibratoSpaceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckLibratoSpaceConfig_basic,
{
Config: testAccCheckLibratoSpaceConfig_basic(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckLibratoSpaceExists("librato_space.foobar", &space),
testAccCheckLibratoSpaceAttributes(&space),
testAccCheckLibratoSpaceAttributes(&space, name),
resource.TestCheckResourceAttr(
"librato_space.foobar", "name", "Foo Bar"),
"librato_space.foobar", "name", name),
),
},
},
Expand Down Expand Up @@ -54,10 +56,10 @@ func testAccCheckLibratoSpaceDestroy(s *terraform.State) error {
return nil
}

func testAccCheckLibratoSpaceAttributes(space *librato.Space) resource.TestCheckFunc {
func testAccCheckLibratoSpaceAttributes(space *librato.Space, name string) resource.TestCheckFunc {
return func(s *terraform.State) error {

if space.Name == nil || *space.Name != "Foo Bar" {
if space.Name == nil || *space.Name != name {
return fmt.Errorf("Bad name: %s", *space.Name)
}

Expand Down Expand Up @@ -100,7 +102,9 @@ func testAccCheckLibratoSpaceExists(n string, space *librato.Space) resource.Tes
}
}

const testAccCheckLibratoSpaceConfig_basic = `
func testAccCheckLibratoSpaceConfig_basic(name string) string {
return fmt.Sprintf(`
resource "librato_space" "foobar" {
name = "Foo Bar"
}`
name = "%s"
}`, name)
}

0 comments on commit a2748d0

Please sign in to comment.