forked from mcardosos/azure-storage-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontainer_test.go
553 lines (459 loc) · 16 KB
/
container_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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package storage
import (
"sort"
"strconv"
"time"
chk "gopkg.in/check.v1"
)
type ContainerSuite struct{}
var _ = chk.Suite(&ContainerSuite{})
func (s *ContainerSuite) Test_containerBuildPath(c *chk.C) {
cli := getBlobClient(c)
cnt := cli.GetContainerReference("lol")
c.Assert(cnt.buildPath(), chk.Equals, "/lol")
}
func (s *ContainerSuite) TestListContainersPagination(c *chk.C) {
cli := getBlobClient(c)
cli.deleteTestContainers(c)
rec := cli.client.appendRecorder(c)
defer rec.Stop()
const n = 5
const pageSize = 2
cntNames := []string{}
for i := 0; i < n; i++ {
cntNames = append(cntNames, containerName(c, strconv.Itoa(i)))
}
sort.Strings(cntNames)
// Create test containers
created := []Container{}
for i := 0; i < n; i++ {
cnt := cli.GetContainerReference(cntNames[i])
c.Assert(cnt.Create(nil), chk.IsNil)
created = append(created, cnt)
defer cnt.Delete(nil)
}
// Paginate results
seen := []Container{}
marker := ""
for {
resp, err := cli.ListContainers(ListContainersParameters{
MaxResults: pageSize,
Marker: marker})
c.Assert(err, chk.IsNil)
if len(resp.Containers) > pageSize {
c.Fatalf("Got a bigger page. Expected: %d, got: %d", pageSize, len(resp.Containers))
}
for _, c := range resp.Containers {
seen = append(seen, c)
}
marker = resp.NextMarker
if marker == "" || len(resp.Containers) == 0 {
break
}
}
for i := range created {
c.Assert(seen[i].Name, chk.DeepEquals, created[i].Name)
}
}
func (s *ContainerSuite) TestContainerExists(c *chk.C) {
cli := getBlobClient(c)
rec := cli.client.appendRecorder(c)
defer rec.Stop()
// Container does not exist
cnt1 := cli.GetContainerReference(containerName(c, "1"))
ok, err := cnt1.Exists()
c.Assert(err, chk.IsNil)
c.Assert(ok, chk.Equals, false)
// COntainer exists
cnt2 := cli.GetContainerReference(containerName(c, "2"))
c.Assert(cnt2.Create(nil), chk.IsNil)
defer cnt2.Delete(nil)
ok, err = cnt2.Exists()
c.Assert(err, chk.IsNil)
c.Assert(ok, chk.Equals, true)
}
func (s *ContainerSuite) TestCreateContainerDeleteContainer(c *chk.C) {
cli := getBlobClient(c)
rec := cli.client.appendRecorder(c)
defer rec.Stop()
cnt := cli.GetContainerReference(containerName(c))
c.Assert(cnt.Create(nil), chk.IsNil)
c.Assert(cnt.Delete(nil), chk.IsNil)
}
func (s *ContainerSuite) TestCreateContainerIfNotExists(c *chk.C) {
cli := getBlobClient(c)
rec := cli.client.appendRecorder(c)
defer rec.Stop()
// Create non exisiting container
cnt := cli.GetContainerReference(containerName(c))
ok, err := cnt.CreateIfNotExists(nil)
defer cnt.Delete(nil)
c.Assert(err, chk.IsNil)
c.Assert(ok, chk.Equals, true)
}
func (s *ContainerSuite) TestCreateContainerIfExists(c *chk.C) {
cli := getBlobClient(c)
cnt := cli.GetContainerReference(containerName(c))
cnt.Create(nil)
defer cnt.Delete(nil)
rec := cli.client.appendRecorder(c)
cnt.bsc = &cli
defer rec.Stop()
// Try to create already exisiting container
ok, err := cnt.CreateIfNotExists(nil)
c.Assert(err, chk.IsNil)
c.Assert(ok, chk.Equals, false)
}
func (s *ContainerSuite) TestDeleteContainerIfExists(c *chk.C) {
cli := getBlobClient(c)
rec := cli.client.appendRecorder(c)
defer rec.Stop()
// Nonexisting container
cnt1 := cli.GetContainerReference(containerName(c, "1"))
ok, err := cnt1.Exists()
c.Assert(err, chk.IsNil)
c.Assert(ok, chk.Equals, false)
ok, err = cnt1.DeleteIfExists(nil)
c.Assert(err, chk.IsNil)
c.Assert(ok, chk.Equals, false)
// Existing container
cnt2 := cli.GetContainerReference(containerName(c, "2"))
c.Assert(cnt2.Create(nil), chk.IsNil)
ok, err = cnt2.DeleteIfExists(nil)
c.Assert(err, chk.IsNil)
c.Assert(ok, chk.Equals, true)
}
func (s *ContainerSuite) TestListBlobsPagination(c *chk.C) {
cli := getBlobClient(c)
rec := cli.client.appendRecorder(c)
defer rec.Stop()
cnt := cli.GetContainerReference(containerName(c))
c.Assert(cnt.Create(nil), chk.IsNil)
defer cnt.Delete(nil)
blobs := []string{}
const n = 5
const pageSize = 2
for i := 0; i < n; i++ {
name := blobName(c, strconv.Itoa(i))
b := cnt.GetBlobReference(name)
c.Assert(b.putSingleBlockBlob([]byte("Hello, world!")), chk.IsNil)
blobs = append(blobs, name)
}
sort.Strings(blobs)
// Paginate
seen := []string{}
marker := ""
for {
resp, err := cnt.ListBlobs(ListBlobsParameters{
MaxResults: pageSize,
Marker: marker})
c.Assert(err, chk.IsNil)
for _, v := range resp.Blobs {
seen = append(seen, v.Name)
}
marker = resp.NextMarker
if marker == "" || len(resp.Blobs) == 0 {
break
}
}
// Compare
c.Assert(seen, chk.DeepEquals, blobs)
}
// listBlobsAsFiles is a helper function to list blobs as "folders" and "files".
func listBlobsAsFiles(cli BlobStorageClient, cnt Container, parentDir string) (folders []string, files []string, err error) {
var blobParams ListBlobsParameters
var blobListResponse BlobListResponse
// Top level "folders"
blobParams = ListBlobsParameters{
Delimiter: "/",
Prefix: parentDir,
}
blobListResponse, err = cnt.ListBlobs(blobParams)
if err != nil {
return nil, nil, err
}
// These are treated as "folders" under the parentDir.
folders = blobListResponse.BlobPrefixes
// "Files"" are blobs which are under the parentDir.
files = make([]string, len(blobListResponse.Blobs))
for i := range blobListResponse.Blobs {
files[i] = blobListResponse.Blobs[i].Name
}
return folders, files, nil
}
// TestListBlobsTraversal tests that we can correctly traverse
// blobs in blob storage as if it were a file system by using
// a combination of Prefix, Delimiter, and BlobPrefixes.
//
// Blob storage is flat, but we can *simulate* the file
// system with folders and files using conventions in naming.
// With the blob namedd "/usr/bin/ls", when we use delimiter '/',
// the "ls" would be a "file"; with "/", /usr" and "/usr/bin" being
// the "folders"
//
// NOTE: The use of delimiter (eg forward slash) is extremely fiddly
// and difficult to get right so some discipline in naming and rules
// when using the API is required to get everything to work as expected.
//
// Assuming our delimiter is a forward slash, the rules are:
//
// - Do use a leading forward slash in blob names to make things
// consistent and simpler (see further).
// Note that doing so will show "<no name>" as the only top-level
// folder in the container in Azure portal, which may look strange.
//
// - The "folder names" are returned *with trailing forward slash* as per MSDN.
//
// - The "folder names" will be "absolute paths", e.g. listing things under "/usr/"
// will return folder names "/usr/bin/".
//
// - The "file names" are returned as full blob names, e.g. when listing
// things under "/usr/bin/", the file names will be "/usr/bin/ls" and
// "/usr/bin/cat".
//
// - Everything is returned with case-sensitive order as expected in real file system
// as per MSDN.
//
// - To list things under a "folder" always use trailing forward slash.
//
// Example: to list top level folders we use root folder named "" with
// trailing forward slash, so we use "/".
//
// Example: to list folders under "/usr", we again append forward slash and
// so we use "/usr/".
//
// Because we use leading forward slash we don't need to have different
// treatment of "get top-level folders" and "get non-top-level folders"
// scenarios.
func (s *ContainerSuite) TestListBlobsTraversal(c *chk.C) {
cli := getBlobClient(c)
rec := cli.client.appendRecorder(c)
defer rec.Stop()
cnt := cli.GetContainerReference(containerName(c))
c.Assert(cnt.Create(nil), chk.IsNil)
defer cnt.Delete(nil)
// Note use of leading forward slash as per naming rules.
blobsToCreate := []string{
"/usr/bin/ls",
"/usr/bin/cat",
"/usr/lib64/libc.so",
"/etc/hosts",
"/etc/init.d/iptables",
}
// Create the above blobs
for _, blobName := range blobsToCreate {
b := cnt.GetBlobReference(blobName)
err := b.CreateBlockBlob(nil)
c.Assert(err, chk.IsNil)
}
var folders []string
var files []string
var err error
// Top level folders and files.
folders, files, err = listBlobsAsFiles(cli, cnt, "/")
c.Assert(err, chk.IsNil)
c.Assert(folders, chk.DeepEquals, []string{"/etc/", "/usr/"})
c.Assert(files, chk.DeepEquals, []string{})
// Things under /etc/. Note use of trailing forward slash here as per rules.
folders, files, err = listBlobsAsFiles(cli, cnt, "/etc/")
c.Assert(err, chk.IsNil)
c.Assert(folders, chk.DeepEquals, []string{"/etc/init.d/"})
c.Assert(files, chk.DeepEquals, []string{"/etc/hosts"})
// Things under /etc/init.d/
folders, files, err = listBlobsAsFiles(cli, cnt, "/etc/init.d/")
c.Assert(err, chk.IsNil)
c.Assert(folders, chk.DeepEquals, []string(nil))
c.Assert(files, chk.DeepEquals, []string{"/etc/init.d/iptables"})
// Things under /usr/
folders, files, err = listBlobsAsFiles(cli, cnt, "/usr/")
c.Assert(err, chk.IsNil)
c.Assert(folders, chk.DeepEquals, []string{"/usr/bin/", "/usr/lib64/"})
c.Assert(files, chk.DeepEquals, []string{})
// Things under /usr/bin/
folders, files, err = listBlobsAsFiles(cli, cnt, "/usr/bin/")
c.Assert(err, chk.IsNil)
c.Assert(folders, chk.DeepEquals, []string(nil))
c.Assert(files, chk.DeepEquals, []string{"/usr/bin/cat", "/usr/bin/ls"})
}
func (s *ContainerSuite) TestListBlobsWithMetadata(c *chk.C) {
cli := getBlobClient(c)
rec := cli.client.appendRecorder(c)
defer rec.Stop()
cnt := cli.GetContainerReference(containerName(c))
c.Assert(cnt.Create(nil), chk.IsNil)
defer cnt.Delete(nil)
expectMeta := make(map[string]BlobMetadata)
// Put 4 blobs with metadata
for i := 0; i < 4; i++ {
name := blobName(c, strconv.Itoa(i))
b := cnt.GetBlobReference(name)
c.Assert(b.putSingleBlockBlob([]byte("Hello, world!")), chk.IsNil)
b.Metadata = BlobMetadata{
"Lol": name,
"Rofl_BAZ": "Waz Qux",
}
c.Assert(b.SetMetadata(nil), chk.IsNil)
expectMeta[name] = BlobMetadata{
"lol": name,
"rofl_baz": "Waz Qux",
}
_, err := b.CreateSnapshot(nil)
c.Assert(err, chk.IsNil)
}
// Put one more blob with no metadata
b := cnt.GetBlobReference(blobName(c, "nometa"))
c.Assert(b.putSingleBlockBlob([]byte("Hello, world!")), chk.IsNil)
expectMeta[b.Name] = nil
// Get ListBlobs with include: metadata and snapshots
resp, err := cnt.ListBlobs(ListBlobsParameters{
Include: &IncludeBlobDataset{
Metadata: true,
Snapshots: true,
},
})
c.Assert(err, chk.IsNil)
originalBlobs := make(map[string]Blob)
snapshotBlobs := make(map[string]Blob)
for _, v := range resp.Blobs {
if v.Snapshot == (time.Time{}) {
originalBlobs[v.Name] = v
} else {
snapshotBlobs[v.Name] = v
}
}
c.Assert(originalBlobs, chk.HasLen, 5)
c.Assert(snapshotBlobs, chk.HasLen, 4)
// Verify the metadata is as expected
for name := range expectMeta {
c.Check(originalBlobs[name].Metadata, chk.DeepEquals, expectMeta[name])
c.Check(snapshotBlobs[name].Metadata, chk.DeepEquals, expectMeta[name])
}
}
func appendContainerPermission(perms ContainerPermissions, accessType ContainerAccessType,
ID string, start time.Time, expiry time.Time,
canRead bool, canWrite bool, canDelete bool) ContainerPermissions {
perms.AccessType = accessType
if ID != "" {
capd := ContainerAccessPolicy{
ID: ID,
StartTime: start,
ExpiryTime: expiry,
CanRead: canRead,
CanWrite: canWrite,
CanDelete: canDelete,
}
perms.AccessPolicies = append(perms.AccessPolicies, capd)
}
return perms
}
func (s *ContainerSuite) TestSetContainerPermissionsWithTimeoutSuccessfully(c *chk.C) {
cli := getBlobClient(c)
rec := cli.client.appendRecorder(c)
defer rec.Stop()
cnt := cli.GetContainerReference(containerName(c))
c.Assert(cnt.Create(nil), chk.IsNil)
defer cnt.Delete(nil)
perms := ContainerPermissions{}
perms = appendContainerPermission(perms, ContainerAccessTypeBlob, "GolangRocksOnAzure", fixedTime, fixedTime.Add(10*time.Hour), true, true, true)
options := SetContainerPermissionOptions{
Timeout: 30,
}
err := cnt.SetPermissions(perms, &options)
c.Assert(err, chk.IsNil)
}
func (s *ContainerSuite) TestSetContainerPermissionsSuccessfully(c *chk.C) {
cli := getBlobClient(c)
rec := cli.client.appendRecorder(c)
defer rec.Stop()
cnt := cli.GetContainerReference(containerName(c))
c.Assert(cnt.Create(nil), chk.IsNil)
defer cnt.Delete(nil)
perms := ContainerPermissions{}
perms = appendContainerPermission(perms, ContainerAccessTypeBlob, "GolangRocksOnAzure", fixedTime, fixedTime.Add(10*time.Hour), true, true, true)
err := cnt.SetPermissions(perms, nil)
c.Assert(err, chk.IsNil)
}
func (s *ContainerSuite) TestSetThenGetContainerPermissionsSuccessfully(c *chk.C) {
cli := getBlobClient(c)
rec := cli.client.appendRecorder(c)
defer rec.Stop()
cnt := cli.GetContainerReference(containerName(c))
c.Assert(cnt.Create(nil), chk.IsNil)
defer cnt.delete(nil)
perms := ContainerPermissions{}
perms = appendContainerPermission(perms, ContainerAccessTypeBlob, "AutoRestIsSuperCool", fixedTime, fixedTime.Add(10*time.Hour), true, true, true)
perms = appendContainerPermission(perms, ContainerAccessTypeBlob, "GolangRocksOnAzure", fixedTime.Add(20*time.Hour), fixedTime.Add(30*time.Hour), true, false, false)
c.Assert(perms.AccessPolicies, chk.HasLen, 2)
err := cnt.SetPermissions(perms, nil)
c.Assert(err, chk.IsNil)
newPerms, err := cnt.GetPermissions(nil)
c.Assert(err, chk.IsNil)
// check container permissions itself.
c.Assert(newPerms.AccessType, chk.Equals, perms.AccessType)
// fixedTime check policy set.
c.Assert(newPerms.AccessPolicies, chk.HasLen, 2)
for i := range perms.AccessPolicies {
c.Assert(newPerms.AccessPolicies[i].ID, chk.Equals, perms.AccessPolicies[i].ID)
// test timestamps down the second
// rounding start/expiry time original perms since the returned perms would have been rounded.
// so need rounded vs rounded.
c.Assert(newPerms.AccessPolicies[i].StartTime.UTC().Round(time.Second).Format(time.RFC1123),
chk.Equals, perms.AccessPolicies[i].StartTime.UTC().Round(time.Second).Format(time.RFC1123))
c.Assert(newPerms.AccessPolicies[i].ExpiryTime.UTC().Round(time.Second).Format(time.RFC1123),
chk.Equals, perms.AccessPolicies[i].ExpiryTime.UTC().Round(time.Second).Format(time.RFC1123))
c.Assert(newPerms.AccessPolicies[i].CanRead, chk.Equals, perms.AccessPolicies[i].CanRead)
c.Assert(newPerms.AccessPolicies[i].CanWrite, chk.Equals, perms.AccessPolicies[i].CanWrite)
c.Assert(newPerms.AccessPolicies[i].CanDelete, chk.Equals, perms.AccessPolicies[i].CanDelete)
}
}
func (s *ContainerSuite) TestSetContainerPermissionsOnlySuccessfully(c *chk.C) {
cli := getBlobClient(c)
rec := cli.client.appendRecorder(c)
defer rec.Stop()
cnt := cli.GetContainerReference(containerName(c))
c.Assert(cnt.Create(nil), chk.IsNil)
defer cnt.Delete(nil)
perms := ContainerPermissions{}
perms = appendContainerPermission(perms, ContainerAccessTypeBlob, "GolangRocksOnAzure", fixedTime, fixedTime.Add(10*time.Hour), true, true, true)
err := cnt.SetPermissions(perms, nil)
c.Assert(err, chk.IsNil)
}
func (s *ContainerSuite) TestSetThenGetContainerPermissionsOnlySuccessfully(c *chk.C) {
cli := getBlobClient(c)
rec := cli.client.appendRecorder(c)
defer rec.Stop()
cnt := cli.GetContainerReference(containerName(c))
c.Assert(cnt.Create(nil), chk.IsNil)
defer cnt.Delete(nil)
perms := ContainerPermissions{}
perms = appendContainerPermission(perms, ContainerAccessTypeBlob, "", fixedTime, fixedTime.Add(10*time.Hour), true, true, true)
err := cnt.SetPermissions(perms, nil)
c.Assert(err, chk.IsNil)
newPerms, err := cnt.GetPermissions(nil)
c.Assert(err, chk.IsNil)
// check container permissions itself.
c.Assert(newPerms.AccessType, chk.Equals, perms.AccessType)
// fixedTime check there are NO policies set
c.Assert(newPerms.AccessPolicies, chk.HasLen, 0)
}
func (cli *BlobStorageClient) deleteTestContainers(c *chk.C) error {
for {
resp, err := cli.ListContainers(ListContainersParameters{})
if err != nil {
return err
}
if len(resp.Containers) == 0 {
break
}
for _, c := range resp.Containers {
err = c.Delete(nil)
if err != nil {
return err
}
}
}
return nil
}
func containerName(c *chk.C, extras ...string) string {
return nameGenerator(32, "cnt-", alphanum, c, extras)
}