providers/heroku: add failing test for var removal

This commit is contained in:
Jack Pearkes 2014-07-23 14:46:12 -04:00
parent 2fc73493a8
commit 708d91a0c5
2 changed files with 77 additions and 4 deletions

View File

@ -120,11 +120,22 @@ func resource_heroku_app_update(
rs.ID = renamedApp.Name rs.ID = renamedApp.Name
} }
if attr, ok := d.Attributes["config_vars.#"]; ok && attr.New == "1" { attr, ok := s.Attributes["config_vars.#"]
// If the config var block was removed, nuke all config vars
if ok && attr == "1" {
vs := flatmap.Expand( vs := flatmap.Expand(
rs.Attributes, "config_vars").([]interface{}) rs.Attributes, "config_vars").([]interface{})
err := update_config_vars(rs.ID, vs, client) err := update_config_vars(rs.ID, vs, client)
if err != nil {
return rs, err
}
} else if ok && attr == "0" {
log.Println("[INFO] Config vars removed, removing all vars")
err := update_config_vars(rs.ID, make([]interface{}, 0), client)
if err != nil { if err != nil {
return rs, err return rs, err
} }
@ -265,6 +276,8 @@ func update_config_vars(id string, vs []interface{}, client *heroku.Client) erro
vars[k] = &val vars[k] = &val
} }
log.Printf("[INFO] Updating config vars: *%#v", vars)
_, err := client.ConfigVarUpdate(id, vars) _, err := client.ConfigVarUpdate(id, vars)
if err != nil { if err != nil {

View File

@ -68,6 +68,40 @@ func TestAccHerokuApp_NameChange(t *testing.T) {
}) })
} }
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", ""),
),
},
},
})
}
func testAccCheckHerokuAppDestroy(s *terraform.State) error { func testAccCheckHerokuAppDestroy(s *terraform.State) error {
client := testAccProvider.client client := testAccProvider.client
@ -142,10 +176,31 @@ func testAccCheckHerokuAppAttributesUpdated(app *heroku.App) resource.TestCheckF
} }
} }
func testAccCheckHerokuAppAttributesNoVars(app *heroku.App) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.client
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
}
}
func testAccCheckHerokuAppExists(n string, app *heroku.App) resource.TestCheckFunc { func testAccCheckHerokuAppExists(n string, app *heroku.App) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
rs, ok := s.Resources[n] rs, ok := s.Resources[n]
fmt.Printf("resources %#v", s.Resources)
if !ok { if !ok {
return fmt.Errorf("Not found: %s", n) return fmt.Errorf("Not found: %s", n)
} }
@ -176,7 +231,7 @@ const testAccCheckHerokuAppConfig_basic = `
resource "heroku_app" "foobar" { resource "heroku_app" "foobar" {
name = "terraform-test-app" name = "terraform-test-app"
config_vars = { config_vars {
FOO = bar FOO = bar
} }
}` }`
@ -185,8 +240,13 @@ const testAccCheckHerokuAppConfig_updated = `
resource "heroku_app" "foobar" { resource "heroku_app" "foobar" {
name = "terraform-test-renamed" name = "terraform-test-renamed"
config_vars = { config_vars {
FOO = bing FOO = bing
BAZ = bar BAZ = bar
} }
}` }`
const testAccCheckHerokuAppConfig_no_vars = `
resource "heroku_app" "foobar" {
name = "terraform-test-app"
}`