-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtagging_test.go
More file actions
442 lines (387 loc) · 11.5 KB
/
tagging_test.go
File metadata and controls
442 lines (387 loc) · 11.5 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package simples3
import (
"fmt"
"os"
"strings"
"testing"
"time"
)
func TestPutGetDeleteObjectTagging(t *testing.T) {
s3 := setupTestS3(t)
testBucket := os.Getenv("AWS_S3_BUCKET")
timestamp := time.Now().Unix()
testKey := fmt.Sprintf("test-tagging-%d.txt", timestamp)
// Setup: upload a test file
_, err := s3.FilePut(UploadInput{
Bucket: testBucket,
ObjectKey: testKey,
ContentType: "text/plain",
Body: strings.NewReader("test content for tagging"),
})
if err != nil {
t.Fatalf("Failed to upload test file: %v", err)
}
defer func() {
s3.FileDelete(DeleteInput{Bucket: testBucket, ObjectKey: testKey})
}()
// Test 1: Put tags on the object
t.Run("PutTags", func(t *testing.T) {
err := s3.PutObjectTagging(PutObjectTaggingInput{
Bucket: testBucket,
ObjectKey: testKey,
Tags: map[string]string{
"Environment": "test",
"Project": "simples3",
"Version": "v0.14.0",
},
})
if err != nil {
t.Fatalf("PutObjectTagging failed: %v", err)
}
t.Log("Successfully put tags on object")
})
// Test 2: Get tags from the object
t.Run("GetTags", func(t *testing.T) {
output, err := s3.GetObjectTagging(GetObjectTaggingInput{
Bucket: testBucket,
ObjectKey: testKey,
})
if err != nil {
t.Fatalf("GetObjectTagging failed: %v", err)
}
if len(output.Tags) != 3 {
t.Errorf("Expected 3 tags, got %d", len(output.Tags))
}
expectedTags := map[string]string{
"Environment": "test",
"Project": "simples3",
"Version": "v0.14.0",
}
for key, expectedValue := range expectedTags {
if gotValue, ok := output.Tags[key]; !ok {
t.Errorf("Expected tag %s not found", key)
} else if gotValue != expectedValue {
t.Errorf("Tag %s: expected %s, got %s", key, expectedValue, gotValue)
}
}
t.Logf("Successfully retrieved tags: %+v", output.Tags)
})
// Test 3: Update tags (replace all existing tags)
t.Run("UpdateTags", func(t *testing.T) {
err := s3.PutObjectTagging(PutObjectTaggingInput{
Bucket: testBucket,
ObjectKey: testKey,
Tags: map[string]string{
"Status": "updated",
},
})
if err != nil {
t.Fatalf("PutObjectTagging (update) failed: %v", err)
}
// Verify only new tag exists
output, err := s3.GetObjectTagging(GetObjectTaggingInput{
Bucket: testBucket,
ObjectKey: testKey,
})
if err != nil {
t.Fatalf("GetObjectTagging failed: %v", err)
}
if len(output.Tags) != 1 {
t.Errorf("Expected 1 tag after update, got %d", len(output.Tags))
}
if output.Tags["Status"] != "updated" {
t.Errorf("Expected Status=updated, got %v", output.Tags)
}
t.Log("Successfully updated tags")
})
// Test 4: Delete all tags
t.Run("DeleteTags", func(t *testing.T) {
err := s3.DeleteObjectTagging(DeleteObjectTaggingInput{
Bucket: testBucket,
ObjectKey: testKey,
})
if err != nil {
t.Fatalf("DeleteObjectTagging failed: %v", err)
}
t.Log("Successfully deleted tags")
// Verify tags are empty
output, err := s3.GetObjectTagging(GetObjectTaggingInput{
Bucket: testBucket,
ObjectKey: testKey,
})
if err != nil {
t.Fatalf("GetObjectTagging after delete failed: %v", err)
}
if len(output.Tags) != 0 {
t.Errorf("Expected 0 tags after deletion, got %d: %v", len(output.Tags), output.Tags)
}
})
}
func TestPutObjectTagging_Validation(t *testing.T) {
s3 := setupTestS3(t)
t.Run("EmptyBucket", func(t *testing.T) {
err := s3.PutObjectTagging(PutObjectTaggingInput{
Bucket: "",
ObjectKey: "key",
Tags: map[string]string{"key": "value"},
})
if err == nil {
t.Error("Expected error for empty bucket")
}
if !strings.Contains(err.Error(), "bucket name is required") {
t.Errorf("Expected 'bucket name is required' error, got: %v", err)
}
})
t.Run("EmptyObjectKey", func(t *testing.T) {
err := s3.PutObjectTagging(PutObjectTaggingInput{
Bucket: "bucket",
ObjectKey: "",
Tags: map[string]string{"key": "value"},
})
if err == nil {
t.Error("Expected error for empty object key")
}
if !strings.Contains(err.Error(), "object key is required") {
t.Errorf("Expected 'object key is required' error, got: %v", err)
}
})
t.Run("EmptyTags", func(t *testing.T) {
err := s3.PutObjectTagging(PutObjectTaggingInput{
Bucket: "bucket",
ObjectKey: "key",
Tags: map[string]string{},
})
if err == nil {
t.Error("Expected error for empty tags")
}
if !strings.Contains(err.Error(), "at least one tag is required") {
t.Errorf("Expected 'at least one tag is required' error, got: %v", err)
}
})
t.Run("TooManyTags", func(t *testing.T) {
tags := make(map[string]string)
for i := 0; i < 11; i++ {
tags[fmt.Sprintf("key%d", i)] = fmt.Sprintf("value%d", i)
}
err := s3.PutObjectTagging(PutObjectTaggingInput{
Bucket: "bucket",
ObjectKey: "key",
Tags: tags,
})
if err == nil {
t.Error("Expected error for >10 tags")
}
if !strings.Contains(err.Error(), "cannot set more than 10 tags per object") {
t.Errorf("Expected '10 tags' error, got: %v", err)
}
})
}
func TestGetObjectTagging_Validation(t *testing.T) {
s3 := setupTestS3(t)
t.Run("EmptyBucket", func(t *testing.T) {
_, err := s3.GetObjectTagging(GetObjectTaggingInput{
Bucket: "",
ObjectKey: "key",
})
if err == nil {
t.Error("Expected error for empty bucket")
}
if !strings.Contains(err.Error(), "bucket name is required") {
t.Errorf("Expected 'bucket name is required' error, got: %v", err)
}
})
t.Run("EmptyObjectKey", func(t *testing.T) {
_, err := s3.GetObjectTagging(GetObjectTaggingInput{
Bucket: "bucket",
ObjectKey: "",
})
if err == nil {
t.Error("Expected error for empty object key")
}
if !strings.Contains(err.Error(), "object key is required") {
t.Errorf("Expected 'object key is required' error, got: %v", err)
}
})
}
func TestDeleteObjectTagging_Validation(t *testing.T) {
s3 := setupTestS3(t)
t.Run("EmptyBucket", func(t *testing.T) {
err := s3.DeleteObjectTagging(DeleteObjectTaggingInput{
Bucket: "",
ObjectKey: "key",
})
if err == nil {
t.Error("Expected error for empty bucket")
}
if !strings.Contains(err.Error(), "bucket name is required") {
t.Errorf("Expected 'bucket name is required' error, got: %v", err)
}
})
t.Run("EmptyObjectKey", func(t *testing.T) {
err := s3.DeleteObjectTagging(DeleteObjectTaggingInput{
Bucket: "bucket",
ObjectKey: "",
})
if err == nil {
t.Error("Expected error for empty object key")
}
if !strings.Contains(err.Error(), "object key is required") {
t.Errorf("Expected 'object key is required' error, got: %v", err)
}
})
}
func TestGetObjectTagging_NonExistent(t *testing.T) {
s3 := setupTestS3(t)
testBucket := os.Getenv("AWS_S3_BUCKET")
// Try to get tags from non-existent object
_, err := s3.GetObjectTagging(GetObjectTaggingInput{
Bucket: testBucket,
ObjectKey: "non-existent-object-12345.txt",
})
if err == nil {
t.Error("Expected error for non-existent object")
}
t.Logf("Got expected error for non-existent object: %v", err)
}
func TestFilePut_WithTags(t *testing.T) {
s3 := setupTestS3(t)
testBucket := os.Getenv("AWS_S3_BUCKET")
timestamp := time.Now().Unix()
testKey := fmt.Sprintf("test-put-with-tags-%d.txt", timestamp)
// Upload with tags using FilePut
_, err := s3.FilePut(UploadInput{
Bucket: testBucket,
ObjectKey: testKey,
ContentType: "text/plain",
Body: strings.NewReader("test content with tags"),
Tags: map[string]string{
"Method": "FilePut",
"Environment": "test",
},
})
if err != nil {
t.Fatalf("FilePut with tags failed: %v", err)
}
defer s3.FileDelete(DeleteInput{Bucket: testBucket, ObjectKey: testKey})
// Verify tags were set
output, err := s3.GetObjectTagging(GetObjectTaggingInput{
Bucket: testBucket,
ObjectKey: testKey,
})
if err != nil {
t.Fatalf("GetObjectTagging failed: %v", err)
}
if len(output.Tags) != 2 {
t.Errorf("Expected 2 tags, got %d", len(output.Tags))
}
if output.Tags["Method"] != "FilePut" {
t.Errorf("Expected Method=FilePut, got %v", output.Tags["Method"])
}
if output.Tags["Environment"] != "test" {
t.Errorf("Expected Environment=test, got %v", output.Tags["Environment"])
}
t.Logf("Successfully uploaded with tags via FilePut: %+v", output.Tags)
}
func TestFileUpload_WithTags(t *testing.T) {
// Skip: MinIO doesn't support x-amz-tagging in POST upload policies
// This functionality works on AWS S3.
t.Skip("MinIO limitation: tagging via POST upload not fully supported")
s3 := setupTestS3(t)
testBucket := os.Getenv("AWS_S3_BUCKET")
timestamp := time.Now().Unix()
testKey := fmt.Sprintf("test-upload-with-tags-%d.txt", timestamp)
// Upload with tags using FileUpload
_, err := s3.FileUpload(UploadInput{
Bucket: testBucket,
ObjectKey: testKey,
FileName: "test.txt",
ContentType: "text/plain",
Body: strings.NewReader("test content with tags via upload"),
Tags: map[string]string{
"Method": "FileUpload",
"Type": "POST",
},
})
if err != nil {
t.Fatalf("FileUpload with tags failed: %v", err)
}
defer s3.FileDelete(DeleteInput{Bucket: testBucket, ObjectKey: testKey})
// Verify tags were set
output, err := s3.GetObjectTagging(GetObjectTaggingInput{
Bucket: testBucket,
ObjectKey: testKey,
})
if err != nil {
t.Fatalf("GetObjectTagging failed: %v", err)
}
if len(output.Tags) != 2 {
t.Errorf("Expected 2 tags, got %d", len(output.Tags))
}
if output.Tags["Method"] != "FileUpload" {
t.Errorf("Expected Method=FileUpload, got %v", output.Tags["Method"])
}
if output.Tags["Type"] != "POST" {
t.Errorf("Expected Type=POST, got %v", output.Tags["Type"])
}
t.Logf("Successfully uploaded with tags via FileUpload: %+v", output.Tags)
}
func TestCopyObject_WithTags(t *testing.T) {
s3 := setupTestS3(t)
testBucket := os.Getenv("AWS_S3_BUCKET")
timestamp := time.Now().Unix()
sourceKey := fmt.Sprintf("test-copy-source-tags-%d.txt", timestamp)
destKey := fmt.Sprintf("test-copy-dest-tags-%d.txt", timestamp)
// Setup: upload source file with tags
_, err := s3.FilePut(UploadInput{
Bucket: testBucket,
ObjectKey: sourceKey,
ContentType: "text/plain",
Body: strings.NewReader("test content for copy with tags"),
Tags: map[string]string{
"Original": "true",
},
})
if err != nil {
t.Fatalf("Failed to upload source file: %v", err)
}
defer func() {
s3.FileDelete(DeleteInput{Bucket: testBucket, ObjectKey: sourceKey})
s3.FileDelete(DeleteInput{Bucket: testBucket, ObjectKey: destKey})
}()
// Copy with new tags (should replace source tags)
_, err = s3.CopyObject(CopyObjectInput{
SourceBucket: testBucket,
SourceKey: sourceKey,
DestBucket: testBucket,
DestKey: destKey,
Tags: map[string]string{
"Method": "CopyObject",
"Copied": "true",
"Version": "v1",
},
})
if err != nil {
t.Fatalf("CopyObject with tags failed: %v", err)
}
// Verify destination has new tags (not source tags)
output, err := s3.GetObjectTagging(GetObjectTaggingInput{
Bucket: testBucket,
ObjectKey: destKey,
})
if err != nil {
t.Fatalf("GetObjectTagging failed: %v", err)
}
if len(output.Tags) != 3 {
t.Errorf("Expected 3 tags, got %d: %+v", len(output.Tags), output.Tags)
}
if output.Tags["Method"] != "CopyObject" {
t.Errorf("Expected Method=CopyObject, got %v", output.Tags["Method"])
}
if output.Tags["Copied"] != "true" {
t.Errorf("Expected Copied=true, got %v", output.Tags["Copied"])
}
if output.Tags["Original"] != "" {
t.Errorf("Expected original tags to be replaced, but found Original tag: %v", output.Tags["Original"])
}
t.Logf("Successfully copied with new tags: %+v", output.Tags)
}