public void CloudPageBlobCopyFromSnapshotTest() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudPageBlob source = container.GetPageBlobReference("source"); string data = new string('a', 512); UploadText(source, data, Encoding.UTF8); source.Metadata["Test"] = "value"; source.SetMetadata(); CloudPageBlob snapshot = source.CreateSnapshot(); //Modify source string newData = new string('b', 512); source.Metadata["Test"] = "newvalue"; source.SetMetadata(); source.Properties.ContentMD5 = null; UploadText(source, newData, Encoding.UTF8); Assert.AreEqual(newData, DownloadText(source, Encoding.UTF8), "Source is modified correctly"); Assert.AreEqual(data, DownloadText(snapshot, Encoding.UTF8), "Modifying source blob should not modify snapshot"); source.FetchAttributes(); snapshot.FetchAttributes(); Assert.AreNotEqual(source.Metadata["Test"], snapshot.Metadata["Test"], "Source and snapshot metadata should be independent"); CloudPageBlob copy = container.GetPageBlobReference("copy"); copy.StartCopyFromBlob(TestHelper.Defiddler(snapshot)); WaitForCopy(copy); Assert.AreEqual(CopyStatus.Success, copy.CopyState.Status); Assert.AreEqual(data, DownloadText(copy, Encoding.UTF8), "Data inside copy of blob not similar"); copy.FetchAttributes(); BlobProperties prop1 = copy.Properties; BlobProperties prop2 = snapshot.Properties; Assert.AreEqual(prop1.CacheControl, prop2.CacheControl); Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding); Assert.AreEqual(prop1.ContentDisposition, prop2.ContentDisposition); 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(); } }
public void PageBlobReadLockToETagTest() { byte[] outBuffer = new byte[1 * 1024 * 1024]; byte[] buffer = GetRandomBuffer(2 * outBuffer.Length); CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); blob.StreamMinimumReadSizeInBytes = outBuffer.Length; using (MemoryStream wholeBlob = new MemoryStream(buffer)) { blob.UploadFromStream(wholeBlob); } using (Stream blobStream = blob.OpenRead()) { blob.SetMetadata(); blobStream.Read(outBuffer, 0, outBuffer.Length); blob.SetMetadata(); TestHelper.ExpectedException( () => blobStream.Read(outBuffer, 0, outBuffer.Length), "Blob read stream should fail if blob is modified during read", HttpStatusCode.PreconditionFailed); } using (Stream blobStream = blob.OpenRead()) { blob.SetMetadata(); long length = blobStream.Length; blob.SetMetadata(); TestHelper.ExpectedException( () => blobStream.Read(outBuffer, 0, outBuffer.Length), "Blob read stream should fail if blob is modified during read", HttpStatusCode.PreconditionFailed); } AccessCondition accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(DateTimeOffset.Now.Subtract(TimeSpan.FromHours(1))); using (Stream blobStream = blob.OpenRead(accessCondition)) { blob.SetMetadata(); TestHelper.ExpectedException( () => blobStream.Read(outBuffer, 0, outBuffer.Length), "Blob read stream should fail if blob is modified during read", HttpStatusCode.PreconditionFailed); } } finally { container.DeleteIfExists(); } }
public void CloudPageBlobCopyTest() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudPageBlob source = container.GetPageBlobReference("source"); string data = new string('a', 512); UploadText(source, data, Encoding.UTF8); source.Metadata["Test"] = "value"; source.SetMetadata(); CloudPageBlob copy = container.GetPageBlobReference("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.ContentDisposition, prop2.ContentDisposition); 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(); } }
public void CloudPageBlobCopyTestWithMetadataOverride() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudPageBlob source = container.GetPageBlobReference("source"); string data = new string('a', 512); UploadText(source, data, Encoding.UTF8); source.Metadata["Test"] = "value"; source.SetMetadata(); CloudPageBlob copy = container.GetPageBlobReference("copy"); copy.Metadata["Test2"] = "value2"; 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))); string copyData = DownloadText(copy, Encoding.UTF8); Assert.AreEqual(data, copyData, "Data inside copy of blob not similar"); copy.FetchAttributes(); source.FetchAttributes(); BlobProperties prop1 = copy.Properties; BlobProperties prop2 = source.Properties; Assert.AreEqual(prop1.CacheControl, prop2.CacheControl); Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding); Assert.AreEqual(prop1.ContentDisposition, prop2.ContentDisposition); Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage); Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5); Assert.AreEqual(prop1.ContentType, prop2.ContentType); Assert.AreEqual("value2", copy.Metadata["Test2"], false, "Copied metadata not same"); Assert.IsFalse(copy.Metadata.ContainsKey("Test"), "Source Metadata should not appear in destination blob"); copy.Delete(); } finally { container.DeleteIfExists(); } }