github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/powerdns/resource_powerdns_record_test.go (about) 1 package powerdns 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 ) 10 11 func TestAccPDNSRecord_A(t *testing.T) { 12 resource.Test(t, resource.TestCase{ 13 PreCheck: func() { testAccPreCheck(t) }, 14 Providers: testAccProviders, 15 CheckDestroy: testAccCheckPDNSRecordDestroy, 16 Steps: []resource.TestStep{ 17 { 18 Config: testPDNSRecordConfigA, 19 Check: resource.ComposeTestCheckFunc( 20 testAccCheckPDNSRecordExists("powerdns_record.test-a"), 21 ), 22 }, 23 }, 24 }) 25 } 26 27 func TestAccPDNSRecord_WithCount(t *testing.T) { 28 resource.Test(t, resource.TestCase{ 29 PreCheck: func() { testAccPreCheck(t) }, 30 Providers: testAccProviders, 31 CheckDestroy: testAccCheckPDNSRecordDestroy, 32 Steps: []resource.TestStep{ 33 { 34 Config: testPDNSRecordConfigHyphenedWithCount, 35 Check: resource.ComposeTestCheckFunc( 36 testAccCheckPDNSRecordExists("powerdns_record.test-counted.0"), 37 testAccCheckPDNSRecordExists("powerdns_record.test-counted.1"), 38 ), 39 }, 40 }, 41 }) 42 } 43 44 func TestAccPDNSRecord_AAAA(t *testing.T) { 45 resource.Test(t, resource.TestCase{ 46 PreCheck: func() { testAccPreCheck(t) }, 47 Providers: testAccProviders, 48 CheckDestroy: testAccCheckPDNSRecordDestroy, 49 Steps: []resource.TestStep{ 50 { 51 Config: testPDNSRecordConfigAAAA, 52 Check: resource.ComposeTestCheckFunc( 53 testAccCheckPDNSRecordExists("powerdns_record.test-aaaa"), 54 ), 55 }, 56 }, 57 }) 58 } 59 60 func TestAccPDNSRecord_CNAME(t *testing.T) { 61 resource.Test(t, resource.TestCase{ 62 PreCheck: func() { testAccPreCheck(t) }, 63 Providers: testAccProviders, 64 CheckDestroy: testAccCheckPDNSRecordDestroy, 65 Steps: []resource.TestStep{ 66 { 67 Config: testPDNSRecordConfigCNAME, 68 Check: resource.ComposeTestCheckFunc( 69 testAccCheckPDNSRecordExists("powerdns_record.test-cname"), 70 ), 71 }, 72 }, 73 }) 74 } 75 76 func TestAccPDNSRecord_HINFO(t *testing.T) { 77 resource.Test(t, resource.TestCase{ 78 PreCheck: func() { testAccPreCheck(t) }, 79 Providers: testAccProviders, 80 CheckDestroy: testAccCheckPDNSRecordDestroy, 81 Steps: []resource.TestStep{ 82 { 83 Config: testPDNSRecordConfigHINFO, 84 Check: resource.ComposeTestCheckFunc( 85 testAccCheckPDNSRecordExists("powerdns_record.test-hinfo"), 86 ), 87 }, 88 }, 89 }) 90 } 91 92 func TestAccPDNSRecord_LOC(t *testing.T) { 93 resource.Test(t, resource.TestCase{ 94 PreCheck: func() { testAccPreCheck(t) }, 95 Providers: testAccProviders, 96 CheckDestroy: testAccCheckPDNSRecordDestroy, 97 Steps: []resource.TestStep{ 98 { 99 Config: testPDNSRecordConfigLOC, 100 Check: resource.ComposeTestCheckFunc( 101 testAccCheckPDNSRecordExists("powerdns_record.test-loc"), 102 ), 103 }, 104 }, 105 }) 106 } 107 108 func TestAccPDNSRecord_MX(t *testing.T) { 109 resource.Test(t, resource.TestCase{ 110 PreCheck: func() { testAccPreCheck(t) }, 111 Providers: testAccProviders, 112 CheckDestroy: testAccCheckPDNSRecordDestroy, 113 Steps: []resource.TestStep{ 114 { 115 Config: testPDNSRecordConfigMX, 116 Check: resource.ComposeTestCheckFunc( 117 testAccCheckPDNSRecordExists("powerdns_record.test-mx"), 118 ), 119 }, 120 { 121 Config: testPDNSRecordConfigMXMulti, 122 Check: resource.ComposeTestCheckFunc( 123 testAccCheckPDNSRecordExists("powerdns_record.test-mx-multi"), 124 ), 125 }, 126 }, 127 }) 128 } 129 130 func TestAccPDNSRecord_NAPTR(t *testing.T) { 131 resource.Test(t, resource.TestCase{ 132 PreCheck: func() { testAccPreCheck(t) }, 133 Providers: testAccProviders, 134 CheckDestroy: testAccCheckPDNSRecordDestroy, 135 Steps: []resource.TestStep{ 136 { 137 Config: testPDNSRecordConfigNAPTR, 138 Check: resource.ComposeTestCheckFunc( 139 testAccCheckPDNSRecordExists("powerdns_record.test-naptr"), 140 ), 141 }, 142 }, 143 }) 144 } 145 146 func TestAccPDNSRecord_NS(t *testing.T) { 147 resource.Test(t, resource.TestCase{ 148 PreCheck: func() { testAccPreCheck(t) }, 149 Providers: testAccProviders, 150 CheckDestroy: testAccCheckPDNSRecordDestroy, 151 Steps: []resource.TestStep{ 152 { 153 Config: testPDNSRecordConfigNS, 154 Check: resource.ComposeTestCheckFunc( 155 testAccCheckPDNSRecordExists("powerdns_record.test-ns"), 156 ), 157 }, 158 }, 159 }) 160 } 161 162 func TestAccPDNSRecord_SPF(t *testing.T) { 163 resource.Test(t, resource.TestCase{ 164 PreCheck: func() { testAccPreCheck(t) }, 165 Providers: testAccProviders, 166 CheckDestroy: testAccCheckPDNSRecordDestroy, 167 Steps: []resource.TestStep{ 168 { 169 Config: testPDNSRecordConfigSPF, 170 Check: resource.ComposeTestCheckFunc( 171 testAccCheckPDNSRecordExists("powerdns_record.test-spf"), 172 ), 173 }, 174 }, 175 }) 176 } 177 178 func TestAccPDNSRecord_SSHFP(t *testing.T) { 179 resource.Test(t, resource.TestCase{ 180 PreCheck: func() { testAccPreCheck(t) }, 181 Providers: testAccProviders, 182 CheckDestroy: testAccCheckPDNSRecordDestroy, 183 Steps: []resource.TestStep{ 184 { 185 Config: testPDNSRecordConfigSSHFP, 186 Check: resource.ComposeTestCheckFunc( 187 testAccCheckPDNSRecordExists("powerdns_record.test-sshfp"), 188 ), 189 }, 190 }, 191 }) 192 } 193 194 func TestAccPDNSRecord_SRV(t *testing.T) { 195 resource.Test(t, resource.TestCase{ 196 PreCheck: func() { testAccPreCheck(t) }, 197 Providers: testAccProviders, 198 CheckDestroy: testAccCheckPDNSRecordDestroy, 199 Steps: []resource.TestStep{ 200 { 201 Config: testPDNSRecordConfigSRV, 202 Check: resource.ComposeTestCheckFunc( 203 testAccCheckPDNSRecordExists("powerdns_record.test-srv"), 204 ), 205 }, 206 }, 207 }) 208 } 209 210 func TestAccPDNSRecord_TXT(t *testing.T) { 211 resource.Test(t, resource.TestCase{ 212 PreCheck: func() { testAccPreCheck(t) }, 213 Providers: testAccProviders, 214 CheckDestroy: testAccCheckPDNSRecordDestroy, 215 Steps: []resource.TestStep{ 216 { 217 Config: testPDNSRecordConfigTXT, 218 Check: resource.ComposeTestCheckFunc( 219 testAccCheckPDNSRecordExists("powerdns_record.test-txt"), 220 ), 221 }, 222 }, 223 }) 224 } 225 226 func testAccCheckPDNSRecordDestroy(s *terraform.State) error { 227 for _, rs := range s.RootModule().Resources { 228 if rs.Type != "powerdns_record" { 229 continue 230 } 231 232 client := testAccProvider.Meta().(*Client) 233 exists, err := client.RecordExistsByID(rs.Primary.Attributes["zone"], rs.Primary.ID) 234 if err != nil { 235 return fmt.Errorf("Error checking if record still exists: %#v", rs.Primary.ID) 236 } 237 if exists { 238 return fmt.Errorf("Record still exists: %#v", rs.Primary.ID) 239 } 240 241 } 242 return nil 243 } 244 245 func testAccCheckPDNSRecordExists(n string) resource.TestCheckFunc { 246 return func(s *terraform.State) error { 247 rs, ok := s.RootModule().Resources[n] 248 if !ok { 249 return fmt.Errorf("Not found: %s", n) 250 } 251 252 if rs.Primary.ID == "" { 253 return fmt.Errorf("No Record ID is set") 254 } 255 256 client := testAccProvider.Meta().(*Client) 257 foundRecords, err := client.ListRecordsByID(rs.Primary.Attributes["zone"], rs.Primary.ID) 258 if err != nil { 259 return err 260 } 261 if len(foundRecords) == 0 { 262 return fmt.Errorf("Record does not exist") 263 } 264 for _, rec := range foundRecords { 265 if rec.Id() == rs.Primary.ID { 266 return nil 267 } 268 } 269 return fmt.Errorf("Record does not exist: %#v", rs.Primary.ID) 270 } 271 } 272 273 const testPDNSRecordConfigA = ` 274 resource "powerdns_record" "test-a" { 275 zone = "sysa.xyz" 276 name = "redis.sysa.xyz" 277 type = "A" 278 ttl = 60 279 records = [ "1.1.1.1", "2.2.2.2" ] 280 }` 281 282 const testPDNSRecordConfigHyphenedWithCount = ` 283 resource "powerdns_record" "test-counted" { 284 count = "2" 285 zone = "sysa.xyz" 286 name = "redis-${count.index}.sysa.xyz" 287 type = "A" 288 ttl = 60 289 records = [ "1.1.1.${count.index}" ] 290 }` 291 292 const testPDNSRecordConfigAAAA = ` 293 resource "powerdns_record" "test-aaaa" { 294 zone = "sysa.xyz" 295 name = "redis.sysa.xyz" 296 type = "AAAA" 297 ttl = 60 298 records = [ "2001:DB8:2000:bf0::1", "2001:DB8:2000:bf1::1" ] 299 }` 300 301 const testPDNSRecordConfigCNAME = ` 302 resource "powerdns_record" "test-cname" { 303 zone = "sysa.xyz" 304 name = "redis.sysa.xyz" 305 type = "CNAME" 306 ttl = 60 307 records = [ "redis.example.com" ] 308 }` 309 310 const testPDNSRecordConfigHINFO = ` 311 resource "powerdns_record" "test-hinfo" { 312 zone = "sysa.xyz" 313 name = "redis.sysa.xyz" 314 type = "HINFO" 315 ttl = 60 316 records = [ "\"PC-Intel-2.4ghz\" \"Linux\"" ] 317 }` 318 319 const testPDNSRecordConfigLOC = ` 320 resource "powerdns_record" "test-loc" { 321 zone = "sysa.xyz" 322 name = "redis.sysa.xyz" 323 type = "LOC" 324 ttl = 60 325 records = [ "51 56 0.123 N 5 54 0.000 E 4.00m 1.00m 10000.00m 10.00m" ] 326 }` 327 328 const testPDNSRecordConfigMX = ` 329 resource "powerdns_record" "test-mx" { 330 zone = "sysa.xyz" 331 name = "sysa.xyz" 332 type = "MX" 333 ttl = 60 334 records = [ "10 mail.example.com" ] 335 }` 336 337 const testPDNSRecordConfigMXMulti = ` 338 resource "powerdns_record" "test-mx-multi" { 339 zone = "sysa.xyz" 340 name = "sysa.xyz" 341 type = "MX" 342 ttl = 60 343 records = [ "10 mail1.example.com", "20 mail2.example.com" ] 344 }` 345 346 const testPDNSRecordConfigNAPTR = ` 347 resource "powerdns_record" "test-naptr" { 348 zone = "sysa.xyz" 349 name = "sysa.xyz" 350 type = "NAPTR" 351 ttl = 60 352 records = [ "100 50 \"s\" \"z3950+I2L+I2C\" \"\" _z3950._tcp.gatech.edu'." ] 353 }` 354 355 const testPDNSRecordConfigNS = ` 356 resource "powerdns_record" "test-ns" { 357 zone = "sysa.xyz" 358 name = "lab.sysa.xyz" 359 type = "NS" 360 ttl = 60 361 records = [ "ns1.sysa.xyz", "ns2.sysa.xyz" ] 362 }` 363 364 const testPDNSRecordConfigSPF = ` 365 resource "powerdns_record" "test-spf" { 366 zone = "sysa.xyz" 367 name = "sysa.xyz" 368 type = "SPF" 369 ttl = 60 370 records = [ "\"v=spf1 +all\"" ] 371 }` 372 373 const testPDNSRecordConfigSSHFP = ` 374 resource "powerdns_record" "test-sshfp" { 375 zone = "sysa.xyz" 376 name = "ssh.sysa.xyz" 377 type = "SSHFP" 378 ttl = 60 379 records = [ "1 1 123456789abcdef67890123456789abcdef67890" ] 380 }` 381 382 const testPDNSRecordConfigSRV = ` 383 resource "powerdns_record" "test-srv" { 384 zone = "sysa.xyz" 385 name = "_redis._tcp.sysa.xyz" 386 type = "SRV" 387 ttl = 60 388 records = [ "0 10 6379 redis1.sysa.xyz", "0 10 6379 redis2.sysa.xyz", "10 10 6379 redis-replica.sysa.xyz" ] 389 }` 390 391 const testPDNSRecordConfigTXT = ` 392 resource "powerdns_record" "test-txt" { 393 zone = "sysa.xyz" 394 name = "text.sysa.xyz" 395 type = "TXT" 396 ttl = 60 397 records = [ "\"text record payload\"" ] 398 }`