public async Task ListContainersScenarioTest(ListingContext listingContext, HttpStatusCode?expectedError, params string[] expectedContainers) { HttpRequestMessage request = BlobTests.ListContainersRequest(BlobContext, listingContext); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { BlobTests.ListContainersResponse(response, BlobContext, expectedError); ListContainersResponse listContainersResponse = await ListContainersResponse.ParseAsync(HttpResponseParsers.GetResponseStream(response), CancellationToken.None); int i = 0; foreach (BlobContainerEntry item in listContainersResponse.Containers) { if (expectedContainers == null) { Assert.Fail("Should not have containers."); } Assert.IsTrue(i < expectedContainers.Length, "Unexpected container: " + item.Name); Assert.AreEqual <string>(expectedContainers[i++], item.Name, "Incorrect container."); } if (expectedContainers != null && i < expectedContainers.Length) { Assert.Fail("Missing container: " + expectedContainers[i] + "(and " + (expectedContainers.Length - i - 1) + " more)."); } } }
public async Task GetBlockListScenarioTest(string containerName, string blobName, BlockListingFilter typesOfBlocks, string leaseId, HttpStatusCode?expectedError, params string[] expectedBlocks) { HttpRequestMessage request = BlobTests.GetBlockListRequest(BlobContext, containerName, blobName, typesOfBlocks, AccessCondition.GenerateLeaseCondition(leaseId)); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { BlobTests.GetBlockListResponse(response, BlobContext, expectedError); IEnumerable <ListBlockItem> getBlockListResponse = await GetBlockListResponse.ParseAsync(HttpResponseParsers.GetResponseStream(response), CancellationToken.None); int i = 0; foreach (ListBlockItem item in getBlockListResponse) { if (expectedBlocks == null) { Assert.Fail("Should not have blocks."); } Assert.IsTrue(i < expectedBlocks.Length, "Unexpected block: " + item.Name); Assert.AreEqual <string>(expectedBlocks[i++], item.Name, "Incorrect block."); } if (expectedBlocks != null && i < expectedBlocks.Length) { Assert.Fail("Missing block: " + expectedBlocks[i] + "(and " + (expectedBlocks.Length - i - 1) + " more)."); } } }
public async Task DeleteBlob(string containerName, string blobName) { HttpRequestMessage request = BlobTests.DeleteBlobRequest(BlobContext, containerName, blobName, null); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); await BlobTestUtils.GetResponse(request, BlobContext); }
public async Task CreateBlob(string containerName, string blobName, bool isPublic) { Properties = new BlobProperties() { BlobType = BlobType.BlockBlob }; HttpRequestMessage request = BlobTests.PutBlobRequest(BlobContext, containerName, blobName, Properties, BlobType.BlockBlob, Content, 0, null); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); //HttpRequestHandler.SetContentLength(request, Content.Length); CancellationTokenSource timeout = new CancellationTokenSource(30000); request.Content = new ByteArrayContent(Content); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext, timeout)) { HttpStatusCode statusCode = response.StatusCode; string statusDescription = response.ReasonPhrase; if (statusCode != HttpStatusCode.Created) { Assert.Fail(string.Format("Failed to create blob: {0}, Status: {1}, Status Description: {2}", containerName, statusCode, statusDescription)); } } }
public async Task BlobProtocolGetContainerWithEmptyHeader() { ListingContext listingContext = new ListingContext("default", null); await cloudOwnerAsync.CreateContainer("emptyheadercontainer", true); HttpRequestMessage request = BlobTests.ListContainersRequest(cloudOwnerAsync.BlobContext, listingContext); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); if (cloudOwnerAsync.BlobContext.Credentials != null) { request.Headers.Add("x-ms-blob-application-metadata", ""); } using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, cloudOwnerAsync.BlobContext)) { BlobTests.ListContainersResponse(response, cloudOwnerAsync.BlobContext, HttpStatusCode.OK /*HttpStatusCode.Forbidden*/); } request = BlobTests.ListContainersRequest(cloudOwnerAsync.BlobContext, listingContext); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); if (cloudOwnerAsync.BlobContext.Credentials != null) { request.Headers.Add("x-ms-blob-application-metadata", ""); } using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, cloudOwnerAsync.BlobContext)) { BlobTests.ListContainersResponse(response, cloudOwnerAsync.BlobContext, HttpStatusCode.OK); } }
public async Task CreateContainer(string containerName, bool isPublic, int retries, int millisecondsBetweenRetries) { HttpStatusCode statusCode; StorageExtendedErrorInformation error; while (true) { HttpRequestMessage request = BlobTests.CreateContainerRequest(BlobContext, containerName); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); if (isPublic) { request.Headers.Add("x-ms-blob-public-access", "container"); } using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { statusCode = response.StatusCode; string statusDescription = response.ReasonPhrase; error = await StorageExtendedErrorInformation.ReadFromStreamAsync( HttpResponseParsers.GetResponseStream(response)); } // if the container is being deleted, retry up to the specified times. if (statusCode == HttpStatusCode.Conflict && error != null && error.ErrorCode == BlobErrorCodeStrings.ContainerBeingDeleted && retries > 0) { await Task.Delay(millisecondsBetweenRetries); retries--; continue; } break; } }
public async static void AcquireAndReleaseLeaseTest(BlobContext context, string containerName, string blobName) { CloudStorageAccount account = new CloudStorageAccount(new StorageCredentials(context.Account, context.Key), false); CloudBlobClient client = new CloudBlobClient(new Uri(context.Address), account.Credentials); CloudBlobContainer container = client.GetContainerReference(containerName); CloudBlockBlob blob = container.GetBlockBlobReference(blobName); BlobTestBase.UploadText(blob, "Text sent to cloud", Encoding.UTF8); // acquire a release on the blob and check LeaseStatus to be "locked" OperationContext opContext = new OperationContext(); HttpRequestMessage blobRequest = BlobHttpRequestMessageFactory.Lease( blob.Uri, context.Timeout, LeaseAction.Acquire, null /* proposed lease ID */, 60 /* lease duration */, null /* break period */, null /* access condition */, null, opContext, SharedKeyCanonicalizer.Instance, context.Credentials); string leaseId = null; using (HttpResponseMessage response = await BlobTestUtils.GetResponse(blobRequest, context)) { leaseId = HttpResponseParsers.GetHeader(response, "x-ms-lease-id"); Assert.AreEqual <HttpStatusCode>(response.StatusCode, HttpStatusCode.Created); } blob.FetchAttributes(); Assert.AreEqual <LeaseStatus>(blob.Properties.LeaseStatus, LeaseStatus.Locked); // release the release on the blob and check LeaseStatus to be "unlocked" opContext = new OperationContext(); blobRequest = BlobHttpRequestMessageFactory.Lease( blob.Uri, context.Timeout, LeaseAction.Release, null /* proposed lease ID */, null /* lease duration */, null /* break period */, AccessCondition.GenerateLeaseCondition(leaseId), null, opContext, SharedKeyCanonicalizer.Instance, context.Credentials); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(blobRequest, context)) { Assert.AreEqual <HttpStatusCode>(response.StatusCode, HttpStatusCode.OK); } blob.FetchAttributes(); Assert.AreEqual <LeaseStatus>(blob.Properties.LeaseStatus, LeaseStatus.Unlocked); blob.Delete(); }
/// <summary> /// Scenario test for releasing a lease. /// </summary> /// <param name="containerName">The name of the container.</param> /// <param name="blobName">The name of the blob, if any.</param> /// <param name="leaseId">The lease ID.</param> /// <param name="expectedError">The error status code to expect.</param> public async Task ReleaseLeaseScenarioTest(string containerName, string blobName, string leaseId, HttpStatusCode?expectedError) { // Create and validate the web request HttpRequestMessage request = BlobTests.ReleaseLeaseRequest(BlobContext, containerName, blobName, AccessCondition.GenerateLeaseCondition(leaseId)); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { BlobTests.ReleaseLeaseResponse(response, expectedError); } }
/// <summary> /// Scenario test for acquiring a lease. /// </summary> /// <param name="containerName">The name of the container.</param> /// <param name="blobName">The name of the blob, if any.</param> /// <param name="leaseDuration">The lease duration.</param> /// <param name="proposedLeaseId">The proposed lease ID.</param> /// <param name="expectedError">The error status code to expect.</param> /// <returns>The lease ID.</returns> public async Task <string> AcquireLeaseScenarioTest(string containerName, string blobName, int leaseDuration, string proposedLeaseId, HttpStatusCode?expectedError) { // Create and validate the web request HttpRequestMessage request = BlobTests.AcquireLeaseRequest(BlobContext, containerName, blobName, leaseDuration, proposedLeaseId, null); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { BlobTests.AcquireLeaseResponse(response, proposedLeaseId, expectedError); return(BlobHttpResponseParsers.GetLeaseId(response)); } }
public async Task PutBlockScenarioTest(string containerName, string blobName, string blockId, string leaseId, byte[] content, HttpStatusCode?expectedError) { HttpRequestMessage request = BlobTests.PutBlockRequest(BlobContext, containerName, blobName, blockId, AccessCondition.GenerateLeaseCondition(leaseId)); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); //HttpRequestHandler.SetContentLength(request, content.Length); request.Content = new ByteArrayContent(content); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { BlobTests.PutBlockResponse(response, BlobContext, expectedError); } }
/// <summary> /// Scenario test for changing a lease. /// </summary> /// <param name="containerName">The name of the container.</param> /// <param name="blobName">The name of the blob, if any.</param> /// <param name="leaseId">The lease ID.</param> /// <param name="proposedLeaseId">The proposed lease ID.</param> /// <param name="expectedError">The error status code to expect.</param> /// <returns>The lease ID.</returns> public async Task <string> ChangeLeaseScenarioTest(string containerName, string blobName, string leaseId, string proposedLeaseId, HttpStatusCode?expectedError) { // Create and validate the web request HttpRequestMessage request = BlobTests.ChangeLeaseRequest(BlobContext, containerName, blobName, proposedLeaseId, AccessCondition.GenerateLeaseCondition(leaseId)); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { BlobTests.ChangeLeaseResponse(response, proposedLeaseId, expectedError); return(BlobHttpResponseParsers.GetLeaseId(response)); } }
public async Task GetBlobScenarioTest(string containerName, string blobName, BlobProperties properties, string leaseId, byte[] content, HttpStatusCode?expectedError) { HttpRequestMessage request = BlobTests.GetBlobRequest(BlobContext, containerName, blobName, AccessCondition.GenerateLeaseCondition(leaseId)); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { BlobTests.GetBlobResponse(response, BlobContext, properties, content, expectedError); } }
public async Task PutBlobScenarioTest(string containerName, string blobName, BlobProperties properties, BlobType blobType, byte[] content, HttpStatusCode?expectedError) { HttpRequestMessage request = BlobTests.PutBlobRequest(BlobContext, containerName, blobName, properties, blobType, content, content.Length, null); request.Content = new ByteArrayContent(content); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); //HttpRequestHandler.SetContentLength(request, content.Length); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { BlobTests.PutBlobResponse(response, BlobContext, expectedError); } }
/// <summary> /// Scenario test for breaking a lease. /// </summary> /// <param name="containerName">The name of the container.</param> /// <param name="blobName">The name of the blob, if any.</param> /// <param name="breakPeriod">The break period.</param> /// <param name="expectedRemainingTime">The expected remaining time.</param> /// <param name="expectedError">The error status code to expect.</param> /// <returns>The remaining lease time.</returns> public async Task <int> BreakLeaseScenarioTest(string containerName, string blobName, int?breakPeriod, int?expectedRemainingTime, HttpStatusCode?expectedError) { // Create and validate the web request HttpRequestMessage request = BlobTests.BreakLeaseRequest(BlobContext, containerName, blobName, breakPeriod, null); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { int expectedTime = expectedRemainingTime ?? breakPeriod.Value; int errorMargin = 10; BlobTests.BreakLeaseResponse(response, expectedTime, errorMargin, expectedError); return(expectedError.HasValue ? 0 : BlobHttpResponseParsers.GetRemainingLeaseTime(response).Value); } }
public async Task PutBlockListScenarioTest(string containerName, string blobName, List <PutBlockListItem> blocks, BlobProperties blobProperties, string leaseId, HttpStatusCode?expectedError) { HttpRequestMessage request = BlobTests.PutBlockListRequest(BlobContext, containerName, blobName, blobProperties, AccessCondition.GenerateLeaseCondition(leaseId)); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); byte[] content; using (MemoryStream stream = new MemoryStream()) { BlobRequest.WriteBlockListBody(blocks, stream); stream.Seek(0, SeekOrigin.Begin); content = new byte[stream.Length]; stream.Read(content, 0, content.Length); } //HttpRequestHandler.SetContentLength(request, content.Length); request.Content = new ByteArrayContent(content); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { BlobTests.PutBlockListResponse(response, BlobContext, expectedError); } }
/// <summary> /// Sends a get blob range request with the given parameters and validates both request and response. /// </summary> /// <param name="containerName">The blob's container's name.</param> /// <param name="blobName">The blob's name.</param> /// <param name="leaseId">The lease ID, or null if there is no lease.</param> /// <param name="content">The total contents of the blob.</param> /// <param name="offset">The offset of the contents we will get.</param> /// <param name="count">The number of bytes we will get, or null to get the rest of the blob.</param> /// <param name="expectedError">The error code we expect from this operation, or null if we expect it to succeed.</param> public async Task GetBlobRangeScenarioTest(string containerName, string blobName, string leaseId, byte[] content, long offset, long?count, HttpStatusCode?expectedError) { HttpRequestMessage request = BlobTests.GetBlobRangeRequest(BlobContext, containerName, blobName, offset, count, AccessCondition.GenerateLeaseCondition(leaseId)); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { long endRange = count.HasValue ? count.Value + offset - 1 : content.Length - 1; byte[] selectedContent = null; // Compute expected content only if call is expected to succeed. if (expectedError == null) { selectedContent = new byte[endRange - offset + 1]; Array.Copy(content, offset, selectedContent, 0, selectedContent.Length); } BlobTests.CheckBlobRangeResponse(response, BlobContext, selectedContent, offset, endRange, content.Length, expectedError); } }
public async Task ClearPageRangeScenarioTest(string containerName, string blobName, HttpStatusCode?expectedError) { // 1. Create Sparse Page Blob int blobSize = 128 * 1024; BlobProperties properties = new BlobProperties() { BlobType = BlobType.PageBlob }; Uri uri = BlobTests.ConstructPutUri(BlobContext.Address, containerName, blobName); OperationContext opContext = new OperationContext(); HttpRequestMessage webRequest = BlobHttpRequestMessageFactory.Put(uri, BlobContext.Timeout, properties, BlobType.PageBlob, blobSize, null, null, null, opContext, null, null); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(webRequest, BlobContext)) { BlobTests.PutBlobResponse(response, BlobContext, expectedError); } // 2. Now upload some page ranges for (int m = 0; m * 512 * 4 < blobSize; m++) { int startOffset = 512 * 4 * m; int length = 512; PageRange range = new PageRange(startOffset, startOffset + length - 1); opContext = new OperationContext(); HttpRequestMessage pageRequest = BlobHttpRequestMessageFactory.PutPage(uri, BlobContext.Timeout, range, PageWrite.Update, null, null, opContext, null, null); HttpRequestHandler.SetContentLength(pageRequest, 512); byte[] content = new byte[length]; random.NextBytes(content); pageRequest.Content = new ByteArrayContent(content); using (HttpResponseMessage pageResponse = await BlobTestUtils.GetResponse(pageRequest, BlobContext)) { } } // 3. Now do a Get Page Ranges List <PageRange> pageRanges = new List <PageRange>(); opContext = new OperationContext(); HttpRequestMessage pageRangeRequest = BlobHttpRequestMessageFactory.GetPageRanges(uri, BlobContext.Timeout, null, null, null, null, null, opContext, null, null); using (HttpResponseMessage pageRangeResponse = await BlobTestUtils.GetResponse(pageRangeRequest, BlobContext)) { IEnumerable <PageRange> ranges = await GetPageRangesResponse.ParseAsync(HttpResponseParsers.GetResponseStream(pageRangeResponse), CancellationToken.None); pageRanges.AddRange(ranges); } // 4. Now Clear some pages bool skipFlag = false; foreach (PageRange pRange in pageRanges) { skipFlag = !skipFlag; if (skipFlag) { continue; } opContext = new OperationContext(); HttpRequestMessage clearPageRequest = BlobHttpRequestMessageFactory.PutPage(uri, BlobContext.Timeout, pRange, PageWrite.Clear, null, null, opContext, null, null); HttpRequestHandler.SetContentLength(clearPageRequest, 0); using (HttpResponseMessage clearResponse = await BlobTestUtils.GetResponse(clearPageRequest, BlobContext)) { } } // 5. Get New Page ranges and verify List <PageRange> newPageRanges = new List <PageRange>(); opContext = new OperationContext(); HttpRequestMessage newPageRangeRequest = BlobHttpRequestMessageFactory.GetPageRanges(uri, BlobContext.Timeout, null, null, null, null, null, opContext, null, null); using (HttpResponseMessage newPageRangeResponse = await BlobTestUtils.GetResponse(newPageRangeRequest, BlobContext)) { IEnumerable <PageRange> ranges = await GetPageRangesResponse.ParseAsync(HttpResponseParsers.GetResponseStream(newPageRangeResponse), CancellationToken.None); newPageRanges.AddRange(ranges); } Assert.AreEqual(pageRanges.Count(), newPageRanges.Count() * 2); for (int l = 0; l < newPageRanges.Count(); l++) { Assert.AreEqual(pageRanges[2 * l].StartOffset, newPageRanges[l].StartOffset); Assert.AreEqual(pageRanges[2 * l].EndOffset, newPageRanges[l].EndOffset); } }