Skip to content

Commit 122b030

Browse files
authored
tests(object): FlattenObjectBucketVersioning testing (#3465)
1 parent d8c6184 commit 122b030

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

internal/services/object/bucket.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ func resourceObjectBucketRead(ctx context.Context, d *schema.ResourceData, m any
577577
}
578578
}
579579

580-
_ = d.Set("versioning", flattenObjectBucketVersioning(versioningResponse))
580+
_ = d.Set("versioning", FlattenObjectBucketVersioning(versioningResponse))
581581

582582
// Read the lifecycle configuration
583583
lifecycle, err := s3Client.GetBucketLifecycleConfiguration(ctx, &s3.GetBucketLifecycleConfigurationInput{

internal/services/object/helpers_object.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func IsS3Err(err error, code string, message string) bool {
257257
return false
258258
}
259259

260-
func flattenObjectBucketVersioning(versioningResponse *s3.GetBucketVersioningOutput) []map[string]any {
260+
func FlattenObjectBucketVersioning(versioningResponse *s3.GetBucketVersioningOutput) []map[string]any {
261261
vcl := []map[string]any{{}}
262262
if versioningResponse != nil {
263263
vcl[0]["enabled"] = versioningResponse.Status == s3Types.BucketVersioningStatusEnabled

internal/services/object/helpers_object_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package object_test
33
import (
44
"testing"
55

6+
"github.com/aws/aws-sdk-go-v2/service/s3"
67
s3Types "github.com/aws/aws-sdk-go-v2/service/s3/types"
78
"github.com/scaleway/scaleway-sdk-go/scw"
89
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/object"
@@ -55,3 +56,55 @@ func TestExpandObjectBucketTags(t *testing.T) {
5556
})
5657
}
5758
}
59+
60+
func TestFlattenObjectBucketVersioning(t *testing.T) {
61+
tests := []struct {
62+
name string
63+
input *s3.GetBucketVersioningOutput
64+
expected []map[string]any
65+
}{
66+
{
67+
name: "nil input",
68+
input: nil,
69+
expected: []map[string]any{
70+
{"enabled": false},
71+
},
72+
},
73+
{
74+
name: "versioning enabled",
75+
input: &s3.GetBucketVersioningOutput{
76+
Status: s3Types.BucketVersioningStatusEnabled,
77+
},
78+
expected: []map[string]any{
79+
{"enabled": true},
80+
},
81+
},
82+
{
83+
name: "versioning suspended",
84+
input: &s3.GetBucketVersioningOutput{
85+
Status: s3Types.BucketVersioningStatusSuspended,
86+
},
87+
expected: []map[string]any{
88+
{"enabled": false},
89+
},
90+
},
91+
{
92+
name: "versioning empty struct",
93+
input: &s3.GetBucketVersioningOutput{},
94+
expected: []map[string]any{
95+
{"enabled": false},
96+
},
97+
},
98+
}
99+
100+
for _, tt := range tests {
101+
t.Run(tt.name, func(t *testing.T) {
102+
result := object.FlattenObjectBucketVersioning(tt.input)
103+
assert.Equal(t, tt.expected, result)
104+
assert.Len(t, result, 1, "Result should contain exactly one map")
105+
assert.Contains(t, result[0], "enabled", "Result map should contain 'enabled' key")
106+
_, ok := result[0]["enabled"].(bool)
107+
assert.True(t, ok, "'enabled' value should be a boolean")
108+
})
109+
}
110+
}

0 commit comments

Comments
 (0)