-
Notifications
You must be signed in to change notification settings - Fork 8
/
versioning_test.go
93 lines (80 loc) · 3.34 KB
/
versioning_test.go
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
package test
import (
"bytes"
"fmt"
mclient "github.com/minio/minio-go/v7"
"github.com/stretchr/testify/require"
"strings"
"testing"
)
func TestApi_Versioning_Bucket(t *testing.T) {
t.Skip()
bucket := "bucket-versioning"
r := require.New(t)
ok, err := mainClient.BucketExists(tstCtx, bucket)
r.NoError(err)
r.False(ok)
err = mainClient.MakeBucket(tstCtx, bucket, mclient.MakeBucketOptions{Region: "us-east"})
r.NoError(err)
t.Cleanup(func() {
cleanup(t, false, bucket)
})
err = mainClient.EnableVersioning(tstCtx, bucket)
r.NoError(err)
obj1 := getTestObj("obj1", bucket)
obj1Info, err := mainClient.PutObject(tstCtx, obj1.bucket, obj1.name, bytes.NewReader(obj1.data), int64(len(obj1.data)), mclient.PutObjectOptions{ContentType: "binary/octet-stream", DisableContentSha256: true})
r.NoError(err)
t.Log(obj1Info.VersionID)
obj2 := getTestObj("photo/sept/obj2", bucket)
_, err = mainClient.PutObject(tstCtx, obj2.bucket, obj2.name, bytes.NewReader(obj2.data), int64(len(obj2.data)), mclient.PutObjectOptions{ContentType: "binary/octet-stream", DisableContentSha256: true})
r.NoError(err)
obj3 := getTestObj("photo/obj3", bucket)
_, err = mainClient.PutObject(tstCtx, obj3.bucket, obj3.name, bytes.NewReader(obj3.data), int64(len(obj3.data)), mclient.PutObjectOptions{ContentType: "binary/octet-stream", DisableContentSha256: true})
r.NoError(err)
obj1upd := getTestObj(obj1.name, obj1.bucket)
r.False(bytes.Equal(obj1.data, obj1upd.data))
obj1updInfo, err := mainClient.PutObject(tstCtx, obj1upd.bucket, obj1upd.name, bytes.NewReader(obj1upd.data), int64(len(obj1upd.data)), mclient.PutObjectOptions{ContentType: "binary/octet-stream", DisableContentSha256: true})
r.NoError(err)
t.Log(obj1updInfo.VersionID)
obj2upd := getTestObj(obj1.name, obj1.bucket)
r.False(bytes.Equal(obj1.data, obj1upd.data))
obj2updInfo, err := mainClient.PutObject(tstCtx, obj2upd.bucket, obj2upd.name, bytes.NewReader(obj2upd.data), int64(len(obj2upd.data)), mclient.PutObjectOptions{ContentType: "binary/octet-stream", DisableContentSha256: true})
r.NoError(err)
t.Log(obj2updInfo.VersionID)
err = mainClient.RemoveObject(tstCtx, bucket, obj1.name, mclient.RemoveObjectOptions{})
r.NoError(err)
obj3upd := getTestObj(obj1.name, obj1.bucket)
obj3updInfo, err := mainClient.PutObject(tstCtx, obj3upd.bucket, obj3upd.name, bytes.NewReader(obj3upd.data), int64(len(obj3upd.data)), mclient.PutObjectOptions{ContentType: "binary/octet-stream", DisableContentSha256: true})
r.NoError(err)
t.Log(obj3updInfo.VersionID)
objs, err := listObjectsVer(mainClient, bucket, "")
r.NoError(err)
t.Log("\n" + strings.Join(objs, "\n"))
}
func listObjectsVer(c *mclient.Client, bucket string, prefix string) ([]string, error) {
var res []string
objCh := c.ListObjects(tstCtx, bucket, mclient.ListObjectsOptions{Prefix: prefix, WithVersions: true})
for obj := range objCh {
if obj.Err != nil {
return nil, obj.Err
}
if obj.Size == 0 && !obj.IsDeleteMarker {
subRes, err := listObjectsVer(c, bucket, obj.Key)
if err != nil {
return nil, err
}
res = append(res, subRes...)
} else {
key := obj.Key
if obj.VersionID != "" {
ver := obj.VersionID
if len(ver) > 10 {
ver = ver[:4] + ".." + ver[len(ver)-4:]
}
key += fmt.Sprintf(": %s : l-%v : d-%v", ver, obj.IsLatest, obj.IsDeleteMarker)
}
res = append(res, key)
}
}
return res, nil
}