func (s *StorageBlobSuite) TestGetBlobSASURI(c *chk.C) { api, err := NewClient("foo", "YmFy", DefaultBaseURL, "2013-08-15", true) c.Assert(err, chk.IsNil) cli := api.GetBlobService() expiry := time.Time{} expectedParts := url.URL{ Scheme: "https", Host: "foo.blob.core.windows.net", Path: "container/name", RawQuery: url.Values{ "sv": {"2013-08-15"}, "sig": {"/OXG7rWh08jYwtU03GzJM0DHZtidRGpC6g69rSGm3I0="}, "sr": {"b"}, "sp": {"r"}, "se": {"0001-01-01T00:00:00Z"}, }.Encode()} u, err := cli.GetBlobSASURI("container", "name", expiry, "r") c.Assert(err, chk.IsNil) sasParts, err := url.Parse(u) c.Assert(err, chk.IsNil) c.Assert(expectedParts.String(), chk.Equals, sasParts.String()) c.Assert(expectedParts.Query(), chk.DeepEquals, sasParts.Query()) }
func (s *StorageBlobSuite) TestGetBlobRange(c *chk.C) { cnt := randContainer() blob := randString(20) body := "0123456789" cli := getBlobClient(c) c.Assert(cli.CreateContainer(cnt, ContainerAccessTypeBlob), chk.IsNil) defer cli.DeleteContainer(cnt) c.Assert(cli.putSingleBlockBlob(cnt, blob, []byte(body)), chk.IsNil) defer cli.DeleteBlob(cnt, blob) // Read 1-3 for _, r := range []struct { rangeStr string expected string }{ {"0-", body}, {"1-3", body[1 : 3+1]}, {"3-", body[3:]}, } { resp, err := cli.GetBlobRange(cnt, blob, r.rangeStr) c.Assert(err, chk.IsNil) blobBody, err := ioutil.ReadAll(resp) c.Assert(err, chk.IsNil) str := string(blobBody) c.Assert(str, chk.Equals, r.expected) } }
func (s *StorageClientSuite) Test_prepareBlockListRequest(c *chk.C) { empty := []Block{} expected := `<?xml version="1.0" encoding="utf-8"?><BlockList></BlockList>` c.Assert(prepareBlockListRequest(empty), chk.DeepEquals, expected) blocks := []Block{{"foo", BlockStatusLatest}, {"bar", BlockStatusUncommitted}} expected = `<?xml version="1.0" encoding="utf-8"?><BlockList><Latest>foo</Latest><Uncommitted>bar</Uncommitted></BlockList>` c.Assert(prepareBlockListRequest(blocks), chk.DeepEquals, expected) }
func (s *StorageClientSuite) Test_createAuthorizationHeader(c *chk.C) { key := base64.StdEncoding.EncodeToString([]byte("bar")) cli, err := NewBasicClient("foo", key) c.Assert(err, chk.IsNil) canonicalizedString := `foobarzoo` expected := `SharedKey foo:h5U0ATVX6SpbFX1H6GNuxIMeXXCILLoIvhflPtuQZ30=` c.Assert(cli.createAuthorizationHeader(canonicalizedString), chk.Equals, expected) }
func (s *StorageClientSuite) TestGetEndpoint_Mixed(c *chk.C) { cli, err := NewBasicClient("foo", "YmFy") c.Assert(err, chk.IsNil) params := url.Values{} params.Set("a", "b") params.Set("c", "d") output := cli.getEndpoint(blobServiceName, "path", params) c.Assert(output, chk.Equals, "https://foo.blob.core.windows.net/path?a=b&c=d") }
func (s *StorageBlobSuite) TestListContainersPagination(c *chk.C) { cli := getBlobClient(c) c.Assert(deleteTestContainers(cli), chk.IsNil) const n = 5 const pageSize = 2 // Create test containers created := []string{} for i := 0; i < n; i++ { name := randContainer() c.Assert(cli.CreateContainer(name, ContainerAccessTypePrivate), chk.IsNil) created = append(created, name) } sort.Strings(created) // Defer test container deletions defer func() { var wg sync.WaitGroup for _, cnt := range created { wg.Add(1) go func(name string) { c.Assert(cli.DeleteContainer(name), chk.IsNil) wg.Done() }(cnt) } wg.Wait() }() // Paginate results seen := []string{} marker := "" for { resp, err := cli.ListContainers(ListContainersParameters{ Prefix: testContainerPrefix, MaxResults: pageSize, Marker: marker}) c.Assert(err, chk.IsNil) containers := resp.Containers if len(containers) > pageSize { c.Fatalf("Got a bigger page. Expected: %d, got: %d", pageSize, len(containers)) } for _, c := range containers { seen = append(seen, c.Name) } marker = resp.NextMarker if marker == "" || len(containers) == 0 { break } } c.Assert(seen, chk.DeepEquals, created) }
func (s *StorageClientSuite) Test_xmlUnmarshal(c *chk.C) { xml := `<?xml version="1.0" encoding="utf-8"?> <Blob> <Name>myblob</Name> </Blob>` var blob Blob body := ioutil.NopCloser(strings.NewReader(xml)) c.Assert(xmlUnmarshal(body, &blob), chk.IsNil) c.Assert(blob.Name, chk.Equals, "myblob") }
func (s *StorageBlobSuite) TestPutBlock(c *chk.C) { cli := getBlobClient(c) cnt := randContainer() c.Assert(cli.CreateContainer(cnt, ContainerAccessTypePrivate), chk.IsNil) defer cli.deleteContainer(cnt) blob := randString(20) chunk := []byte(randString(1024)) blockID := base64.StdEncoding.EncodeToString([]byte("foo")) c.Assert(cli.PutBlock(cnt, blob, blockID, chunk), chk.IsNil) }
func (s *StorageQueueSuite) TestPostMessage_PeekMessage_DeleteMessage(c *chk.C) { q := randString(20) cli := getQueueClient(c) c.Assert(cli.CreateQueue(q), chk.IsNil) defer cli.DeleteQueue(q) msg := randString(64 * 1024) // exercise max length c.Assert(cli.PutMessage(q, msg, PutMessageParameters{}), chk.IsNil) r, err := cli.PeekMessages(q, PeekMessagesParameters{}) c.Assert(err, chk.IsNil) c.Assert(len(r.QueueMessagesList), chk.Equals, 1) c.Assert(r.QueueMessagesList[0].MessageText, chk.Equals, msg) }
func (s *StorageQueueSuite) TestDeleteMessages(c *chk.C) { q := randString(20) cli := getQueueClient(c) c.Assert(cli.CreateQueue(q), chk.IsNil) defer cli.DeleteQueue(q) c.Assert(cli.PutMessage(q, "message", PutMessageParameters{}), chk.IsNil) r, err := cli.GetMessages(q, GetMessagesParameters{VisibilityTimeout: 1}) c.Assert(err, chk.IsNil) c.Assert(len(r.QueueMessagesList), chk.Equals, 1) m := r.QueueMessagesList[0] c.Assert(cli.DeleteMessage(q, m.MessageID, m.PopReceipt), chk.IsNil) }
// getBasicClient returns a test client from storage credentials in the env func getBasicClient(c *chk.C) Client { name := os.Getenv("ACCOUNT_NAME") if name == "" { c.Fatal("ACCOUNT_NAME not set, need an empty storage account to test") } key := os.Getenv("ACCOUNT_KEY") if key == "" { c.Fatal("ACCOUNT_KEY not set") } cli, err := NewBasicClient(name, key) c.Assert(err, chk.IsNil) return cli }
func (s *StorageBlobSuite) TestContainerExists(c *chk.C) { cnt := randContainer() cli := getBlobClient(c) ok, err := cli.ContainerExists(cnt) c.Assert(err, chk.IsNil) c.Assert(ok, chk.Equals, false) c.Assert(cli.CreateContainer(cnt, ContainerAccessTypeBlob), chk.IsNil) defer cli.DeleteContainer(cnt) ok, err = cli.ContainerExists(cnt) c.Assert(err, chk.IsNil) c.Assert(ok, chk.Equals, true) }
func (s *StorageQueueSuite) TestQueueExists(c *chk.C) { cli := getQueueClient(c) ok, err := cli.QueueExists("nonexistent-queue") c.Assert(err, chk.IsNil) c.Assert(ok, chk.Equals, false) name := randString(20) c.Assert(cli.CreateQueue(name), chk.IsNil) defer cli.DeleteQueue(name) ok, err = cli.QueueExists(name) c.Assert(err, chk.IsNil) c.Assert(ok, chk.Equals, true) }
func (s *StorageClientSuite) TestGetBaseURL_Basic_Https(c *chk.C) { cli, err := NewBasicClient("foo", "YmFy") c.Assert(err, chk.IsNil) c.Assert(cli.apiVersion, chk.Equals, DefaultAPIVersion) c.Assert(err, chk.IsNil) c.Assert(cli.getBaseURL("table"), chk.Equals, "https://foo.table.core.windows.net") }
func (s *StorageBlobSuite) TestCreateBlockBlob(c *chk.C) { cli := getBlobClient(c) cnt := randContainer() c.Assert(cli.CreateContainer(cnt, ContainerAccessTypePrivate), chk.IsNil) defer cli.deleteContainer(cnt) blob := randString(20) c.Assert(cli.CreateBlockBlob(cnt, blob), chk.IsNil) // Verify blocks, err := cli.GetBlockList(cnt, blob, BlockListTypeAll) c.Assert(err, chk.IsNil) c.Assert(len(blocks.CommittedBlocks), chk.Equals, 0) c.Assert(len(blocks.UncommittedBlocks), chk.Equals, 0) }
func (s *StorageBlobSuite) TestPutPageBlob(c *chk.C) { cli := getBlobClient(c) cnt := randContainer() c.Assert(cli.CreateContainer(cnt, ContainerAccessTypePrivate), chk.IsNil) defer cli.deleteContainer(cnt) blob := randString(20) size := int64(10 * 1024 * 1024) c.Assert(cli.PutPageBlob(cnt, blob, size, nil), chk.IsNil) // Verify props, err := cli.GetBlobProperties(cnt, blob) c.Assert(err, chk.IsNil) c.Assert(props.ContentLength, chk.Equals, size) c.Assert(props.BlobType, chk.Equals, BlobTypePage) }
func (s *StorageFileSuite) TestDeleteShareIfNotExists(c *chk.C) { cli := getFileClient(c) name := randShare() // delete non-existing share ok, err := cli.DeleteShareIfExists(name) c.Assert(err, chk.IsNil) c.Assert(ok, chk.Equals, false) c.Assert(cli.CreateShare(name), chk.IsNil) // delete existing share ok, err = cli.DeleteShareIfExists(name) c.Assert(err, chk.IsNil) c.Assert(ok, chk.Equals, true) }
func (s *StorageBlobSuite) TestGetBlobURL(c *chk.C) { api, err := NewBasicClient("foo", "YmFy") c.Assert(err, chk.IsNil) cli := api.GetBlobService() c.Assert(cli.GetBlobURL("c", "nested/blob"), chk.Equals, "https://foo.blob.core.windows.net/c/nested/blob") c.Assert(cli.GetBlobURL("", "blob"), chk.Equals, "https://foo.blob.core.windows.net/$root/blob") c.Assert(cli.GetBlobURL("", "nested/blob"), chk.Equals, "https://foo.blob.core.windows.net/$root/nested/blob") }
func (s *StorageQueueSuite) Test_SetMetadataGetMetadata_Roundtrips(c *chk.C) { cli := getQueueClient(c) name := randString(20) c.Assert(cli.CreateQueue(name), chk.IsNil) defer cli.DeleteQueue(name) metadata := make(map[string]string) metadata["Foo1"] = "bar1" metadata["fooBaz"] = "bar" err := cli.SetMetadata(name, metadata) c.Assert(err, chk.IsNil) qm, err := cli.GetMetadata(name) c.Assert(err, chk.IsNil) c.Assert(qm.UserDefinedMetadata["foo1"], chk.Equals, "bar1") c.Assert(qm.UserDefinedMetadata["foobaz"], chk.Equals, "bar") }
func (s *StorageBlobSuite) TestCreateBlockBlobFromReader(c *chk.C) { cli := getBlobClient(c) cnt := randContainer() c.Assert(cli.CreateContainer(cnt, ContainerAccessTypePrivate), chk.IsNil) defer cli.deleteContainer(cnt) name := randString(20) data := randBytes(8888) c.Assert(cli.CreateBlockBlobFromReader(cnt, name, uint64(len(data)), bytes.NewReader(data), nil), chk.IsNil) body, err := cli.GetBlob(cnt, name) c.Assert(err, chk.IsNil) gotData, err := ioutil.ReadAll(body) body.Close() c.Assert(err, chk.IsNil) c.Assert(gotData, chk.DeepEquals, data) }
func (s *StorageClientSuite) TestReturnsStorageServiceError(c *chk.C) { // attempt to delete a nonexisting container _, err := getBlobClient(c).deleteContainer(randContainer()) c.Assert(err, chk.NotNil) v, ok := err.(AzureStorageServiceError) c.Check(ok, chk.Equals, true) c.Assert(v.StatusCode, chk.Equals, 404) c.Assert(v.Code, chk.Equals, "ContainerNotFound") c.Assert(v.Code, chk.Not(chk.Equals), "") }
func (s *StorageClientSuite) Test_buildCanonicalizedHeader(c *chk.C) { cli, err := NewBasicClient("foo", "YmFy") c.Assert(err, chk.IsNil) type test struct { headers map[string]string expected string } tests := []test{ {map[string]string{}, ""}, {map[string]string{"x-ms-foo": "bar"}, "x-ms-foo:bar"}, {map[string]string{"foo:": "bar"}, ""}, {map[string]string{"foo:": "bar", "x-ms-foo": "bar"}, "x-ms-foo:bar"}, {map[string]string{ "x-ms-version": "9999-99-99", "x-ms-blob-type": "BlockBlob"}, "x-ms-blob-type:BlockBlob\nx-ms-version:9999-99-99"}} for _, i := range tests { c.Assert(cli.buildCanonicalizedHeader(i.headers), chk.Equals, i.expected) } }
func (s *StorageClientSuite) TestGetBaseURL_Custom_NoHttps(c *chk.C) { apiVersion := "2015-01-01" // a non existing one cli, err := NewClient("foo", "YmFy", "core.chinacloudapi.cn", apiVersion, false) c.Assert(err, chk.IsNil) c.Assert(cli.apiVersion, chk.Equals, apiVersion) c.Assert(cli.getBaseURL("table"), chk.Equals, "http://foo.table.core.chinacloudapi.cn") }
func (s *StorageBlobSuite) TestPutEmptyBlockBlob(c *chk.C) { cli := getBlobClient(c) cnt := randContainer() c.Assert(cli.CreateContainer(cnt, ContainerAccessTypePrivate), chk.IsNil) defer cli.deleteContainer(cnt) blob := randString(20) c.Assert(cli.putSingleBlockBlob(cnt, blob, []byte{}), chk.IsNil) props, err := cli.GetBlobProperties(cnt, blob) c.Assert(err, chk.IsNil) c.Assert(props.ContentLength, chk.Not(chk.Equals), 0) }
func (s *StorageQueueSuite) TestGetMessages(c *chk.C) { q := randString(20) cli := getQueueClient(c) c.Assert(cli.CreateQueue(q), chk.IsNil) defer cli.DeleteQueue(q) n := 4 for i := 0; i < n; i++ { c.Assert(cli.PutMessage(q, randString(10), PutMessageParameters{}), chk.IsNil) } r, err := cli.GetMessages(q, GetMessagesParameters{NumOfMessages: n}) c.Assert(err, chk.IsNil) c.Assert(len(r.QueueMessagesList), chk.Equals, n) }
func (s *StorageBlobSuite) TestCreateContainerIfNotExists(c *chk.C) { cnt := randContainer() cli := getBlobClient(c) defer cli.DeleteContainer(cnt) // First create ok, err := cli.CreateContainerIfNotExists(cnt, ContainerAccessTypePrivate) c.Assert(err, chk.IsNil) c.Assert(ok, chk.Equals, true) // Second create, should not give errors ok, err = cli.CreateContainerIfNotExists(cnt, ContainerAccessTypePrivate) c.Assert(err, chk.IsNil) c.Assert(ok, chk.Equals, false) }
func (s *StorageBlobSuite) Test_blobSASStringToSign(c *chk.C) { _, err := blobSASStringToSign("2012-02-12", "CS", "SE", "SP") c.Assert(err, chk.NotNil) // not implemented SAS for versions earlier than 2013-08-15 out, err := blobSASStringToSign("2013-08-15", "CS", "SE", "SP") c.Assert(err, chk.IsNil) c.Assert(out, chk.Equals, "SP\n\nSE\nCS\n\n2013-08-15\n\n\n\n\n") }
func (s *StorageFileSuite) TestCreateShareIfNotExists(c *chk.C) { cli := getFileClient(c) name := randShare() defer cli.DeleteShare(name) // First create ok, err := cli.CreateShareIfNotExists(name) c.Assert(err, chk.IsNil) c.Assert(ok, chk.Equals, true) // Second create, should not give errors ok, err = cli.CreateShareIfNotExists(name) c.Assert(err, chk.IsNil) c.Assert(ok, chk.Equals, false) }
func (s *StorageClientSuite) Test_xmlMarshal(c *chk.C) { type t struct { XMLName xml.Name `xml:"S"` Name string `xml:"Name"` } b := t{Name: "myblob"} expected := `<S><Name>myblob</Name></S>` r, i, err := xmlMarshal(b) c.Assert(err, chk.IsNil) o, err := ioutil.ReadAll(r) c.Assert(err, chk.IsNil) out := string(o) c.Assert(out, chk.Equals, expected) c.Assert(i, chk.Equals, len(expected)) }
func (s *StorageClientSuite) Test_getStandardHeaders(c *chk.C) { cli, err := NewBasicClient("foo", "YmFy") c.Assert(err, chk.IsNil) headers := cli.getStandardHeaders() c.Assert(len(headers), chk.Equals, 2) c.Assert(headers["x-ms-version"], chk.Equals, cli.apiVersion) if _, ok := headers["x-ms-date"]; !ok { c.Fatal("Missing date header") } }