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(); }
private static void TestContainerFetchAttributes(LocationMode?optionsLocationMode, LocationMode clientLocationMode, StorageLocation initialLocation, IList <RetryContext> retryContextList, IList <RetryInfo> retryInfoList) { CloudBlobContainer container = BlobTestBase.GetRandomContainerReference(); using (MultiLocationTestHelper helper = new MultiLocationTestHelper(container.ServiceClient.StorageUri, initialLocation, retryContextList, retryInfoList)) { container.ServiceClient.LocationMode = clientLocationMode; BlobRequestOptions options = new BlobRequestOptions() { LocationMode = optionsLocationMode, RetryPolicy = helper.RetryPolicy, }; TestHelper.ExpectedException( () => container.FetchAttributes(null, options, helper.OperationContext), "FetchAttributes on a non-existing container should fail", HttpStatusCode.NotFound); } }
public void RetryPolicyEnsure304IsNotRetried() { CloudBlobContainer container = BlobTestBase.GetRandomContainerReference(); container.Create(); try { CloudBlockBlob blob = container.GetBlockBlobReference("blob"); blob.UploadFromStream(new MemoryStream(new byte[50])); AccessCondition accessCondition = AccessCondition.GenerateIfModifiedSinceCondition(blob.Properties.LastModified.Value.AddMinutes(10)); OperationContext context = new OperationContext(); TestHelper.ExpectedException( () => blob.FetchAttributes(accessCondition, new BlobRequestOptions() { RetryPolicy = new ExponentialRetry() }, context), "FetchAttributes with invalid modified condition should return NotModified", HttpStatusCode.NotModified); TestHelper.AssertNAttempts(context, 1); context = new OperationContext(); TestHelper.ExpectedException( () => blob.FetchAttributes(accessCondition, new BlobRequestOptions() { RetryPolicy = new LinearRetry() }, context), "FetchAttributes with invalid modified condition should return NotModified", HttpStatusCode.NotModified); TestHelper.AssertNAttempts(context, 1); } finally { container.Delete(); } }
public void BlobIfExistsShouldNotHitSecondary() { AssertSecondaryEndpoint(); BlobRequestOptions options = new BlobRequestOptions(); CloudBlobContainer container = BlobTestBase.GetRandomContainerReference(); TestPrimaryOnlyCommand((opt, ctx) => container.CreateIfNotExists(opt, ctx), options); TestPrimaryOnlyCommand((opt, ctx) => container.EndCreateIfNotExists(container.BeginCreateIfNotExists(opt, ctx, null, null)), options); TestPrimaryOnlyCommand((opt, ctx) => container.DeleteIfExists(null, opt, ctx), options); TestPrimaryOnlyCommand((opt, ctx) => container.EndDeleteIfExists(container.BeginDeleteIfExists(null, opt, ctx, null, null)), options); CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1"); TestPrimaryOnlyCommand((opt, ctx) => blockBlob.DeleteIfExists(DeleteSnapshotsOption.None, null, opt, ctx), options); TestPrimaryOnlyCommand((opt, ctx) => blockBlob.EndDeleteIfExists(blockBlob.BeginDeleteIfExists(DeleteSnapshotsOption.None, null, opt, ctx, null, null)), options); CloudPageBlob pageBlob = container.GetPageBlobReference("blob2"); TestPrimaryOnlyCommand((opt, ctx) => pageBlob.DeleteIfExists(DeleteSnapshotsOption.None, null, opt, ctx), options); TestPrimaryOnlyCommand((opt, ctx) => pageBlob.EndDeleteIfExists(pageBlob.BeginDeleteIfExists(DeleteSnapshotsOption.None, null, opt, ctx, null, null)), options); }