2014-07-23 06:03:30 +02:00
|
|
|
package heroku
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2014-08-27 16:50:37 +02:00
|
|
|
"github.com/cyberdelia/heroku-go/v3"
|
2014-07-23 06:03:30 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccHerokuApp_Basic(t *testing.T) {
|
|
|
|
var app heroku.App
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckHerokuAppDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckHerokuAppConfig_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckHerokuAppExists("heroku_app.foobar", &app),
|
|
|
|
testAccCheckHerokuAppAttributes(&app),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"heroku_app.foobar", "name", "terraform-test-app"),
|
2014-07-23 17:09:05 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-07-23 18:11:58 +02:00
|
|
|
"heroku_app.foobar", "config_vars.0.FOO", "bar"),
|
2014-07-23 06:03:30 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-23 18:38:45 +02:00
|
|
|
func TestAccHerokuApp_NameChange(t *testing.T) {
|
|
|
|
var app heroku.App
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckHerokuAppDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckHerokuAppConfig_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckHerokuAppExists("heroku_app.foobar", &app),
|
|
|
|
testAccCheckHerokuAppAttributes(&app),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"heroku_app.foobar", "name", "terraform-test-app"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"heroku_app.foobar", "config_vars.0.FOO", "bar"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckHerokuAppConfig_updated,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckHerokuAppExists("heroku_app.foobar", &app),
|
|
|
|
testAccCheckHerokuAppAttributesUpdated(&app),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"heroku_app.foobar", "name", "terraform-test-renamed"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"heroku_app.foobar", "config_vars.0.FOO", "bing"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"heroku_app.foobar", "config_vars.0.BAZ", "bar"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-23 20:46:12 +02:00
|
|
|
func TestAccHerokuApp_NukeVars(t *testing.T) {
|
|
|
|
var app heroku.App
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckHerokuAppDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckHerokuAppConfig_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckHerokuAppExists("heroku_app.foobar", &app),
|
|
|
|
testAccCheckHerokuAppAttributes(&app),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"heroku_app.foobar", "name", "terraform-test-app"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"heroku_app.foobar", "config_vars.0.FOO", "bar"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckHerokuAppConfig_no_vars,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckHerokuAppExists("heroku_app.foobar", &app),
|
|
|
|
testAccCheckHerokuAppAttributesNoVars(&app),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"heroku_app.foobar", "name", "terraform-test-app"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"heroku_app.foobar", "config_vars.0.FOO", ""),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-23 06:03:30 +02:00
|
|
|
func testAccCheckHerokuAppDestroy(s *terraform.State) error {
|
2014-08-27 16:50:37 +02:00
|
|
|
client := testAccProvider.Meta().(*heroku.Service)
|
2014-07-23 06:03:30 +02:00
|
|
|
|
|
|
|
for _, rs := range s.Resources {
|
|
|
|
if rs.Type != "heroku_app" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := client.AppInfo(rs.ID)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("App still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckHerokuAppAttributes(app *heroku.App) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-08-27 16:50:37 +02:00
|
|
|
client := testAccProvider.Meta().(*heroku.Service)
|
2014-07-23 17:09:05 +02:00
|
|
|
|
|
|
|
if app.Region.Name != "us" {
|
|
|
|
return fmt.Errorf("Bad region: %s", app.Region.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if app.Stack.Name != "cedar" {
|
|
|
|
return fmt.Errorf("Bad stack: %s", app.Stack.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if app.Name != "terraform-test-app" {
|
|
|
|
return fmt.Errorf("Bad name: %s", app.Name)
|
|
|
|
}
|
2014-07-23 06:03:30 +02:00
|
|
|
|
2014-07-23 17:09:05 +02:00
|
|
|
vars, err := client.ConfigVarInfo(app.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-07-23 18:11:58 +02:00
|
|
|
if vars["FOO"] != "bar" {
|
2014-07-23 17:09:05 +02:00
|
|
|
return fmt.Errorf("Bad config vars: %v", vars)
|
|
|
|
}
|
2014-07-23 06:03:30 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-23 18:38:45 +02:00
|
|
|
func testAccCheckHerokuAppAttributesUpdated(app *heroku.App) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-08-27 16:50:37 +02:00
|
|
|
client := testAccProvider.Meta().(*heroku.Service)
|
2014-07-23 18:38:45 +02:00
|
|
|
|
|
|
|
if app.Name != "terraform-test-renamed" {
|
|
|
|
return fmt.Errorf("Bad name: %s", app.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
vars, err := client.ConfigVarInfo(app.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we kept the old one
|
|
|
|
if vars["FOO"] != "bing" {
|
|
|
|
return fmt.Errorf("Bad config vars: %v", vars)
|
|
|
|
}
|
|
|
|
|
|
|
|
if vars["BAZ"] != "bar" {
|
|
|
|
return fmt.Errorf("Bad config vars: %v", vars)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-23 20:46:12 +02:00
|
|
|
func testAccCheckHerokuAppAttributesNoVars(app *heroku.App) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-08-27 16:50:37 +02:00
|
|
|
client := testAccProvider.Meta().(*heroku.Service)
|
2014-07-23 20:46:12 +02:00
|
|
|
|
|
|
|
if app.Name != "terraform-test-app" {
|
|
|
|
return fmt.Errorf("Bad name: %s", app.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
vars, err := client.ConfigVarInfo(app.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(vars) != 0 {
|
|
|
|
return fmt.Errorf("vars exist: %v", vars)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-23 06:03:30 +02:00
|
|
|
func testAccCheckHerokuAppExists(n string, app *heroku.App) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.Resources[n]
|
2014-07-23 20:46:12 +02:00
|
|
|
|
2014-07-23 06:03:30 +02:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.ID == "" {
|
|
|
|
return fmt.Errorf("No App Name is set")
|
|
|
|
}
|
|
|
|
|
2014-08-27 16:50:37 +02:00
|
|
|
client := testAccProvider.Meta().(*heroku.Service)
|
2014-07-23 06:03:30 +02:00
|
|
|
|
|
|
|
foundApp, err := client.AppInfo(rs.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if foundApp.Name != rs.ID {
|
|
|
|
return fmt.Errorf("App not found")
|
|
|
|
}
|
|
|
|
|
2014-07-23 17:09:05 +02:00
|
|
|
*app = *foundApp
|
2014-07-23 06:03:30 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccCheckHerokuAppConfig_basic = `
|
|
|
|
resource "heroku_app" "foobar" {
|
2014-07-23 21:37:10 +02:00
|
|
|
name = "terraform-test-app"
|
2014-08-28 00:50:38 +02:00
|
|
|
region = "us"
|
2014-07-23 17:09:05 +02:00
|
|
|
|
2014-07-23 21:37:10 +02:00
|
|
|
config_vars {
|
|
|
|
FOO = "bar"
|
|
|
|
}
|
2014-07-23 06:03:30 +02:00
|
|
|
}`
|
2014-07-23 18:38:45 +02:00
|
|
|
|
|
|
|
const testAccCheckHerokuAppConfig_updated = `
|
|
|
|
resource "heroku_app" "foobar" {
|
2014-07-23 21:37:10 +02:00
|
|
|
name = "terraform-test-renamed"
|
2014-08-28 00:50:38 +02:00
|
|
|
region = "us"
|
2014-07-23 18:38:45 +02:00
|
|
|
|
2014-07-23 21:37:10 +02:00
|
|
|
config_vars {
|
|
|
|
FOO = "bing"
|
|
|
|
BAZ = "bar"
|
|
|
|
}
|
2014-07-23 18:38:45 +02:00
|
|
|
}`
|
2014-07-23 20:46:12 +02:00
|
|
|
|
|
|
|
const testAccCheckHerokuAppConfig_no_vars = `
|
|
|
|
resource "heroku_app" "foobar" {
|
2014-07-23 21:37:10 +02:00
|
|
|
name = "terraform-test-app"
|
2014-08-28 00:50:38 +02:00
|
|
|
region = "us"
|
2014-07-23 20:46:12 +02:00
|
|
|
}`
|