github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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 resource.TestStep{ 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 resource.TestStep{ 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 resource.TestStep{ 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 resource.TestStep{ 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 resource.TestStep{ 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 testAccCheckDigitalOceanRecordDestroy(s *terraform.State) error { 168 client := testAccProvider.Meta().(*godo.Client) 169 170 for _, rs := range s.RootModule().Resources { 171 if rs.Type != "digitalocean_record" { 172 continue 173 } 174 domain := rs.Primary.Attributes["domain"] 175 id, err := strconv.Atoi(rs.Primary.ID) 176 if err != nil { 177 return err 178 } 179 180 _, _, err = client.Domains.Record(domain, id) 181 182 if err == nil { 183 return fmt.Errorf("Record still exists") 184 } 185 } 186 187 return nil 188 } 189 190 func testAccCheckDigitalOceanRecordAttributes(record *godo.DomainRecord) resource.TestCheckFunc { 191 return func(s *terraform.State) error { 192 193 if record.Data != "192.168.0.10" { 194 return fmt.Errorf("Bad value: %s", record.Data) 195 } 196 197 return nil 198 } 199 } 200 201 func testAccCheckDigitalOceanRecordAttributesUpdated(record *godo.DomainRecord) resource.TestCheckFunc { 202 return func(s *terraform.State) error { 203 204 if record.Data != "192.168.0.11" { 205 return fmt.Errorf("Bad value: %s", record.Data) 206 } 207 208 return nil 209 } 210 } 211 212 func testAccCheckDigitalOceanRecordExists(n string, record *godo.DomainRecord) resource.TestCheckFunc { 213 return func(s *terraform.State) error { 214 rs, ok := s.RootModule().Resources[n] 215 216 if !ok { 217 return fmt.Errorf("Not found: %s", n) 218 } 219 220 if rs.Primary.ID == "" { 221 return fmt.Errorf("No Record ID is set") 222 } 223 224 client := testAccProvider.Meta().(*godo.Client) 225 226 domain := rs.Primary.Attributes["domain"] 227 id, err := strconv.Atoi(rs.Primary.ID) 228 if err != nil { 229 return err 230 } 231 232 foundRecord, _, err := client.Domains.Record(domain, id) 233 234 if err != nil { 235 return err 236 } 237 238 if strconv.Itoa(foundRecord.ID) != rs.Primary.ID { 239 return fmt.Errorf("Record not found") 240 } 241 242 *record = *foundRecord 243 244 return nil 245 } 246 } 247 248 func testAccCheckDigitalOceanRecordAttributesHostname(data string, record *godo.DomainRecord) resource.TestCheckFunc { 249 return func(s *terraform.State) error { 250 251 if record.Data != data { 252 return fmt.Errorf("Bad value: expected %s, got %s", data, record.Data) 253 } 254 255 return nil 256 } 257 } 258 259 const testAccCheckDigitalOceanRecordConfig_basic = ` 260 resource "digitalocean_domain" "foobar" { 261 name = "%s" 262 ip_address = "192.168.0.10" 263 } 264 265 resource "digitalocean_record" "foobar" { 266 domain = "${digitalocean_domain.foobar.name}" 267 268 name = "terraform" 269 value = "192.168.0.10" 270 type = "A" 271 }` 272 273 const testAccCheckDigitalOceanRecordConfig_new_value = ` 274 resource "digitalocean_domain" "foobar" { 275 name = "%s" 276 ip_address = "192.168.0.10" 277 } 278 279 resource "digitalocean_record" "foobar" { 280 domain = "${digitalocean_domain.foobar.name}" 281 282 name = "terraform" 283 value = "192.168.0.11" 284 type = "A" 285 }` 286 287 const testAccCheckDigitalOceanRecordConfig_cname = ` 288 resource "digitalocean_domain" "foobar" { 289 name = "%s" 290 ip_address = "192.168.0.10" 291 } 292 293 resource "digitalocean_record" "foobar" { 294 domain = "${digitalocean_domain.foobar.name}" 295 296 name = "terraform" 297 value = "a.foobar-test-terraform.com." 298 type = "CNAME" 299 }` 300 301 const testAccCheckDigitalOceanRecordConfig_external_cname = ` 302 resource "digitalocean_domain" "foobar" { 303 name = "%s" 304 ip_address = "192.168.0.10" 305 } 306 307 resource "digitalocean_record" "foobar" { 308 domain = "${digitalocean_domain.foobar.name}" 309 310 name = "terraform" 311 value = "a.foobar-test-terraform.net." 312 type = "CNAME" 313 }`