github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/dnsimple/resource_dnsimple_record_test.go (about)

     1  package dnsimple
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/pearkes/dnsimple"
    11  )
    12  
    13  func TestAccDNSimpleRecord_Basic(t *testing.T) {
    14  	var record dnsimple.Record
    15  	domain := os.Getenv("DNSIMPLE_DOMAIN")
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckDNSimpleRecordDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: fmt.Sprintf(testAccCheckDNSimpleRecordConfig_basic, domain),
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckDNSimpleRecordExists("dnsimple_record.foobar", &record),
    26  					testAccCheckDNSimpleRecordAttributes(&record),
    27  					resource.TestCheckResourceAttr(
    28  						"dnsimple_record.foobar", "name", "terraform"),
    29  					resource.TestCheckResourceAttr(
    30  						"dnsimple_record.foobar", "domain", domain),
    31  					resource.TestCheckResourceAttr(
    32  						"dnsimple_record.foobar", "value", "192.168.0.10"),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func TestAccDNSimpleRecord_Updated(t *testing.T) {
    40  	var record dnsimple.Record
    41  	domain := os.Getenv("DNSIMPLE_DOMAIN")
    42  
    43  	resource.Test(t, resource.TestCase{
    44  		PreCheck:     func() { testAccPreCheck(t) },
    45  		Providers:    testAccProviders,
    46  		CheckDestroy: testAccCheckDNSimpleRecordDestroy,
    47  		Steps: []resource.TestStep{
    48  			resource.TestStep{
    49  				Config: fmt.Sprintf(testAccCheckDNSimpleRecordConfig_basic, domain),
    50  				Check: resource.ComposeTestCheckFunc(
    51  					testAccCheckDNSimpleRecordExists("dnsimple_record.foobar", &record),
    52  					testAccCheckDNSimpleRecordAttributes(&record),
    53  					resource.TestCheckResourceAttr(
    54  						"dnsimple_record.foobar", "name", "terraform"),
    55  					resource.TestCheckResourceAttr(
    56  						"dnsimple_record.foobar", "domain", domain),
    57  					resource.TestCheckResourceAttr(
    58  						"dnsimple_record.foobar", "value", "192.168.0.10"),
    59  				),
    60  			},
    61  			resource.TestStep{
    62  				Config: fmt.Sprintf(testAccCheckDNSimpleRecordConfig_new_value, domain),
    63  				Check: resource.ComposeTestCheckFunc(
    64  					testAccCheckDNSimpleRecordExists("dnsimple_record.foobar", &record),
    65  					testAccCheckDNSimpleRecordAttributesUpdated(&record),
    66  					resource.TestCheckResourceAttr(
    67  						"dnsimple_record.foobar", "name", "terraform"),
    68  					resource.TestCheckResourceAttr(
    69  						"dnsimple_record.foobar", "domain", domain),
    70  					resource.TestCheckResourceAttr(
    71  						"dnsimple_record.foobar", "value", "192.168.0.11"),
    72  				),
    73  			},
    74  		},
    75  	})
    76  }
    77  
    78  func testAccCheckDNSimpleRecordDestroy(s *terraform.State) error {
    79  	client := testAccProvider.Meta().(*dnsimple.Client)
    80  
    81  	for _, rs := range s.RootModule().Resources {
    82  		if rs.Type != "dnsimple_record" {
    83  			continue
    84  		}
    85  
    86  		_, err := client.RetrieveRecord(rs.Primary.Attributes["domain"], rs.Primary.ID)
    87  
    88  		if err == nil {
    89  			return fmt.Errorf("Record still exists")
    90  		}
    91  	}
    92  
    93  	return nil
    94  }
    95  
    96  func testAccCheckDNSimpleRecordAttributes(record *dnsimple.Record) resource.TestCheckFunc {
    97  	return func(s *terraform.State) error {
    98  
    99  		if record.Content != "192.168.0.10" {
   100  			return fmt.Errorf("Bad content: %s", record.Content)
   101  		}
   102  
   103  		return nil
   104  	}
   105  }
   106  
   107  func testAccCheckDNSimpleRecordAttributesUpdated(record *dnsimple.Record) resource.TestCheckFunc {
   108  	return func(s *terraform.State) error {
   109  
   110  		if record.Content != "192.168.0.11" {
   111  			return fmt.Errorf("Bad content: %s", record.Content)
   112  		}
   113  
   114  		return nil
   115  	}
   116  }
   117  
   118  func testAccCheckDNSimpleRecordExists(n string, record *dnsimple.Record) resource.TestCheckFunc {
   119  	return func(s *terraform.State) error {
   120  		rs, ok := s.RootModule().Resources[n]
   121  
   122  		if !ok {
   123  			return fmt.Errorf("Not found: %s", n)
   124  		}
   125  
   126  		if rs.Primary.ID == "" {
   127  			return fmt.Errorf("No Record ID is set")
   128  		}
   129  
   130  		client := testAccProvider.Meta().(*dnsimple.Client)
   131  
   132  		foundRecord, err := client.RetrieveRecord(rs.Primary.Attributes["domain"], rs.Primary.ID)
   133  
   134  		if err != nil {
   135  			return err
   136  		}
   137  
   138  		if foundRecord.StringId() != rs.Primary.ID {
   139  			return fmt.Errorf("Record not found")
   140  		}
   141  
   142  		*record = *foundRecord
   143  
   144  		return nil
   145  	}
   146  }
   147  
   148  const testAccCheckDNSimpleRecordConfig_basic = `
   149  resource "dnsimple_record" "foobar" {
   150  	domain = "%s"
   151  
   152  	name = "terraform"
   153  	value = "192.168.0.10"
   154  	type = "A"
   155  	ttl = 3600
   156  }`
   157  
   158  const testAccCheckDNSimpleRecordConfig_new_value = `
   159  resource "dnsimple_record" "foobar" {
   160  	domain = "%s"
   161  
   162  	name = "terraform"
   163  	value = "192.168.0.11"
   164  	type = "A"
   165  	ttl = 3600
   166  }`