github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/heroku/resource_heroku_space_test.go (about) 1 package heroku 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 "testing" 8 9 heroku "github.com/cyberdelia/heroku-go/v3" 10 "github.com/hashicorp/terraform/helper/acctest" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccHerokuSpace_Basic(t *testing.T) { 16 var space heroku.SpaceInfoResult 17 spaceName := fmt.Sprintf("tftest-%s", acctest.RandString(10)) 18 spaceName2 := fmt.Sprintf("tftest-%s", acctest.RandString(10)) 19 org := os.Getenv("HEROKU_ORGANIZATION") 20 21 resource.Test(t, resource.TestCase{ 22 PreCheck: func() { 23 testAccPreCheck(t) 24 if org == "" { 25 t.Skip("HEROKU_ORGANIZATION is not set; skipping test.") 26 } 27 }, 28 Providers: testAccProviders, 29 CheckDestroy: testAccCheckHerokuSpaceDestroy, 30 Steps: []resource.TestStep{ 31 { 32 Config: testAccCheckHerokuSpaceConfig_basic(spaceName, org), 33 Check: resource.ComposeTestCheckFunc( 34 testAccCheckHerokuSpaceExists("heroku_space.foobar", &space), 35 testAccCheckHerokuSpaceAttributes(&space, spaceName), 36 ), 37 }, 38 { 39 Config: testAccCheckHerokuSpaceConfig_basic(spaceName2, org), 40 Check: resource.ComposeTestCheckFunc( 41 testAccCheckHerokuSpaceExists("heroku_space.foobar", &space), 42 testAccCheckHerokuSpaceAttributes(&space, spaceName2), 43 ), 44 }, 45 }, 46 }) 47 } 48 49 func testAccCheckHerokuSpaceConfig_basic(spaceName, orgName string) string { 50 return fmt.Sprintf(` 51 resource "heroku_space" "foobar" { 52 name = "%s" 53 organization = "%s" 54 region = "virginia" 55 } 56 `, spaceName, orgName) 57 } 58 59 func testAccCheckHerokuSpaceExists(n string, space *heroku.SpaceInfoResult) resource.TestCheckFunc { 60 return func(s *terraform.State) error { 61 rs, ok := s.RootModule().Resources[n] 62 63 if !ok { 64 return fmt.Errorf("Not found: %s", n) 65 } 66 67 if rs.Primary.ID == "" { 68 return fmt.Errorf("No space name set") 69 } 70 71 client := testAccProvider.Meta().(*heroku.Service) 72 73 foundSpace, err := client.SpaceInfo(context.TODO(), rs.Primary.ID) 74 if err != nil { 75 return err 76 } 77 78 if foundSpace.ID != rs.Primary.ID { 79 return fmt.Errorf("Space not found") 80 } 81 82 *space = *foundSpace 83 84 return nil 85 } 86 } 87 88 func testAccCheckHerokuSpaceAttributes(space *heroku.SpaceInfoResult, spaceName string) resource.TestCheckFunc { 89 return func(s *terraform.State) error { 90 if space.Name != spaceName { 91 return fmt.Errorf("Bad name: %s", space.Name) 92 } 93 94 return nil 95 } 96 } 97 98 func testAccCheckHerokuSpaceDestroy(s *terraform.State) error { 99 client := testAccProvider.Meta().(*heroku.Service) 100 101 for _, rs := range s.RootModule().Resources { 102 if rs.Type != "heroku_space" { 103 continue 104 } 105 106 _, err := client.SpaceInfo(context.TODO(), rs.Primary.ID) 107 108 if err == nil { 109 return fmt.Errorf("Space still exists") 110 } 111 } 112 113 return nil 114 }