github.com/cbroglie/terraform@v0.7.0-rc3.0.20170410193827-735dfc416d46/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 resource.TestStep{ 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 resource.TestStep{ 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 resource.TestStep{ 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 resource.TestStep{ 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_UpdatedMx(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 resource.TestStep{ 116 Config: fmt.Sprintf(testAccCheckDNSimpleRecordConfig_mx, domain), 117 Check: resource.ComposeTestCheckFunc( 118 testAccCheckDNSimpleRecordExists("dnsimple_record.foobar", &record), 119 resource.TestCheckResourceAttr( 120 "dnsimple_record.foobar", "name", ""), 121 resource.TestCheckResourceAttr( 122 "dnsimple_record.foobar", "domain", domain), 123 resource.TestCheckResourceAttr( 124 "dnsimple_record.foobar", "value", "mx.example.com"), 125 resource.TestCheckResourceAttr( 126 "dnsimple_record.foobar", "priority", "5"), 127 ), 128 }, 129 resource.TestStep{ 130 Config: fmt.Sprintf(testAccCheckDNSimpleRecordConfig_mx_new_value, domain), 131 Check: resource.ComposeTestCheckFunc( 132 testAccCheckDNSimpleRecordExists("dnsimple_record.foobar", &record), 133 resource.TestCheckResourceAttr( 134 "dnsimple_record.foobar", "name", ""), 135 resource.TestCheckResourceAttr( 136 "dnsimple_record.foobar", "domain", domain), 137 resource.TestCheckResourceAttr( 138 "dnsimple_record.foobar", "value", "mx2.example.com"), 139 resource.TestCheckResourceAttr( 140 "dnsimple_record.foobar", "priority", "10"), 141 ), 142 }, 143 }, 144 }) 145 } 146 147 func testAccCheckDNSimpleRecordDestroy(s *terraform.State) error { 148 provider := testAccProvider.Meta().(*Client) 149 150 for _, rs := range s.RootModule().Resources { 151 if rs.Type != "dnsimple_record" { 152 continue 153 } 154 155 recordID, _ := strconv.Atoi(rs.Primary.ID) 156 _, err := provider.client.Zones.GetRecord(provider.config.Account, rs.Primary.Attributes["domain"], recordID) 157 if err == nil { 158 return fmt.Errorf("Record still exists") 159 } 160 } 161 162 return nil 163 } 164 165 func testAccCheckDNSimpleRecordAttributes(record *dnsimple.ZoneRecord) resource.TestCheckFunc { 166 return func(s *terraform.State) error { 167 168 if record.Content != "192.168.0.10" { 169 return fmt.Errorf("Bad content: %s", record.Content) 170 } 171 172 return nil 173 } 174 } 175 176 func testAccCheckDNSimpleRecordAttributesUpdated(record *dnsimple.ZoneRecord) resource.TestCheckFunc { 177 return func(s *terraform.State) error { 178 179 if record.Content != "192.168.0.11" { 180 return fmt.Errorf("Bad content: %s", record.Content) 181 } 182 183 return nil 184 } 185 } 186 187 func testAccCheckDNSimpleRecordExists(n string, record *dnsimple.ZoneRecord) resource.TestCheckFunc { 188 return func(s *terraform.State) error { 189 rs, ok := s.RootModule().Resources[n] 190 191 if !ok { 192 return fmt.Errorf("Not found: %s", n) 193 } 194 195 if rs.Primary.ID == "" { 196 return fmt.Errorf("No Record ID is set") 197 } 198 199 provider := testAccProvider.Meta().(*Client) 200 201 recordID, _ := strconv.Atoi(rs.Primary.ID) 202 resp, err := provider.client.Zones.GetRecord(provider.config.Account, rs.Primary.Attributes["domain"], recordID) 203 if err != nil { 204 return err 205 } 206 207 foundRecord := resp.Data 208 if foundRecord.ID != recordID { 209 return fmt.Errorf("Record not found") 210 } 211 212 *record = *foundRecord 213 214 return nil 215 } 216 } 217 218 const testAccCheckDNSimpleRecordConfig_basic = ` 219 resource "dnsimple_record" "foobar" { 220 domain = "%s" 221 222 name = "terraform" 223 value = "192.168.0.10" 224 type = "A" 225 ttl = 3600 226 }` 227 228 const testAccCheckDNSimpleRecordConfig_new_value = ` 229 resource "dnsimple_record" "foobar" { 230 domain = "%s" 231 232 name = "terraform" 233 value = "192.168.0.11" 234 type = "A" 235 ttl = 3600 236 }` 237 238 const testAccCheckDNSimpleRecordConfig_mx = ` 239 resource "dnsimple_record" "foobar" { 240 domain = "%s" 241 242 name = "" 243 value = "mx.example.com" 244 type = "MX" 245 ttl = 3600 246 priority = 5 247 }` 248 249 const testAccCheckDNSimpleRecordConfig_mx_new_value = ` 250 resource "dnsimple_record" "foobar" { 251 domain = "%s" 252 253 name = "" 254 value = "mx2.example.com" 255 type = "MX" 256 ttl = 3600 257 priority = 10 258 }`