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