Skip to content

Commit 1fdbd28

Browse files
authored
chore(r2_bucket_lock, r2_bucket_lifecycle): add acceptance tests (#6299)
1 parent e8a2650 commit 1fdbd28

17 files changed

+796
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package r2_bucket_lifecycle_test
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/cloudflare/terraform-provider-cloudflare/internal/acctest"
8+
"github.com/cloudflare/terraform-provider-cloudflare/internal/utils"
9+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
11+
"github.com/hashicorp/terraform-plugin-testing/statecheck"
12+
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
13+
)
14+
15+
func TestAccCloudflareR2BucketLifecycleDataSource_Basic(t *testing.T) {
16+
rnd := utils.GenerateRandomResourceName()
17+
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
18+
dataSourceName := "data.cloudflare_r2_bucket_lifecycle." + rnd
19+
20+
resource.Test(t, resource.TestCase{
21+
PreCheck: func() { acctest.TestAccPreCheck(t) },
22+
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
23+
Steps: []resource.TestStep{
24+
{
25+
Config: testAccR2BucketLifecycleDataSourceConfig(rnd, accountID),
26+
ConfigStateChecks: []statecheck.StateCheck{
27+
statecheck.ExpectKnownValue(dataSourceName, tfjsonpath.New("account_id"), knownvalue.StringExact(accountID)),
28+
statecheck.ExpectKnownValue(dataSourceName, tfjsonpath.New("bucket_name"), knownvalue.StringExact(rnd)),
29+
statecheck.ExpectKnownValue(dataSourceName, tfjsonpath.New("rules"), knownvalue.ListSizeExact(1)),
30+
statecheck.ExpectKnownValue(dataSourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("id"), knownvalue.StringExact("datasource-test-rule")),
31+
statecheck.ExpectKnownValue(dataSourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("enabled"), knownvalue.Bool(true)),
32+
statecheck.ExpectKnownValue(dataSourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("conditions").AtMapKey("prefix"), knownvalue.StringExact("test-data/")),
33+
statecheck.ExpectKnownValue(dataSourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("delete_objects_transition").AtMapKey("condition").AtMapKey("type"), knownvalue.StringExact("Age")),
34+
statecheck.ExpectKnownValue(dataSourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("delete_objects_transition").AtMapKey("condition").AtMapKey("max_age"), knownvalue.Int64Exact(604800)),
35+
},
36+
},
37+
},
38+
})
39+
}
40+
41+
func testAccR2BucketLifecycleDataSourceConfig(rnd, accountID string) string {
42+
return acctest.LoadTestCase("datasource_basic.tf", rnd, accountID)
43+
}
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
package r2_bucket_lifecycle_test
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/cloudflare/terraform-provider-cloudflare/internal/acctest"
8+
"github.com/cloudflare/terraform-provider-cloudflare/internal/utils"
9+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
11+
"github.com/hashicorp/terraform-plugin-testing/statecheck"
12+
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
13+
)
14+
15+
func TestAccCloudflareR2BucketLifecycle_Basic(t *testing.T) {
16+
rnd := utils.GenerateRandomResourceName()
17+
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
18+
resourceName := "cloudflare_r2_bucket_lifecycle." + rnd
19+
20+
resource.Test(t, resource.TestCase{
21+
PreCheck: func() { acctest.TestAccPreCheck(t) },
22+
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
23+
Steps: []resource.TestStep{
24+
{
25+
Config: testAccR2BucketLifecycleConfig(rnd, accountID),
26+
ConfigStateChecks: []statecheck.StateCheck{
27+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("account_id"), knownvalue.StringExact(accountID)),
28+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("bucket_name"), knownvalue.StringExact(rnd)),
29+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("jurisdiction"), knownvalue.StringExact("default")),
30+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rules"), knownvalue.ListSizeExact(1)),
31+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("id"), knownvalue.StringExact("delete-old-objects")),
32+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("enabled"), knownvalue.Bool(true)),
33+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("conditions").AtMapKey("prefix"), knownvalue.StringExact("logs/")),
34+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("delete_objects_transition").AtMapKey("condition").AtMapKey("type"), knownvalue.StringExact("Age")),
35+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("delete_objects_transition").AtMapKey("condition").AtMapKey("max_age"), knownvalue.Int64Exact(2592000)),
36+
},
37+
},
38+
},
39+
})
40+
}
41+
42+
func TestAccCloudflareR2BucketLifecycle_Update(t *testing.T) {
43+
rnd := utils.GenerateRandomResourceName()
44+
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
45+
resourceName := "cloudflare_r2_bucket_lifecycle." + rnd
46+
47+
resource.Test(t, resource.TestCase{
48+
PreCheck: func() { acctest.TestAccPreCheck(t) },
49+
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
50+
Steps: []resource.TestStep{
51+
{
52+
Config: testAccR2BucketLifecycleConfig(rnd, accountID),
53+
ConfigStateChecks: []statecheck.StateCheck{
54+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rules"), knownvalue.ListSizeExact(1)),
55+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("delete_objects_transition").AtMapKey("condition").AtMapKey("max_age"), knownvalue.Int64Exact(2592000)),
56+
},
57+
},
58+
{
59+
Config: testAccR2BucketLifecycleUpdateConfig(rnd, accountID),
60+
ConfigStateChecks: []statecheck.StateCheck{
61+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rules"), knownvalue.ListSizeExact(3)),
62+
statecheck.ExpectKnownValue(
63+
resourceName,
64+
tfjsonpath.New("rules"),
65+
knownvalue.SetPartial([]knownvalue.Check{
66+
knownvalue.ObjectPartial(map[string]knownvalue.Check{
67+
"id": knownvalue.StringExact("delete-old-objects"),
68+
"enabled": knownvalue.Bool(true),
69+
"conditions": knownvalue.ObjectPartial(map[string]knownvalue.Check{
70+
"prefix": knownvalue.StringExact("logs/"),
71+
}),
72+
"delete_objects_transition": knownvalue.ObjectPartial(map[string]knownvalue.Check{
73+
"condition": knownvalue.ObjectPartial(map[string]knownvalue.Check{
74+
"type": knownvalue.StringExact("Age"),
75+
"max_age": knownvalue.Int64Exact(5184000),
76+
}),
77+
}),
78+
}),
79+
knownvalue.ObjectPartial(map[string]knownvalue.Check{
80+
"id": knownvalue.StringExact("archive-objects"),
81+
"enabled": knownvalue.Bool(true),
82+
"conditions": knownvalue.ObjectPartial(map[string]knownvalue.Check{
83+
"prefix": knownvalue.StringExact("archive/"),
84+
}),
85+
"storage_class_transitions": knownvalue.ListPartial(map[int]knownvalue.Check{
86+
0: knownvalue.ObjectPartial(map[string]knownvalue.Check{
87+
"condition": knownvalue.ObjectPartial(map[string]knownvalue.Check{
88+
"type": knownvalue.StringExact("Age"),
89+
"max_age": knownvalue.Int64Exact(604800),
90+
}),
91+
"storage_class": knownvalue.StringExact("InfrequentAccess"),
92+
}),
93+
}),
94+
}),
95+
knownvalue.ObjectPartial(map[string]knownvalue.Check{
96+
"id": knownvalue.StringExact("cleanup-multipart"),
97+
"enabled": knownvalue.Bool(true),
98+
"conditions": knownvalue.ObjectPartial(map[string]knownvalue.Check{
99+
"prefix": knownvalue.StringExact(""),
100+
}),
101+
"abort_multipart_uploads_transition": knownvalue.ObjectPartial(map[string]knownvalue.Check{
102+
"condition": knownvalue.ObjectPartial(map[string]knownvalue.Check{
103+
"type": knownvalue.StringExact("Age"),
104+
"max_age": knownvalue.Int64Exact(86400),
105+
}),
106+
}),
107+
}),
108+
}),
109+
),
110+
},
111+
},
112+
},
113+
})
114+
}
115+
116+
func TestAccCloudflareR2BucketLifecycle_JurisdictionEU(t *testing.T) {
117+
rnd := utils.GenerateRandomResourceName()
118+
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
119+
resourceName := "cloudflare_r2_bucket_lifecycle." + rnd
120+
121+
resource.Test(t, resource.TestCase{
122+
PreCheck: func() { acctest.TestAccPreCheck(t) },
123+
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
124+
Steps: []resource.TestStep{
125+
{
126+
Config: testAccR2BucketLifecycleJurisdictionEUConfig(rnd, accountID),
127+
ConfigStateChecks: []statecheck.StateCheck{
128+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("account_id"), knownvalue.StringExact(accountID)),
129+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("bucket_name"), knownvalue.StringExact(rnd)),
130+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("jurisdiction"), knownvalue.StringExact("eu")),
131+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("id"), knownvalue.StringExact("eu-compliance-delete")),
132+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("conditions").AtMapKey("prefix"), knownvalue.StringExact("gdpr/")),
133+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("delete_objects_transition").AtMapKey("condition").AtMapKey("max_age"), knownvalue.Int64Exact(7776000)),
134+
},
135+
},
136+
},
137+
})
138+
}
139+
140+
func TestAccCloudflareR2BucketLifecycle_DateCondition(t *testing.T) {
141+
rnd := utils.GenerateRandomResourceName()
142+
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
143+
resourceName := "cloudflare_r2_bucket_lifecycle." + rnd
144+
145+
resource.Test(t, resource.TestCase{
146+
PreCheck: func() { acctest.TestAccPreCheck(t) },
147+
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
148+
Steps: []resource.TestStep{
149+
{
150+
Config: testAccR2BucketLifecycleDateConditionConfig(rnd, accountID),
151+
ConfigStateChecks: []statecheck.StateCheck{
152+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("id"), knownvalue.StringExact("delete-by-date")),
153+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("delete_objects_transition").AtMapKey("condition").AtMapKey("type"), knownvalue.StringExact("Date")),
154+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("delete_objects_transition").AtMapKey("condition").AtMapKey("date"), knownvalue.StringExact("2024-12-31T23:59:59Z")),
155+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("delete_objects_transition").AtMapKey("condition").AtMapKey("max_age"), knownvalue.Null()),
156+
},
157+
},
158+
},
159+
})
160+
}
161+
162+
func TestAccCloudflareR2BucketLifecycle_MinimalRules(t *testing.T) {
163+
rnd := utils.GenerateRandomResourceName()
164+
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
165+
resourceName := "cloudflare_r2_bucket_lifecycle." + rnd
166+
167+
resource.Test(t, resource.TestCase{
168+
PreCheck: func() { acctest.TestAccPreCheck(t) },
169+
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
170+
Steps: []resource.TestStep{
171+
{
172+
Config: testAccR2BucketLifecycleMinimalRulesConfig(rnd, accountID),
173+
ConfigStateChecks: []statecheck.StateCheck{
174+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("id"), knownvalue.StringExact("minimal-rule")),
175+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("enabled"), knownvalue.Bool(false)),
176+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("conditions").AtMapKey("prefix"), knownvalue.StringExact("")),
177+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("delete_objects_transition"), knownvalue.Null()),
178+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("storage_class_transitions"), knownvalue.ListSizeExact(0)),
179+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("rules").AtSliceIndex(0).AtMapKey("abort_multipart_uploads_transition"), knownvalue.Null()),
180+
},
181+
},
182+
},
183+
})
184+
}
185+
186+
func testAccR2BucketLifecycleConfig(rnd, accountID string) string {
187+
return acctest.LoadTestCase("r2_lifecycle_basic.tf", rnd, accountID)
188+
}
189+
190+
func testAccR2BucketLifecycleUpdateConfig(rnd, accountID string) string {
191+
return acctest.LoadTestCase("r2_lifecycle_multiple_rules.tf", rnd, accountID)
192+
}
193+
194+
func testAccR2BucketLifecycleJurisdictionEUConfig(rnd, accountID string) string {
195+
return acctest.LoadTestCase("r2_lifecycle_jurisdiction_eu.tf", rnd, accountID)
196+
}
197+
198+
func testAccR2BucketLifecycleDateConditionConfig(rnd, accountID string) string {
199+
return acctest.LoadTestCase("r2_lifecycle_date_condition.tf", rnd, accountID)
200+
}
201+
202+
func testAccR2BucketLifecycleMinimalRulesConfig(rnd, accountID string) string {
203+
return acctest.LoadTestCase("r2_lifecycle_minimal_rules.tf", rnd, accountID)
204+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
resource "cloudflare_r2_bucket" "%[1]s" {
2+
account_id = "%[2]s"
3+
name = "%[1]s"
4+
}
5+
6+
resource "cloudflare_r2_bucket_lifecycle" "%[1]s" {
7+
account_id = "%[2]s"
8+
bucket_name = cloudflare_r2_bucket.%[1]s.name
9+
10+
rules = [{
11+
id = "datasource-test-rule"
12+
enabled = true
13+
conditions = {
14+
prefix = "test-data/"
15+
}
16+
delete_objects_transition = {
17+
condition = {
18+
type = "Age"
19+
max_age = 604800 # 7 days
20+
}
21+
}
22+
}]
23+
}
24+
25+
data "cloudflare_r2_bucket_lifecycle" "%[1]s" {
26+
account_id = "%[2]s"
27+
bucket_name = cloudflare_r2_bucket_lifecycle.%[1]s.bucket_name
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
resource "cloudflare_r2_bucket" "%[1]s" {
2+
account_id = "%[2]s"
3+
name = "%[1]s"
4+
}
5+
6+
resource "cloudflare_r2_bucket_lifecycle" "%[1]s" {
7+
account_id = "%[2]s"
8+
bucket_name = cloudflare_r2_bucket.%[1]s.name
9+
10+
rules = [{
11+
id = "delete-old-objects"
12+
enabled = true
13+
conditions = {
14+
prefix = "logs/"
15+
}
16+
delete_objects_transition = {
17+
condition = {
18+
type = "Age"
19+
max_age = 2592000 # 30 days
20+
}
21+
}
22+
}]
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
resource "cloudflare_r2_bucket" "%[1]s" {
2+
account_id = "%[2]s"
3+
name = "%[1]s"
4+
}
5+
6+
resource "cloudflare_r2_bucket_lifecycle" "%[1]s" {
7+
account_id = "%[2]s"
8+
bucket_name = cloudflare_r2_bucket.%[1]s.name
9+
10+
rules = [{
11+
id = "delete-by-date"
12+
enabled = true
13+
conditions = {
14+
prefix = "temp/"
15+
}
16+
delete_objects_transition = {
17+
condition = {
18+
type = "Date"
19+
date = "2024-12-31T23:59:59Z"
20+
}
21+
}
22+
}]
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
resource "cloudflare_r2_bucket" "%[1]s" {
2+
account_id = "%[2]s"
3+
name = "%[1]s"
4+
jurisdiction = "eu"
5+
}
6+
7+
resource "cloudflare_r2_bucket_lifecycle" "%[1]s" {
8+
account_id = "%[2]s"
9+
bucket_name = cloudflare_r2_bucket.%[1]s.name
10+
jurisdiction = "eu"
11+
12+
rules = [{
13+
id = "eu-compliance-delete"
14+
enabled = true
15+
conditions = {
16+
prefix = "gdpr/"
17+
}
18+
delete_objects_transition = {
19+
condition = {
20+
type = "Age"
21+
max_age = 7776000 # 90 days
22+
}
23+
}
24+
}]
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
resource "cloudflare_r2_bucket" "%[1]s" {
2+
account_id = "%[2]s"
3+
name = "%[1]s"
4+
}
5+
6+
resource "cloudflare_r2_bucket_lifecycle" "%[1]s" {
7+
account_id = "%[2]s"
8+
bucket_name = cloudflare_r2_bucket.%[1]s.name
9+
10+
rules = [{
11+
id = "minimal-rule"
12+
enabled = false
13+
conditions = {
14+
prefix = ""
15+
}
16+
storage_class_transitions = []
17+
}]
18+
}

0 commit comments

Comments
 (0)