Ejemplo n.º 1
0
        public void CloudBlockBlobCopyTest()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                CloudBlockBlob source = container.GetBlockBlobReference("source");

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

                source.Metadata["Test"] = "value";
                source.SetMetadata();

                CloudBlockBlob copy   = container.GetBlockBlobReference("copy");
                string         copyId = copy.StartCopyFromBlob(TestHelper.Defiddler(source));
                WaitForCopy(copy);
                Assert.AreEqual(CopyStatus.Success, copy.CopyState.Status);
                Assert.AreEqual(source.Uri.AbsolutePath, copy.CopyState.Source.AbsolutePath);
                Assert.AreEqual(data.Length, copy.CopyState.TotalBytes);
                Assert.AreEqual(data.Length, copy.CopyState.BytesCopied);
                Assert.AreEqual(copyId, copy.CopyState.CopyId);
                Assert.IsTrue(copy.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));

                TestHelper.ExpectedException(
                    () => copy.AbortCopy(copyId),
                    "Aborting a copy operation after completion should fail",
                    HttpStatusCode.Conflict,
                    "NoPendingCopyOperation");

                source.FetchAttributes();
                Assert.IsNotNull(copy.Properties.ETag);
                Assert.AreNotEqual(source.Properties.ETag, copy.Properties.ETag);
                Assert.IsTrue(copy.Properties.LastModified > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));

                string copyData = DownloadText(copy, Encoding.UTF8);
                Assert.AreEqual(data, copyData, "Data inside copy of blob not similar");

                copy.FetchAttributes();
                BlobProperties prop1 = copy.Properties;
                BlobProperties prop2 = source.Properties;

                Assert.AreEqual(prop1.CacheControl, prop2.CacheControl);
                Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding);
                Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage);
                Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5);
                Assert.AreEqual(prop1.ContentType, prop2.ContentType);

                Assert.AreEqual("value", copy.Metadata["Test"], false, "Copied metadata not same");

                copy.Delete();
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
Ejemplo n.º 2
0
        public void GetCopyStateWhenCopyingTest()
        {
            StorageBlob.CloudBlobContainer Container = blobUtil.CreateContainer();
            string ContainerName = Container.Name;
            string BlobName      = Utility.GenNameString("blockblob");

            StorageBlob.CloudBlockBlob Blob = (StorageBlob.CloudBlockBlob)blobUtil.CreateBlockBlob(Container, BlobName);

            string uri = Test.Data.Get("BigFileUri");

            Test.Assert(!String.IsNullOrEmpty(uri), string.Format("Big file uri should be not empty, actually it's {0}", uri));

            if (String.IsNullOrEmpty(uri))
            {
                return;
            }

            Blob.StartCopy(new Uri(uri));

            int maxMonitorTime = 30;   //seconds
            int checkCount     = 0;
            int sleepInterval  = 1000; //ms

            StorageBlob.CopyStatus status = StorageBlob.CopyStatus.Pending;

            try
            {
                int expectedCopyStateCount = 1;

                do
                {
                    Test.Info(String.Format("{0}th check current copy state", checkCount));
                    Test.Assert(agent.GetAzureStorageBlobCopyState(ContainerName, BlobName, false), "Get copy state in dest container should be successed.");

                    Test.Assert(agent.Output.Count == expectedCopyStateCount, String.Format("Should contain {0} copy state, and actually it's {1}", expectedCopyStateCount, agent.Output.Count));
                    status = (StorageBlob.CopyStatus)agent.Output[0]["Status"];
                    Test.Assert(status == StorageBlob.CopyStatus.Pending, String.Format("Copy status should be Pending, actually it's {0}", status));
                    checkCount++;
                    Thread.Sleep(sleepInterval);
                }while (status == StorageBlob.CopyStatus.Pending && checkCount < maxMonitorTime);

                Test.Info("Finish the monitor loop and try to abort copy");

                try
                {
                    Blob.AbortCopy(Blob.CopyState.CopyId);
                }
                catch (StorageException e)
                {
                    //TODO use extension method
                    if (e.RequestInformation != null && e.RequestInformation.HttpStatusCode == 409)
                    {
                        Test.Info("Skip 409 abort conflict exception. Error:{0}", e.Message);
                        Test.Info("Detail Error Message: {0}", e.RequestInformation.HttpStatusMessage);
                    }
                    else
                    {
                        Test.AssertFail(String.Format("Can't abort copy. Error: {0}", e.Message));
                    }
                }

                Test.Assert(agent.GetAzureStorageBlobCopyState(ContainerName, BlobName, false), "Get copy state in dest container should be successed.");
                Test.Assert(agent.Output.Count == expectedCopyStateCount, String.Format("Should contain {0} copy state, and actually it's {1}", expectedCopyStateCount, agent.Output.Count));
                status = (StorageBlob.CopyStatus)agent.Output[0]["Status"];
                Test.Assert(status == StorageBlob.CopyStatus.Aborted, String.Format("Copy status should be Aborted, actually it's {0}", status));
            }
            finally
            {
                blobUtil.RemoveContainer(Container.Name);
            }
        }