Skip to content

Commit 021c6d1

Browse files
committed
r/ovh_domain_zone_record: add sweeper for tests
1 parent 69fcb58 commit 021c6d1

File tree

2 files changed

+148
-74
lines changed

2 files changed

+148
-74
lines changed

ovh/ovh_sweeper_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform/helper/resource"
9+
10+
"github.com/ovh/go-ovh/ovh"
11+
)
12+
13+
func TestMain(m *testing.M) {
14+
resource.TestMain(m)
15+
}
16+
17+
// sharedClientForRegion returns a common OVHClient setup needed for the sweeper
18+
// functions for a given region
19+
func sharedClientForRegion(region string) (*ovh.Client, error) {
20+
v := os.Getenv("OVH_ENDPOINT")
21+
if v == "" {
22+
return nil, fmt.Errorf("OVH_ENDPOINT must be set")
23+
}
24+
25+
v = os.Getenv("OVH_APPLICATION_KEY")
26+
if v == "" {
27+
return nil, fmt.Errorf("OVH_APPLICATION_KEY must be set")
28+
}
29+
30+
v = os.Getenv("OVH_APPLICATION_SECRET")
31+
if v == "" {
32+
return nil, fmt.Errorf("OVH_APPLICATION_SECRET must be set")
33+
}
34+
35+
v = os.Getenv("OVH_CONSUMER_KEY")
36+
if v == "" {
37+
return nil, fmt.Errorf("OVH_CONSUMER_KEY must be set")
38+
}
39+
40+
config := Config{
41+
Endpoint: os.Getenv("OVH_ENDPOINT"),
42+
ApplicationKey: os.Getenv("OVH_APPLICATION_KEY"),
43+
ApplicationSecret: os.Getenv("OVH_APPLICATION_SECRET"),
44+
ConsumerKey: os.Getenv("OVH_CONSUMER_KEY"),
45+
}
46+
47+
if err := config.loadAndValidate(); err != nil {
48+
return nil, fmt.Errorf("couln't load OVH Client: %s", err)
49+
50+
}
51+
52+
return config.OVHClient, nil
53+
}

ovh/resource_ovh_domain_zone_record_test.go

Lines changed: 95 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,89 @@ import (
88

99
"github.com/hashicorp/terraform/helper/resource"
1010
"github.com/hashicorp/terraform/terraform"
11+
"log"
12+
"strings"
13+
"time"
1114
)
1215

