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

     1  package digitalocean
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/digitalocean/godo"
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccDigitalOceanDomain_Basic(t *testing.T) {
    14  	var domain godo.Domain
    15  	domainName := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10))
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckDigitalOceanDomainDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: fmt.Sprintf(testAccCheckDigitalOceanDomainConfig_basic, domainName),
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckDigitalOceanDomainExists("digitalocean_domain.foobar", &domain),
    26  					testAccCheckDigitalOceanDomainAttributes(&domain, domainName),
    27  					resource.TestCheckResourceAttr(
    28  						"digitalocean_domain.foobar", "name", domainName),
    29  					resource.TestCheckResourceAttr(
    30  						"digitalocean_domain.foobar", "ip_address", "192.168.0.10"),
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func testAccCheckDigitalOceanDomainDestroy(s *terraform.State) error {
    38  	client := testAccProvider.Meta().(*godo.Client)
    39  
    40  	for _, rs := range s.RootModule().Resources {
    41  		if rs.Type != "digitalocean_domain" {
    42  			continue
    43  		}
    44  
    45  		// Try to find the domain
    46  		_, _, err := client.Domains.Get(rs.Primary.ID)
    47  
    48  		if err == nil {
    49  			return fmt.Errorf("Domain still exists")
    50  		}
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  func testAccCheckDigitalOceanDomainAttributes(domain *godo.Domain, name string) resource.TestCheckFunc {
    57  	return func(s *terraform.State) error {
    58  
    59  		if domain.Name != name {
    60  			return fmt.Errorf("Bad name: %s", domain.Name)
    61  		}
    62  
    63  		return nil
    64  	}
    65  }
    66  
    67  func testAccCheckDigitalOceanDomainExists(n string, domain *godo.Domain) resource.TestCheckFunc {
    68  	return func(s *terraform.State) error {
    69  		rs, ok := s.RootModule().Resources[n]
    70  
    71  		if !ok {
    72  			return fmt.Errorf("Not found: %s", n)
    73  		}
    74  
    75  		if rs.Primary.ID == "" {
    76  			return fmt.Errorf("No Record ID is set")
    77  		}
    78  
    79  		client := testAccProvider.Meta().(*godo.Client)
    80  
    81  		foundDomain, _, err := client.Domains.Get(rs.Primary.ID)
    82  
    83  		if err != nil {
    84  			return err
    85  		}
    86  
    87  		if foundDomain.Name != rs.Primary.ID {
    88  			return fmt.Errorf("Record not found")
    89  		}
    90  
    91  		*domain = *foundDomain
    92  
    93  		return nil
    94  	}
    95  }
    96  
    97  const testAccCheckDigitalOceanDomainConfig_basic = `
    98  resource "digitalocean_domain" "foobar" {
    99  	name       = "%s"
   100  	ip_address = "192.168.0.10"
   101  }`