s3api_bucket_metadata_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package s3api
  2. import (
  3. "testing"
  4. "github.com/seaweedfs/seaweedfs/weed/pb/s3_pb"
  5. "github.com/seaweedfs/seaweedfs/weed/s3api/cors"
  6. )
  7. func TestBucketMetadataStruct(t *testing.T) {
  8. // Test creating empty metadata
  9. metadata := NewBucketMetadata()
  10. if !metadata.IsEmpty() {
  11. t.Error("New metadata should be empty")
  12. }
  13. // Test setting tags
  14. metadata.Tags["Environment"] = "production"
  15. metadata.Tags["Owner"] = "team-alpha"
  16. if !metadata.HasTags() {
  17. t.Error("Metadata should have tags")
  18. }
  19. if metadata.IsEmpty() {
  20. t.Error("Metadata with tags should not be empty")
  21. }
  22. // Test setting encryption
  23. encryption := &s3_pb.EncryptionConfiguration{
  24. SseAlgorithm: "aws:kms",
  25. KmsKeyId: "test-key-id",
  26. }
  27. metadata.Encryption = encryption
  28. if !metadata.HasEncryption() {
  29. t.Error("Metadata should have encryption")
  30. }
  31. // Test setting CORS
  32. maxAge := 3600
  33. corsRule := cors.CORSRule{
  34. AllowedOrigins: []string{"*"},
  35. AllowedMethods: []string{"GET", "POST"},
  36. AllowedHeaders: []string{"*"},
  37. MaxAgeSeconds: &maxAge,
  38. }
  39. corsConfig := &cors.CORSConfiguration{
  40. CORSRules: []cors.CORSRule{corsRule},
  41. }
  42. metadata.CORS = corsConfig
  43. if !metadata.HasCORS() {
  44. t.Error("Metadata should have CORS")
  45. }
  46. // Test all flags
  47. if !metadata.HasTags() || !metadata.HasEncryption() || !metadata.HasCORS() {
  48. t.Error("All metadata flags should be true")
  49. }
  50. if metadata.IsEmpty() {
  51. t.Error("Metadata with all configurations should not be empty")
  52. }
  53. }
  54. func TestBucketMetadataUpdatePattern(t *testing.T) {
  55. // This test demonstrates the update pattern using the function signature
  56. // (without actually testing the S3ApiServer which would require setup)
  57. // Simulate what UpdateBucketMetadata would do
  58. updateFunc := func(metadata *BucketMetadata) error {
  59. // Add some tags
  60. metadata.Tags["Project"] = "seaweedfs"
  61. metadata.Tags["Version"] = "v3.0"
  62. // Set encryption
  63. metadata.Encryption = &s3_pb.EncryptionConfiguration{
  64. SseAlgorithm: "AES256",
  65. }
  66. return nil
  67. }
  68. // Start with empty metadata
  69. metadata := NewBucketMetadata()
  70. // Apply the update
  71. if err := updateFunc(metadata); err != nil {
  72. t.Fatalf("Update function failed: %v", err)
  73. }
  74. // Verify the results
  75. if len(metadata.Tags) != 2 {
  76. t.Errorf("Expected 2 tags, got %d", len(metadata.Tags))
  77. }
  78. if metadata.Tags["Project"] != "seaweedfs" {
  79. t.Error("Project tag not set correctly")
  80. }
  81. if metadata.Encryption == nil || metadata.Encryption.SseAlgorithm != "AES256" {
  82. t.Error("Encryption not set correctly")
  83. }
  84. }
  85. func TestBucketMetadataHelperFunctions(t *testing.T) {
  86. metadata := NewBucketMetadata()
  87. // Test empty state
  88. if metadata.HasTags() || metadata.HasCORS() || metadata.HasEncryption() {
  89. t.Error("Empty metadata should have no configurations")
  90. }
  91. // Test adding tags
  92. metadata.Tags["key1"] = "value1"
  93. if !metadata.HasTags() {
  94. t.Error("Should have tags after adding")
  95. }
  96. // Test adding CORS
  97. metadata.CORS = &cors.CORSConfiguration{}
  98. if !metadata.HasCORS() {
  99. t.Error("Should have CORS after adding")
  100. }
  101. // Test adding encryption
  102. metadata.Encryption = &s3_pb.EncryptionConfiguration{}
  103. if !metadata.HasEncryption() {
  104. t.Error("Should have encryption after adding")
  105. }
  106. // Test clearing
  107. metadata.Tags = make(map[string]string)
  108. metadata.CORS = nil
  109. metadata.Encryption = nil
  110. if metadata.HasTags() || metadata.HasCORS() || metadata.HasEncryption() {
  111. t.Error("Cleared metadata should have no configurations")
  112. }
  113. if !metadata.IsEmpty() {
  114. t.Error("Cleared metadata should be empty")
  115. }
  116. }