github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/digitalocean/resource_digitalocean_record_test.go (about) 1 package digitalocean 2 3 import ( 4 "fmt" 5 "strconv" 6 "testing" 7 8 "github.com/digitalocean/godo" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccDigitalOceanRecord_Basic(t *testing.T) { 14 var record godo.DomainRecord 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckDigitalOceanRecordDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccCheckDigitalOceanRecordConfig_basic, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record), 25 testAccCheckDigitalOceanRecordAttributes(&record), 26 resource.TestCheckResourceAttr( 27 "digitalocean_record.foobar", "name", "terraform"), 28 resource.TestCheckResourceAttr( 29 "digitalocean_record.foobar", "domain", "foobar-test-terraform.com"), 30 resource.TestCheckResourceAttr( 31 "digitalocean_record.foobar", "value", "192.168.0.10"), 32 ), 33 }, 34 }, 35 }) 36 } 37 38 func TestAccDigitalOceanRecord_Updated(t *testing.T) { 39 var record godo.DomainRecord 40 41 resource.Test(t, resource.TestCase{ 42 PreCheck: func() { testAccPreCheck(t) }, 43 Providers: testAccProviders, 44 CheckDestroy: testAccCheckDigitalOceanRecordDestroy, 45 Steps: []resource.TestStep{ 46 resource.TestStep{ 47 Config: testAccCheckDigitalOceanRecordConfig_basic, 48 Check: resource.ComposeTestCheckFunc( 49 testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record), 50 testAccCheckDigitalOceanRecordAttributes(&record), 51 resource.TestCheckResourceAttr( 52 "digitalocean_record.foobar", "name", "terraform"), 53 resource.TestCheckResourceAttr( 54 "digitalocean_record.foobar", "domain", "foobar-test-terraform.com"), 55 resource.TestCheckResourceAttr( 56 "digitalocean_record.foobar", "value", "192.168.0.10"), 57 resource.TestCheckResourceAttr( 58 "digitalocean_record.foobar", "type", "A"), 59 ), 60 }, 61 resource.TestStep{ 62 Config: testAccCheckDigitalOceanRecordConfig_new_value, 63 Check: resource.ComposeTestCheckFunc( 64 testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record), 65 testAccCheckDigitalOceanRecordAttributesUpdated(&record), 66 resource.TestCheckResourceAttr( 67 "digitalocean_record.foobar", "name", "terraform"), 68 resource.TestCheckResourceAttr( 69 "digitalocean_record.foobar", "domain", "foobar-test-terraform.com"), 70 resource.TestCheckResourceAttr( 71 "digitalocean_record.foobar", "value", "192.168.0.11"), 72 resource.TestCheckResourceAttr( 73 "digitalocean_record.foobar", "type", "A"), 74 ), 75 }, 76 }, 77 }) 78 } 79 80 func TestAccDigitalOceanRecord_HostnameValue(t *testing.T) { 81 var record godo.DomainRecord 82 83 resource.Test(t, resource.TestCase{ 84 PreCheck: func() { testAccPreCheck(t) }, 85 Providers: testAccProviders, 86 CheckDestroy: testAccCheckDigitalOceanRecordDestroy, 87 Steps: []resource.TestStep{ 88 resource.TestStep{ 89 Config: testAccCheckDigitalOceanRecordConfig_cname, 90 Check: resource.ComposeTestCheckFunc( 91 testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record), 92 testAccCheckDigitalOceanRecordAttributesHostname("a", &record), 93 resource.TestCheckResourceAttr( 94 "digitalocean_record.foobar", "name", "terraform"), 95 resource.TestCheckResourceAttr( 96 "digitalocean_record.foobar", "domain", "foobar-test-terraform.com"), 97 resource.TestCheckResourceAttr( 98 "digitalocean_record.foobar", "value", "a.foobar-test-terraform.com."), 99 resource.TestCheckResourceAttr( 100 "digitalocean_record.foobar", "type", "CNAME"), 101 ), 102 }, 103 }, 104 }) 105 } 106 107 func TestAccDigitalOceanRecord_ExternalHostnameValue(t *testing.T) { 108 var record godo.DomainRecord 109 110 resource.Test(t, resource.TestCase{ 111 PreCheck: func() { testAccPreCheck(t) }, 112 Providers: testAccProviders, 113 CheckDestroy: testAccCheckDigitalOceanRecordDestroy, 114 Steps: []resource.TestStep{ 115 resource.TestStep{ 116 Config: testAccCheckDigitalOceanRecordConfig_external_cname, 117 Check: resource.ComposeTestCheckFunc( 118 testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record), 119 testAccCheckDigitalOceanRecordAttributesHostname("a.foobar-test-terraform.net", &record), 120 resource.TestCheckResourceAttr( 121 "digitalocean_record.foobar", "name", "terraform"), 122 resource.TestCheckResourceAttr( 123 "digitalocean_record.foobar", "domain", "foobar-test-terraform.com"), 124 resource.TestCheckResourceAttr( 125 "digitalocean_record.foobar", "value", "a.foobar-test-terraform.net."), 126 resource.TestCheckResourceAttr( 127 "digitalocean_record.foobar", "type", "CNAME"), 128 ), 129 }, 130 }, 131 }) 132 } 133 134 func testAccCheckDigitalOceanRecordDestroy(s *terraform.State) error { 135 client := testAccProvider.Meta().(*godo.Client) 136 137 for _, rs := range s.RootModule().Resources { 138 if rs.Type != "digitalocean_record" { 139 continue 140 } 141 domain := rs.Primary.Attributes["domain"] 142 id, err := strconv.Atoi(rs.Primary.ID) 143 if err != nil { 144 return err 145 } 146 147 _, _, err = client.Domains.Record(domain, id) 148 149 if err == nil { 150 return fmt.Errorf("Record still exists") 151 } 152 } 153 154 return nil 155 } 156 157 func testAccCheckDigitalOceanRecordAttributes(record *godo.DomainRecord) resource.TestCheckFunc { 158 return func(s *terraform.State) error { 159 160 if record.Data != "192.168.0.10" { 161 return fmt.Errorf("Bad value: %s", record.Data) 162 } 163 164 return nil 165 } 166 } 167 168 func testAccCheckDigitalOceanRecordAttributesUpdated(record *godo.DomainRecord) resource.TestCheckFunc { 169 return func(s *terraform.State) error { 170 171 if record.Data != "192.168.0.11" { 172 return fmt.Errorf("Bad value: %s", record.Data) 173 } 174 175 return nil 176 } 177 } 178 179 func testAccCheckDigitalOceanRecordExists(n string, record *godo.DomainRecord) resource.TestCheckFunc { 180 return func(s *terraform.State) error { 181 rs, ok := s.RootModule().Resources[n] 182 183 if !ok { 184 return fmt.Errorf("Not found: %s", n) 185 } 186 187 if rs.Primary.ID == "" { 188 return fmt.Errorf("No Record ID is set") 189 } 190 191 client := testAccProvider.Meta().(*godo.Client) 192 193 domain := rs.Primary.Attributes["domain"] 194 id, err := strconv.Atoi(rs.Primary.ID) 195 if err != nil { 196 return err 197 } 198 199 foundRecord, _, err := client.Domains.Record(domain, id) 200 201 if err != nil { 202 return err 203 } 204 205 if strconv.Itoa(foundRecord.ID) != rs.Primary.ID { 206 return fmt.Errorf("Record not found") 207 } 208 209 *record = *foundRecord 210 211 return nil 212 } 213 } 214 215 func testAccCheckDigitalOceanRecordAttributesHostname(data string, record *godo.DomainRecord) resource.TestCheckFunc { 216 return func(s *terraform.State) error { 217 218 if record.Data != data { 219 return fmt.Errorf("Bad value: expected %s, got %s", data, record.Data) 220 } 221 222 return nil 223 } 224 } 225 226 const testAccCheckDigitalOceanRecordConfig_basic = ` 227 resource "digitalocean_domain" "foobar" { 228 name = "foobar-test-terraform.com" 229 ip_address = "192.168.0.10" 230 } 231 232 resource "digitalocean_record" "foobar" { 233 domain = "${digitalocean_domain.foobar.name}" 234 235 name = "terraform" 236 value = "192.168.0.10" 237 type = "A" 238 }` 239 240 const testAccCheckDigitalOceanRecordConfig_new_value = ` 241 resource "digitalocean_domain" "foobar" { 242 name = "foobar-test-terraform.com" 243 ip_address = "192.168.0.10" 244 } 245 246 resource "digitalocean_record" "foobar" { 247 domain = "${digitalocean_domain.foobar.name}" 248 249 name = "terraform" 250 value = "192.168.0.11" 251 type = "A" 252 }` 253 254 const testAccCheckDigitalOceanRecordConfig_cname = ` 255 resource "digitalocean_domain" "foobar" { 256 name = "foobar-test-terraform.com" 257 ip_address = "192.168.0.10" 258 } 259 260 resource "digitalocean_record" "foobar" { 261 domain = "${digitalocean_domain.foobar.name}" 262 263 name = "terraform" 264 value = "a.foobar-test-terraform.com." 265 type = "CNAME" 266 }` 267 268 const testAccCheckDigitalOceanRecordConfig_relative_cname = ` 269 resource "digitalocean_domain" "foobar" { 270 name = "foobar-test-terraform.com" 271 ip_address = "192.168.0.10" 272 } 273 274 resource "digitalocean_record" "foobar" { 275 domain = "${digitalocean_domain.foobar.name}" 276 277 name = "terraform" 278 value = "a.b" 279 type = "CNAME" 280 }` 281 282 const testAccCheckDigitalOceanRecordConfig_external_cname = ` 283 resource "digitalocean_domain" "foobar" { 284 name = "foobar-test-terraform.com" 285 ip_address = "192.168.0.10" 286 } 287 288 resource "digitalocean_record" "foobar" { 289 domain = "${digitalocean_domain.foobar.name}" 290 291 name = "terraform" 292 value = "a.foobar-test-terraform.net." 293 type = "CNAME" 294 }`