aboutsummaryrefslogtreecommitdiff
path: root/weed/s3api/s3api_bucket_metadata_test.go
blob: ac269163e6548a745d32ceb4801c6f72ec8d04bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package s3api

import (
	"testing"

	"github.com/seaweedfs/seaweedfs/weed/pb/s3_pb"
	"github.com/seaweedfs/seaweedfs/weed/s3api/cors"
)

func TestBucketMetadataStruct(t *testing.T) {
	// Test creating empty metadata
	metadata := NewBucketMetadata()
	if !metadata.IsEmpty() {
		t.Error("New metadata should be empty")
	}

	// Test setting tags
	metadata.Tags["Environment"] = "production"
	metadata.Tags["Owner"] = "team-alpha"
	if !metadata.HasTags() {
		t.Error("Metadata should have tags")
	}
	if metadata.IsEmpty() {
		t.Error("Metadata with tags should not be empty")
	}

	// Test setting encryption
	encryption := &s3_pb.EncryptionConfiguration{
		SseAlgorithm: "aws:kms",
		KmsKeyId:     "test-key-id",
	}
	metadata.Encryption = encryption
	if !metadata.HasEncryption() {
		t.Error("Metadata should have encryption")
	}

	// Test setting CORS
	maxAge := 3600
	corsRule := cors.CORSRule{
		AllowedOrigins: []string{"*"},
		AllowedMethods: []string{"GET", "POST"},
		AllowedHeaders: []string{"*"},
		MaxAgeSeconds:  &maxAge,
	}
	corsConfig := &cors.CORSConfiguration{
		CORSRules: []cors.CORSRule{corsRule},
	}
	metadata.CORS = corsConfig
	if !metadata.HasCORS() {
		t.Error("Metadata should have CORS")
	}

	// Test all flags
	if !metadata.HasTags() || !metadata.HasEncryption() || !metadata.HasCORS() {
		t.Error("All metadata flags should be true")
	}
	if metadata.IsEmpty() {
		t.Error("Metadata with all configurations should not be empty")
	}
}

func TestBucketMetadataUpdatePattern(t *testing.T) {
	// This test demonstrates the update pattern using the function signature
	// (without actually testing the S3ApiServer which would require setup)

	// Simulate what UpdateBucketMetadata would do
	updateFunc := func(metadata *BucketMetadata) error {
		// Add some tags
		metadata.Tags["Project"] = "seaweedfs"
		metadata.Tags["Version"] = "v3.0"

		// Set encryption
		metadata.Encryption = &s3_pb.EncryptionConfiguration{
			SseAlgorithm: "AES256",
		}

		return nil
	}

	// Start with empty metadata
	metadata := NewBucketMetadata()

	// Apply the update
	if err := updateFunc(metadata); err != nil {
		t.Fatalf("Update function failed: %v", err)
	}

	// Verify the results
	if len(metadata.Tags) != 2 {
		t.Errorf("Expected 2 tags, got %d", len(metadata.Tags))
	}
	if metadata.Tags["Project"] != "seaweedfs" {
		t.Error("Project tag not set correctly")
	}
	if metadata.Encryption == nil || metadata.Encryption.SseAlgorithm != "AES256" {
		t.Error("Encryption not set correctly")
	}
}

func TestBucketMetadataHelperFunctions(t *testing.T) {
	metadata := NewBucketMetadata()

	// Test empty state
	if metadata.HasTags() || metadata.HasCORS() || metadata.HasEncryption() {
		t.Error("Empty metadata should have no configurations")
	}

	// Test adding tags
	metadata.Tags["key1"] = "value1"
	if !metadata.HasTags() {
		t.Error("Should have tags after adding")
	}

	// Test adding CORS
	metadata.CORS = &cors.CORSConfiguration{}
	if !metadata.HasCORS() {
		t.Error("Should have CORS after adding")
	}

	// Test adding encryption
	metadata.Encryption = &s3_pb.EncryptionConfiguration{}
	if !metadata.HasEncryption() {
		t.Error("Should have encryption after adding")
	}

	// Test clearing
	metadata.Tags = make(map[string]string)
	metadata.CORS = nil
	metadata.Encryption = nil

	if metadata.HasTags() || metadata.HasCORS() || metadata.HasEncryption() {
		t.Error("Cleared metadata should have no configurations")
	}
	if !metadata.IsEmpty() {
		t.Error("Cleared metadata should be empty")
	}
}