github.com/jrperritt/terraform@v0.1.1-0.20170525065507-96f391dafc38/builtin/providers/digitalocean/resource_digitalocean_record_test.go (about)

     1  package digitalocean
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"strings"
     9  
    10  	"github.com/digitalocean/godo"
    11  	"github.com/hashicorp/terraform/helper/acctest"
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  	"github.com/hashicorp/terraform/terraform"
    14  )
    15  
    16  func TestDigitalOceanRecordConstructFqdn(t *testing.T) {
    17  	cases := []struct {
    18  		Input, Output string
    19  	}{
    20  		{"www", "www.nonexample.com"},
    21  		{"dev.www", "dev.www.nonexample.com"},
    22  		{"*", "*.nonexample.com"},
    23  		{"nonexample.com", "nonexample.com"},
    24  		{"test.nonexample.com", "test.nonexample.com"},
    25  		{"test.nonexample.com.", "test.nonexample.com"},
    26  	}
    27  
    28  	domain := "nonexample.com"
    29  	for _, tc := range cases {
    30  		actual := constructFqdn(tc.Input, domain)
    31  		if actual != tc.Output {
    32  			t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
    33  		}
    34  	}
    35  }
    36  
    37  func TestAccDigitalOceanRecord_Basic(t *testing.T) {
    38  	var record godo.DomainRecord
    39  	domain := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10))
    40  
    41  	resource.Test(t, resource.TestCase{
    42  		PreCheck:     func() { testAccPreCheck(t) },
    43  		Providers:    testAccProviders,
    44  		CheckDestroy: testAccCheckDigitalOceanRecordDestroy,
    45  		Steps: []resource.TestStep{
    46  			{
    47  				Config: fmt.Sprintf(testAccCheckDigitalOceanRecordConfig_basic, domain),
    48  				Check: resource.ComposeTestCheckFunc(
    49  					testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record),
    50  					testAccCheckDigitalOceanRecordAttributes(&record),
    51  					resource.TestCheckResourceAttr(
    52  						"digitalocean_record.foobar", "name", "terraform"),
    53  					resource.TestCheckResourceAttr(
    54  						"digitalocean_record.foobar", "domain", domain),
    55  					resource.TestCheckResourceAttr(
    56  						"digitalocean_record.foobar", "value", "192.168.0.10"),
    57  					resource.TestCheckResourceAttr(
    58  						"digitalocean_record.foobar", "fqdn", strings.Join([]string{"terraform", domain}, ".")),
    59  				),
    60  			},
    61  		},
    62  	})
    63  }
    64  
    65  func TestAccDigitalOceanRecord_Updated(t *testing.T) {
    66  	var record godo.DomainRecord
    67  	domain := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10))
    68  
    69  	resource.Test(t, resource.TestCase{
    70  		PreCheck:     func() { testAccPreCheck(t) },
    71  		Providers:    testAccProviders,
    72  		CheckDestroy: testAccCheckDigitalOceanRecordDestroy,
    73  		Steps: []resource.TestStep{
    74  			{
    75  				Config: fmt.Sprintf(testAccCheckDigitalOceanRecordConfig_basic, domain),
    76  				Check: resource.ComposeTestCheckFunc(
    77  					testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record),
    78  					testAccCheckDigitalOceanRecordAttributes(&record),
    79  					resource.TestCheckResourceAttr(
    80  						"digitalocean_record.foobar", "name", "terraform"),
    81  					resource.TestCheckResourceAttr(
    82  						"digitalocean_record.foobar", "domain", domain),
    83  					resource.TestCheckResourceAttr(
    84  						"digitalocean_record.foobar", "value", "192.168.0.10"),
    85  					resource.TestCheckResourceAttr(
    86  						"digitalocean_record.foobar", "type", "A"),
    87  				),
    88  			},
    89  			{
    90  				Config: fmt.Sprintf(
    91  					testAccCheckDigitalOceanRecordConfig_new_value, domain),
    92  				Check: resource.ComposeTestCheckFunc(
    93  					testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record),
    94  					testAccCheckDigitalOceanRecordAttributesUpdated(&record),
    95  					resource.TestCheckResourceAttr(
    96  						"digitalocean_record.foobar", "name", "terraform"),
    97  					resource.TestCheckResourceAttr(
    98  						"digitalocean_record.foobar", "domain", domain),
    99  					resource.TestCheckResourceAttr(
   100  						"digitalocean_record.foobar", "value", "192.168.0.11"),
   101  					resource.TestCheckResourceAttr(
   102  						"digitalocean_record.foobar", "type", "A"),
   103  				),
   104  			},
   105  		},
   106  	})
   107  }
   108  
   109  func TestAccDigitalOceanRecord_HostnameValue(t *testing.T) {
   110  	var record godo.DomainRecord
   111  	domain := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10))
   112  
   113  	resource.Test(t, resource.TestCase{
   114  		PreCheck:     func() { testAccPreCheck(t) },
   115  		Providers:    testAccProviders,
   116  		CheckDestroy: testAccCheckDigitalOceanRecordDestroy,
   117  		Steps: []resource.TestStep{
   118  			{
   119  				Config: fmt.Sprintf(
   120  					testAccCheckDigitalOceanRecordConfig_cname, domain),
   121  				Check: resource.ComposeTestCheckFunc(
   122  					testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record),
   123  					testAccCheckDigitalOceanRecordAttributesHostname("a.foobar-test-terraform.com", &record),
   124  					resource.TestCheckResourceAttr(
   125  						"digitalocean_record.foobar", "name", "terraform"),
   126  					resource.TestCheckResourceAttr(
   127  						"digitalocean_record.foobar", "domain", domain),
   128  					resource.TestCheckResourceAttr(
   129  						"digitalocean_record.foobar", "value", "a.foobar-test-terraform.com."),
   130  					resource.TestCheckResourceAttr(
   131  						"digitalocean_record.foobar", "type", "CNAME"),
   132  				),
   133  			},
   134  		},
   135  	})
   136  }
   137  
   138  func TestAccDigitalOceanRecord_ExternalHostnameValue(t *testing.T) {
   139  	var record godo.DomainRecord
   140  	domain := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10))
   141  
   142  	resource.Test(t, resource.TestCase{
   143  		PreCheck:     func() { testAccPreCheck(t) },
   144  		Providers:    testAccProviders,
   145  		CheckDestroy: testAccCheckDigitalOceanRecordDestroy,
   146  		Steps: []resource.TestStep{
   147  			{
   148  				Config: fmt.Sprintf(
   149  					testAccCheckDigitalOceanRecordConfig_external_cname, domain),
   150  				Check: resource.ComposeTestCheckFunc(
   151  					testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record),
   152  					testAccCheckDigitalOceanRecordAttributesHostname("a.foobar-test-terraform.net", &record),
   153  					resource.TestCheckResourceAttr(
   154  						"digitalocean_record.foobar", "name", "terraform"),
   155  					resource.TestCheckResourceAttr(
   156  						"digitalocean_record.foobar", "domain", domain),
   157  					resource.TestCheckResourceAttr(
   158  						"digitalocean_record.foobar", "value", "a.foobar-test-terraform.net."),
   159  					resource.TestCheckResourceAttr(
   160  						"digitalocean_record.foobar", "type", "CNAME"),
   161  				),
   162  			},
   163  		},
   164  	})
   165  }
   166  
   167  func TestAccDigitalOceanRecord_MX(t *testing.T) {
   168  	var record godo.DomainRecord
   169  	domain := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10))
   170  
   171  	resource.Test(t, resource.TestCase{
   172  		PreCheck:     func() { testAccPreCheck(t) },
   173  		Providers:    testAccProviders,
   174  		CheckDestroy: testAccCheckDigitalOceanRecordDestroy,
   175  		Steps: []resource.TestStep{
   176  			{
   177  				Config: fmt.Sprintf(
   178  					testAccCheckDigitalOceanRecordConfig_mx, domain),
   179  				Check: resource.ComposeTestCheckFunc(
   180  					testAccCheckDigitalOceanRecordExists("digitalocean_record.foo_record", &record),
   181  					testAccCheckDigitalOceanRecordAttributesHostname("foobar."+domain, &record),
   182  					resource.TestCheckResourceAttr(
   183  						"digitalocean_record.foo_record", "name", "terraform"),
   184  					resource.TestCheckResourceAttr(
   185  						"digitalocean_record.foo_record", "domain", domain),
   186  					resource.TestCheckResourceAttr(
   187  						"digitalocean_record.foo_record", "value", "foobar."+domain+"."),
   188  					resource.TestCheckResourceAttr(
   189  						"digitalocean_record.foo_record", "type", "MX"),
   190  				),
   191  			},
   192  		},
   193  	})
   194  }
   195  
   196  func TestAccDigitalOceanRecord_MX_at(t *testing.T) {
   197  	var record godo.DomainRecord
   198  	domain := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10))
   199  
   200  	resource.Test(t, resource.TestCase{
   201  		PreCheck:     func() { testAccPreCheck(t) },
   202  		Providers:    testAccProviders,
   203  		CheckDestroy: testAccCheckDigitalOceanRecordDestroy,
   204  		Steps: []resource.TestStep{
   205  			{
   206  				Config: fmt.Sprintf(
   207  					testAccCheckDigitalOceanRecordConfig_mx_at, domain),
   208  				Check: resource.ComposeTestCheckFunc(
   209  					testAccCheckDigitalOceanRecordExists("digitalocean_record.foo_record", &record),
   210  					testAccCheckDigitalOceanRecordAttributesHostname("@", &record),
   211  					resource.TestCheckResourceAttr(
   212  						"digitalocean_record.foo_record", "name", "terraform"),
   213  					resource.TestCheckResourceAttr(
   214  						"digitalocean_record.foo_record", "domain", domain),
   215  					resource.TestCheckResourceAttr(
   216  						"digitalocean_record.foo_record", "value", domain+"."),
   217  					resource.TestCheckResourceAttr(
   218  						"digitalocean_record.foo_record", "type", "MX"),
   219  				),
   220  			},
   221  		},
   222  	})
   223  }
   224  
   225  func testAccCheckDigitalOceanRecordDestroy(s *terraform.State) error {
   226  	client := testAccProvider.Meta().(*godo.Client)
   227  
   228  	for _, rs := range s.RootModule().Resources {
   229  		if rs.Type != "digitalocean_record" {
   230  			continue
   231  		}
   232  		domain := rs.Primary.Attributes["domain"]
   233  		id, err := strconv.Atoi(rs.Primary.ID)
   234  		if err != nil {
   235  			return err
   236  		}
   237  
   238  		_, _, err = client.Domains.Record(domain, id)
   239  
   240  		if err == nil {
   241  			return fmt.Errorf("Record still exists")
   242  		}
   243  	}
   244  
   245  	return nil
   246  }
   247  
   248  func testAccCheckDigitalOceanRecordAttributes(record *godo.DomainRecord) resource.TestCheckFunc {
   249  	return func(s *terraform.State) error {
   250  
   251  		if record.Data != "192.168.0.10" {
   252  			return fmt.Errorf("Bad value: %s", record.Data)
   253  		}
   254  
   255  		return nil
   256  	}
   257  }
   258  
   259  func testAccCheckDigitalOceanRecordAttributesUpdated(record *godo.DomainRecord) resource.TestCheckFunc {
   260  	return func(s *terraform.State) error {
   261  
   262  		if record.Data != "192.168.0.11" {
   263  			return fmt.Errorf("Bad value: %s", record.Data)
   264  		}
   265  
   266  		return nil
   267  	}
   268  }
   269  
   270  func testAccCheckDigitalOceanRecordExists(n string, record *godo.DomainRecord) resource.TestCheckFunc {
   271  	return func(s *terraform.State) error {
   272  		rs, ok := s.RootModule().Resources[n]
   273  
   274  		if !ok {
   275  			return fmt.Errorf("Not found: %s", n)
   276  		}
   277  
   278  		if rs.Primary.ID == "" {
   279  			return fmt.Errorf("No Record ID is set")
   280  		}
   281  
   282  		client := testAccProvider.Meta().(*godo.Client)
   283  
   284  		domain := rs.Primary.Attributes["domain"]
   285  		id, err := strconv.Atoi(rs.Primary.ID)
   286  		if err != nil {
   287  			return err
   288  		}
   289  
   290  		foundRecord, _, err := client.Domains.Record(domain, id)
   291  
   292  		if err != nil {
   293  			return err
   294  		}
   295  
   296  		if strconv.Itoa(foundRecord.ID) != rs.Primary.ID {
   297  			return fmt.Errorf("Record not found")
   298  		}
   299  
   300  		*record = *foundRecord
   301  
   302  		return nil
   303  	}
   304  }
   305  
   306  func testAccCheckDigitalOceanRecordAttributesHostname(data string, record *godo.DomainRecord) resource.TestCheckFunc {
   307  	return func(s *terraform.State) error {
   308  
   309  		if record.Data != data {
   310  			return fmt.Errorf("Bad value: expected %s, got %s", data, record.Data)
   311  		}
   312  
   313  		return nil
   314  	}
   315  }
   316  
   317  const testAccCheckDigitalOceanRecordConfig_basic = `
   318  resource "digitalocean_domain" "foobar" {
   319    name       = "%s"
   320    ip_address = "192.168.0.10"
   321  }
   322  
   323  resource "digitalocean_record" "foobar" {
   324    domain = "${digitalocean_domain.foobar.name}"
   325  
   326    name  = "terraform"
   327    value = "192.168.0.10"
   328    type  = "A"
   329  }`
   330  
   331  const testAccCheckDigitalOceanRecordConfig_new_value = `
   332  resource "digitalocean_domain" "foobar" {
   333    name       = "%s"
   334    ip_address = "192.168.0.10"
   335  }
   336  
   337  resource "digitalocean_record" "foobar" {
   338    domain = "${digitalocean_domain.foobar.name}"
   339  
   340    name  = "terraform"
   341    value = "192.168.0.11"
   342    type  = "A"
   343  }`
   344  
   345  const testAccCheckDigitalOceanRecordConfig_cname = `
   346  resource "digitalocean_domain" "foobar" {
   347    name       = "%s"
   348    ip_address = "192.168.0.10"
   349  }
   350  
   351  resource "digitalocean_record" "foobar" {
   352    domain = "${digitalocean_domain.foobar.name}"
   353  
   354    name  = "terraform"
   355    value = "a.foobar-test-terraform.com."
   356    type  = "CNAME"
   357  }`
   358  
   359  const testAccCheckDigitalOceanRecordConfig_mx_at = `
   360  resource "digitalocean_domain" "foobar" {
   361    name       = "%s"
   362    ip_address = "192.168.0.10"
   363  }
   364  
   365  resource "digitalocean_record" "foo_record" {
   366    domain = "${digitalocean_domain.foobar.name}"
   367  
   368    name  = "terraform"
   369    value = "${digitalocean_domain.foobar.name}."
   370    type  = "MX"
   371    priority = "10"
   372  }`
   373  
   374  const testAccCheckDigitalOceanRecordConfig_mx = `
   375  resource "digitalocean_domain" "foobar" {
   376    name       = "%s"
   377    ip_address = "192.168.0.10"
   378  }
   379  
   380  resource "digitalocean_record" "foo_record" {
   381    domain = "${digitalocean_domain.foobar.name}"
   382  
   383    name  = "terraform"
   384    value = "foobar.${digitalocean_domain.foobar.name}."
   385    type  = "MX"
   386    priority = "10"
   387  }`
   388  
   389  const testAccCheckDigitalOceanRecordConfig_external_cname = `
   390  resource "digitalocean_domain" "foobar" {
   391    name       = "%s"
   392    ip_address = "192.168.0.10"
   393  }
   394  
   395  resource "digitalocean_record" "foobar" {
   396    domain = "${digitalocean_domain.foobar.name}"
   397  
   398    name  = "terraform"
   399    value = "a.foobar-test-terraform.net."
   400    type  = "CNAME"
   401  }`