github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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 resource.TestCheckResourceAttr( 89 "digitalocean_record.foobar", "ttl", "1800"), 90 ), 91 }, 92 { 93 Config: fmt.Sprintf( 94 testAccCheckDigitalOceanRecordConfig_new_value, domain), 95 Check: resource.ComposeTestCheckFunc( 96 testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record), 97 testAccCheckDigitalOceanRecordAttributesUpdated(&record), 98 resource.TestCheckResourceAttr( 99 "digitalocean_record.foobar", "name", "terraform"), 100 resource.TestCheckResourceAttr( 101 "digitalocean_record.foobar", "domain", domain), 102 resource.TestCheckResourceAttr( 103 "digitalocean_record.foobar", "value", "192.168.0.11"), 104 resource.TestCheckResourceAttr( 105 "digitalocean_record.foobar", "type", "A"), 106 resource.TestCheckResourceAttr( 107 "digitalocean_record.foobar", "ttl", "90"), 108 ), 109 }, 110 }, 111 }) 112 } 113 114 func TestAccDigitalOceanRecord_HostnameValue(t *testing.T) { 115 var record godo.DomainRecord 116 domain := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10)) 117 118 resource.Test(t, resource.TestCase{ 119 PreCheck: func() { testAccPreCheck(t) }, 120 Providers: testAccProviders, 121 CheckDestroy: testAccCheckDigitalOceanRecordDestroy, 122 Steps: []resource.TestStep{ 123 { 124 Config: fmt.Sprintf( 125 testAccCheckDigitalOceanRecordConfig_cname, domain), 126 Check: resource.ComposeTestCheckFunc( 127 testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record), 128 testAccCheckDigitalOceanRecordAttributesHostname("a.foobar-test-terraform.com", &record), 129 resource.TestCheckResourceAttr( 130 "digitalocean_record.foobar", "name", "terraform"), 131 resource.TestCheckResourceAttr( 132 "digitalocean_record.foobar", "domain", domain), 133 resource.TestCheckResourceAttr( 134 "digitalocean_record.foobar", "value", "a.foobar-test-terraform.com."), 135 resource.TestCheckResourceAttr( 136 "digitalocean_record.foobar", "type", "CNAME"), 137 ), 138 }, 139 }, 140 }) 141 } 142 143 func TestAccDigitalOceanRecord_ExternalHostnameValue(t *testing.T) { 144 var record godo.DomainRecord 145 domain := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10)) 146 147 resource.Test(t, resource.TestCase{ 148 PreCheck: func() { testAccPreCheck(t) }, 149 Providers: testAccProviders, 150 CheckDestroy: testAccCheckDigitalOceanRecordDestroy, 151 Steps: []resource.TestStep{ 152 { 153 Config: fmt.Sprintf( 154 testAccCheckDigitalOceanRecordConfig_external_cname, domain), 155 Check: resource.ComposeTestCheckFunc( 156 testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record), 157 testAccCheckDigitalOceanRecordAttributesHostname("a.foobar-test-terraform.net", &record), 158 resource.TestCheckResourceAttr( 159 "digitalocean_record.foobar", "name", "terraform"), 160 resource.TestCheckResourceAttr( 161 "digitalocean_record.foobar", "domain", domain), 162 resource.TestCheckResourceAttr( 163 "digitalocean_record.foobar", "value", "a.foobar-test-terraform.net."), 164 resource.TestCheckResourceAttr( 165 "digitalocean_record.foobar", "type", "CNAME"), 166 ), 167 }, 168 }, 169 }) 170 } 171 172 func TestAccDigitalOceanRecord_MX(t *testing.T) { 173 var record godo.DomainRecord 174 domain := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10)) 175 176 resource.Test(t, resource.TestCase{ 177 PreCheck: func() { testAccPreCheck(t) }, 178 Providers: testAccProviders, 179 CheckDestroy: testAccCheckDigitalOceanRecordDestroy, 180 Steps: []resource.TestStep{ 181 { 182 Config: fmt.Sprintf( 183 testAccCheckDigitalOceanRecordConfig_mx, domain), 184 Check: resource.ComposeTestCheckFunc( 185 testAccCheckDigitalOceanRecordExists("digitalocean_record.foo_record", &record), 186 testAccCheckDigitalOceanRecordAttributesHostname("foobar."+domain, &record), 187 resource.TestCheckResourceAttr( 188 "digitalocean_record.foo_record", "name", "terraform"), 189 resource.TestCheckResourceAttr( 190 "digitalocean_record.foo_record", "domain", domain), 191 resource.TestCheckResourceAttr( 192 "digitalocean_record.foo_record", "value", "foobar."+domain+"."), 193 resource.TestCheckResourceAttr( 194 "digitalocean_record.foo_record", "type", "MX"), 195 ), 196 }, 197 }, 198 }) 199 } 200 201 func TestAccDigitalOceanRecord_MX_at(t *testing.T) { 202 var record godo.DomainRecord 203 domain := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10)) 204 205 resource.Test(t, resource.TestCase{ 206 PreCheck: func() { testAccPreCheck(t) }, 207 Providers: testAccProviders, 208 CheckDestroy: testAccCheckDigitalOceanRecordDestroy, 209 Steps: []resource.TestStep{ 210 { 211 Config: fmt.Sprintf( 212 testAccCheckDigitalOceanRecordConfig_mx_at, domain), 213 Check: resource.ComposeTestCheckFunc( 214 testAccCheckDigitalOceanRecordExists("digitalocean_record.foo_record", &record), 215 testAccCheckDigitalOceanRecordAttributesHostname("@", &record), 216 resource.TestCheckResourceAttr( 217 "digitalocean_record.foo_record", "name", "terraform"), 218 resource.TestCheckResourceAttr( 219 "digitalocean_record.foo_record", "domain", domain), 220 resource.TestCheckResourceAttr( 221 "digitalocean_record.foo_record", "value", domain+"."), 222 resource.TestCheckResourceAttr( 223 "digitalocean_record.foo_record", "type", "MX"), 224 ), 225 }, 226 }, 227 }) 228 } 229 230 func testAccCheckDigitalOceanRecordDestroy(s *terraform.State) error { 231 client := testAccProvider.Meta().(*godo.Client) 232 233 for _, rs := range s.RootModule().Resources { 234 if rs.Type != "digitalocean_record" { 235 continue 236 } 237 domain := rs.Primary.Attributes["domain"] 238 id, err := strconv.Atoi(rs.Primary.ID) 239 if err != nil { 240 return err 241 } 242 243 _, _, err = client.Domains.Record(context.Background(), domain, id) 244 245 if err == nil { 246 return fmt.Errorf("Record still exists") 247 } 248 } 249 250 return nil 251 } 252 253 func testAccCheckDigitalOceanRecordAttributes(record *godo.DomainRecord) resource.TestCheckFunc { 254 return func(s *terraform.State) error { 255 256 if record.Data != "192.168.0.10" { 257 return fmt.Errorf("Bad value: %s", record.Data) 258 } 259 260 return nil 261 } 262 } 263 264 func testAccCheckDigitalOceanRecordAttributesUpdated(record *godo.DomainRecord) resource.TestCheckFunc { 265 return func(s *terraform.State) error { 266 267 if record.Data != "192.168.0.11" { 268 return fmt.Errorf("Bad value: %s", record.Data) 269 } 270 271 return nil 272 } 273 } 274 275 func testAccCheckDigitalOceanRecordExists(n string, record *godo.DomainRecord) resource.TestCheckFunc { 276 return func(s *terraform.State) error { 277 rs, ok := s.RootModule().Resources[n] 278 279 if !ok { 280 return fmt.Errorf("Not found: %s", n) 281 } 282 283 if rs.Primary.ID == "" { 284 return fmt.Errorf("No Record ID is set") 285 } 286 287 client := testAccProvider.Meta().(*godo.Client) 288 289 domain := rs.Primary.Attributes["domain"] 290 id, err := strconv.Atoi(rs.Primary.ID) 291 if err != nil { 292 return err 293 } 294 295 foundRecord, _, err := client.Domains.Record(context.Background(), domain, id) 296 297 if err != nil { 298 return err 299 } 300 301 if strconv.Itoa(foundRecord.ID) != rs.Primary.ID { 302 return fmt.Errorf("Record not found") 303 } 304 305 *record = *foundRecord 306 307 return nil 308 } 309 } 310 311 func testAccCheckDigitalOceanRecordAttributesHostname(data string, record *godo.DomainRecord) resource.TestCheckFunc { 312 return func(s *terraform.State) error { 313 314 if record.Data != data { 315 return fmt.Errorf("Bad value: expected %s, got %s", data, record.Data) 316 } 317 318 return nil 319 } 320 } 321 322 const testAccCheckDigitalOceanRecordConfig_basic = ` 323 resource "digitalocean_domain" "foobar" { 324 name = "%s" 325 ip_address = "192.168.0.10" 326 } 327 328 resource "digitalocean_record" "foobar" { 329 domain = "${digitalocean_domain.foobar.name}" 330 331 name = "terraform" 332 value = "192.168.0.10" 333 type = "A" 334 }` 335 336 const testAccCheckDigitalOceanRecordConfig_new_value = ` 337 resource "digitalocean_domain" "foobar" { 338 name = "%s" 339 ip_address = "192.168.0.10" 340 } 341 342 resource "digitalocean_record" "foobar" { 343 domain = "${digitalocean_domain.foobar.name}" 344 345 name = "terraform" 346 value = "192.168.0.11" 347 type = "A" 348 ttl = 90 349 }` 350 351 const testAccCheckDigitalOceanRecordConfig_cname = ` 352 resource "digitalocean_domain" "foobar" { 353 name = "%s" 354 ip_address = "192.168.0.10" 355 } 356 357 resource "digitalocean_record" "foobar" { 358 domain = "${digitalocean_domain.foobar.name}" 359 360 name = "terraform" 361 value = "a.foobar-test-terraform.com." 362 type = "CNAME" 363 }` 364 365 const testAccCheckDigitalOceanRecordConfig_mx_at = ` 366 resource "digitalocean_domain" "foobar" { 367 name = "%s" 368 ip_address = "192.168.0.10" 369 } 370 371 resource "digitalocean_record" "foo_record" { 372 domain = "${digitalocean_domain.foobar.name}" 373 374 name = "terraform" 375 value = "${digitalocean_domain.foobar.name}." 376 type = "MX" 377 priority = "10" 378 }` 379 380 const testAccCheckDigitalOceanRecordConfig_mx = ` 381 resource "digitalocean_domain" "foobar" { 382 name = "%s" 383 ip_address = "192.168.0.10" 384 } 385 386 resource "digitalocean_record" "foo_record" { 387 domain = "${digitalocean_domain.foobar.name}" 388 389 name = "terraform" 390 value = "foobar.${digitalocean_domain.foobar.name}." 391 type = "MX" 392 priority = "10" 393 }` 394 395 const testAccCheckDigitalOceanRecordConfig_external_cname = ` 396 resource "digitalocean_domain" "foobar" { 397 name = "%s" 398 ip_address = "192.168.0.10" 399 } 400 401 resource "digitalocean_record" "foobar" { 402 domain = "${digitalocean_domain.foobar.name}" 403 404 name = "terraform" 405 value = "a.foobar-test-terraform.net." 406 type = "CNAME" 407 }`