github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/dme/resource_dme_record_test.go (about) 1 package dme 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/soniah/dnsmadeeasy" 11 ) 12 13 var _ = fmt.Sprintf("dummy") // dummy 14 var _ = os.DevNull // dummy 15 16 func TestAccDMERecord_basic(t *testing.T) { 17 var record dnsmadeeasy.Record 18 domainid := os.Getenv("DME_DOMAINID") 19 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { testAccPreCheck(t) }, 22 Providers: testAccProviders, 23 CheckDestroy: testAccCheckDMERecordDestroy, 24 Steps: []resource.TestStep{ 25 resource.TestStep{ 26 Config: fmt.Sprintf(testDMERecordConfigA, domainid), 27 Check: resource.ComposeTestCheckFunc( 28 testAccCheckDMERecordExists("dme_record.test", &record), 29 resource.TestCheckResourceAttr( 30 "dme_record.test", "domainid", domainid), 31 resource.TestCheckResourceAttr( 32 "dme_record.test", "name", "testa"), 33 resource.TestCheckResourceAttr( 34 "dme_record.test", "type", "A"), 35 resource.TestCheckResourceAttr( 36 "dme_record.test", "value", "1.1.1.1"), 37 resource.TestCheckResourceAttr( 38 "dme_record.test", "ttl", "2000"), 39 resource.TestCheckResourceAttr( 40 "dme_record.test", "gtdLocation", "DEFAULT"), 41 ), 42 }, 43 }, 44 }) 45 } 46 47 func TestAccDMERecordCName(t *testing.T) { 48 var record dnsmadeeasy.Record 49 domainid := os.Getenv("DME_DOMAINID") 50 51 resource.Test(t, resource.TestCase{ 52 PreCheck: func() { testAccPreCheck(t) }, 53 Providers: testAccProviders, 54 CheckDestroy: testAccCheckDMERecordDestroy, 55 Steps: []resource.TestStep{ 56 resource.TestStep{ 57 Config: fmt.Sprintf(testDMERecordConfigCName, domainid), 58 Check: resource.ComposeTestCheckFunc( 59 testAccCheckDMERecordExists("dme_record.test", &record), 60 resource.TestCheckResourceAttr( 61 "dme_record.test", "domainid", domainid), 62 resource.TestCheckResourceAttr( 63 "dme_record.test", "name", "testcname"), 64 resource.TestCheckResourceAttr( 65 "dme_record.test", "type", "CNAME"), 66 resource.TestCheckResourceAttr( 67 "dme_record.test", "value", "foo"), 68 resource.TestCheckResourceAttr( 69 "dme_record.test", "ttl", "2000"), 70 resource.TestCheckResourceAttr( 71 "dme_record.test", "gtdLocation", "DEFAULT"), 72 ), 73 }, 74 }, 75 }) 76 } 77 78 /* 79 80 ANAME can't be tested under sandbox, as the value of the ANAME must be a 81 resolvable address. 82 83 func TestAccDMERecordAName(t *testing.T) { 84 var record dnsmadeeasy.Record 85 domainid := os.Getenv("DME_DOMAINID") 86 87 resource.Test(t, resource.TestCase{ 88 PreCheck: func() { testAccPreCheck(t) }, 89 Providers: testAccProviders, 90 CheckDestroy: testAccCheckDMERecordDestroy, 91 Steps: []resource.TestStep{ 92 resource.TestStep{ 93 Config: fmt.Sprintf(testDMERecordConfigAName, domainid), 94 Check: resource.ComposeTestCheckFunc( 95 testAccCheckDMERecordExists("dme_record.test", &record), 96 resource.TestCheckResourceAttr( 97 "dme_record.test", "domainid", domainid), 98 resource.TestCheckResourceAttr( 99 "dme_record.test", "name", "testaname"), 100 resource.TestCheckResourceAttr( 101 "dme_record.test", "type", "ANAME"), 102 resource.TestCheckResourceAttr( 103 "dme_record.test", "value", "foo"), 104 resource.TestCheckResourceAttr( 105 "dme_record.test", "ttl", "2000"), 106 ), 107 }, 108 }, 109 }) 110 } 111 */ 112 113 func TestAccDMERecordMX(t *testing.T) { 114 var record dnsmadeeasy.Record 115 domainid := os.Getenv("DME_DOMAINID") 116 117 resource.Test(t, resource.TestCase{ 118 PreCheck: func() { testAccPreCheck(t) }, 119 Providers: testAccProviders, 120 CheckDestroy: testAccCheckDMERecordDestroy, 121 Steps: []resource.TestStep{ 122 resource.TestStep{ 123 Config: fmt.Sprintf(testDMERecordConfigMX, domainid), 124 Check: resource.ComposeTestCheckFunc( 125 testAccCheckDMERecordExists("dme_record.test", &record), 126 resource.TestCheckResourceAttr( 127 "dme_record.test", "domainid", domainid), 128 resource.TestCheckResourceAttr( 129 "dme_record.test", "name", "testmx"), 130 resource.TestCheckResourceAttr( 131 "dme_record.test", "type", "MX"), 132 resource.TestCheckResourceAttr( 133 "dme_record.test", "value", "foo"), 134 resource.TestCheckResourceAttr( 135 "dme_record.test", "mxLevel", "10"), 136 resource.TestCheckResourceAttr( 137 "dme_record.test", "ttl", "2000"), 138 resource.TestCheckResourceAttr( 139 "dme_record.test", "gtdLocation", "DEFAULT"), 140 ), 141 }, 142 }, 143 }) 144 } 145 146 func TestAccDMERecordHTTPRED(t *testing.T) { 147 var record dnsmadeeasy.Record 148 domainid := os.Getenv("DME_DOMAINID") 149 150 resource.Test(t, resource.TestCase{ 151 PreCheck: func() { testAccPreCheck(t) }, 152 Providers: testAccProviders, 153 CheckDestroy: testAccCheckDMERecordDestroy, 154 Steps: []resource.TestStep{ 155 resource.TestStep{ 156 Config: fmt.Sprintf(testDMERecordConfigHTTPRED, domainid), 157 Check: resource.ComposeTestCheckFunc( 158 testAccCheckDMERecordExists("dme_record.test", &record), 159 resource.TestCheckResourceAttr( 160 "dme_record.test", "domainid", domainid), 161 resource.TestCheckResourceAttr( 162 "dme_record.test", "name", "testhttpred"), 163 resource.TestCheckResourceAttr( 164 "dme_record.test", "type", "HTTPRED"), 165 166 resource.TestCheckResourceAttr( 167 "dme_record.test", "value", "https://github.com/soniah/terraform-provider-dme"), 168 resource.TestCheckResourceAttr( 169 "dme_record.test", "hardLink", "true"), 170 resource.TestCheckResourceAttr( 171 "dme_record.test", "redirectType", "Hidden Frame Masked"), 172 resource.TestCheckResourceAttr( 173 "dme_record.test", "title", "An Example"), 174 resource.TestCheckResourceAttr( 175 "dme_record.test", "keywords", "terraform example"), 176 resource.TestCheckResourceAttr( 177 "dme_record.test", "description", "This is a description"), 178 179 resource.TestCheckResourceAttr( 180 "dme_record.test", "ttl", "2000"), 181 resource.TestCheckResourceAttr( 182 "dme_record.test", "gtdLocation", "DEFAULT"), 183 ), 184 }, 185 }, 186 }) 187 } 188 189 func TestAccDMERecordTXT(t *testing.T) { 190 var record dnsmadeeasy.Record 191 domainid := os.Getenv("DME_DOMAINID") 192 193 resource.Test(t, resource.TestCase{ 194 PreCheck: func() { testAccPreCheck(t) }, 195 Providers: testAccProviders, 196 CheckDestroy: testAccCheckDMERecordDestroy, 197 Steps: []resource.TestStep{ 198 resource.TestStep{ 199 Config: fmt.Sprintf(testDMERecordConfigTXT, domainid), 200 Check: resource.ComposeTestCheckFunc( 201 testAccCheckDMERecordExists("dme_record.test", &record), 202 resource.TestCheckResourceAttr( 203 "dme_record.test", "domainid", domainid), 204 resource.TestCheckResourceAttr( 205 "dme_record.test", "name", "testtxt"), 206 resource.TestCheckResourceAttr( 207 "dme_record.test", "type", "TXT"), 208 resource.TestCheckResourceAttr( 209 "dme_record.test", "value", "\"foo\""), 210 resource.TestCheckResourceAttr( 211 "dme_record.test", "ttl", "2000"), 212 resource.TestCheckResourceAttr( 213 "dme_record.test", "gtdLocation", "DEFAULT"), 214 ), 215 }, 216 }, 217 }) 218 } 219 220 func TestAccDMERecordSPF(t *testing.T) { 221 var record dnsmadeeasy.Record 222 domainid := os.Getenv("DME_DOMAINID") 223 224 resource.Test(t, resource.TestCase{ 225 PreCheck: func() { testAccPreCheck(t) }, 226 Providers: testAccProviders, 227 CheckDestroy: testAccCheckDMERecordDestroy, 228 Steps: []resource.TestStep{ 229 resource.TestStep{ 230 Config: fmt.Sprintf(testDMERecordConfigSPF, domainid), 231 Check: resource.ComposeTestCheckFunc( 232 testAccCheckDMERecordExists("dme_record.test", &record), 233 resource.TestCheckResourceAttr( 234 "dme_record.test", "domainid", domainid), 235 resource.TestCheckResourceAttr( 236 "dme_record.test", "name", "testspf"), 237 resource.TestCheckResourceAttr( 238 "dme_record.test", "type", "SPF"), 239 resource.TestCheckResourceAttr( 240 "dme_record.test", "value", "\"foo\""), 241 resource.TestCheckResourceAttr( 242 "dme_record.test", "ttl", "2000"), 243 resource.TestCheckResourceAttr( 244 "dme_record.test", "gtdLocation", "DEFAULT"), 245 ), 246 }, 247 }, 248 }) 249 } 250 251 func TestAccDMERecordPTR(t *testing.T) { 252 var record dnsmadeeasy.Record 253 domainid := os.Getenv("DME_DOMAINID") 254 255 resource.Test(t, resource.TestCase{ 256 PreCheck: func() { testAccPreCheck(t) }, 257 Providers: testAccProviders, 258 CheckDestroy: testAccCheckDMERecordDestroy, 259 Steps: []resource.TestStep{ 260 resource.TestStep{ 261 Config: fmt.Sprintf(testDMERecordConfigPTR, domainid), 262 Check: resource.ComposeTestCheckFunc( 263 testAccCheckDMERecordExists("dme_record.test", &record), 264 resource.TestCheckResourceAttr( 265 "dme_record.test", "domainid", domainid), 266 resource.TestCheckResourceAttr( 267 "dme_record.test", "name", "testptr"), 268 resource.TestCheckResourceAttr( 269 "dme_record.test", "type", "PTR"), 270 resource.TestCheckResourceAttr( 271 "dme_record.test", "value", "foo"), 272 resource.TestCheckResourceAttr( 273 "dme_record.test", "ttl", "2000"), 274 resource.TestCheckResourceAttr( 275 "dme_record.test", "gtdLocation", "DEFAULT"), 276 ), 277 }, 278 }, 279 }) 280 } 281 282 func TestAccDMERecordNS(t *testing.T) { 283 var record dnsmadeeasy.Record 284 domainid := os.Getenv("DME_DOMAINID") 285 286 resource.Test(t, resource.TestCase{ 287 PreCheck: func() { testAccPreCheck(t) }, 288 Providers: testAccProviders, 289 CheckDestroy: testAccCheckDMERecordDestroy, 290 Steps: []resource.TestStep{ 291 resource.TestStep{ 292 Config: fmt.Sprintf(testDMERecordConfigNS, domainid), 293 Check: resource.ComposeTestCheckFunc( 294 testAccCheckDMERecordExists("dme_record.test", &record), 295 resource.TestCheckResourceAttr( 296 "dme_record.test", "domainid", domainid), 297 resource.TestCheckResourceAttr( 298 "dme_record.test", "name", "testns"), 299 resource.TestCheckResourceAttr( 300 "dme_record.test", "type", "NS"), 301 resource.TestCheckResourceAttr( 302 "dme_record.test", "value", "foo"), 303 resource.TestCheckResourceAttr( 304 "dme_record.test", "ttl", "2000"), 305 resource.TestCheckResourceAttr( 306 "dme_record.test", "gtdLocation", "DEFAULT"), 307 ), 308 }, 309 }, 310 }) 311 } 312 313 func TestAccDMERecordAAAA(t *testing.T) { 314 var record dnsmadeeasy.Record 315 domainid := os.Getenv("DME_DOMAINID") 316 317 resource.Test(t, resource.TestCase{ 318 PreCheck: func() { testAccPreCheck(t) }, 319 Providers: testAccProviders, 320 CheckDestroy: testAccCheckDMERecordDestroy, 321 Steps: []resource.TestStep{ 322 resource.TestStep{ 323 Config: fmt.Sprintf(testDMERecordConfigAAAA, domainid), 324 Check: resource.ComposeTestCheckFunc( 325 testAccCheckDMERecordExists("dme_record.test", &record), 326 resource.TestCheckResourceAttr( 327 "dme_record.test", "domainid", domainid), 328 resource.TestCheckResourceAttr( 329 "dme_record.test", "name", "testaaaa"), 330 resource.TestCheckResourceAttr( 331 "dme_record.test", "type", "AAAA"), 332 resource.TestCheckResourceAttr( 333 "dme_record.test", "value", "fe80::0202:b3ff:fe1e:8329"), 334 resource.TestCheckResourceAttr( 335 "dme_record.test", "ttl", "2000"), 336 resource.TestCheckResourceAttr( 337 "dme_record.test", "gtdLocation", "DEFAULT"), 338 ), 339 }, 340 }, 341 }) 342 } 343 344 func TestAccDMERecordSRV(t *testing.T) { 345 var record dnsmadeeasy.Record 346 domainid := os.Getenv("DME_DOMAINID") 347 348 resource.Test(t, resource.TestCase{ 349 PreCheck: func() { testAccPreCheck(t) }, 350 Providers: testAccProviders, 351 CheckDestroy: testAccCheckDMERecordDestroy, 352 Steps: []resource.TestStep{ 353 resource.TestStep{ 354 Config: fmt.Sprintf(testDMERecordConfigSRV, domainid), 355 Check: resource.ComposeTestCheckFunc( 356 testAccCheckDMERecordExists("dme_record.test", &record), 357 resource.TestCheckResourceAttr( 358 "dme_record.test", "domainid", domainid), 359 resource.TestCheckResourceAttr( 360 "dme_record.test", "name", "testsrv"), 361 resource.TestCheckResourceAttr( 362 "dme_record.test", "type", "SRV"), 363 resource.TestCheckResourceAttr( 364 "dme_record.test", "value", "foo"), 365 resource.TestCheckResourceAttr( 366 "dme_record.test", "priority", "10"), 367 resource.TestCheckResourceAttr( 368 "dme_record.test", "weight", "20"), 369 resource.TestCheckResourceAttr( 370 "dme_record.test", "port", "30"), 371 resource.TestCheckResourceAttr( 372 "dme_record.test", "ttl", "2000"), 373 resource.TestCheckResourceAttr( 374 "dme_record.test", "gtdLocation", "DEFAULT"), 375 ), 376 }, 377 }, 378 }) 379 } 380 381 func testAccCheckDMERecordDestroy(s *terraform.State) error { 382 client := testAccProvider.Meta().(*dnsmadeeasy.Client) 383 384 for _, rs := range s.RootModule().Resources { 385 if rs.Type != "dnsmadeeasy_record" { 386 continue 387 } 388 389 _, err := client.ReadRecord(rs.Primary.Attributes["domainid"], rs.Primary.ID) 390 391 if err == nil { 392 return fmt.Errorf("Record still exists") 393 } 394 } 395 396 return nil 397 } 398 399 func testAccCheckDMERecordExists(n string, record *dnsmadeeasy.Record) resource.TestCheckFunc { 400 return func(s *terraform.State) error { 401 rs, ok := s.RootModule().Resources[n] 402 403 if !ok { 404 return fmt.Errorf("Not found: %s", n) 405 } 406 407 if rs.Primary.ID == "" { 408 return fmt.Errorf("No Record ID is set") 409 } 410 411 client := testAccProvider.Meta().(*dnsmadeeasy.Client) 412 413 foundRecord, err := client.ReadRecord(rs.Primary.Attributes["domainid"], rs.Primary.ID) 414 415 if err != nil { 416 return err 417 } 418 419 if foundRecord.StringRecordID() != rs.Primary.ID { 420 return fmt.Errorf("Record not found") 421 } 422 423 *record = *foundRecord 424 425 return nil 426 } 427 } 428 429 const testDMERecordConfigA = ` 430 resource "dme_record" "test" { 431 domainid = "%s" 432 name = "testa" 433 type = "A" 434 value = "1.1.1.1" 435 ttl = 2000 436 gtdLocation = "DEFAULT" 437 }` 438 439 const testDMERecordConfigCName = ` 440 resource "dme_record" "test" { 441 domainid = "%s" 442 name = "testcname" 443 type = "CNAME" 444 value = "foo" 445 ttl = 2000 446 gtdLocation = "DEFAULT" 447 }` 448 449 const testDMERecordConfigAName = ` 450 resource "dme_record" "test" { 451 domainid = "%s" 452 name = "testaname" 453 type = "ANAME" 454 value = "foo" 455 ttl = 2000 456 gtdLocation = "DEFAULT" 457 }` 458 459 const testDMERecordConfigMX = ` 460 resource "dme_record" "test" { 461 domainid = "%s" 462 name = "testmx" 463 type = "MX" 464 value = "foo" 465 mxLevel = 10 466 ttl = 2000 467 gtdLocation = "DEFAULT" 468 }` 469 470 const testDMERecordConfigHTTPRED = ` 471 resource "dme_record" "test" { 472 domainid = "%s" 473 name = "testhttpred" 474 type = "HTTPRED" 475 value = "https://github.com/soniah/terraform-provider-dme" 476 hardLink = true 477 redirectType = "Hidden Frame Masked" 478 title = "An Example" 479 keywords = "terraform example" 480 description = "This is a description" 481 ttl = 2000 482 gtdLocation = "DEFAULT" 483 }` 484 485 const testDMERecordConfigTXT = ` 486 resource "dme_record" "test" { 487 domainid = "%s" 488 name = "testtxt" 489 type = "TXT" 490 value = "foo" 491 ttl = 2000 492 gtdLocation = "DEFAULT" 493 }` 494 495 const testDMERecordConfigSPF = ` 496 resource "dme_record" "test" { 497 domainid = "%s" 498 name = "testspf" 499 type = "SPF" 500 value = "foo" 501 ttl = 2000 502 gtdLocation = "DEFAULT" 503 }` 504 505 const testDMERecordConfigPTR = ` 506 resource "dme_record" "test" { 507 domainid = "%s" 508 name = "testptr" 509 type = "PTR" 510 value = "foo" 511 ttl = 2000 512 gtdLocation = "DEFAULT" 513 }` 514 515 const testDMERecordConfigNS = ` 516 resource "dme_record" "test" { 517 domainid = "%s" 518 name = "testns" 519 type = "NS" 520 value = "foo" 521 ttl = 2000 522 gtdLocation = "DEFAULT" 523 }` 524 525 const testDMERecordConfigAAAA = ` 526 resource "dme_record" "test" { 527 domainid = "%s" 528 name = "testaaaa" 529 type = "AAAA" 530 value = "FE80::0202:B3FF:FE1E:8329" 531 ttl = 2000 532 gtdLocation = "DEFAULT" 533 }` 534 535 const testDMERecordConfigSRV = ` 536 resource "dme_record" "test" { 537 domainid = "%s" 538 name = "testsrv" 539 type = "SRV" 540 value = "foo" 541 priority = 10 542 weight = 20 543 port = 30 544 ttl = 2000 545 gtdLocation = "DEFAULT" 546 }`