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