github.com/ricardclau/terraform@v0.6.17-0.20160519222547-283e3ae6b5a9/builtin/providers/cloudflare/resource_cloudflare_record_test.go (about) 1 package cloudflare 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 11 // NOTE: Temporary until they merge my PR: 12 "github.com/mitchellh/cloudflare-go" 13 ) 14 15 func TestAccCloudFlareRecord_Basic(t *testing.T) { 16 var record cloudflare.DNSRecord 17 domain := os.Getenv("CLOUDFLARE_DOMAIN") 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testAccCheckCloudFlareRecordDestroy, 23 Steps: []resource.TestStep{ 24 resource.TestStep{ 25 Config: fmt.Sprintf(testAccCheckCloudFlareRecordConfigBasic, domain), 26 Check: resource.ComposeTestCheckFunc( 27 testAccCheckCloudFlareRecordExists("cloudflare_record.foobar", &record), 28 testAccCheckCloudFlareRecordAttributes(&record), 29 resource.TestCheckResourceAttr( 30 "cloudflare_record.foobar", "name", "terraform"), 31 resource.TestCheckResourceAttr( 32 "cloudflare_record.foobar", "domain", domain), 33 resource.TestCheckResourceAttr( 34 "cloudflare_record.foobar", "value", "192.168.0.10"), 35 ), 36 }, 37 }, 38 }) 39 } 40 41 func TestAccCloudFlareRecord_Apex(t *testing.T) { 42 var record cloudflare.DNSRecord 43 domain := os.Getenv("CLOUDFLARE_DOMAIN") 44 45 resource.Test(t, resource.TestCase{ 46 PreCheck: func() { testAccPreCheck(t) }, 47 Providers: testAccProviders, 48 CheckDestroy: testAccCheckCloudFlareRecordDestroy, 49 Steps: []resource.TestStep{ 50 resource.TestStep{ 51 Config: fmt.Sprintf(testAccCheckCloudFlareRecordConfigApex, domain), 52 Check: resource.ComposeTestCheckFunc( 53 testAccCheckCloudFlareRecordExists("cloudflare_record.foobar", &record), 54 testAccCheckCloudFlareRecordAttributes(&record), 55 resource.TestCheckResourceAttr( 56 "cloudflare_record.foobar", "name", "@"), 57 resource.TestCheckResourceAttr( 58 "cloudflare_record.foobar", "domain", domain), 59 resource.TestCheckResourceAttr( 60 "cloudflare_record.foobar", "value", "192.168.0.10"), 61 ), 62 }, 63 }, 64 }) 65 } 66 67 func TestAccCloudFlareRecord_Proxied(t *testing.T) { 68 var record cloudflare.DNSRecord 69 domain := os.Getenv("CLOUDFLARE_DOMAIN") 70 71 resource.Test(t, resource.TestCase{ 72 PreCheck: func() { testAccPreCheck(t) }, 73 Providers: testAccProviders, 74 CheckDestroy: testAccCheckCloudFlareRecordDestroy, 75 Steps: []resource.TestStep{ 76 resource.TestStep{ 77 Config: fmt.Sprintf(testAccCheckCloudFlareRecordConfigProxied, domain, domain), 78 Check: resource.ComposeTestCheckFunc( 79 testAccCheckCloudFlareRecordExists("cloudflare_record.foobar", &record), 80 resource.TestCheckResourceAttr( 81 "cloudflare_record.foobar", "domain", domain), 82 resource.TestCheckResourceAttr( 83 "cloudflare_record.foobar", "name", "terraform"), 84 resource.TestCheckResourceAttr( 85 "cloudflare_record.foobar", "proxied", "true"), 86 resource.TestCheckResourceAttr( 87 "cloudflare_record.foobar", "type", "CNAME"), 88 resource.TestCheckResourceAttr( 89 "cloudflare_record.foobar", "value", domain), 90 ), 91 }, 92 }, 93 }) 94 } 95 96 func TestAccCloudFlareRecord_Updated(t *testing.T) { 97 var record cloudflare.DNSRecord 98 domain := os.Getenv("CLOUDFLARE_DOMAIN") 99 100 resource.Test(t, resource.TestCase{ 101 PreCheck: func() { testAccPreCheck(t) }, 102 Providers: testAccProviders, 103 CheckDestroy: testAccCheckCloudFlareRecordDestroy, 104 Steps: []resource.TestStep{ 105 resource.TestStep{ 106 Config: fmt.Sprintf(testAccCheckCloudFlareRecordConfigBasic, domain), 107 Check: resource.ComposeTestCheckFunc( 108 testAccCheckCloudFlareRecordExists("cloudflare_record.foobar", &record), 109 testAccCheckCloudFlareRecordAttributes(&record), 110 resource.TestCheckResourceAttr( 111 "cloudflare_record.foobar", "name", "terraform"), 112 resource.TestCheckResourceAttr( 113 "cloudflare_record.foobar", "domain", domain), 114 resource.TestCheckResourceAttr( 115 "cloudflare_record.foobar", "value", "192.168.0.10"), 116 ), 117 }, 118 resource.TestStep{ 119 Config: fmt.Sprintf(testAccCheckCloudFlareRecordConfigNewValue, domain), 120 Check: resource.ComposeTestCheckFunc( 121 testAccCheckCloudFlareRecordExists("cloudflare_record.foobar", &record), 122 testAccCheckCloudFlareRecordAttributesUpdated(&record), 123 resource.TestCheckResourceAttr( 124 "cloudflare_record.foobar", "name", "terraform"), 125 resource.TestCheckResourceAttr( 126 "cloudflare_record.foobar", "domain", domain), 127 resource.TestCheckResourceAttr( 128 "cloudflare_record.foobar", "value", "192.168.0.11"), 129 ), 130 }, 131 }, 132 }) 133 } 134 135 func TestAccCloudFlareRecord_forceNewRecord(t *testing.T) { 136 var afterCreate, afterUpdate cloudflare.DNSRecord 137 domain := os.Getenv("CLOUDFLARE_DOMAIN") 138 139 resource.Test(t, resource.TestCase{ 140 PreCheck: func() { testAccPreCheck(t) }, 141 Providers: testAccProviders, 142 CheckDestroy: testAccCheckCloudFlareRecordDestroy, 143 Steps: []resource.TestStep{ 144 resource.TestStep{ 145 Config: fmt.Sprintf(testAccCheckCloudFlareRecordConfigBasic, domain), 146 Check: resource.ComposeTestCheckFunc( 147 testAccCheckCloudFlareRecordExists("cloudflare_record.foobar", &afterCreate), 148 ), 149 }, 150 resource.TestStep{ 151 Config: fmt.Sprintf(testAccCheckCloudFlareRecordConfigForceNew, domain, domain), 152 Check: resource.ComposeTestCheckFunc( 153 testAccCheckCloudFlareRecordExists("cloudflare_record.foobar", &afterUpdate), 154 testAccCheckCloudFlareRecordRecreated(t, &afterCreate, &afterUpdate), 155 ), 156 }, 157 }, 158 }) 159 } 160 161 func testAccCheckCloudFlareRecordRecreated(t *testing.T, 162 before, after *cloudflare.DNSRecord) resource.TestCheckFunc { 163 return func(s *terraform.State) error { 164 if before.ID == after.ID { 165 t.Fatalf("Expected change of Record Ids, but both were %v", before.ID) 166 } 167 return nil 168 } 169 } 170 171 func testAccCheckCloudFlareRecordDestroy(s *terraform.State) error { 172 client := testAccProvider.Meta().(*cloudflare.API) 173 174 for _, rs := range s.RootModule().Resources { 175 if rs.Type != "cloudflare_record" { 176 continue 177 } 178 179 _, err := client.DNSRecord(rs.Primary.Attributes["zone_id"], rs.Primary.ID) 180 if err == nil { 181 return fmt.Errorf("Record still exists") 182 } 183 } 184 185 return nil 186 } 187 188 func testAccCheckCloudFlareRecordAttributes(record *cloudflare.DNSRecord) resource.TestCheckFunc { 189 return func(s *terraform.State) error { 190 191 if record.Content != "192.168.0.10" { 192 return fmt.Errorf("Bad content: %s", record.Content) 193 } 194 195 return nil 196 } 197 } 198 199 func testAccCheckCloudFlareRecordAttributesUpdated(record *cloudflare.DNSRecord) resource.TestCheckFunc { 200 return func(s *terraform.State) error { 201 202 if record.Content != "192.168.0.11" { 203 return fmt.Errorf("Bad content: %s", record.Content) 204 } 205 206 return nil 207 } 208 } 209 210 func testAccCheckCloudFlareRecordExists(n string, record *cloudflare.DNSRecord) resource.TestCheckFunc { 211 return func(s *terraform.State) error { 212 rs, ok := s.RootModule().Resources[n] 213 if !ok { 214 return fmt.Errorf("Not found: %s", n) 215 } 216 217 if rs.Primary.ID == "" { 218 return fmt.Errorf("No Record ID is set") 219 } 220 221 client := testAccProvider.Meta().(*cloudflare.API) 222 foundRecord, err := client.DNSRecord(rs.Primary.Attributes["zone_id"], rs.Primary.ID) 223 if err != nil { 224 return err 225 } 226 227 if foundRecord.ID != rs.Primary.ID { 228 return fmt.Errorf("Record not found") 229 } 230 231 *record = foundRecord 232 233 return nil 234 } 235 } 236 237 const testAccCheckCloudFlareRecordConfigBasic = ` 238 resource "cloudflare_record" "foobar" { 239 domain = "%s" 240 241 name = "terraform" 242 value = "192.168.0.10" 243 type = "A" 244 ttl = 3600 245 }` 246 247 const testAccCheckCloudFlareRecordConfigApex = ` 248 resource "cloudflare_record" "foobar" { 249 domain = "%s" 250 name = "@" 251 value = "192.168.0.10" 252 type = "A" 253 ttl = 3600 254 }` 255 256 const testAccCheckCloudFlareRecordConfigProxied = ` 257 resource "cloudflare_record" "foobar" { 258 domain = "%s" 259 260 name = "terraform" 261 value = "%s" 262 type = "CNAME" 263 proxied = true 264 }` 265 266 const testAccCheckCloudFlareRecordConfigNewValue = ` 267 resource "cloudflare_record" "foobar" { 268 domain = "%s" 269 270 name = "terraform" 271 value = "192.168.0.11" 272 type = "A" 273 ttl = 3600 274 }` 275 276 const testAccCheckCloudFlareRecordConfigForceNew = ` 277 resource "cloudflare_record" "foobar" { 278 domain = "%s" 279 280 name = "terraform" 281 value = "%s" 282 type = "CNAME" 283 ttl = 3600 284 }`