github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/heroku/resource_heroku_domain_test.go (about) 1 package heroku 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/cyberdelia/heroku-go/v3" 9 "github.com/hashicorp/terraform/helper/acctest" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccHerokuDomain_Basic(t *testing.T) { 15 var domain heroku.DomainInfoResult 16 appName := fmt.Sprintf("tftest-%s", acctest.RandString(10)) 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckHerokuDomainDestroy, 22 Steps: []resource.TestStep{ 23 { 24 Config: testAccCheckHerokuDomainConfig_basic(appName), 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckHerokuDomainExists("heroku_domain.foobar", &domain), 27 testAccCheckHerokuDomainAttributes(&domain), 28 resource.TestCheckResourceAttr( 29 "heroku_domain.foobar", "hostname", "terraform.example.com"), 30 resource.TestCheckResourceAttr( 31 "heroku_domain.foobar", "app", appName), 32 resource.TestCheckResourceAttr( 33 "heroku_domain.foobar", "cname", 34 fmt.Sprintf("%s.herokuapp.com", appName)), 35 ), 36 }, 37 }, 38 }) 39 } 40 41 func testAccCheckHerokuDomainDestroy(s *terraform.State) error { 42 client := testAccProvider.Meta().(*heroku.Service) 43 44 for _, rs := range s.RootModule().Resources { 45 if rs.Type != "heroku_domain" { 46 continue 47 } 48 49 _, err := client.DomainInfo(context.TODO(), rs.Primary.Attributes["app"], rs.Primary.ID) 50 51 if err == nil { 52 return fmt.Errorf("Domain still exists") 53 } 54 } 55 56 return nil 57 } 58 59 func testAccCheckHerokuDomainAttributes(Domain *heroku.DomainInfoResult) resource.TestCheckFunc { 60 return func(s *terraform.State) error { 61 62 if Domain.Hostname != "terraform.example.com" { 63 return fmt.Errorf("Bad hostname: %s", Domain.Hostname) 64 } 65 66 return nil 67 } 68 } 69 70 func testAccCheckHerokuDomainExists(n string, Domain *heroku.DomainInfoResult) resource.TestCheckFunc { 71 return func(s *terraform.State) error { 72 rs, ok := s.RootModule().Resources[n] 73 74 if !ok { 75 return fmt.Errorf("Not found: %s", n) 76 } 77 78 if rs.Primary.ID == "" { 79 return fmt.Errorf("No Domain ID is set") 80 } 81 82 client := testAccProvider.Meta().(*heroku.Service) 83 84 foundDomain, err := client.DomainInfo(context.TODO(), rs.Primary.Attributes["app"], rs.Primary.ID) 85 86 if err != nil { 87 return err 88 } 89 90 if foundDomain.ID != rs.Primary.ID { 91 return fmt.Errorf("Domain not found") 92 } 93 94 *Domain = *foundDomain 95 96 return nil 97 } 98 } 99 100 func testAccCheckHerokuDomainConfig_basic(appName string) string { 101 return fmt.Sprintf(`resource "heroku_app" "foobar" { 102 name = "%s" 103 region = "us" 104 } 105 106 resource "heroku_domain" "foobar" { 107 app = "${heroku_app.foobar.name}" 108 hostname = "terraform.example.com" 109 }`, appName) 110 }