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