2017-04-25 02:08:13 +02:00
|
|
|
package heroku
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
heroku "github.com/cyberdelia/heroku-go/v3"
|
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccHerokuSpace_Basic(t *testing.T) {
|
2017-05-24 13:04:17 +02:00
|
|
|
var space heroku.Space
|
2017-04-25 02:08:13 +02:00
|
|
|
spaceName := fmt.Sprintf("tftest-%s", acctest.RandString(10))
|
2017-04-25 02:34:34 +02:00
|
|
|
spaceName2 := fmt.Sprintf("tftest-%s", acctest.RandString(10))
|
2017-04-25 02:08:13 +02:00
|
|
|
org := os.Getenv("HEROKU_ORGANIZATION")
|
|
|
|
|
2017-06-08 18:57:31 +02:00
|
|
|
// HEROKU_SPACES_ORGANIZATION allows us to use a special Organization managed by Heroku for the
|
|
|
|
// strict purpose of testing Heroku Spaces. It has the following resource limits
|
|
|
|
// - 2 spaces
|
|
|
|
// - 2 apps per space
|
|
|
|
// - 2 dynos per space
|
|
|
|
spacesOrg := os.Getenv("HEROKU_SPACES_ORGANIZATION")
|
|
|
|
if spacesOrg != "" {
|
|
|
|
org = spacesOrg
|
|
|
|
}
|
|
|
|
|
2017-04-25 02:08:13 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() {
|
|
|
|
testAccPreCheck(t)
|
|
|
|
if org == "" {
|
|
|
|
t.Skip("HEROKU_ORGANIZATION is not set; skipping test.")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckHerokuSpaceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: testAccCheckHerokuSpaceConfig_basic(spaceName, org),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckHerokuSpaceExists("heroku_space.foobar", &space),
|
2017-04-25 02:34:34 +02:00
|
|
|
testAccCheckHerokuSpaceAttributes(&space, spaceName),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Config: testAccCheckHerokuSpaceConfig_basic(spaceName2, org),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckHerokuSpaceExists("heroku_space.foobar", &space),
|
|
|
|
testAccCheckHerokuSpaceAttributes(&space, spaceName2),
|
2017-04-25 02:08:13 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckHerokuSpaceConfig_basic(spaceName, orgName string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "heroku_space" "foobar" {
|
|
|
|
name = "%s"
|
|
|
|
organization = "%s"
|
|
|
|
region = "virginia"
|
|
|
|
}
|
|
|
|
`, spaceName, orgName)
|
|
|
|
}
|
|
|
|
|
2017-05-24 13:04:17 +02:00
|
|
|
func testAccCheckHerokuSpaceExists(n string, space *heroku.Space) resource.TestCheckFunc {
|
2017-04-25 02:08:13 +02:00
|
|
|
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 space name set")
|
|
|
|
}
|
|
|
|
|
|
|
|
client := testAccProvider.Meta().(*heroku.Service)
|
|
|
|
|
|
|
|
foundSpace, err := client.SpaceInfo(context.TODO(), rs.Primary.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-25 02:34:34 +02:00
|
|
|
if foundSpace.ID != rs.Primary.ID {
|
2017-04-25 02:08:13 +02:00
|
|
|
return fmt.Errorf("Space not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*space = *foundSpace
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-24 13:04:17 +02:00
|
|
|
func testAccCheckHerokuSpaceAttributes(space *heroku.Space, spaceName string) resource.TestCheckFunc {
|
2017-04-25 02:34:34 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
if space.Name != spaceName {
|
|
|
|
return fmt.Errorf("Bad name: %s", space.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 02:08:13 +02:00
|
|
|
func testAccCheckHerokuSpaceDestroy(s *terraform.State) error {
|
|
|
|
client := testAccProvider.Meta().(*heroku.Service)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "heroku_space" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := client.SpaceInfo(context.TODO(), rs.Primary.ID)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Space still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|