github.com/subuk/terraform@v0.6.14-0.20160317140351-de1567c2e732/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 "github.com/pearkes/cloudflare" 11 ) 12 13 func TestAccCLOudflareRecord_Basic(t *testing.T) { 14 var record cloudflare.Record 15 domain := os.Getenv("CLOUDFLARE_DOMAIN") 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckCLOudflareRecordDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: fmt.Sprintf(testAccCheckCLoudFlareRecordConfig_basic, domain), 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckCLOudflareRecordExists("cloudflare_record.foobar", &record), 26 testAccCheckCLOudflareRecordAttributes(&record), 27 resource.TestCheckResourceAttr( 28 "cloudflare_record.foobar", "name", "terraform"), 29 resource.TestCheckResourceAttr( 30 "cloudflare_record.foobar", "domain", domain), 31 resource.TestCheckResourceAttr( 32 "cloudflare_record.foobar", "value", "192.168.0.10"), 33 ), 34 }, 35 }, 36 }) 37 } 38 39 func TestAccCLOudflareRecord_Updated(t *testing.T) { 40 var record cloudflare.Record 41 domain := os.Getenv("CLOUDFLARE_DOMAIN") 42 43 resource.Test(t, resource.TestCase{ 44 PreCheck: func() { testAccPreCheck(t) }, 45 Providers: testAccProviders, 46 CheckDestroy: testAccCheckCLOudflareRecordDestroy, 47 Steps: []resource.TestStep{ 48 resource.TestStep{ 49 Config: fmt.Sprintf(testAccCheckCLoudFlareRecordConfig_basic, domain), 50 Check: resource.ComposeTestCheckFunc( 51 testAccCheckCLOudflareRecordExists("cloudflare_record.foobar", &record), 52 testAccCheckCLOudflareRecordAttributes(&record), 53 resource.TestCheckResourceAttr( 54 "cloudflare_record.foobar", "name", "terraform"), 55 resource.TestCheckResourceAttr( 56 "cloudflare_record.foobar", "domain", domain), 57 resource.TestCheckResourceAttr( 58 "cloudflare_record.foobar", "value", "192.168.0.10"), 59 ), 60 }, 61 resource.TestStep{ 62 Config: fmt.Sprintf(testAccCheckCloudFlareRecordConfig_new_value, domain), 63 Check: resource.ComposeTestCheckFunc( 64 testAccCheckCLOudflareRecordExists("cloudflare_record.foobar", &record), 65 testAccCheckCLOudflareRecordAttributesUpdated(&record), 66 resource.TestCheckResourceAttr( 67 "cloudflare_record.foobar", "name", "terraform"), 68 resource.TestCheckResourceAttr( 69 "cloudflare_record.foobar", "domain", domain), 70 resource.TestCheckResourceAttr( 71 "cloudflare_record.foobar", "value", "192.168.0.11"), 72 ), 73 }, 74 }, 75 }) 76 } 77 78 func TestAccCLOudflareRecord_forceNewRecord(t *testing.T) { 79 var afterCreate, afterUpdate cloudflare.Record 80 domain := os.Getenv("CLOUDFLARE_DOMAIN") 81 82 resource.Test(t, resource.TestCase{ 83 PreCheck: func() { testAccPreCheck(t) }, 84 Providers: testAccProviders, 85 CheckDestroy: testAccCheckCLOudflareRecordDestroy, 86 Steps: []resource.TestStep{ 87 resource.TestStep{ 88 Config: fmt.Sprintf(testAccCheckCLoudFlareRecordConfig_basic, domain), 89 Check: resource.ComposeTestCheckFunc( 90 testAccCheckCLOudflareRecordExists("cloudflare_record.foobar", &afterCreate), 91 ), 92 }, 93 resource.TestStep{ 94 Config: fmt.Sprintf(testAccCheckCloudFlareRecordConfig_forceNew, domain, domain), 95 Check: resource.ComposeTestCheckFunc( 96 testAccCheckCLOudflareRecordExists("cloudflare_record.foobar", &afterUpdate), 97 testAccCheckCloudFlareRecordRecreated(t, &afterCreate, &afterUpdate), 98 ), 99 }, 100 }, 101 }) 102 } 103 104 func testAccCheckCloudFlareRecordRecreated(t *testing.T, 105 before, after *cloudflare.Record) resource.TestCheckFunc { 106 return func(s *terraform.State) error { 107 if before.Id == after.Id { 108 t.Fatalf("Expected change of Record Ids, but both were %v", before.Id) 109 } 110 return nil 111 } 112 } 113 114 func testAccCheckCLOudflareRecordDestroy(s *terraform.State) error { 115 client := testAccProvider.Meta().(*cloudflare.Client) 116 117 for _, rs := range s.RootModule().Resources { 118 if rs.Type != "cloudflare_record" { 119 continue 120 } 121 122 _, err := client.RetrieveRecord(rs.Primary.Attributes["domain"], rs.Primary.ID) 123 124 if err == nil { 125 return fmt.Errorf("Record still exists") 126 } 127 } 128 129 return nil 130 } 131 132 func testAccCheckCLOudflareRecordAttributes(record *cloudflare.Record) resource.TestCheckFunc { 133 return func(s *terraform.State) error { 134 135 if record.Value != "192.168.0.10" { 136 return fmt.Errorf("Bad value: %s", record.Value) 137 } 138 139 return nil 140 } 141 } 142 143 func testAccCheckCLOudflareRecordAttributesUpdated(record *cloudflare.Record) resource.TestCheckFunc { 144 return func(s *terraform.State) error { 145 146 if record.Value != "192.168.0.11" { 147 return fmt.Errorf("Bad value: %s", record.Value) 148 } 149 150 return nil 151 } 152 } 153 154 func testAccCheckCLOudflareRecordExists(n string, record *cloudflare.Record) resource.TestCheckFunc { 155 return func(s *terraform.State) error { 156 rs, ok := s.RootModule().Resources[n] 157 158 if !ok { 159 return fmt.Errorf("Not found: %s", n) 160 } 161 162 if rs.Primary.ID == "" { 163 return fmt.Errorf("No Record ID is set") 164 } 165 166 client := testAccProvider.Meta().(*cloudflare.Client) 167 168 foundRecord, err := client.RetrieveRecord(rs.Primary.Attributes["domain"], rs.Primary.ID) 169 170 if err != nil { 171 return err 172 } 173 174 if foundRecord.Id != rs.Primary.ID { 175 return fmt.Errorf("Record not found") 176 } 177 178 *record = *foundRecord 179 180 return nil 181 } 182 } 183 184 const testAccCheckCLoudFlareRecordConfig_basic = ` 185 resource "cloudflare_record" "foobar" { 186 domain = "%s" 187 188 name = "terraform" 189 value = "192.168.0.10" 190 type = "A" 191 ttl = 3600 192 }` 193 194 const testAccCheckCloudFlareRecordConfig_new_value = ` 195 resource "cloudflare_record" "foobar" { 196 domain = "%s" 197 198 name = "terraform" 199 value = "192.168.0.11" 200 type = "A" 201 ttl = 3600 202 }` 203 204 const testAccCheckCloudFlareRecordConfig_forceNew = ` 205 resource "cloudflare_record" "foobar" { 206 domain = "%s" 207 208 name = "terraform" 209 value = "%s" 210 type = "CNAME" 211 ttl = 3600 212 }`