Skip to content

Commit d886aa8

Browse files
authored
Merge pull request #5838 from orium/acc-test-url-normalization
Improve url normalization acceptance tests
2 parents c1101f8 + 1f71aa5 commit d886aa8

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

internal/services/url_normalization_settings/resource_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,66 @@
11
package url_normalization_settings_test
22

33
import (
4+
"context"
45
"fmt"
6+
"github.com/cloudflare/cloudflare-go/v5/url_normalization"
7+
"github.com/hashicorp/terraform-plugin-log/tflog"
8+
"log"
59
"os"
610
"regexp"
711
"testing"
812

13+
cloudflare "github.com/cloudflare/cloudflare-go/v5"
914
"github.com/cloudflare/terraform-provider-cloudflare/internal/acctest"
1015
"github.com/cloudflare/terraform-provider-cloudflare/internal/consts"
1116
"github.com/cloudflare/terraform-provider-cloudflare/internal/utils"
1217
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1318
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
1419
"github.com/hashicorp/terraform-plugin-testing/statecheck"
1520
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
21+
"github.com/pkg/errors"
1622
)
1723

24+
func init() {
25+
resource.AddTestSweepers("cloudflare_url_normalization_settings", &resource.Sweeper{
26+
Name: "cloudflare_url_normalization_settings",
27+
F: testSweepCloudflareURLNormalizationSettings,
28+
})
29+
}
30+
31+
func testSweepCloudflareURLNormalizationSettings(r string) error {
32+
ctx := context.Background()
33+
client := acctest.SharedClient()
34+
35+
// Clean up the account level rulesets
36+
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
37+
if zoneID == "" {
38+
return errors.New("CLOUDFLARE_ZONE_ID must be set")
39+
}
40+
41+
settings, err := client.URLNormalization.Get(context.Background(), url_normalization.URLNormalizationGetParams{
42+
ZoneID: cloudflare.F(zoneID),
43+
})
44+
if err != nil {
45+
tflog.Error(ctx, fmt.Sprintf("Failed to fetch Cloudflare url normalization settings: %s", err))
46+
}
47+
48+
if settings == nil {
49+
log.Print("[DEBUG] No Cloudflare url normalization settings to sweep")
50+
return nil
51+
}
52+
53+
err = client.URLNormalization.Delete(context.Background(), url_normalization.URLNormalizationDeleteParams{
54+
ZoneID: cloudflare.F(zoneID),
55+
})
56+
57+
if err != nil {
58+
tflog.Error(ctx, fmt.Sprintf("Failed to delete Cloudflare url normalization settings: %s", err))
59+
}
60+
61+
return nil
62+
}
63+
1864
func TestAccCloudflareURLNormalizationSettings_CreateThenUpdate(t *testing.T) {
1965

2066
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")

0 commit comments

Comments
 (0)