-
Notifications
You must be signed in to change notification settings - Fork 5
/
s3cli_test.go
394 lines (335 loc) · 12.7 KB
/
s3cli_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
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
package main
import (
"bytes"
"context"
"crypto/rand"
"encoding/hex"
"fmt"
mrand "math/rand"
"testing"
"time"
"github.com/aws/aws-sdk-go/service/s3"
)
var (
testBucketName = "bucket0001"
testObjectKey = "key0001"
testObjectContent = []byte("testObjectContents")
)
func randomString() string {
buf := make([]byte, 10)
_, err := rand.Read(buf)
if err != nil {
for i := 0; i < 16; i++ {
buf[i] = byte(mrand.Intn(128))
}
}
return hex.EncodeToString(buf)
}
func Test_bucketCreate(t *testing.T) {
buckets := make([]string, 3)
for i := range buckets {
bucket := randomString()
if exists, err := s3Backend.BucketExists(bucket); err != nil || exists {
continue
}
buckets[i] = bucket
}
err := s3cliTest.bucketCreate(context.Background(), buckets)
if err != nil {
t.Errorf("bucketCreate failed: %s", err)
}
}
func Test_bucketList(t *testing.T) {
err := s3cliTest.bucketList(context.Background())
if err != nil {
t.Errorf("listBuckets failed: %s", err)
}
}
func Test_bucketHead(t *testing.T) {
if err := s3cliTest.bucketHead(context.Background(), testBucketName); err != nil {
t.Error("bucketHead error: ", err)
}
}
func Test_bucketACLGet(t *testing.T) {
if err := s3cliTest.bucketACLGet(context.Background(), testBucketName); err != nil {
t.Error("bucketACLGet error: ", err)
}
}
func Test_bucketACLSet(t *testing.T) {
t.Skip("seems gofakes3 set bucketACL has bug")
if err := s3cliTest.bucketACLSet(context.Background(), testBucketName, s3.BucketCannedACLPublicReadWrite); err != nil {
t.Error("bucketACLSet error: ", err)
}
}
func Test_bucketPolicyGet(t *testing.T) {
if err := s3cliTest.bucketPolicyGet(context.Background(), testBucketName); err != nil {
t.Error("bucketACLGet error: ", err)
}
}
func Test_bucketPolicySet(t *testing.T) {
t.Skip("not read to test")
if err := s3cliTest.bucketPolicySet(context.Background(), testBucketName, "{}"); err != nil {
t.Error("bucketPolicySet error: ", err)
}
}
func Test_bucketVersioningGet(t *testing.T) {
if err := s3cliTest.bucketVersioningGet(context.Background(), testBucketName); err != nil {
t.Error("bucketVersioningGet error: ", err)
}
}
func Test_bucketVersioningSet(t *testing.T) {
if err := s3cliTest.bucketVersioningSet(context.Background(), testBucketName, s3.BucketVersioningStatusEnabled); err != nil {
t.Errorf("bucketVersioningSet failed: %s", err)
}
}
func Test_bucketDelete(t *testing.T) {
bucket := "bucketToDelete"
if err := s3Backend.CreateBucket(bucket); err != nil {
t.Error("backend CreateBucket error: ", err)
return
}
if err := s3cliTest.bucketDelete(context.Background(), bucket); err != nil {
t.Errorf("bucketDelete %s failed: %s", bucket, err)
}
}
func Test_putObject(t *testing.T) {
key := "testPutObject"
if err := s3cliTest.putObject(context.Background(), testBucketName, key, "", nil, false, bytes.NewReader(nil)); err != nil {
t.Errorf("putObject failed: %s", err)
return
}
_, err := s3Backend.GetObject(testBucketName, key, nil)
if err != nil {
t.Errorf("backend GetObject failed: %s", err)
return
}
}
func Test_headObject(t *testing.T) {
if err := s3cliTest.headObject(context.Background(), testBucketName, testObjectKey, false, false); err != nil {
t.Errorf("headObject failed: %s", err)
}
}
func Test_getObjectACL(t *testing.T) {
if err := s3cliTest.getObjectACL(context.Background(), testBucketName, testObjectKey); err != nil {
t.Errorf("getObjectACL failed: %s", err)
}
}
func Test_setObjectACL(t *testing.T) {
if err := s3cliTest.setObjectACL(context.Background(), testBucketName, testObjectKey, s3.ObjectCannedACLPublicRead); err != nil {
t.Errorf("setObjectACL failed: %s", err)
}
}
func Test_listAllObjects(t *testing.T) {
if err := s3cliTest.listAllObjects(context.Background(), testBucketName, "t", "/", true, time.Time{}, time.Time{}); err != nil {
t.Errorf("listAllObjects failed: %s", err)
}
}
func Test_listObjects(t *testing.T) {
if err := s3cliTest.listObjects(context.Background(), testBucketName, "t", "/", "", 1000, true, time.Time{}, time.Time{}); err != nil {
t.Errorf("listObjects failed: %s", err)
}
}
func Test_listObjectVersions(t *testing.T) {
if err := s3cliTest.listObjectVersions(context.Background(), testBucketName, ""); err != nil {
t.Errorf("listObjectVersions failed: %s", err)
}
}
func Test_getObject(t *testing.T) {
err := s3cliTest.getObject(context.Background(), testBucketName, testObjectKey, "", "")
if err != nil {
t.Errorf("getObject failed: %s", err)
return
}
}
func Test_catObject(t *testing.T) {
if err := s3cliTest.catObject(context.Background(), testBucketName, testObjectKey, "", ""); err != nil {
t.Errorf("catObject failed: %s", err)
}
}
func Test_renameObject(t *testing.T) {
t.Skip("not impl")
if err := s3cliTest.renameObject(context.Background(), "source", testBucketName, "key"); err != nil {
t.Errorf("renameObject failed: %s", err)
}
}
func Test_copyObject(t *testing.T) {
source := fmt.Sprintf("%s/%s", testBucketName, testObjectKey)
newKey := "testCopyObjectKey"
if err := s3cliTest.copyObject(context.Background(), source, testBucketName, newKey, "", nil, false); err != nil {
t.Errorf("copyObject failed: %s", err)
return
}
if _, err := s3Backend.HeadObject(testBucketName, newKey); err != nil {
t.Errorf("copyObject backand HeadObject failed: %s", err)
}
}
func Test_deleteObjects(t *testing.T) {
prefix := "testPrefix"
if err := s3cliTest.deletePrefix(context.Background(), testBucketName, prefix); err != nil {
t.Errorf("deleteObjects failed: %s", err)
}
}
func Test_deleteBucketAndObjects(t *testing.T) {
bucket := "bucketNameToDelete"
if err := s3Backend.CreateBucket(bucket); err != nil {
t.Errorf("deleteBucketAndObjects backend CreateBucket failed: %s", err)
return
}
_, err := s3Backend.PutObject(testBucketName, "key01", nil, bytes.NewReader(testObjectContent), int64(len(testObjectContent)))
if err != nil {
t.Errorf("deleteBucketAndObjects backend PutObject failed: %s", err)
return
}
_, err = s3Backend.PutObject(testBucketName, "key02", nil, bytes.NewReader(testObjectContent), int64(len(testObjectContent)))
if err != nil {
t.Errorf("deleteBucketAndObjects backend PutObject failed: %s", err)
return
}
if err := s3cliTest.deleteBucketAndObjects(context.Background(), bucket, true); err != nil {
t.Errorf("deleteBucketAndObjects failed: %s", err)
}
}
func Test_deleteObject(t *testing.T) {
key := "keyToTestDeleteObject"
_, err := s3Backend.PutObject(testBucketName, key, nil, bytes.NewReader(testObjectContent), int64(len(testObjectContent)))
if err != nil {
t.Errorf("deleteObject backend PutObject failed: %s", err)
return
}
if err := s3cliTest.deleteObject(context.Background(), testBucketName, key); err != nil {
t.Errorf("deleteObject failed: %s", err)
}
}
func Test_mpuCreate(t *testing.T) {
if err := s3cliTest.mpuCreate(context.Background(), testBucketName, "key"); err != nil {
t.Errorf("mpuCreate failed: %s", err)
}
}
func Test_mpuUpload(t *testing.T) {
t.Skip("not ready to test")
files := map[int64]string{
1: "filename1",
2: "filename2",
}
if err := s3cliTest.mpuUpload(context.Background(), testBucketName, "key", "upload-id", files); err != nil {
t.Errorf("mpuUpload failed: %s", err)
}
}
func Test_mpuAbort(t *testing.T) {
t.Skip("not ready to test")
if err := s3cliTest.mpuAbort(context.Background(), testBucketName, "key", "upload-id"); err != nil {
t.Errorf("mpuAbort failed: %s", err)
}
}
func Test_mpuList(t *testing.T) {
t.Skip("not ready to test")
if err := s3cliTest.mpuList(context.Background(), testBucketName, "prefix"); err != nil {
t.Errorf("mpuList failed: %s", err)
}
}
func Test_mpuComplete(t *testing.T) {
t.Skip("not ready to test")
if err := s3cliTest.mpuComplete(context.Background(), testBucketName, "key", "upload-id", []string{"tag1", "tag2"}); err != nil {
t.Errorf("mpuComplete failed: %s", err)
}
}
func Test_splitBucketObject(t *testing.T) {
cases := map[string][2]string{
"": {"", ""},
"/": {"", ""},
"b/": {"b", ""},
"bucket/object": {"bucket", "object"},
"b/c.ef/fff/": {"b", "c.ef/fff/"},
"bucket/dir/subdir/file": {"bucket", "dir/subdir/file"},
}
for k, v := range cases {
bucket, object := s3cliTest.splitKeyValue(k, "/")
if bucket != v[0] || object != v[1] {
t.Errorf("expect: %s, got: %s, %s", v, bucket, object)
}
}
}
/*
aws help
NAME
aws -
DESCRIPTION
The AWS Command Line Interface is a unified tool to manage your AWS
services.
SYNOPSIS
aws [options] <command> <subcommand> [parameters]
Use aws command help for information on a specific command. Use aws
help topics to view a list of available help topics. The synopsis for
each command shows its parameters and their usage. Optional parameters
are shown in square brackets.
OPTIONS
--debug (boolean)
Turn on debug logging.
--endpoint-url (string)
Override command's default URL with the given URL.
--no-verify-ssl (boolean)
By default, the AWS CLI uses SSL when communicating with AWS services.
For each SSL connection, the AWS CLI will verify SSL certificates. This
option overrides the default behavior of verifying SSL certificates.
--no-paginate (boolean)
Disable automatic pagination.
--output (string)
*/
/*
aws s3api cmd
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:
aws help
aws <command> help
aws <command> <subcommand> help
aws: error: argument operation: Invalid choice, valid choices are:
abort-multipart-upload | complete-multipart-upload
copy-object | create-bucket
create-multipart-upload | delete-bucket
delete-bucket-analytics-configuration | delete-bucket-cors
delete-bucket-encryption | delete-bucket-intelligent-tiering-configuration
delete-bucket-inventory-configuration | delete-bucket-lifecycle
delete-bucket-metrics-configuration | delete-bucket-ownership-controls
delete-bucket-policy | delete-bucket-replication
delete-bucket-tagging | delete-bucket-website
delete-object | delete-object-tagging
delete-objects | delete-public-access-block
get-bucket-accelerate-configuration | get-bucket-acl
get-bucket-analytics-configuration | get-bucket-cors
get-bucket-encryption | get-bucket-intelligent-tiering-configuration
get-bucket-inventory-configuration | get-bucket-lifecycle
get-bucket-lifecycle-configuration | get-bucket-location
get-bucket-logging | get-bucket-metrics-configuration
get-bucket-notification | get-bucket-notification-configuration
get-bucket-ownership-controls | get-bucket-policy
get-bucket-policy-status | get-bucket-replication
get-bucket-request-payment | get-bucket-tagging
get-bucket-versioning | get-bucket-website
get-object | get-object-acl
get-object-legal-hold | get-object-lock-configuration
get-object-retention | get-object-tagging
get-object-torrent | get-public-access-block
head-bucket | head-object
list-bucket-analytics-configurations | list-bucket-intelligent-tiering-configurations
list-bucket-inventory-configurations | list-bucket-metrics-configurations
list-buckets | list-multipart-uploads
list-object-versions | list-objects
list-objects-v2 | list-parts
put-bucket-accelerate-configuration | put-bucket-acl
put-bucket-analytics-configuration | put-bucket-cors
put-bucket-encryption | put-bucket-intelligent-tiering-configuration
put-bucket-inventory-configuration | put-bucket-lifecycle
put-bucket-lifecycle-configuration | put-bucket-logging
put-bucket-metrics-configuration | put-bucket-notification
put-bucket-notification-configuration | put-bucket-ownership-controls
put-bucket-policy | put-bucket-replication
put-bucket-request-payment | put-bucket-tagging
put-bucket-versioning | put-bucket-website
put-object | put-object-acl
put-object-legal-hold | put-object-lock-configuration
put-object-retention | put-object-tagging
put-public-access-block | restore-object
select-object-content | upload-part
upload-part-copy | write-get-object-response
wait | help
*/