github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/ultradns/resource_ultradns_record_test.go (about) 1 package ultradns 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 8 "github.com/Ensighten/udnssdk" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccUltraDNSRecord_Basic(t *testing.T) { 14 var record udnssdk.RRSet 15 domain := os.Getenv("ULTRADNS_DOMAIN") 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckUltraDNSRecordDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: fmt.Sprintf(testAccCheckUltraDNSRecordConfigBasic, domain), 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckUltraDNSRecordExists("ultradns_record.foobar", &record), 26 testAccCheckUltraDNSRecordAttributes(&record), 27 resource.TestCheckResourceAttr( 28 "ultradns_record.foobar", "name", "terraform"), 29 resource.TestCheckResourceAttr( 30 "ultradns_record.foobar", "zone", domain), 31 resource.TestCheckResourceAttr( 32 "ultradns_record.foobar", "rdata.0", "192.168.0.10"), 33 ), 34 }, 35 }, 36 }) 37 } 38 39 func TestAccUltraDNSRecord_Updated(t *testing.T) { 40 var record udnssdk.RRSet 41 domain := os.Getenv("ULTRADNS_DOMAIN") 42 43 resource.Test(t, resource.TestCase{ 44 PreCheck: func() { testAccPreCheck(t) }, 45 Providers: testAccProviders, 46 CheckDestroy: testAccCheckUltraDNSRecordDestroy, 47 Steps: []resource.TestStep{ 48 resource.TestStep{ 49 Config: fmt.Sprintf(testAccCheckUltraDNSRecordConfigBasic, domain), 50 Check: resource.ComposeTestCheckFunc( 51 testAccCheckUltraDNSRecordExists("ultradns_record.foobar", &record), 52 testAccCheckUltraDNSRecordAttributes(&record), 53 resource.TestCheckResourceAttr( 54 "ultradns_record.foobar", "name", "terraform"), 55 resource.TestCheckResourceAttr( 56 "ultradns_record.foobar", "zone", domain), 57 resource.TestCheckResourceAttr( 58 "ultradns_record.foobar", "rdata.0", "192.168.0.10"), 59 ), 60 }, 61 resource.TestStep{ 62 Config: fmt.Sprintf(testAccCheckUltraDNSRecordConfigNewValue, domain), 63 Check: resource.ComposeTestCheckFunc( 64 testAccCheckUltraDNSRecordExists("ultradns_record.foobar", &record), 65 testAccCheckUltraDNSRecordAttributesUpdated(&record), 66 resource.TestCheckResourceAttr( 67 "ultradns_record.foobar", "name", "terraform"), 68 resource.TestCheckResourceAttr( 69 "ultradns_record.foobar", "zone", domain), 70 resource.TestCheckResourceAttr( 71 "ultradns_record.foobar", "rdata.0", "192.168.0.11"), 72 ), 73 }, 74 }, 75 }) 76 } 77 78 func testAccCheckUltraDNSRecordDestroy(s *terraform.State) error { 79 client := testAccProvider.Meta().(*udnssdk.Client) 80 81 for _, rs := range s.RootModule().Resources { 82 if rs.Type != "ultradns_record" { 83 continue 84 } 85 86 k := udnssdk.RRSetKey{ 87 Zone: rs.Primary.Attributes["zone"], 88 Name: rs.Primary.Attributes["name"], 89 Type: rs.Primary.Attributes["type"], 90 } 91 92 _, err := client.RRSets.Select(k) 93 94 if err == nil { 95 return fmt.Errorf("Record still exists") 96 } 97 } 98 99 return nil 100 } 101 102 func testAccCheckUltraDNSRecordAttributes(record *udnssdk.RRSet) resource.TestCheckFunc { 103 return func(s *terraform.State) error { 104 105 if record.RData[0] != "192.168.0.10" { 106 return fmt.Errorf("Bad content: %v", record.RData) 107 } 108 109 return nil 110 } 111 } 112 113 func testAccCheckUltraDNSRecordAttributesUpdated(record *udnssdk.RRSet) resource.TestCheckFunc { 114 return func(s *terraform.State) error { 115 116 if record.RData[0] != "192.168.0.11" { 117 return fmt.Errorf("Bad content: %v", record.RData) 118 } 119 120 return nil 121 } 122 } 123 124 func testAccCheckUltraDNSRecordExists(n string, record *udnssdk.RRSet) resource.TestCheckFunc { 125 return func(s *terraform.State) error { 126 rs, ok := s.RootModule().Resources[n] 127 128 if !ok { 129 return fmt.Errorf("Not found: %s", n) 130 } 131 132 if rs.Primary.ID == "" { 133 return fmt.Errorf("No Record ID is set") 134 } 135 136 client := testAccProvider.Meta().(*udnssdk.Client) 137 k := udnssdk.RRSetKey{ 138 Zone: rs.Primary.Attributes["zone"], 139 Name: rs.Primary.Attributes["name"], 140 Type: rs.Primary.Attributes["type"], 141 } 142 143 foundRecord, err := client.RRSets.Select(k) 144 145 if err != nil { 146 return err 147 } 148 149 if foundRecord[0].OwnerName != rs.Primary.Attributes["hostname"] { 150 return fmt.Errorf("Record not found: %+v,\n %+v\n", foundRecord, rs.Primary.Attributes) 151 } 152 153 *record = foundRecord[0] 154 155 return nil 156 } 157 } 158 159 const testAccCheckUltraDNSRecordConfigBasic = ` 160 resource "ultradns_record" "foobar" { 161 zone = "%s" 162 163 name = "terraform" 164 rdata = [ "192.168.0.10" ] 165 type = "A" 166 ttl = 3600 167 }` 168 169 const testAccCheckUltraDNSRecordConfigNewValue = ` 170 resource "ultradns_record" "foobar" { 171 zone = "%s" 172 173 name = "terraform" 174 rdata = [ "192.168.0.11" ] 175 type = "A" 176 ttl = 3600 177 }`