|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +extern alias DMBlobs; |
| 4 | +extern alias BaseBlobs; |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Azure.Storage.Test.Shared; |
| 9 | +using Azure.Storage.DataMovement.Tests; |
| 10 | +using DMBlobs::Azure.Storage.DataMovement.Blobs; |
| 11 | +using BaseBlobs::Azure.Storage.Blobs; |
| 12 | +using BaseBlobs::Azure.Storage.Blobs.Specialized; |
| 13 | +using BaseBlobs::Azure.Storage.Blobs.Models; |
| 14 | +using System.IO; |
| 15 | +using NUnit.Framework; |
| 16 | +using Metadata = System.Collections.Generic.IDictionary<string, string>; |
| 17 | + |
| 18 | +namespace Azure.Storage.DataMovement.Blobs.Tests |
| 19 | +{ |
| 20 | + [DataMovementBlobsClientTestFixture] |
| 21 | + public class BlobPauseResumeTransferTests : PauseResumeTransferTestBase |
| 22 | + <BlobServiceClient, |
| 23 | + BlobContainerClient, |
| 24 | + BlockBlobClient, |
| 25 | + BlobClientOptions, |
| 26 | + StorageTestEnvironment> |
| 27 | + { |
| 28 | + protected readonly BlobClientOptions.ServiceVersion _serviceVersion; |
| 29 | + |
| 30 | + public BlobPauseResumeTransferTests(bool async, BlobClientOptions.ServiceVersion serviceVersion) |
| 31 | + : base(async, null /* RecordedTestMode.Record /* to re-record */) |
| 32 | + { |
| 33 | + _serviceVersion = serviceVersion; |
| 34 | + ClientBuilder = ClientBuilderExtensions.GetNewBlobsClientBuilder(Tenants, serviceVersion); |
| 35 | + } |
| 36 | + |
| 37 | + protected override async Task<IDisposingContainer<BlobContainerClient>> GetDisposingContainerAsync( |
| 38 | + string containerName = default, |
| 39 | + BlobServiceClient service = default) |
| 40 | + => await ClientBuilder.GetTestContainerAsync(service, containerName); |
| 41 | + |
| 42 | + protected override StorageResourceProvider GetStorageResourceProvider() |
| 43 | + => new BlobsStorageResourceProvider(TestEnvironment.Credential); |
| 44 | + |
| 45 | + protected override async Task<StorageResource> CreateSourceStorageResourceItemAsync( |
| 46 | + long size, |
| 47 | + string blobName, |
| 48 | + BlobContainerClient container) |
| 49 | + { |
| 50 | + BlockBlobClient blobClient = container.GetBlockBlobClient(blobName); |
| 51 | + // create a new file and copy contents of stream into it, and then close the FileStream |
| 52 | + using (Stream originalStream = await CreateLimitedMemoryStream(size)) |
| 53 | + { |
| 54 | + // Upload blob to storage account |
| 55 | + originalStream.Position = 0; |
| 56 | + await blobClient.UploadAsync(originalStream); |
| 57 | + } |
| 58 | + return BlobsStorageResourceProvider.FromClient(blobClient); |
| 59 | + } |
| 60 | + |
| 61 | + protected override StorageResource CreateDestinationStorageResourceItem( |
| 62 | + string blobName, |
| 63 | + BlobContainerClient container, |
| 64 | + Metadata metadata = default, |
| 65 | + string contentLanguage = default) |
| 66 | + { |
| 67 | + BlockBlobStorageResourceOptions testOptions = new() |
| 68 | + { |
| 69 | + Metadata = metadata, |
| 70 | + ContentLanguage = contentLanguage, |
| 71 | + }; |
| 72 | + BlockBlobClient destinationClient = container.GetBlockBlobClient(blobName); |
| 73 | + return BlobsStorageResourceProvider.FromClient(destinationClient, testOptions); |
| 74 | + } |
| 75 | + |
| 76 | + protected override async Task AssertDestinationProperties( |
| 77 | + string blobName, |
| 78 | + Metadata expectedMetadata, |
| 79 | + string expectedContentLanguage, |
| 80 | + BlobContainerClient container) |
| 81 | + { |
| 82 | + BlockBlobClient blob = container.GetBlockBlobClient(blobName); |
| 83 | + BlobProperties props = (await blob.GetPropertiesAsync()).Value; |
| 84 | + Assert.That(props.Metadata, Is.EqualTo(expectedMetadata)); |
| 85 | + Assert.That(props.ContentLanguage, Is.EqualTo(expectedContentLanguage)); |
| 86 | + } |
| 87 | + |
| 88 | + protected override async Task<Stream> GetStreamFromContainerAsync(Uri uri, BlobContainerClient container) |
| 89 | + { |
| 90 | + BlobUriBuilder builder = new BlobUriBuilder(uri); |
| 91 | + BlockBlobClient blobClient = container.GetBlockBlobClient(builder.BlobName); |
| 92 | + if (!await blobClient.ExistsAsync()) |
| 93 | + { |
| 94 | + throw new FileNotFoundException($"Blob not found: {uri}"); |
| 95 | + } |
| 96 | + |
| 97 | + MemoryStream stream = new MemoryStream(); |
| 98 | + await blobClient.DownloadToAsync(stream); |
| 99 | + stream.Position = 0; |
| 100 | + return stream; |
| 101 | + } |
| 102 | + |
| 103 | + protected override async Task<StorageResource> CreateSourceStorageResourceContainerAsync( |
| 104 | + long size, |
| 105 | + int blobCount, |
| 106 | + string directoryPath, |
| 107 | + BlobContainerClient container) |
| 108 | + { |
| 109 | + for (int i = 0; i < blobCount; i++) |
| 110 | + { |
| 111 | + if (i % 3 == 0) |
| 112 | + { |
| 113 | + BlockBlobClient blobClient = container.GetBlockBlobClient(string.Join("/", directoryPath, GetNewItemName())); |
| 114 | + // create a new file and copy contents of stream into it, and then close the FileStream |
| 115 | + using (Stream originalStream = await CreateLimitedMemoryStream(size)) |
| 116 | + { |
| 117 | + // Upload blob to storage account |
| 118 | + originalStream.Position = 0; |
| 119 | + await blobClient.UploadAsync(originalStream); |
| 120 | + } |
| 121 | + } |
| 122 | + else if (i % 3 == 1) |
| 123 | + { |
| 124 | + AppendBlobClient blobClient = container.GetAppendBlobClient(string.Join("/", directoryPath, GetNewItemName())); |
| 125 | + await blobClient.CreateAsync(); |
| 126 | + // create a new file and copy contents of stream into it, and then close the FileStream |
| 127 | + using (Stream originalStream = await CreateLimitedMemoryStream(size)) |
| 128 | + { |
| 129 | + // Upload blob to storage account |
| 130 | + originalStream.Position = 0; |
| 131 | + await blobClient.AppendBlockAsync(originalStream); |
| 132 | + } |
| 133 | + } |
| 134 | + else |
| 135 | + { |
| 136 | + PageBlobClient blobClient = container.GetPageBlobClient(string.Join("/", directoryPath, GetNewItemName())); |
| 137 | + await blobClient.CreateAsync(size); |
| 138 | + // create a new file and copy contents of stream into it, and then close the FileStream |
| 139 | + using (Stream originalStream = await CreateLimitedMemoryStream(size)) |
| 140 | + { |
| 141 | + // Upload blob to storage account |
| 142 | + originalStream.Position = 0; |
| 143 | + await blobClient.UploadPagesAsync(originalStream, 0); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + BlobStorageResourceContainerOptions options = new(); |
| 148 | + options.BlobPrefix = directoryPath; |
| 149 | + return BlobsStorageResourceProvider.FromClient(container, options); |
| 150 | + } |
| 151 | + |
| 152 | + protected override StorageResource CreateDestinationStorageResourceContainer( |
| 153 | + BlobContainerClient container) |
| 154 | + { |
| 155 | + BlobStorageResourceContainerOptions options = new() |
| 156 | + { |
| 157 | + BlobPrefix = GetNewContainerName() |
| 158 | + }; |
| 159 | + return BlobsStorageResourceProvider.FromClient(container, options); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments