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 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
|
package storage
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
import (
"net/url"
"sort"
"strconv"
"time"
"github.com/Azure/go-autorest/autorest/azure"
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)
rec := cli.client.appendRecorder(c)
defer rec.Stop()
cli.deleteTestContainers(c)
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)
cnt.Metadata = map[string]string{
"hello": "world",
"name": cnt.Name,
}
c.Assert(cnt.SetMetadata(nil), chk.IsNil)
defer cnt.Delete(nil)
}
// Paginate results
seen := []Container{}
marker := ""
for {
resp, err := cli.ListContainers(ListContainersParameters{
MaxResults: pageSize,
Marker: marker,
Include: "metadata",
})
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)
c.Assert(seen[i].Metadata, chk.DeepEquals, created[i].Metadata)
}
}
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"))
err = cnt2.Create(nil)
defer cnt2.Delete(nil)
c.Assert(err, chk.IsNil)
ok, err = cnt2.Exists()
c.Assert(err, chk.IsNil)
c.Assert(ok, chk.Equals, true)
// Service SASURI test (funcs should fail, service SAS has not enough permissions)
sasuriOptions := ContainerSASOptions{}
sasuriOptions.Expiry = fixedTime
sasuriOptions.Read = true
sasuriOptions.Add = true
sasuriOptions.Create = true
sasuriOptions.Write = true
sasuriOptions.Delete = true
sasuriOptions.List = true
sasuriString1, err := cnt1.GetSASURI(sasuriOptions)
c.Assert(err, chk.IsNil)
sasuri1, err := url.Parse(sasuriString1)
c.Assert(err, chk.IsNil)
cntServiceSAS1, err := GetContainerReferenceFromSASURI(*sasuri1)
c.Assert(err, chk.IsNil)
cntServiceSAS1.Client().HTTPClient = cli.client.HTTPClient
ok, err = cntServiceSAS1.Exists()
c.Assert(err, chk.NotNil)
c.Assert(ok, chk.Equals, false)
sasuriString2, err := cnt2.GetSASURI(sasuriOptions)
c.Assert(err, chk.IsNil)
sasuri2, err := url.Parse(sasuriString2)
c.Assert(err, chk.IsNil)
cntServiceSAS2, err := GetContainerReferenceFromSASURI(*sasuri2)
c.Assert(err, chk.IsNil)
cntServiceSAS2.Client().HTTPClient = cli.client.HTTPClient
ok, err = cntServiceSAS2.Exists()
c.Assert(err, chk.NotNil)
c.Assert(ok, chk.Equals, false)
// Account SASURI test
token, err := cli.client.GetAccountSASToken(accountSASOptions)
c.Assert(err, chk.IsNil)
SAScli := NewAccountSASClient(cli.client.accountName, token, azure.PublicCloud).GetBlobService()
cntAccountSAS1 := SAScli.GetContainerReference(cnt1.Name)
cntAccountSAS1.Client().HTTPClient = cli.client.HTTPClient
ok, err = cntAccountSAS1.Exists()
c.Assert(err, chk.IsNil)
c.Assert(ok, chk.Equals, false)
cntAccountSAS2 := SAScli.GetContainerReference(cnt2.Name)
cntAccountSAS2.Client().HTTPClient = cli.client.HTTPClient
ok, err = cntAccountSAS2.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)
rec := cli.client.appendRecorder(c)
defer rec.Stop()
cnt := cli.GetContainerReference(containerName(c))
cnt.Create(nil)
defer cnt.Delete(nil)
// 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))
err := cnt.Create(nil)
defer cnt.Delete(nil)
c.Assert(err, chk.IsNil)
blobs := []string{}
types := []BlobType{}
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)
types = append(types, b.Properties.BlobType)
}
sort.Strings(blobs)
listBlobsPagination(c, cnt, pageSize, blobs, types)
// Service SAS test
sasuriOptions := ContainerSASOptions{}
sasuriOptions.Expiry = fixedTime
sasuriOptions.Read = true
sasuriOptions.Add = true
sasuriOptions.Create = true
sasuriOptions.Write = true
sasuriOptions.Delete = true
sasuriOptions.List = true
sasuriString, err := cnt.GetSASURI(sasuriOptions)
c.Assert(err, chk.IsNil)
sasuri, err := url.Parse(sasuriString)
c.Assert(err, chk.IsNil)
cntServiceSAS, err := GetContainerReferenceFromSASURI(*sasuri)
c.Assert(err, chk.IsNil)
cntServiceSAS.Client().HTTPClient = cli.client.HTTPClient
listBlobsPagination(c, cntServiceSAS, pageSize, blobs, types)
// Account SAS test
token, err := cli.client.GetAccountSASToken(accountSASOptions)
c.Assert(err, chk.IsNil)
SAScli := NewAccountSASClient(cli.client.accountName, token, azure.PublicCloud).GetBlobService()
cntAccountSAS := SAScli.GetContainerReference(cnt.Name)
cntAccountSAS.Client().HTTPClient = cli.client.HTTPClient
listBlobsPagination(c, cntAccountSAS, pageSize, blobs, types)
}
func listBlobsPagination(c *chk.C, cnt *Container, pageSize uint, blobs []string, types []BlobType) {
// Paginate
seen := []string{}
seenTypes := []BlobType{}
marker := ""
for {
resp, err := cnt.ListBlobs(ListBlobsParameters{
MaxResults: pageSize,
Marker: marker})
c.Assert(err, chk.IsNil)
for _, b := range resp.Blobs {
seen = append(seen, b.Name)
seenTypes = append(seenTypes, b.Properties.BlobType)
c.Assert(b.Container, chk.Equals, cnt)
}
marker = resp.NextMarker
if marker == "" || len(resp.Blobs) == 0 {
break
}
}
// Compare
c.Assert(seen, chk.DeepEquals, blobs)
c.Assert(seenTypes, chk.DeepEquals, types)
}
// 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 (s *ContainerSuite) TestGetAndSetContainerMetadata(c *chk.C) {
cli := getBlobClient(c)
rec := cli.client.appendRecorder(c)
defer rec.Stop()
// Get empty metadata
cnt1 := cli.GetContainerReference(containerName(c, "1"))
c.Assert(cnt1.Create(nil), chk.IsNil)
defer cnt1.Delete(nil)
err := cnt1.GetMetadata(nil)
c.Assert(err, chk.IsNil)
c.Assert(cnt1.Metadata, chk.HasLen, 0)
// Get and set the metadata
cnt2 := cli.GetContainerReference(containerName(c, "2"))
c.Assert(cnt2.Create(nil), chk.IsNil)
defer cnt2.Delete(nil)
metaPut := map[string]string{
"lol": "rofl",
"rofl_baz": "waz qux",
}
cnt2.Metadata = metaPut
err = cnt2.SetMetadata(nil)
c.Assert(err, chk.IsNil)
err = cnt2.GetMetadata(nil)
c.Assert(err, chk.IsNil)
c.Check(cnt2.Metadata, chk.DeepEquals, metaPut)
}
func (s *ContainerSuite) TestGetContainerProperties(c *chk.C) {
cli := getBlobClient(c)
rec := cli.client.appendRecorder(c)
defer rec.Stop()
// Get empty metadata
cnt1 := cli.GetContainerReference(containerName(c, "1"))
c.Assert(cnt1.Create(nil), chk.IsNil)
defer cnt1.Delete(nil)
// should be empty until we get properties
c.Assert(cnt1.Properties.Etag, chk.HasLen, 0)
err := cnt1.GetProperties()
c.Assert(err, chk.IsNil)
c.Assert(cnt1.Properties.Etag, chk.Equals, `"0x8D9001BBA6C4080"`)
c.Assert(cnt1.Properties.PublicAccess, chk.Equals, ContainerAccessType(""))
}
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, "", alphanum, c, extras)
}
|