github.com/sarguru/terraform@v0.6.17-0.20160525232901-8fcdfd7e3dc9/builtin/providers/nsone/resource_record_test.go (about) 1 package nsone 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/bobtfish/go-nsone-api" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestAccRecord_basic(t *testing.T) { 13 var record nsone.Record 14 resource.Test(t, resource.TestCase{ 15 PreCheck: func() { testAccPreCheck(t) }, 16 Providers: testAccProviders, 17 CheckDestroy: testAccCheckRecordDestroy, 18 Steps: []resource.TestStep{ 19 resource.TestStep{ 20 Config: testAccRecordBasic, 21 Check: resource.ComposeTestCheckFunc( 22 testAccCheckRecordState("domain", "test.terraform.io"), 23 testAccCheckRecordExists("nsone_record.foobar", &record), 24 testAccCheckRecordAttributes(&record), 25 ), 26 }, 27 }, 28 }) 29 } 30 31 func TestAccRecord_updated(t *testing.T) { 32 var record nsone.Record 33 resource.Test(t, resource.TestCase{ 34 PreCheck: func() { testAccPreCheck(t) }, 35 Providers: testAccProviders, 36 CheckDestroy: testAccCheckRecordDestroy, 37 Steps: []resource.TestStep{ 38 resource.TestStep{ 39 Config: testAccRecordBasic, 40 Check: resource.ComposeTestCheckFunc( 41 testAccCheckRecordState("domain", "test.terraform.io"), 42 testAccCheckRecordExists("nsone_record.foobar", &record), 43 testAccCheckRecordAttributes(&record), 44 ), 45 }, 46 resource.TestStep{ 47 Config: testAccRecordUpdated, 48 Check: resource.ComposeTestCheckFunc( 49 testAccCheckRecordState("domain", "test.terraform.io"), 50 testAccCheckRecordExists("nsone_record.foobar", &record), 51 testAccCheckRecordAttributesUpdated(&record), 52 ), 53 }, 54 }, 55 }) 56 } 57 58 func testAccCheckRecordState(key, value string) resource.TestCheckFunc { 59 return func(s *terraform.State) error { 60 rs, ok := s.RootModule().Resources["nsone_record.foobar"] 61 if !ok { 62 return fmt.Errorf("Not found: %s", "nsone_record.foobar") 63 } 64 65 if rs.Primary.ID == "" { 66 return fmt.Errorf("No ID is set") 67 } 68 69 p := rs.Primary 70 if p.Attributes[key] != value { 71 return fmt.Errorf( 72 "%s != %s (actual: %s)", key, value, p.Attributes[key]) 73 } 74 75 return nil 76 } 77 } 78 79 func testAccCheckRecordExists(n string, record *nsone.Record) resource.TestCheckFunc { 80 return func(s *terraform.State) error { 81 rs, ok := s.RootModule().Resources[n] 82 83 if !ok { 84 return fmt.Errorf("Not found: %s", n) 85 } 86 87 if rs.Primary.ID == "" { 88 return fmt.Errorf("NoID is set") 89 } 90 91 client := testAccProvider.Meta().(*nsone.APIClient) 92 93 p := rs.Primary 94 95 foundRecord, err := client.GetRecord(p.Attributes["zone"], p.Attributes["domain"], p.Attributes["type"]) 96 97 if err != nil { 98 // return err 99 return fmt.Errorf("Record not found") 100 } 101 102 if foundRecord.Domain != p.Attributes["domain"] { 103 return fmt.Errorf("Record not found") 104 } 105 106 *record = *foundRecord 107 108 return nil 109 } 110 } 111 112 func testAccCheckRecordDestroy(s *terraform.State) error { 113 client := testAccProvider.Meta().(*nsone.APIClient) 114 115 var recordDomain string 116 var recordZone string 117 var recordType string 118 119 for _, rs := range s.RootModule().Resources { 120 if rs.Type != "nsone_record" { 121 continue 122 } 123 124 if rs.Type == "nsone_record" { 125 recordType = rs.Primary.Attributes["type"] 126 recordDomain = rs.Primary.Attributes["domain"] 127 recordZone = rs.Primary.Attributes["zone"] 128 } 129 } 130 131 foundRecord, _ := client.GetRecord(recordDomain, recordZone, recordType) 132 133 if foundRecord.Id != "" { 134 return fmt.Errorf("Record still exists") 135 } 136 137 return nil 138 } 139 140 func testAccCheckRecordAttributes(record *nsone.Record) resource.TestCheckFunc { 141 return func(s *terraform.State) error { 142 143 if record.Ttl != 60 { 144 return fmt.Errorf("Bad value : %d", record.Ttl) 145 } 146 147 recordAnswer := record.Answers[0] 148 recordAnswerString := recordAnswer.Answer[0] 149 150 if recordAnswerString != "test1.terraform.io" { 151 return fmt.Errorf("Bad value : %s", record.Ttl) 152 } 153 154 if recordAnswer.Region != "cal" { 155 return fmt.Errorf("Bad value : %s", recordAnswer.Region) 156 } 157 158 recordMetas := recordAnswer.Meta 159 160 if recordMetas["weight"].(float64) != 10 { 161 return fmt.Errorf("Bad value : %b", recordMetas["weight"].(float64)) 162 } 163 164 return nil 165 } 166 } 167 168 func testAccCheckRecordAttributesUpdated(record *nsone.Record) resource.TestCheckFunc { 169 return func(s *terraform.State) error { 170 171 if record.Ttl != 120 { 172 return fmt.Errorf("Bad value : %s", record.Ttl) 173 } 174 175 recordAnswer := record.Answers[1] 176 recordAnswerString := recordAnswer.Answer[0] 177 178 if recordAnswerString != "test3.terraform.io" { 179 return fmt.Errorf("Bad value for updated record: %s", recordAnswerString) 180 } 181 182 if recordAnswer.Region != "wa" { 183 return fmt.Errorf("Bad value : %s", recordAnswer.Region) 184 } 185 186 recordMetas := recordAnswer.Meta 187 188 if recordMetas["weight"].(float64) != 5 { 189 return fmt.Errorf("Bad value : %b", recordMetas["weight"].(float64)) 190 } 191 192 return nil 193 } 194 } 195 196 const testAccRecordBasic = ` 197 resource "nsone_record" "foobar" { 198 zone = "terraform.io" 199 domain = "test.terraform.io" 200 type = "CNAME" 201 ttl = 60 202 use_client_subnet = false 203 answers { 204 answer = "test1.terraform.io" 205 region = "cal" 206 meta { 207 field = "weight" 208 value = "10" 209 } 210 meta { 211 field = "up" 212 value = "1" 213 } 214 } 215 answers { 216 answer = "test2.terraform.io" 217 region = "ny" 218 meta { 219 field = "weight" 220 value = "10" 221 } 222 meta { 223 field = "up" 224 value = "1" 225 } 226 } 227 regions { 228 name = "cal" 229 us_state = "CA" 230 } 231 regions { 232 name = "ny" 233 us_state = "NY" 234 } 235 236 filters { 237 filter = "up" 238 } 239 filters { 240 filter = "geotarget_country" 241 } 242 } 243 resource "nsone_zone" "test" { 244 zone = "terraform.io" 245 }` 246 247 const testAccRecordUpdated = ` 248 resource "nsone_record" "foobar" { 249 zone = "terraform.io" 250 domain = "test.terraform.io" 251 type = "CNAME" 252 ttl = 120 253 use_client_subnet = false 254 answers { 255 answer = "test3.terraform.io" 256 region = "wa" 257 meta { 258 field = "weight" 259 value = "5" 260 } 261 meta { 262 field = "up" 263 value = "1" 264 } 265 } 266 answers { 267 answer = "test2.terraform.io" 268 region = "ny" 269 meta { 270 field = "weight" 271 value = "10" 272 } 273 meta { 274 field = "up" 275 value = "1" 276 } 277 } 278 regions { 279 name = "wa" 280 us_state = "WA" 281 } 282 regions { 283 name = "ny" 284 us_state = "NY" 285 } 286 287 filters { 288 filter = "up" 289 } 290 filters { 291 filter = "geotarget_country" 292 } 293 } 294 resource "nsone_zone" "test" { 295 zone = "terraform.io" 296 }`