Skip to content

Commit 716c36f

Browse files
committed
r/ovh_domain_zone_redirection: add sweeper for test
1 parent 021c6d1 commit 716c36f

3 files changed

+96
-18
lines changed

ovh/resource_ovh_domain_zone_record_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ import (
1313
"time"
1414
)
1515

16-
const (
17-
test_prefix = "testacc-terraform"
18-
)
19-
2016
func init() {
2117
resource.AddTestSweepers("ovh_domain_zone_record", &resource.Sweeper{
2218
Name: "ovh_domain_zone_record",

ovh/resource_ovh_domain_zone_redirection_test.go

Lines changed: 91 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,85 @@ import (
88

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

16+
func init() {
17+
resource.AddTestSweepers("ovh_domain_zone_redirection", &resource.Sweeper{
18+
Name: "ovh_domain_zone_redirection",
19+
F: testSweepDomainZoneRedirection,
20+
})
21+
}
22+
23+
func testSweepDomainZoneRedirection(region string) error {
24+
client, err := sharedClientForRegion(region)
25+
if err != nil {
26+
return fmt.Errorf("error getting client: %s", err)
27+
}
28+
29+
zoneName := os.Getenv("OVH_ZONE")
30+
if zoneName == "" {
31+
return fmt.Errorf("OVH_ZONE must be set")
32+
}
33+
34+
dz := &DomainZone{}
35+
36+
if err := client.Get(fmt.Sprintf("/domain/zone/%s", zoneName), &dz); err != nil {
37+
return fmt.Errorf("Error calling /domain/zone/%s:\n\t %q", zoneName, err)
38+
}
39+
40+
redirections := make([]int64, 0)
41+
if err := client.Get(fmt.Sprintf("/domain/zone/%s/redirection", zoneName), &redirections); err != nil {
42+
return fmt.Errorf("Error calling /domain/zone/%s:\n\t %q", zoneName, err)
43+
}
44+
45+
if len(redirections) == 0 {
46+
log.Print("[DEBUG] No redirection to sweep")
47+
return nil
48+
}
49+
50+
for _, rec := range redirections {
51+
redirection := &OvhDomainZoneRedirection{}
52+
53+
if err := client.Get(fmt.Sprintf("/domain/zone/%s/redirection/%v", zoneName, rec), &redirection); err != nil {
54+
return fmt.Errorf("Error calling /domain/zone/%s/redirection/%v:\n\t %q", zoneName, rec, err)
55+
}
56+
57+
if !strings.HasPrefix(redirection.SubDomain, test_prefix) {
58+
continue
59+
}
60+
61+
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
62+
if err := client.Delete(fmt.Sprintf("/domain/zone/%s/redirection/%v", zoneName, rec), nil); err != nil {
63+
return resource.RetryableError(err)
64+
}
65+
// Successful delete
66+
return nil
67+
})
68+
if err != nil {
69+
return err
70+
}
71+
}
72+
73+
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
74+
err := client.Post(
75+
fmt.Sprintf("/domain/zone/%s/refresh", zoneName),
76+
nil,
77+
nil,
78+
)
79+
80+
if err != nil {
81+
return resource.RetryableError(fmt.Errorf("Error refresh OVH Zone: %s", err))
82+
}
83+
// Successful refresh
84+
return nil
85+
})
86+
87+
return nil
88+
}
89+
1390
func TestAccOvhDomainZoneRedirection_Basic(t *testing.T) {
1491
var redirection OvhDomainZoneRedirection
1592
zone := os.Getenv("OVH_ZONE")
@@ -20,11 +97,11 @@ func TestAccOvhDomainZoneRedirection_Basic(t *testing.T) {
2097
CheckDestroy: testAccCheckOvhDomainZoneRedirectionDestroy,
2198
Steps: []resource.TestStep{
2299
resource.TestStep{
23-
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRedirectionConfig_basic, zone),
100+
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRedirectionConfig_basic, zone, test_prefix),
24101
Check: resource.ComposeTestCheckFunc(
25102
testAccCheckOvhDomainZoneRedirectionExists("ovh_domain_zone_redirection.foobar", &redirection),
26103
resource.TestCheckResourceAttr(
27-
"ovh_domain_zone_redirection.foobar", "subdomain", "terraform"),
104+
"ovh_domain_zone_redirection.foobar", "subdomain", test_prefix),
28105
resource.TestCheckResourceAttr(
29106
"ovh_domain_zone_redirection.foobar", "zone", zone),
30107
resource.TestCheckResourceAttr(
@@ -47,47 +124,47 @@ func TestAccOvhDomainZoneRedirection_Updated(t *testing.T) {
47124
CheckDestroy: testAccCheckOvhDomainZoneRedirectionDestroy,
48125
Steps: []resource.TestStep{
49126
resource.TestStep{
50-
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRedirectionConfig_basic, zone),
127+
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRedirectionConfig_basic, zone, test_prefix),
51128
Check: resource.ComposeTestCheckFunc(
52129
testAccCheckOvhDomainZoneRedirectionExists("ovh_domain_zone_redirection.foobar", &redirection),
53130
resource.TestCheckResourceAttr(
54-
"ovh_domain_zone_redirection.foobar", "subdomain", "terraform"),
131+
"ovh_domain_zone_redirection.foobar", "subdomain", test_prefix),
55132
resource.TestCheckResourceAttr(
56133
"ovh_domain_zone_redirection.foobar", "zone", zone),
57134
resource.TestCheckResourceAttr(
58135
"ovh_domain_zone_redirection.foobar", "target", "https://terraform.net"),
59136
),
60137
},
61138
resource.TestStep{
62-
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRedirectionConfig_new_value_1, zone),
139+
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRedirectionConfig_new_value_1, zone, test_prefix),
63140
Check: resource.ComposeTestCheckFunc(
64141
testAccCheckOvhDomainZoneRedirectionExists("ovh_domain_zone_redirection.foobar", &redirection),
65142
resource.TestCheckResourceAttr(
66-
"ovh_domain_zone_redirection.foobar", "subdomain", "terraform"),
143+
"ovh_domain_zone_redirection.foobar", "subdomain", test_prefix),
67144
resource.TestCheckResourceAttr(
68145
"ovh_domain_zone_redirection.foobar", "zone", zone),
69146
resource.TestCheckResourceAttr(
70147
"ovh_domain_zone_redirection.foobar", "target", "https://terraform.io"),
71148
),
72149
},
73150
resource.TestStep{
74-
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRedirectionConfig_new_value_2, zone),
151+
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRedirectionConfig_new_value_2, zone, test_prefix),
75152
Check: resource.ComposeTestCheckFunc(
76153
testAccCheckOvhDomainZoneRedirectionExists("ovh_domain_zone_redirection.foobar", &redirection),
77154
resource.TestCheckResourceAttr(
78-
"ovh_domain_zone_redirection.foobar", "subdomain", "terraform2"),
155+
"ovh_domain_zone_redirection.foobar", "subdomain", fmt.Sprintf("%s2", test_prefix)),
79156
resource.TestCheckResourceAttr(
80157
"ovh_domain_zone_redirection.foobar", "zone", zone),
81158
resource.TestCheckResourceAttr(
82159
"ovh_domain_zone_redirection.foobar", "target", "https://terraform.io"),
83160
),
84161
},
85162
resource.TestStep{
86-
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRedirectionConfig_new_value_3, zone),
163+
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRedirectionConfig_new_value_3, zone, test_prefix),
87164
Check: resource.ComposeTestCheckFunc(
88165
testAccCheckOvhDomainZoneRedirectionExists("ovh_domain_zone_redirection.foobar", &redirection),
89166
resource.TestCheckResourceAttr(
90-
"ovh_domain_zone_redirection.foobar", "subdomain", "terraform3"),
167+
"ovh_domain_zone_redirection.foobar", "subdomain", fmt.Sprintf("%s3", test_prefix)),
91168
resource.TestCheckResourceAttr(
92169
"ovh_domain_zone_redirection.foobar", "zone", zone),
93170
resource.TestCheckResourceAttr(
@@ -156,15 +233,15 @@ func testAccCheckOvhDomainZoneRedirectionExists(n string, redirection *OvhDomain
156233
const testAccCheckOvhDomainZoneRedirectionConfig_basic = `
157234
resource "ovh_domain_zone_redirection" "foobar" {
158235
zone = "%s"
159-
subdomain = "terraform"
236+
subdomain = "%s"
160237
target = "https://terraform.net"
161238
type = "visible"
162239
}`
163240

164241
const testAccCheckOvhDomainZoneRedirectionConfig_new_value_1 = `
165242
resource "ovh_domain_zone_redirection" "foobar" {
166243
zone = "%s"
167-
subdomain = "terraform"
244+
subdomain = "%s"
168245
target = "https://terraform.io"
169246
type = "visible"
170247
}
@@ -173,7 +250,7 @@ resource "ovh_domain_zone_redirection" "foobar" {
173250
const testAccCheckOvhDomainZoneRedirectionConfig_new_value_2 = `
174251
resource "ovh_domain_zone_redirection" "foobar" {
175252
zone = "%s"
176-
subdomain = "terraform2"
253+
subdomain = "%s2"
177254
target = "https://terraform.io"
178255
type = "visible"
179256
}
@@ -182,7 +259,7 @@ resource "ovh_domain_zone_redirection" "foobar" {
182259
const testAccCheckOvhDomainZoneRedirectionConfig_new_value_3 = `
183260
resource "ovh_domain_zone_redirection" "foobar" {
184261
zone = "%s"
185-
subdomain = "terraform3"
262+
subdomain = "%s3"
186263
target = "https://terraform.com"
187264
type = "visible"
188265
}`

ovh/testing.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package ovh
2+
3+
const (
4+
test_prefix = "testacc-terraform"
5+
)

0 commit comments

Comments
 (0)