terraform/builtin/providers/heroku/resource_heroku_addon_test.go

162 lines
4.1 KiB
Go
Raw Normal View History

2014-08-19 00:49:54 +02:00
package heroku
import (
"fmt"
"testing"
"github.com/cyberdelia/heroku-go/v3"
"github.com/hashicorp/terraform/helper/acctest"
2014-08-19 00:49:54 +02:00
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccHerokuAddon_Basic(t *testing.T) {
var addon heroku.Addon
appName := fmt.Sprintf("tftest-%s", acctest.RandString(10))
2014-08-19 00:49:54 +02:00
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckHerokuAddonDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckHerokuAddonConfig_basic(appName),
2014-08-19 00:49:54 +02:00
Check: resource.ComposeTestCheckFunc(
testAccCheckHerokuAddonExists("heroku_addon.foobar", &addon),
testAccCheckHerokuAddonAttributes(&addon, "deployhooks:http"),
2014-08-19 00:49:54 +02:00
resource.TestCheckResourceAttr(
"heroku_addon.foobar", "config.0.url", "http://google.com"),
resource.TestCheckResourceAttr(
"heroku_addon.foobar", "app", appName),
2014-08-19 00:49:54 +02:00
resource.TestCheckResourceAttr(
"heroku_addon.foobar", "plan", "deployhooks:http"),
),
},
},
})
}
// GH-198
func TestAccHerokuAddon_noPlan(t *testing.T) {
var addon heroku.Addon
appName := fmt.Sprintf("tftest-%s", acctest.RandString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckHerokuAddonDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckHerokuAddonConfig_no_plan(appName),
Check: resource.ComposeTestCheckFunc(
testAccCheckHerokuAddonExists("heroku_addon.foobar", &addon),
testAccCheckHerokuAddonAttributes(&addon, "memcachier:dev"),
resource.TestCheckResourceAttr(
"heroku_addon.foobar", "app", appName),
resource.TestCheckResourceAttr(
"heroku_addon.foobar", "plan", "memcachier"),
),
},
resource.TestStep{
Config: testAccCheckHerokuAddonConfig_no_plan(appName),
Check: resource.ComposeTestCheckFunc(
testAccCheckHerokuAddonExists("heroku_addon.foobar", &addon),
testAccCheckHerokuAddonAttributes(&addon, "memcachier:dev"),
resource.TestCheckResourceAttr(
"heroku_addon.foobar", "app", appName),
resource.TestCheckResourceAttr(
"heroku_addon.foobar", "plan", "memcachier"),
),
},
},
})
}
2014-08-19 00:49:54 +02:00
func testAccCheckHerokuAddonDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*heroku.Service)
2014-08-19 00:49:54 +02:00
for _, rs := range s.RootModule().Resources {
2014-08-19 00:49:54 +02:00
if rs.Type != "heroku_addon" {
continue
}
_, err := client.AddonInfo(rs.Primary.Attributes["app"], rs.Primary.ID)
2014-08-19 00:49:54 +02:00
if err == nil {
return fmt.Errorf("Addon still exists")
}
}
return nil
}
func testAccCheckHerokuAddonAttributes(addon *heroku.Addon, n string) resource.TestCheckFunc {
2014-08-19 00:49:54 +02:00
return func(s *terraform.State) error {
if addon.Plan.Name != n {
return fmt.Errorf("Bad plan: %s", addon.Plan.Name)
2014-08-19 00:49:54 +02:00
}
return nil
}
}
func testAccCheckHerokuAddonExists(n string, addon *heroku.Addon) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
2014-08-19 00:49:54 +02:00
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
2014-08-19 00:49:54 +02:00
return fmt.Errorf("No Addon ID is set")
}
client := testAccProvider.Meta().(*heroku.Service)
2014-08-19 00:49:54 +02:00
foundAddon, err := client.AddonInfo(rs.Primary.Attributes["app"], rs.Primary.ID)
2014-08-19 00:49:54 +02:00
if err != nil {
return err
}
if foundAddon.ID != rs.Primary.ID {
2014-08-19 00:49:54 +02:00
return fmt.Errorf("Addon not found")
}
*addon = *foundAddon
return nil
}
}
func testAccCheckHerokuAddonConfig_basic(appName string) string {
return fmt.Sprintf(`
2014-08-19 00:49:54 +02:00
resource "heroku_app" "foobar" {
name = "%s"
region = "us"
2014-08-19 00:49:54 +02:00
}
resource "heroku_addon" "foobar" {
app = "${heroku_app.foobar.name}"
plan = "deployhooks:http"
config {
url = "http://google.com"
}
}`, appName)
}
func testAccCheckHerokuAddonConfig_no_plan(appName string) string {
return fmt.Sprintf(`
resource "heroku_app" "foobar" {
name = "%s"
region = "us"
}
resource "heroku_addon" "foobar" {
app = "${heroku_app.foobar.name}"
plan = "memcachier"
}`, appName)
}