16+
const (
17+
test_prefix = "testacc-terraform"
18+
)
19+
20+
func init() {
21+
resource.AddTestSweepers("ovh_domain_zone_record", &resource.Sweeper{
22+
Name: "ovh_domain_zone_record",
23+
F: testSweepDomainZoneRecord,
24+
})
25+
}
26+
27+
func testSweepDomainZoneRecord(region string) error {
28+
client, err := sharedClientForRegion(region)
29+
if err != nil {
30+
return fmt.Errorf("error getting client: %s", err)
31+
}
32+
33+
zoneName := os.Getenv("OVH_ZONE")
34+
if zoneName == "" {
35+
return fmt.Errorf("OVH_ZONE must be set")
36+
}
37+
38+
dz := &DomainZone{}
39+
40+
if err := client.Get(fmt.Sprintf("/domain/zone/%s", zoneName), &dz); err != nil {
41+
return fmt.Errorf("Error calling /domain/zone/%s:\n\t %q", zoneName, err)
42+
}
43+
44+
records := make([]int64, 0)
45+
if err := client.Get(fmt.Sprintf("/domain/zone/%s/record", zoneName), &records); err != nil {
46+
return fmt.Errorf("Error calling /domain/zone/%s:\n\t %q", zoneName, err)
47+
}
48+
49+
if len(records) == 0 {
50+
log.Print("[DEBUG] No record to sweep")
51+
return nil
52+
}
53+
54+
for _, rec := range records {
55+
record := &OvhDomainZoneRecord{}
56+
57+
if err := client.Get(fmt.Sprintf("/domain/zone/%s/record/%v", zoneName, rec), &record); err != nil {
58+
return fmt.Errorf("Error calling /domain/zone/%s/record/%v:\n\t %q", zoneName, rec, err)
59+
}
60+
61+
if !strings.HasPrefix(record.SubDomain, test_prefix) {
62+
continue
63+
}
64+
65+
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
66+
if err := client.Delete(fmt.Sprintf("/domain/zone/%s/record/%v", zoneName, rec), nil); err != nil {
67+
return resource.RetryableError(err)
68+
}
69+
// Successful delete
70+
return nil
71+
})
72+
if err != nil {
73+
return err
74+
}
75+
}
76+
77+
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
78+
err := client.Post(
79+
fmt.Sprintf("/domain/zone/%s/refresh", zoneName),
80+
nil,
81+
nil,
82+
)
83+
84+
if err != nil {
85+
return resource.RetryableError(fmt.Errorf("Error refresh OVH Zone: %s", err))
86+
}
87+
// Successful refresh
88+
return nil
89+
})
90+
91+
return nil
92+
}
93+
1394
func TestAccOvhDomainZoneRecord_Basic(t *testing.T) {
1495
var record OvhDomainZoneRecord
1596
zone := os.Getenv("OVH_ZONE")
@@ -20,12 +101,11 @@ func TestAccOvhDomainZoneRecord_Basic(t *testing.T) {
20101
CheckDestroy: testAccCheckOvhDomainZoneRecordDestroy,
21102
Steps: []resource.TestStep{
22103
resource.TestStep{
23-
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRecordConfig_basic, zone),
104+
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRecordConfig_basic, zone, test_prefix),
24105
Check: resource.ComposeTestCheckFunc(
25106
testAccCheckOvhDomainZoneRecordExists("ovh_domain_zone_record.foobar", &record),
26-
testAccCheckOvhDomainZoneRecordAttributes(&record),
27107
resource.TestCheckResourceAttr(
28-
"ovh_domain_zone_record.foobar", "subdomain", "terraform"),
108+
"ovh_domain_zone_record.foobar", "subdomain", test_prefix),
29109
resource.TestCheckResourceAttr(
30110
"ovh_domain_zone_record.foobar", "zone", zone),
31111
resource.TestCheckResourceAttr(
@@ -48,12 +128,11 @@ func TestAccOvhDomainZoneRecord_Updated(t *testing.T) {
48128
CheckDestroy: testAccCheckOvhDomainZoneRecordDestroy,
49129
Steps: []resource.TestStep{
50130
resource.TestStep{
51-
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRecordConfig_basic, zone),
131+
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRecordConfig_basic, zone, test_prefix),
52132
Check: resource.ComposeTestCheckFunc(
53133
testAccCheckOvhDomainZoneRecordExists("ovh_domain_zone_record.foobar", &record),
54-
testAccCheckOvhDomainZoneRecordAttributes(&record),
55134
resource.TestCheckResourceAttr(
56-
"ovh_domain_zone_record.foobar", "subdomain", "terraform"),
135+
"ovh_domain_zone_record.foobar", "subdomain", test_prefix),
57136
resource.TestCheckResourceAttr(
58137
"ovh_domain_zone_record.foobar", "zone", zone),
59138
resource.TestCheckResourceAttr(
@@ -63,12 +142,11 @@ func TestAccOvhDomainZoneRecord_Updated(t *testing.T) {
63142
),
64143
},
65144
resource.TestStep{
66-
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRecordConfig_new_value_1, zone),
145+
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRecordConfig_new_value_1, zone, test_prefix),
67146
Check: resource.ComposeTestCheckFunc(
68147
testAccCheckOvhDomainZoneRecordExists("ovh_domain_zone_record.foobar", &record),
69-
testAccCheckOvhDomainZoneRecordAttributesUpdated_1(&record),
70148
resource.TestCheckResourceAttr(
71-
"ovh_domain_zone_record.foobar", "subdomain", "terraform"),
149+
"ovh_domain_zone_record.foobar", "subdomain", test_prefix),
72150
resource.TestCheckResourceAttr(
73151
"ovh_domain_zone_record.foobar", "zone", zone),
74152
resource.TestCheckResourceAttr(
@@ -78,12 +156,11 @@ func TestAccOvhDomainZoneRecord_Updated(t *testing.T) {
78156
),
79157
},
80158
resource.TestStep{
81-
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRecordConfig_new_value_2, zone),
159+
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRecordConfig_new_value_2, zone, test_prefix),
82160
Check: resource.ComposeTestCheckFunc(
83161
testAccCheckOvhDomainZoneRecordExists("ovh_domain_zone_record.foobar", &record),
84-
testAccCheckOvhDomainZoneRecordAttributesUpdated_2(&record),
85162
resource.TestCheckResourceAttr(
86-
"ovh_domain_zone_record.foobar", "subdomain", "terraform2"),
163+
"ovh_domain_zone_record.foobar", "subdomain", fmt.Sprintf("%s2", test_prefix)),
87164
resource.TestCheckResourceAttr(
88165
"ovh_domain_zone_record.foobar", "zone", zone),
89166
resource.TestCheckResourceAttr(
@@ -93,12 +170,11 @@ func TestAccOvhDomainZoneRecord_Updated(t *testing.T) {
93170
),
94171
},
95172
resource.TestStep{
96-
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRecordConfig_new_value_3, zone),
173+
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRecordConfig_new_value_3, zone, test_prefix),
97174
Check: resource.ComposeTestCheckFunc(
98175
testAccCheckOvhDomainZoneRecordExists("ovh_domain_zone_record.foobar", &record),
99-
testAccCheckOvhDomainZoneRecordAttributesUpdated_3(&record),
100176
resource.TestCheckResourceAttr(
101-
"ovh_domain_zone_record.foobar", "subdomain", "terraform3"),
177+
"ovh_domain_zone_record.foobar", "subdomain", fmt.Sprintf("%s3", test_prefix)),
102178
resource.TestCheckResourceAttr(
103179
"ovh_domain_zone_record.foobar", "zone", zone),
104180
resource.TestCheckResourceAttr(
@@ -166,65 +242,10 @@ func testAccCheckOvhDomainZoneRecordExists(n string, record *OvhDomainZoneRecord
166242
}
167243
}
168244

169-
func testAccCheckOvhDomainZoneRecordAttributes(record *OvhDomainZoneRecord) resource.TestCheckFunc {
170-
return func(s *terraform.State) error {
171-
172-
if record.Target != "192.168.0.10" {
173-
return fmt.Errorf("Bad content: %#v", record)
174-
}
175-
176-
return nil
177-
}
178-
}
179-
180-
func testAccCheckOvhDomainZoneRecordAttributesUpdated_1(record *OvhDomainZoneRecord) resource.TestCheckFunc {
181-
return func(s *terraform.State) error {
182-
183-
if record.Target != "192.168.0.11" {
184-
return fmt.Errorf("Bad content: %#v", record)
185-
}
186-
187-
return nil
188-
}
189-
}
190-
191-
func testAccCheckOvhDomainZoneRecordAttributesUpdated_2(record *OvhDomainZoneRecord) resource.TestCheckFunc {
192-
return func(s *terraform.State) error {
193-
194-
if record.Target != "192.168.0.11" {
195-
return fmt.Errorf("Bad content: %#v", record)
196-
}
197-
198-
if record.SubDomain != "terraform2" {
199-
return fmt.Errorf("Bad content: %#v", record)
200-
}
201-
202-
return nil
203-
}
204-
}
205-
206-
func testAccCheckOvhDomainZoneRecordAttributesUpdated_3(record *OvhDomainZoneRecord) resource.TestCheckFunc {
207-
return func(s *terraform.State) error {
208-
209-
if record.Target != "192.168.0.13" {
210-
return fmt.Errorf("Bad content: %#v", record)
211-
}
212-
213-
if record.SubDomain != "terraform3" {
214-
return fmt.Errorf("Bad content: %#v", record)
215-
}
216-
217-
if record.Ttl != 3604 {
218-
return fmt.Errorf("Bad content: %#v", record)
219-
}
220-
return nil
221-
}
222-
}
223-
224245
const testAccCheckOvhDomainZoneRecordConfig_basic = `
225246
resource "ovh_domain_zone_record" "foobar" {
226247
zone = "%s"
227-
subdomain = "terraform"
248+
subdomain = "%s"
228249
target = "192.168.0.10"
229250
fieldtype = "A"
230251
ttl = 3600
@@ -233,7 +254,7 @@ resource "ovh_domain_zone_record" "foobar" {
233254
const testAccCheckOvhDomainZoneRecordConfig_new_value_1 = `
234255
resource "ovh_domain_zone_record" "foobar" {
235256
zone = "%s"
236-
subdomain = "terraform"
257+
subdomain = "%s"
237258
target = "192.168.0.11"
238259
fieldtype = "A"
239260
ttl = 3600
@@ -242,7 +263,7 @@ resource "ovh_domain_zone_record" "foobar" {
242263
const testAccCheckOvhDomainZoneRecordConfig_new_value_2 = `
243264
resource "ovh_domain_zone_record" "foobar" {
244265
zone = "%s"
245-
subdomain = "terraform2"
266+
subdomain = "%s2"
246267
target = "192.168.0.11"
247268
fieldtype = "A"
248269
ttl = 3600
@@ -251,7 +272,7 @@ resource "ovh_domain_zone_record" "foobar" {
251272
const testAccCheckOvhDomainZoneRecordConfig_new_value_3 = `
252273
resource "ovh_domain_zone_record" "foobar" {
253274
zone = "%s"
254-
subdomain = "terraform3"
275+
subdomain = "%s3"
255276
target = "192.168.0.13"
256277
fieldtype = "A"
257278
ttl = 3604

0 commit comments

Comments
 (0)