-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
dlo_test.go
207 lines (197 loc) · 6.58 KB
/
dlo_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
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
package swift
import (
"bytes"
"context"
"net/http"
"testing"
"time"
"github.com/ncw/swift/v2/swifttest"
)
var srv *swifttest.SwiftServer
var con *Connection
var err error
var filecontent = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
var segmentContainer = "segment_container112"
func initTest(t *testing.T) {
con, err = initTestConnection(t)
if err != nil {
t.Fail()
}
}
func teardown() {
if srv != nil {
srv.Close()
}
}
func initTestConnection(t *testing.T) (*Connection, error) {
//Uses /swifttest
//in-memory implementation to start
//a swift object store to test against
srv, err = swifttest.NewSwiftServer("localhost")
if err != nil {
return nil, err
}
swiftCon := Connection{
UserName: "swifttest",
ApiKey: "swifttest",
AuthUrl: srv.AuthURL,
Region: "",
Tenant: "",
ConnectTimeout: time.Second,
Timeout: time.Second,
Transport: new(http.Transport),
Domain: "Default",
AuthVersion: 1,
}
err = swiftCon.Authenticate(context.Background())
return &swiftCon, err
}
func TestCases(t *testing.T) {
initTest(t)
createContainers([]string{"c1", "c2", segmentContainer}, t)
createDynamicObject("c1", "o", t)
moveDynamicObject("c1", "o", "c2", "oo", t)
deleteDynamicObject("c2", "oo", t)
createStaticObject("c2", "o2", t)
moveStaticObject("c2", "o2", "c1", "oo2", t)
deleteStaticObject("c1", "oo2", t)
teardown()
}
func createContainers(containers []string, t *testing.T) {
ctx := context.Background()
for i := 0; i < len(containers); i++ {
err = con.ContainerCreate(ctx, containers[i], nil) // Create container
if err != nil {
t.Errorf("Fail at create container %s", containers[i])
}
}
}
func createDynamicObject(container, object string, t *testing.T) {
ctx := context.Background()
metadata := map[string]string{}
metadata["Custom-Field"] = "SomeValue"
ops := LargeObjectOpts{
Container: container, // Name of container to place object
ObjectName: object, // Name of object
CheckHash: false, // If set Check the hash
ContentType: "application/octet-stream", // Content-Type of the object
Headers: Metadata(metadata).ObjectHeaders(), // Additional headers to upload the object with
SegmentContainer: segmentContainer, // Name of the container to place segments
SegmentPrefix: "sg", // Prefix to use for the segments
}
bigfile, err := con.DynamicLargeObjectCreate(ctx, &ops)
if err != nil {
t.Errorf("Fail at dynamic create Large Object")
}
_, err = bigfile.WriteWithContext(ctx, filecontent)
if err != nil {
t.Errorf("WriteWithContext failed: %v", err)
}
err = bigfile.CloseWithContext(ctx)
if err != nil {
t.Errorf("CloseWithContext failed: %v", err)
}
checkObject(container, object, t)
}
func checkObject(container, object string, t *testing.T) {
ctx := context.Background()
info, header, err := con.Object(ctx, container, object)
if err != nil {
t.Errorf("Fail at get Large Object metadata: %s", err.Error())
}
if !header.IsLargeObject() {
t.Errorf("Fail: object is not a large object")
}
if info.Bytes != 10 {
t.Errorf("Fail: mismatch content lengh")
}
if val, ok := header["X-Object-Meta-Custom-Field"]; !ok || val != "SomeValue" {
t.Errorf("Fail: lost custom metadata header")
}
content, err := con.ObjectGetBytes(ctx, container, object)
if err != nil {
t.Errorf("Fail at read Large Object : %s", err.Error())
}
if !bytes.Equal(content, filecontent) {
t.Errorf("Fail: mismatch content")
}
}
func checkNotExistObject(container, object string, t *testing.T) {
_, _, err = con.Object(context.Background(), container, object)
if err == nil || err.Error() != "Object Not Found" {
t.Errorf("Fail at checkNotExistObject object: %s", err)
}
}
func moveDynamicObject(sc, so, dc, do string, t *testing.T) {
err = con.DynamicLargeObjectMove(context.Background(), sc, so, dc, do)
if err != nil {
t.Errorf("Fail at dynamic move Large Object: %s", err.Error())
}
checkNotExistObject(sc, so, t)
checkObject(dc, do, t)
}
func deleteDynamicObject(container, object string, t *testing.T) {
ctx := context.Background()
err = con.DynamicLargeObjectDelete(ctx, container, object)
if err != nil {
t.Errorf("Fail at delte dynamic Large Object: %s", err.Error())
}
checkNotExistObject(container, object, t)
objs, err := con.ObjectsAll(ctx, segmentContainer, nil)
if err != nil {
t.Errorf("Fail at check delte dynamic Large Object: %s", err.Error())
}
if len(objs) != 0 {
t.Errorf("Fail at check delte dynamic Large Object: seg not deleted")
}
}
func createStaticObject(container, object string, t *testing.T) {
ctx := context.Background()
metadata := map[string]string{}
metadata["Custom-Field"] = "SomeValue"
ops := LargeObjectOpts{
Container: container, // Name of container to place object
ObjectName: object, // Name of object
CheckHash: false, // If set Check the hash
ContentType: "application/octet-stream", // Content-Type of the object
Headers: Metadata(metadata).ObjectHeaders(), // Additional headers to upload the object with
SegmentContainer: segmentContainer, // Name of the container to place segments
SegmentPrefix: "sg", // Prefix to use for the segments
}
bigfile, err := con.StaticLargeObjectCreate(ctx, &ops)
if err != nil {
t.Errorf("Fail at static create Large Object")
}
_, err = bigfile.WriteWithContext(ctx, filecontent)
if err != nil {
t.Errorf("WriteWithContext failed: %v", err)
}
err = bigfile.CloseWithContext(ctx)
if err != nil {
t.Errorf("CloseWithContext failed: %v", err)
}
checkObject(container, object, t)
}
func moveStaticObject(sc, so, dc, do string, t *testing.T) {
err = con.StaticLargeObjectMove(context.Background(), sc, so, dc, do)
if err != nil {
t.Errorf("Fail at static move Large Object: %s", err.Error())
}
checkNotExistObject(sc, so, t)
checkObject(dc, do, t)
}
func deleteStaticObject(container, object string, t *testing.T) {
ctx := context.Background()
err = con.StaticLargeObjectDelete(ctx, container, object)
if err != nil {
t.Errorf("Fail at delte dynamic Large Object: %s", err.Error())
}
checkNotExistObject(container, object, t)
objs, err := con.ObjectsAll(ctx, segmentContainer, nil)
if err != nil {
t.Errorf("Fail at check delte dynamic Large Object: %s", err.Error())
}
if len(objs) != 0 {
t.Errorf("Fail at check delte dynamic Large Object: seg not deleted")
}
}