github.com/bradfeehan/terraform@v0.7.0-rc3.0.20170529055808-34b45c5ad841/builtin/providers/digitalocean/resource_digitalocean_record_test.go (about)

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