github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/dyn/resource_dyn_record_test.go (about) 1 package dyn 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/nesv/go-dynect/dynect" 11 ) 12 13 func TestAccDynRecord_Basic(t *testing.T) { 14 var record dynect.Record 15 zone := os.Getenv("DYN_ZONE") 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckDynRecordDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: fmt.Sprintf(testAccCheckDynRecordConfig_basic, zone), 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckDynRecordExists("dyn_record.foobar", &record), 26 testAccCheckDynRecordAttributes(&record), 27 resource.TestCheckResourceAttr( 28 "dyn_record.foobar", "name", "terraform"), 29 resource.TestCheckResourceAttr( 30 "dyn_record.foobar", "zone", zone), 31 resource.TestCheckResourceAttr( 32 "dyn_record.foobar", "value", "192.168.0.10"), 33 ), 34 }, 35 }, 36 }) 37 } 38 39 func TestAccDynRecord_Updated(t *testing.T) { 40 var record dynect.Record 41 zone := os.Getenv("DYN_ZONE") 42 43 resource.Test(t, resource.TestCase{ 44 PreCheck: func() { testAccPreCheck(t) }, 45 Providers: testAccProviders, 46 CheckDestroy: testAccCheckDynRecordDestroy, 47 Steps: []resource.TestStep{ 48 resource.TestStep{ 49 Config: fmt.Sprintf(testAccCheckDynRecordConfig_basic, zone), 50 Check: resource.ComposeTestCheckFunc( 51 testAccCheckDynRecordExists("dyn_record.foobar", &record), 52 testAccCheckDynRecordAttributes(&record), 53 resource.TestCheckResourceAttr( 54 "dyn_record.foobar", "name", "terraform"), 55 resource.TestCheckResourceAttr( 56 "dyn_record.foobar", "zone", zone), 57 resource.TestCheckResourceAttr( 58 "dyn_record.foobar", "value", "192.168.0.10"), 59 ), 60 }, 61 resource.TestStep{ 62 Config: fmt.Sprintf(testAccCheckDynRecordConfig_new_value, zone), 63 Check: resource.ComposeTestCheckFunc( 64 testAccCheckDynRecordExists("dyn_record.foobar", &record), 65 testAccCheckDynRecordAttributesUpdated(&record), 66 resource.TestCheckResourceAttr( 67 "dyn_record.foobar", "name", "terraform"), 68 resource.TestCheckResourceAttr( 69 "dyn_record.foobar", "zone", zone), 70 resource.TestCheckResourceAttr( 71 "dyn_record.foobar", "value", "192.168.0.11"), 72 ), 73 }, 74 }, 75 }) 76 } 77 78 func TestAccDynRecord_Multiple(t *testing.T) { 79 var record dynect.Record 80 zone := os.Getenv("DYN_ZONE") 81 82 resource.Test(t, resource.TestCase{ 83 PreCheck: func() { testAccPreCheck(t) }, 84 Providers: testAccProviders, 85 CheckDestroy: testAccCheckDynRecordDestroy, 86 Steps: []resource.TestStep{ 87 resource.TestStep{ 88 Config: fmt.Sprintf(testAccCheckDynRecordConfig_multiple, zone, zone, zone), 89 Check: resource.ComposeTestCheckFunc( 90 testAccCheckDynRecordExists("dyn_record.foobar1", &record), 91 testAccCheckDynRecordAttributes(&record), 92 resource.TestCheckResourceAttr( 93 "dyn_record.foobar1", "name", "terraform1"), 94 resource.TestCheckResourceAttr( 95 "dyn_record.foobar1", "zone", zone), 96 resource.TestCheckResourceAttr( 97 "dyn_record.foobar1", "value", "192.168.0.10"), 98 resource.TestCheckResourceAttr( 99 "dyn_record.foobar2", "name", "terraform2"), 100 resource.TestCheckResourceAttr( 101 "dyn_record.foobar2", "zone", zone), 102 resource.TestCheckResourceAttr( 103 "dyn_record.foobar2", "value", "192.168.1.10"), 104 resource.TestCheckResourceAttr( 105 "dyn_record.foobar3", "name", "terraform3"), 106 resource.TestCheckResourceAttr( 107 "dyn_record.foobar3", "zone", zone), 108 resource.TestCheckResourceAttr( 109 "dyn_record.foobar3", "value", "192.168.2.10"), 110 ), 111 }, 112 }, 113 }) 114 } 115 116 func testAccCheckDynRecordDestroy(s *terraform.State) error { 117 client := testAccProvider.Meta().(*dynect.ConvenientClient) 118 119 for _, rs := range s.RootModule().Resources { 120 if rs.Type != "dyn_record" { 121 continue 122 } 123 124 foundRecord := &dynect.Record{ 125 Zone: rs.Primary.Attributes["zone"], 126 ID: rs.Primary.ID, 127 FQDN: rs.Primary.Attributes["fqdn"], 128 Type: rs.Primary.Attributes["type"], 129 } 130 131 err := client.GetRecord(foundRecord) 132 133 if err != nil { 134 return fmt.Errorf("Record still exists") 135 } 136 } 137 138 return nil 139 } 140 141 func testAccCheckDynRecordAttributes(record *dynect.Record) resource.TestCheckFunc { 142 return func(s *terraform.State) error { 143 144 if record.Value != "192.168.0.10" { 145 return fmt.Errorf("Bad value: %s", record.Value) 146 } 147 148 return nil 149 } 150 } 151 152 func testAccCheckDynRecordAttributesUpdated(record *dynect.Record) resource.TestCheckFunc { 153 return func(s *terraform.State) error { 154 155 if record.Value != "192.168.0.11" { 156 return fmt.Errorf("Bad value: %s", record.Value) 157 } 158 159 return nil 160 } 161 } 162 163 func testAccCheckDynRecordExists(n string, record *dynect.Record) resource.TestCheckFunc { 164 return func(s *terraform.State) error { 165 rs, ok := s.RootModule().Resources[n] 166 167 if !ok { 168 return fmt.Errorf("Not found: %s", n) 169 } 170 171 if rs.Primary.ID == "" { 172 return fmt.Errorf("No Record ID is set") 173 } 174 175 client := testAccProvider.Meta().(*dynect.ConvenientClient) 176 177 foundRecord := &dynect.Record{ 178 Zone: rs.Primary.Attributes["zone"], 179 ID: rs.Primary.ID, 180 FQDN: rs.Primary.Attributes["fqdn"], 181 Type: rs.Primary.Attributes["type"], 182 } 183 184 err := client.GetRecord(foundRecord) 185 186 if err != nil { 187 return err 188 } 189 190 if foundRecord.ID != rs.Primary.ID { 191 return fmt.Errorf("Record not found") 192 } 193 194 *record = *foundRecord 195 196 return nil 197 } 198 } 199 200 const testAccCheckDynRecordConfig_basic = ` 201 resource "dyn_record" "foobar" { 202 zone = "%s" 203 name = "terraform" 204 value = "192.168.0.10" 205 type = "A" 206 ttl = 3600 207 }` 208 209 const testAccCheckDynRecordConfig_new_value = ` 210 resource "dyn_record" "foobar" { 211 zone = "%s" 212 name = "terraform" 213 value = "192.168.0.11" 214 type = "A" 215 ttl = 3600 216 }` 217 218 const testAccCheckDynRecordConfig_multiple = ` 219 resource "dyn_record" "foobar1" { 220 zone = "%s" 221 name = "terraform1" 222 value = "192.168.0.10" 223 type = "A" 224 ttl = 3600 225 } 226 resource "dyn_record" "foobar2" { 227 zone = "%s" 228 name = "terraform2" 229 value = "192.168.1.10" 230 type = "A" 231 ttl = 3600 232 } 233 resource "dyn_record" "foobar3" { 234 zone = "%s" 235 name = "terraform3" 236 value = "192.168.2.10" 237 type = "A" 238 ttl = 3600 239 }`