github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/heroku/resource_heroku_domain_test.go (about)

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