Ejemplo n.º 1
0
        internal void NewContainerTest(Agent agent)
        {
            string NEW_CONTAINER_NAME = Utility.GenNameString("astoria-");

            Dictionary <string, object> dic = Utility.GenComparisonData(StorageObjectType.Container, NEW_CONTAINER_NAME);
            Collection <Dictionary <string, object> > comp = new Collection <Dictionary <string, object> > {
                dic
            };

            // delete container if it exists
            StorageBlob.CloudBlobContainer container = CommonStorageAccount.CreateCloudBlobClient().GetContainerReference(NEW_CONTAINER_NAME);
            container.DeleteIfExists();

            try
            {
                //--------------New operation--------------
                Test.Assert(agent.NewAzureStorageContainer(NEW_CONTAINER_NAME), Utility.GenComparisonData("NewAzureStorageContainer", true));
                // Verification for returned values
                CloudBlobUtil.PackContainerCompareData(container, dic);
                agent.OutputValidation(comp);
                Test.Assert(container.Exists(), "container {0} should exist!", NEW_CONTAINER_NAME);
            }
            finally
            {
                // clean up
                container.DeleteIfExists();
            }
        }
Ejemplo n.º 2
0
        internal void GetContainerTest(Agent agent)
        {
            string NEW_CONTAINER_NAME = Utility.GenNameString("astoria-");

            Dictionary <string, object> dic = Utility.GenComparisonData(StorageObjectType.Container, NEW_CONTAINER_NAME);

            // create container if it does not exist
            StorageBlob.CloudBlobContainer container = CommonStorageAccount.CreateCloudBlobClient().GetContainerReference(NEW_CONTAINER_NAME);
            container.CreateIfNotExists();

            Collection <Dictionary <string, object> > comp = new Collection <Dictionary <string, object> > {
                dic
            };

            try
            {
                //--------------Get operation--------------
                Test.Assert(agent.GetAzureStorageContainer(NEW_CONTAINER_NAME), Utility.GenComparisonData("GetAzureStorageContainer", true));
                // Verification for returned values
                container.FetchAttributes();
                dic.Add("CloudBlobContainer", container);
                CloudBlobUtil.PackContainerCompareData(container, dic);

                agent.OutputValidation(comp);
            }
            finally
            {
                // clean up
                container.DeleteIfExists();
            }
        }
        internal void UploadBlobTestGB(Agent agent, StorageBlob.BlobType blobType)
        {
            string uploadFilePath = @".\" + Utility.GenNameString("gbupload");
            string containerName  = Utility.GenNameString("gbupload-");
            string blobName       = Path.GetFileName(uploadFilePath);

            // create the container
            StorageBlob.CloudBlobContainer container = blobUtil.CreateContainer(containerName);

            // Generate a 512 bytes file which contains GB18030 characters
            File.WriteAllText(uploadFilePath, GB18030String);

            try
            {
                //--------------Upload operation--------------
                Test.Assert(agent.SetAzureStorageBlobContent(uploadFilePath, containerName, blobType),
                            Utility.GenComparisonData("SendAzureStorageBlob", true));

                StorageBlob.ICloudBlob blob = CloudBlobUtil.GetBlob(container, blobName, blobType);
                Test.Assert(blob != null && blob.Exists(), "blob " + blobName + " should exist!");

                // Check MD5
                string localMd5 = Helper.GetFileContentMD5(uploadFilePath);
                blob.FetchAttributes();
                Test.Assert(localMd5 == blob.Properties.ContentMD5,
                            string.Format("blob content md5 should be {0}, and actualy it's {1}", localMd5, blob.Properties.ContentMD5));
            }
            finally
            {
                // cleanup
                container.DeleteIfExists();
                File.Delete(uploadFilePath);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Parameters:
        ///     Block:
        ///         True for BlockBlob, false for PageBlob
        /// </summary>
        internal void UploadBlobTest(Agent agent, string UploadFilePath, Microsoft.WindowsAzure.Storage.Blob.BlobType Type)
        {
            string NEW_CONTAINER_NAME = Utility.GenNameString("upload-");
            string blobName           = Path.GetFileName(UploadFilePath);

            Collection <Dictionary <string, object> > comp = new Collection <Dictionary <string, object> >();
            Dictionary <string, object> dic = Utility.GenComparisonData(StorageObjectType.Blob, blobName);

            dic["BlobType"] = Type;
            comp.Add(dic);

            // create the container
            StorageBlob.CloudBlobContainer container = CommonStorageAccount.CreateCloudBlobClient().GetContainerReference(NEW_CONTAINER_NAME);
            container.CreateIfNotExists();

            try
            {
                //--------------Upload operation--------------
                Test.Assert(agent.SetAzureStorageBlobContent(UploadFilePath, NEW_CONTAINER_NAME, Type), Utility.GenComparisonData("SendAzureStorageBlob", true));

                StorageBlob.ICloudBlob blob = CommonBlobHelper.QueryBlob(NEW_CONTAINER_NAME, blobName);
                CloudBlobUtil.PackBlobCompareData(blob, dic);
                // Verification for returned values
                agent.OutputValidation(comp);
                Test.Assert(blob != null && blob.Exists(), "blob " + blobName + " should exist!");
            }
            finally
            {
                // cleanup
                container.DeleteIfExists();
            }
        }
Ejemplo n.º 5
0
        public void DeleteBlobContainer(string toDelete = null)
        {
            var containerToDelete = !string.IsNullOrEmpty(toDelete) ? toDelete : containerName;
            
            var blobClient = storageAccount.CreateCloudBlobClient();
            container = blobClient.GetContainerReference(containerToDelete);

            container.DeleteIfExists();
        }
        internal void CopyBlobTestGB(Agent agent, StorageBlob.BlobType blobType)
        {
            string uploadFilePath    = @".\" + Utility.GenNameString("gbupload");
            string srcContainerName  = Utility.GenNameString("gbupload-", 15);
            string destContainerName = Utility.GenNameString("gbupload-", 15);
            string blobName          = Path.GetFileName(uploadFilePath);

            // create the container
            StorageBlob.CloudBlobContainer srcContainer  = blobUtil.CreateContainer(srcContainerName);
            StorageBlob.CloudBlobContainer destContainer = blobUtil.CreateContainer(destContainerName);

            // Generate a 512 bytes file which contains GB18030 characters
            File.WriteAllText(uploadFilePath, GB18030String);

            string localMd5 = Helper.GetFileContentMD5(uploadFilePath);

            try
            {
                // create source blob
                StorageBlob.ICloudBlob blob = CloudBlobUtil.GetBlob(srcContainer, blobName, blobType);

                // upload file data
                using (var fileStream = System.IO.File.OpenRead(uploadFilePath))
                {
                    blob.UploadFromStream(fileStream);
                    // need to set MD5 as for page blob, it won't set MD5 automatically
                    blob.Properties.ContentMD5 = localMd5;
                    blob.SetProperties();
                }

                //--------------Copy blob operation--------------
                Test.Assert(agent.StartAzureStorageBlobCopy(srcContainerName, blobName, destContainerName, blobName), Utility.GenComparisonData("Start copy blob using blob name", true));

                // Get destination blob
                blob = CloudBlobUtil.GetBlob(destContainer, blobName, blobType);

                // Check MD5
                blob.FetchAttributes();
                Test.Assert(localMd5 == blob.Properties.ContentMD5,
                            string.Format("blob content md5 should be {0}, and actualy it's {1}", localMd5, blob.Properties.ContentMD5));
            }
            finally
            {
                // cleanup
                srcContainer.DeleteIfExists();
                destContainer.DeleteIfExists();
                File.Delete(uploadFilePath);
            }
        }
        public void CloudBlockBlobDownloadToStreamAPMCancel()
        {
            byte[]             buffer    = GetRandomBuffer(1 * 1024 * 1024);
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                CloudBlockBlob blob = container.GetBlockBlobReference("blob1");
                using (MemoryStream originalBlob = new MemoryStream(buffer))
                {
                    using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                    {
                        ICancellableAsyncResult result = blob.BeginUploadFromStream(originalBlob,
                                                                                    ar => waitHandle.Set(),
                                                                                    null);
                        waitHandle.WaitOne();
                        blob.EndUploadFromStream(result);

                        using (MemoryStream downloadedBlob = new MemoryStream())
                        {
                            OperationContext operationContext = new OperationContext();
                            result = blob.BeginDownloadToStream(downloadedBlob, null, null, operationContext,
                                                                ar => waitHandle.Set(),
                                                                null);
                            Thread.Sleep(100);
                            result.Cancel();
                            waitHandle.WaitOne();
                            try
                            {
                                blob.EndDownloadToStream(result);
                            }
                            catch (StorageException ex)
                            {
                                Assert.AreEqual(ex.Message, "Operation was canceled by user.");
                                Assert.AreEqual(ex.RequestInformation.HttpStatusCode, 306);
                                Assert.AreEqual(ex.RequestInformation.HttpStatusMessage, "Unused");
                            }
                            TestHelper.AssertNAttempts(operationContext, 1);
                        }
                    }
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
        public void PageBlobReadStreamReadSizeTest()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();
                CloudPageBlob blob = container.GetPageBlobReference("blob1");
                BlobReadStreamReadSizeTest(blob);
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
        public async Task LargeDownloadVerifyRangeSizeRestrictions()
        {
            string             inputFileName  = Path.GetTempFileName();
            string             outputFileName = Path.GetTempFileName();
            CloudBlobContainer container      = GetRandomContainerReference();

            try
            {
                await container.CreateAsync();

                CloudBlockBlob blob = container.GetBlockBlobReference("largeblob1");
                await blob.UploadTextAsync("Tent");

                try
                {
                    await blob.DownloadToFileParallelAsync(outputFileName, FileMode.Create, 16, 16 *Constants.MB + 3, CancellationToken.None);

                    Assert.Fail("Expected a failure");
                }
                catch (ArgumentException) {}

                try
                {
                    await blob.DownloadToFileParallelAsync(outputFileName, FileMode.Create, 16, 2 *Constants.MB, CancellationToken.None);

                    Assert.Fail("Expected a failure");
                }
                catch (ArgumentOutOfRangeException) {}

                try
                {
                    BlobRequestOptions options = new BlobRequestOptions();
                    options.UseTransactionalMD5 = true;
                    await blob.DownloadToFileParallelAsync(outputFileName, FileMode.Create, 16, 16 *Constants.MB, 0, null, null, options, null, CancellationToken.None);

                    Assert.Fail("Expected a failure");
                }
                catch (ArgumentException) {}
                blob.Delete();
            }
            finally
            {
                container.DeleteIfExists();

                File.Delete(inputFileName);
                File.Delete(outputFileName);
            }
        }
Ejemplo n.º 10
0
        public void CloudBlobContainerExists()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                Assert.IsFalse(container.Exists());
                container.Create();
                Assert.IsTrue(container.Exists());
            }
            finally
            {
                container.DeleteIfExists();
            }
            Assert.IsFalse(container.Exists());
        }
Ejemplo n.º 11
0
        public void PageBlobWriteStreamOneByteTest()
        {
            byte buffer = 127;

            MD5 hasher = MD5.Create();
            CloudBlobContainer container = GetRandomContainerReference();

            container.ServiceClient.ParallelOperationThreadCount = 2;
            try
            {
                container.Create();

                CloudPageBlob blob = container.GetPageBlobReference("blob1");
                blob.StreamWriteSizeInBytes = 16 * 1024;
                using (MemoryStream wholeBlob = new MemoryStream())
                {
                    BlobRequestOptions options = new BlobRequestOptions()
                    {
                        StoreBlobContentMD5 = true,
                    };
                    using (Stream blobStream = blob.OpenWrite(1 * 1024 * 1024, null, options))
                    {
                        for (int i = 0; i < 1 * 1024 * 1024; i++)
                        {
                            blobStream.WriteByte(buffer);
                            wholeBlob.WriteByte(buffer);
                            Assert.AreEqual(wholeBlob.Position, blobStream.Position);
                        }
                    }

                    wholeBlob.Seek(0, SeekOrigin.Begin);
                    string md5 = Convert.ToBase64String(hasher.ComputeHash(wholeBlob));
                    blob.FetchAttributes();
                    Assert.AreEqual(md5, blob.Properties.ContentMD5);

                    using (MemoryStream downloadedBlob = new MemoryStream())
                    {
                        blob.DownloadToStream(downloadedBlob);
                        TestHelper.AssertStreamsAreEqual(wholeBlob, downloadedBlob);
                    }
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
        public void CloudBlobDirectoryGetParent()
        {
            foreach (String delimiter in Delimiters)
            {
                CloudBlobClient client = GenerateCloudBlobClient();
                client.DefaultDelimiter = delimiter;
                string             name      = GetRandomContainerName();
                CloudBlobContainer container = client.GetContainerReference(name);
                try
                {
                    container.Create();
                    CloudPageBlob blob = container.GetPageBlobReference("Dir1" + delimiter + "Blob1");
                    blob.Create(0);
                    Assert.AreEqual("Dir1" + delimiter + "Blob1", blob.Name);

                    // get the blob's parent
                    CloudBlobDirectory parent = blob.Parent;
                    Assert.AreEqual(parent.Prefix, "Dir1" + delimiter);

                    // get container as parent
                    CloudBlobDirectory root = parent.Parent;
                    Assert.AreEqual(root.Prefix, "");

                    // make sure the parent of the container dir is null
                    CloudBlobDirectory empty = root.Parent;
                    Assert.IsNull(empty);

                    // from container, get directory reference to container
                    root = container.GetDirectoryReference("");
                    Assert.AreEqual("", root.Prefix);
                    Assert.AreEqual(container.Uri.AbsoluteUri, root.Uri.AbsoluteUri);

                    List <IListBlobItem> list = root.ListBlobs().ToList();
                    Assert.AreEqual(1, list.Count);

                    // make sure the parent of the container dir is null
                    empty = root.Parent;
                    Assert.IsNull(empty);

                    blob.DeleteIfExists();
                }
                finally
                {
                    container.DeleteIfExists();
                }
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Parameters:
        ///     Block:
        ///         True for BlockBlob, false for PageBlob
        /// </summary>
        internal void DownloadBlobTest(Agent agent, string UploadFilePath, string DownloadDirPath, Microsoft.WindowsAzure.Storage.Blob.BlobType Type)
        {
            string NEW_CONTAINER_NAME = Utility.GenNameString("upload-");
            string blobName           = Path.GetFileName(UploadFilePath);

            Collection <Dictionary <string, object> > comp = new Collection <Dictionary <string, object> >();
            Dictionary <string, object> dic = Utility.GenComparisonData(StorageObjectType.Blob, blobName);

            dic["BlobType"] = Type;
            comp.Add(dic);

            // create the container
            StorageBlob.CloudBlobContainer container = CommonStorageAccount.CreateCloudBlobClient().GetContainerReference(NEW_CONTAINER_NAME);
            container.CreateIfNotExists();

            try
            {
                bool bSuccess = false;
                // upload the blob file
                if (Type == Microsoft.WindowsAzure.Storage.Blob.BlobType.BlockBlob)
                {
                    bSuccess = CommonBlobHelper.UploadFileToBlockBlob(NEW_CONTAINER_NAME, blobName, UploadFilePath);
                }
                else if (Type == Microsoft.WindowsAzure.Storage.Blob.BlobType.PageBlob)
                {
                    bSuccess = CommonBlobHelper.UploadFileToPageBlob(NEW_CONTAINER_NAME, blobName, UploadFilePath);
                }
                Test.Assert(bSuccess, "upload file {0} to container {1} should succeed", UploadFilePath, NEW_CONTAINER_NAME);

                //--------------Download operation--------------
                string downloadFilePath = Path.Combine(DownloadDirPath, blobName);
                Test.Assert(agent.GetAzureStorageBlobContent(blobName, downloadFilePath, NEW_CONTAINER_NAME),
                            Utility.GenComparisonData("GetAzureStorageBlobContent", true));
                StorageBlob.ICloudBlob blob = CommonBlobHelper.QueryBlob(NEW_CONTAINER_NAME, blobName);
                CloudBlobUtil.PackBlobCompareData(blob, dic);
                // Verification for returned values
                agent.OutputValidation(comp);

                Test.Assert(Helper.CompareTwoFiles(downloadFilePath, UploadFilePath),
                            String.Format("File '{0}' should be bit-wise identicial to '{1}'", downloadFilePath, UploadFilePath));
            }
            finally
            {
                // cleanup
                container.DeleteIfExists();
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Parameters:
        ///     Block:
        ///         True for BlockBlob, false for PageBlob
        /// </summary>
        internal void GetBlobTest(Agent agent, string UploadFilePath, Microsoft.WindowsAzure.Storage.Blob.BlobType Type)
        {
            string NEW_CONTAINER_NAME = Utility.GenNameString("upload-");
            string blobName           = Path.GetFileName(UploadFilePath);

            Collection <Dictionary <string, object> > comp = new Collection <Dictionary <string, object> >();
            Dictionary <string, object> dic = Utility.GenComparisonData(StorageObjectType.Blob, blobName);

            dic["BlobType"] = Type;
            comp.Add(dic);

            // create the container
            StorageBlob.CloudBlobContainer container = CommonStorageAccount.CreateCloudBlobClient().GetContainerReference(NEW_CONTAINER_NAME);
            container.CreateIfNotExists();

            try
            {
                bool bSuccess = false;
                // upload the blob file
                if (Type == Microsoft.WindowsAzure.Storage.Blob.BlobType.BlockBlob)
                {
                    bSuccess = CommonBlobHelper.UploadFileToBlockBlob(NEW_CONTAINER_NAME, blobName, UploadFilePath);
                }
                else if (Type == Microsoft.WindowsAzure.Storage.Blob.BlobType.PageBlob)
                {
                    bSuccess = CommonBlobHelper.UploadFileToPageBlob(NEW_CONTAINER_NAME, blobName, UploadFilePath);
                }
                Test.Assert(bSuccess, "upload file {0} to container {1} should succeed", UploadFilePath, NEW_CONTAINER_NAME);

                //--------------Get operation--------------
                Test.Assert(agent.GetAzureStorageBlob(blobName, NEW_CONTAINER_NAME), Utility.GenComparisonData("GetAzureStorageBlob", true));

                // Verification for returned values
                // get blob object using XSCL
                StorageBlob.ICloudBlob blob = CommonBlobHelper.QueryBlob(NEW_CONTAINER_NAME, blobName);
                blob.FetchAttributes();
                CloudBlobUtil.PackBlobCompareData(blob, dic);
                dic.Add("ICloudBlob", blob);

                agent.OutputValidation(comp);
            }
            finally
            {
                // cleanup
                container.DeleteIfExists();
            }
        }
Ejemplo n.º 15
0
        public void CloudBlobSnapshotMetadataAPM()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                CloudAppendBlob appendBlob = container.GetAppendBlobReference(BlobName);
                appendBlob.CreateOrReplace();

                CloudBlob blob = container.GetBlobReference(BlobName);
                blob.Metadata["Hello"] = "World";
                blob.Metadata["Marco"] = "Polo";
                blob.SetMetadata();

                IDictionary <string, string> snapshotMetadata = new Dictionary <string, string>();
                snapshotMetadata["Hello"] = "Dolly";
                snapshotMetadata["Yoyo"]  = "Ma";

                IAsyncResult result;

                using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                {
                    result = blob.BeginSnapshot(snapshotMetadata, null, null, null, ar => waitHandle.Set(), null);
                    waitHandle.WaitOne();
                    CloudBlob snapshot = blob.EndSnapshot(result);

                    // Test the client view against the expected metadata
                    // None of the original metadata should be present
                    Assert.AreEqual("Dolly", snapshot.Metadata["Hello"]);
                    Assert.AreEqual("Ma", snapshot.Metadata["Yoyo"]);
                    Assert.IsFalse(snapshot.Metadata.ContainsKey("Marco"));

                    // Test the server view against the expected metadata
                    snapshot.FetchAttributes();
                    Assert.AreEqual("Dolly", snapshot.Metadata["Hello"]);
                    Assert.AreEqual("Ma", snapshot.Metadata["Yoyo"]);
                    Assert.IsFalse(snapshot.Metadata.ContainsKey("Marco"));
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
Ejemplo n.º 16
0
        public void CloudBlobContainerSetMetadataAPMCancel()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();
                container.Metadata.Add("key1", "value1");

                TestHelper.ExecuteAPMMethodWithCancellation(4000,
                                                            new[] { DelayBehaviors.DelayAllRequestsIf(4000 * 3, AzureStorageSelectors.BlobTraffic().IfHostNameContains(container.ServiceClient.Credentials.AccountName)) },
                                                            (options, opContext, callback, state) => container.BeginSetMetadata(null, (BlobRequestOptions)options, opContext, callback, state),
                                                            container.EndSetMetadata);
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
Ejemplo n.º 17
0
        public void CloudBlobContainerSetPermissions()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                BlobContainerPermissions permissions = container.GetPermissions();
                Assert.AreEqual(BlobContainerPublicAccessType.Off, permissions.PublicAccess);
                Assert.AreEqual(0, permissions.SharedAccessPolicies.Count);

                // We do not have precision at milliseconds level. Hence, we need
                // to recreate the start DateTime to be able to compare it later.
                DateTime start = DateTime.UtcNow;
                start = new DateTime(start.Year, start.Month, start.Day, start.Hour, start.Minute, start.Second, DateTimeKind.Utc);
                DateTime expiry = start.AddMinutes(30);

                permissions.PublicAccess = BlobContainerPublicAccessType.Container;
                permissions.SharedAccessPolicies.Add("key1", new SharedAccessBlobPolicy()
                {
                    SharedAccessStartTime  = start,
                    SharedAccessExpiryTime = expiry,
                    Permissions            = SharedAccessBlobPermissions.List,
                });
                container.SetPermissions(permissions);
                Thread.Sleep(30 * 1000);

                CloudBlobContainer container2 = container.ServiceClient.GetContainerReference(container.Name);
                permissions = container2.GetPermissions();
                Assert.AreEqual(BlobContainerPublicAccessType.Container, permissions.PublicAccess);
                Assert.AreEqual(1, permissions.SharedAccessPolicies.Count);
                Assert.IsTrue(permissions.SharedAccessPolicies["key1"].SharedAccessStartTime.HasValue);
                Assert.AreEqual(start, permissions.SharedAccessPolicies["key1"].SharedAccessStartTime.Value.UtcDateTime);
                Assert.IsTrue(permissions.SharedAccessPolicies["key1"].SharedAccessExpiryTime.HasValue);
                Assert.AreEqual(expiry, permissions.SharedAccessPolicies["key1"].SharedAccessExpiryTime.Value.UtcDateTime);
                Assert.AreEqual(SharedAccessBlobPermissions.List, permissions.SharedAccessPolicies["key1"].Permissions);
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
        public void CloudBlobDirectoryMultipleDelimiters()
        {
            foreach (String delimiter in Delimiters)
            {
                CloudBlobClient client = GenerateCloudBlobClient();
                ////Set the default delimiter to \
                client.DefaultDelimiter = delimiter;
                string             name      = GetRandomContainerName();
                CloudBlobContainer container = client.GetContainerReference(name);
                try
                {
                    container.Create();
                    if (CloudBlobDirectorySetupWithDelimiter(container, delimiter))
                    {
                        IEnumerable <IListBlobItem> list1 = container.ListBlobs("TopDir1" + delimiter, false, BlobListingDetails.None, null, null);

                        List <IListBlobItem> simpleList1 = list1.ToList();
                        ////Check if for 3 because if there were more than 3, the previous assert would have failed.
                        ////So the only thing we need to make sure is that it is not less than 3.
                        Assert.IsTrue(simpleList1.Count == 3);

                        IListBlobItem item11 = simpleList1.ElementAt(0);
                        Assert.IsTrue(item11.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "Blob1"));

                        IListBlobItem item12 = simpleList1.ElementAt(1);
                        Assert.IsTrue(item12.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter));

                        IListBlobItem item13 = simpleList1.ElementAt(2);
                        Assert.IsTrue(item13.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter));

                        CloudBlobDirectory directory    = container.GetDirectoryReference("TopDir1" + delimiter);
                        CloudBlobDirectory subDirectory = directory.GetSubdirectoryReference("MidDir1" + delimiter);
                        CloudBlobDirectory parent       = subDirectory.Parent;
                        Assert.AreEqual(parent.Prefix, directory.Prefix);
                        Assert.AreEqual(parent.Uri, directory.Uri);
                    }
                }
                finally
                {
                    container.DeleteIfExists();
                }
            }
        }
Ejemplo n.º 19
0
        internal void SetContainerACLTest(Agent agent)
        {
            string NEW_CONTAINER_NAME = Utility.GenNameString("astoria-");

            Collection <Dictionary <string, object> > comp = new Collection <Dictionary <string, object> >();
            Dictionary <string, object> dic = Utility.GenComparisonData(StorageObjectType.Container, NEW_CONTAINER_NAME);

            comp.Add(dic);

            // create container if it does not exist
            StorageBlob.CloudBlobContainer container = CommonStorageAccount.CreateCloudBlobClient().GetContainerReference(NEW_CONTAINER_NAME);
            container.CreateIfNotExists();

            try
            {
                StorageBlob.BlobContainerPublicAccessType[] accessTypes = new StorageBlob.BlobContainerPublicAccessType[] {
                    StorageBlob.BlobContainerPublicAccessType.Blob,
                    StorageBlob.BlobContainerPublicAccessType.Container,
                    StorageBlob.BlobContainerPublicAccessType.Off
                };

                // set PublicAccess as one value respetively
                foreach (var accessType in accessTypes)
                {
                    //--------------Set operation--------------
                    Test.Assert(agent.SetAzureStorageContainerACL(NEW_CONTAINER_NAME, accessType),
                                "SetAzureStorageContainerACL operation should succeed");
                    // Verification for returned values
                    dic["PublicAccess"] = accessType;
                    CloudBlobUtil.PackContainerCompareData(container, dic);
                    agent.OutputValidation(comp);

                    Test.Assert(container.GetPermissions().PublicAccess == accessType,
                                "PublicAccess should be equal: {0} = {1}", container.GetPermissions().PublicAccess, accessType);
                }
            }
            finally
            {
                // clean up
                container.DeleteIfExists();
            }
        }
Ejemplo n.º 20
0
        internal void RemoveContainerTest(Agent agent)
        {
            string NEW_CONTAINER_NAME = Utility.GenNameString("astoria-");

            // create container if it does not exist
            StorageBlob.CloudBlobContainer container = CommonStorageAccount.CreateCloudBlobClient().GetContainerReference(NEW_CONTAINER_NAME);
            container.CreateIfNotExists();

            try
            {
                //--------------Remove operation--------------
                Test.Assert(agent.RemoveAzureStorageContainer(NEW_CONTAINER_NAME), Utility.GenComparisonData("RemoveAzureStorageContainer", true));
                Test.Assert(!container.Exists(), "container {0} should not exist!", NEW_CONTAINER_NAME);
            }
            finally
            {
                // clean up
                container.DeleteIfExists();
            }
        }
Ejemplo n.º 21
0
        public void CloudBlobContainerRegionalSetMetadata()
        {
            CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;

            Thread.CurrentThread.CurrentCulture = new CultureInfo("sk-SK");

            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Metadata.Add("sequence", "value");
                container.Metadata.Add("schema", "value");
                container.Create();
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = currentCulture;
                container.DeleteIfExists();
            }
        }
Ejemplo n.º 22
0
        public void PageBlobDownloadToStreamRangeTest()
        {
            byte[]             buffer    = GetRandomBuffer(2 * 1024);
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                CloudPageBlob blob = container.GetPageBlobReference("blob1");
                using (MemoryStream wholeBlob = new MemoryStream(buffer))
                {
                    blob.UploadFromStream(wholeBlob);

                    byte[]           testBuffer = new byte[1024];
                    MemoryStream     blobStream = new MemoryStream(testBuffer);
                    StorageException storageEx  = TestHelper.ExpectedException <StorageException>(
                        () => blob.DownloadRangeToStream(blobStream, 0, 0),
                        "Requesting 0 bytes when downloading range should not work");
                    Assert.IsInstanceOfType(storageEx.InnerException, typeof(ArgumentOutOfRangeException));
                    blob.DownloadRangeToStream(blobStream, 0, 1024);
                    Assert.AreEqual(blobStream.Position, 1024);
                    TestHelper.AssertStreamsAreEqualAtIndex(blobStream, wholeBlob, 0, 0, 1024);

                    CloudPageBlob blob2       = container.GetPageBlobReference("blob1");
                    MemoryStream  blobStream2 = new MemoryStream(testBuffer);
                    storageEx = TestHelper.ExpectedException <StorageException>(
                        () => blob2.DownloadRangeToStream(blobStream, 1024, 0),
                        "Requesting 0 bytes when downloading range should not work");
                    Assert.IsInstanceOfType(storageEx.InnerException, typeof(ArgumentOutOfRangeException));
                    blob2.DownloadRangeToStream(blobStream2, 1024, 1024);
                    TestHelper.AssertStreamsAreEqualAtIndex(blobStream2, wholeBlob, 0, 1024, 1024);

                    AssertAreEqual(blob, blob2);
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
Ejemplo n.º 23
0
        public void CloudBlobSASUnknownParams()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                // Create Source on server
                CloudBlockBlob blob = container.GetBlockBlobReference("source");

                string data = "String data";
                UploadText(blob, data, Encoding.UTF8);

                UriBuilder blobURIBuilder = new UriBuilder(blob.Uri);
                blobURIBuilder.Query = "MyQuery=value&YOURQUERY=value2"; // Add the query params unknown to the service

                // Source SAS must have read permissions
                SharedAccessBlobPermissions permissions = SharedAccessBlobPermissions.Read;
                SharedAccessBlobPolicy      policy      = new SharedAccessBlobPolicy()
                {
                    SharedAccessStartTime  = DateTimeOffset.UtcNow.AddMinutes(-5),
                    SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
                    Permissions            = permissions,
                };
                string sasToken = blob.GetSharedAccessSignature(policy);

                // Replace one of the SAS keys with a capitalized version to ensure we are case-insensitive on expected parameter keys as well
                StorageCredentials credentials = new StorageCredentials(sasToken);
                StringBuilder      sasString   = new StringBuilder(credentials.TransformUri(blobURIBuilder.Uri).ToString());
                sasString.Replace("sp=", "SP=");
                CloudBlockBlob sasBlob = new CloudBlockBlob(new Uri(sasString.ToString()));

                // Validate that we can fetch the attributes on the blob (no exception thrown)
                sasBlob.FetchAttributes();
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
        public void CloudBlobDirectoryDelimitersInARow()
        {
            foreach (String delimiter in Delimiters)
            {
                CloudBlobClient client = GenerateCloudBlobClient();
                client.DefaultDelimiter = delimiter;
                string             name      = GetRandomContainerName();
                CloudBlobContainer container = client.GetContainerReference(name);

                try
                {
                    CloudPageBlob blob = container.GetPageBlobReference(delimiter + delimiter + delimiter + "Blob1");

                    ////Traverse from leaf to root
                    CloudBlobDirectory directory1 = blob.Parent;
                    Assert.AreEqual(directory1.Prefix, delimiter + delimiter + delimiter);

                    CloudBlobDirectory directory2 = directory1.Parent;
                    Assert.AreEqual(directory2.Prefix, delimiter + delimiter);

                    CloudBlobDirectory directory3 = directory2.Parent;
                    Assert.AreEqual(directory3.Prefix, delimiter);

                    ////Traverse from root to leaf
                    CloudBlobDirectory directory4 = container.GetDirectoryReference(delimiter);
                    CloudBlobDirectory directory5 = directory4.GetSubdirectoryReference(delimiter);
                    Assert.AreEqual(directory5.Prefix, delimiter + delimiter);

                    CloudBlobDirectory directory6 = directory5.GetSubdirectoryReference(delimiter);
                    Assert.AreEqual(directory6.Prefix, delimiter + delimiter + delimiter);

                    CloudPageBlob blob2 = directory6.GetPageBlobReference("Blob1");
                    Assert.AreEqual(blob2.Name, delimiter + delimiter + delimiter + "Blob1");
                    Assert.AreEqual(blob2.Uri, blob.Uri);
                }
                finally
                {
                    container.DeleteIfExists();
                }
            }
        }
        internal void DownloadBlobTestGB(Agent agent, StorageBlob.BlobType blobType)
        {
            string uploadFilePath   = @".\" + Utility.GenNameString("gbupload");
            string downloadFilePath = @".\" + Utility.GenNameString("gbdownload");
            string containerName    = Utility.GenNameString("gbupload-");
            string blobName         = Path.GetFileName(uploadFilePath);

            // create the container
            StorageBlob.CloudBlobContainer container = blobUtil.CreateContainer(containerName);

            // Generate a 512 bytes file which contains GB18030 characters
            File.WriteAllText(uploadFilePath, GB18030String);

            try
            {
                // create source blob
                StorageBlob.ICloudBlob blob = CloudBlobUtil.GetBlob(container, blobName, blobType);

                // upload file data
                using (var fileStream = System.IO.File.OpenRead(uploadFilePath))
                {
                    blob.UploadFromStream(fileStream);
                }

                //--------------Download operation--------------
                Test.Assert(agent.GetAzureStorageBlobContent(blobName, downloadFilePath, containerName, true), "download blob should be successful");

                // Check MD5
                string localMd5  = Helper.GetFileContentMD5(downloadFilePath);
                string uploadMd5 = Helper.GetFileContentMD5(uploadFilePath);
                blob.FetchAttributes();
                Test.Assert(localMd5 == uploadMd5, string.Format("blob content md5 should be {0}, and actualy it's {1}", localMd5, uploadMd5));
            }
            finally
            {
                // cleanup
                container.DeleteIfExists();
                File.Delete(uploadFilePath);
                File.Delete(downloadFilePath);
            }
        }
Ejemplo n.º 26
0
        /// <summary>
        /// Create a container and then get it using powershell cmdlet
        /// </summary>
        /// <returns>A CloudBlobContainer object which is returned by PowerShell</returns>
        protected StorageBlob.CloudBlobContainer CreateAndPsGetARandomContainer()
        {
            string containerName = Utility.GenNameString("bvt");

            StorageBlob.CloudBlobContainer container = SetUpStorageAccount.CreateCloudBlobClient().GetContainerReference(containerName);
            container.CreateIfNotExists();

            try
            {
                PowerShellAgent agent = new PowerShellAgent();
                Test.Assert(agent.GetAzureStorageContainer(containerName), Utility.GenComparisonData("GetAzureStorageContainer", true));
                int count = 1;
                Test.Assert(agent.Output.Count == count, string.Format("get container should return only 1 container, actully it's {0}", agent.Output.Count));
                return((StorageBlob.CloudBlobContainer)agent.Output[0]["CloudBlobContainer"]);
            }
            finally
            {
                // clean up
                container.DeleteIfExists();
            }
        }
 public void CloudBlobDirectoryBlobParentValidate()
 {
     foreach (String delimiter in Delimiters)
     {
         CloudBlobClient client = GenerateCloudBlobClient();
         client.DefaultDelimiter = delimiter;
         string             name      = GetRandomContainerName();
         CloudBlobContainer container = client.GetContainerReference(name);
         try
         {
             CloudBlockBlob     blob      = container.GetBlockBlobReference("TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir1" + delimiter + "EndBlob1");
             CloudBlobDirectory directory = blob.Parent;
             Assert.AreEqual(directory.Prefix, "TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir1" + delimiter);
             Assert.AreEqual(directory.Uri, container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir1" + delimiter);
         }
         finally
         {
             container.DeleteIfExists();
         }
     }
 }
Ejemplo n.º 28
0
        public void BlobReadWhenOpenWrite()
        {
            byte[]             buffer    = GetRandomBuffer(2 * 1024);
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();
                CloudPageBlob blob         = container.GetPageBlobReference("blob1");
                MemoryStream  memoryStream = new MemoryStream(buffer);
                Stream        blobStream   = blob.OpenWrite(2048);
                blobStream.Write(buffer, 0, 2048);
                byte[] testBuffer = new byte[2048];
                TestHelper.ExpectedException <NotSupportedException>(() => blobStream.Read(testBuffer, 0, 2048),
                                                                     "Try reading from a stream opened for Write");
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
        public void CloudBlobDirectoryGetParentOnRoot()
        {
            foreach (String delimiter in Delimiters)
            {
                CloudBlobClient client = GenerateCloudBlobClient();
                client.DefaultDelimiter = delimiter;
                string             name      = GetRandomContainerName();
                CloudBlobContainer container = client.GetContainerReference(name);

                try
                {
                    CloudBlobDirectory root   = container.GetDirectoryReference("TopDir1" + delimiter);
                    CloudBlobDirectory parent = root.Parent;
                    Assert.IsNull(parent);
                }
                finally
                {
                    container.DeleteIfExists();
                }
            }
        }
Ejemplo n.º 30
0
        public void BlockBlobWriteStreamSeekTest()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                CloudBlockBlob blob = container.GetBlockBlobReference("blob1");
                using (Stream blobStream = blob.OpenWrite())
                {
                    TestHelper.ExpectedException <NotSupportedException>(
                        () => blobStream.Seek(1, SeekOrigin.Begin),
                        "Block blob write stream should not be seekable");
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
Ejemplo n.º 31
0
        /// <summary>
        /// Parameters:
        ///     Block:
        ///         True for BlockBlob, false for PageBlob
        /// </summary>
        internal void RemoveBlobTest(Agent agent, string UploadFilePath, Microsoft.WindowsAzure.Storage.Blob.BlobType Type)
        {
            string NEW_CONTAINER_NAME = Utility.GenNameString("upload-");
            string blobName           = Path.GetFileName(UploadFilePath);

            Collection <Dictionary <string, object> > comp = new Collection <Dictionary <string, object> >();
            Dictionary <string, object> dic = Utility.GenComparisonData(StorageObjectType.Blob, blobName);

            dic["BlobType"] = Type;
            comp.Add(dic);

            // create the container
            StorageBlob.CloudBlobContainer container = CommonStorageAccount.CreateCloudBlobClient().GetContainerReference(NEW_CONTAINER_NAME);
            container.CreateIfNotExists();

            try
            {
                bool bSuccess = false;
                // upload the blob file
                if (Type == Microsoft.WindowsAzure.Storage.Blob.BlobType.BlockBlob)
                {
                    bSuccess = CommonBlobHelper.UploadFileToBlockBlob(NEW_CONTAINER_NAME, blobName, UploadFilePath);
                }
                else if (Type == Microsoft.WindowsAzure.Storage.Blob.BlobType.PageBlob)
                {
                    bSuccess = CommonBlobHelper.UploadFileToPageBlob(NEW_CONTAINER_NAME, blobName, UploadFilePath);
                }
                Test.Assert(bSuccess, "upload file {0} to container {1} should succeed", UploadFilePath, NEW_CONTAINER_NAME);

                //--------------Remove operation--------------
                Test.Assert(agent.RemoveAzureStorageBlob(blobName, NEW_CONTAINER_NAME), Utility.GenComparisonData("RemoveAzureStorageBlob", true));
                StorageBlob.ICloudBlob blob = CommonBlobHelper.QueryBlob(NEW_CONTAINER_NAME, blobName);
                Test.Assert(blob == null, "blob {0} should not exist!", blobName);
            }
            finally
            {
                // cleanup
                container.DeleteIfExists();
            }
        }