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