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