github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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 testAccCheckCLOudflareRecordDestroy(s *terraform.State) error { 79 client := testAccProvider.Meta().(*cloudflare.Client) 80 81 for _, rs := range s.RootModule().Resources { 82 if rs.Type != "cloudflare_record" { 83 continue 84 } 85 86 _, err := client.RetrieveRecord(rs.Primary.Attributes["domain"], rs.Primary.ID) 87 88 if err == nil { 89 return fmt.Errorf("Record still exists") 90 } 91 } 92 93 return nil 94 } 95 96 func testAccCheckCLOudflareRecordAttributes(record *cloudflare.Record) resource.TestCheckFunc { 97 return func(s *terraform.State) error { 98 99 if record.Value != "192.168.0.10" { 100 return fmt.Errorf("Bad value: %s", record.Value) 101 } 102 103 return nil 104 } 105 } 106 107 func testAccCheckCLOudflareRecordAttributesUpdated(record *cloudflare.Record) resource.TestCheckFunc { 108 return func(s *terraform.State) error { 109 110 if record.Value != "192.168.0.11" { 111 return fmt.Errorf("Bad value: %s", record.Value) 112 } 113 114 return nil 115 } 116 } 117 118 func testAccCheckCLOudflareRecordExists(n string, record *cloudflare.Record) resource.TestCheckFunc { 119 return func(s *terraform.State) error { 120 rs, ok := s.RootModule().Resources[n] 121 122 if !ok { 123 return fmt.Errorf("Not found: %s", n) 124 } 125 126 if rs.Primary.ID == "" { 127 return fmt.Errorf("No Record ID is set") 128 } 129 130 client := testAccProvider.Meta().(*cloudflare.Client) 131 132 foundRecord, err := client.RetrieveRecord(rs.Primary.Attributes["domain"], rs.Primary.ID) 133 134 if err != nil { 135 return err 136 } 137 138 if foundRecord.Id != rs.Primary.ID { 139 return fmt.Errorf("Record not found") 140 } 141 142 *record = *foundRecord 143 144 return nil 145 } 146 } 147 148 const testAccCheckCLoudFlareRecordConfig_basic = ` 149 resource "cloudflare_record" "foobar" { 150 domain = "%s" 151 152 name = "terraform" 153 value = "192.168.0.10" 154 type = "A" 155 ttl = 3600 156 }` 157 158 const testAccCheckCloudFlareRecordConfig_new_value = ` 159 resource "cloudflare_record" "foobar" { 160 domain = "%s" 161 162 name = "terraform" 163 value = "192.168.0.11" 164 type = "A" 165 ttl = 3600 166 }`