|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Azure.Storage.Test.Shared; |
| 7 | +using Azure.Storage.DataMovement.Tests; |
| 8 | +using Azure.Storage.Files.Shares; |
| 9 | +using Azure.Storage.Files.Shares.Tests; |
| 10 | +using System.IO; |
| 11 | +using Azure.Core; |
| 12 | +using Azure.Core.TestFramework; |
| 13 | + |
| 14 | +namespace Azure.Storage.DataMovement.Files.Shares.Tests |
| 15 | +{ |
| 16 | + [ShareClientTestFixture] |
| 17 | + public class ShareFileStartTransferCopyTests : StartTransferCopyTestBase |
| 18 | + <ShareServiceClient, |
| 19 | + ShareClient, |
| 20 | + ShareFileClient, |
| 21 | + ShareClientOptions, |
| 22 | + ShareServiceClient, |
| 23 | + ShareClient, |
| 24 | + ShareFileClient, |
| 25 | + ShareClientOptions, |
| 26 | + StorageTestEnvironment> |
| 27 | + { |
| 28 | + private const string _fileResourcePrefix = "test-file-"; |
| 29 | + private const string _expectedOverwriteExceptionMessage = "Cannot overwrite file."; |
| 30 | + protected readonly ShareClientOptions.ServiceVersion _serviceVersion; |
| 31 | + |
| 32 | + public ShareFileStartTransferCopyTests(bool async, ShareClientOptions.ServiceVersion serviceVersion) |
| 33 | + : base(async, _expectedOverwriteExceptionMessage, _fileResourcePrefix, null /* RecordedTestMode.Record /* to re-record */) |
| 34 | + { |
| 35 | + _serviceVersion = serviceVersion; |
| 36 | + SourceClientBuilder = ClientBuilderExtensions.GetNewShareClientBuilder(Tenants, serviceVersion); |
| 37 | + DestinationClientBuilder = ClientBuilderExtensions.GetNewShareClientBuilder(Tenants, serviceVersion); |
| 38 | + } |
| 39 | + |
| 40 | + protected override async Task<bool> SourceExistsAsync(ShareFileClient objectClient) |
| 41 | + => await objectClient.ExistsAsync(); |
| 42 | + |
| 43 | + protected override async Task<bool> DestinationExistsAsync(ShareFileClient objectClient) |
| 44 | + => await objectClient.ExistsAsync(); |
| 45 | + |
| 46 | + protected override async Task<IDisposingContainer<ShareClient>> GetSourceDisposingContainerAsync(ShareServiceClient service = null, string containerName = null) |
| 47 | + => await SourceClientBuilder.GetTestShareAsync(service, containerName); |
| 48 | + |
| 49 | + protected override async Task<IDisposingContainer<ShareClient>> GetDestinationDisposingContainerAsync(ShareServiceClient service = null, string containerName = null) |
| 50 | + => await DestinationClientBuilder.GetTestShareAsync(service, containerName); |
| 51 | + |
| 52 | + private async Task<ShareFileClient> CreateFileClientAsync( |
| 53 | + ShareClient container, |
| 54 | + long? objectLength = null, |
| 55 | + bool createResource = false, |
| 56 | + string objectName = null, |
| 57 | + ShareClientOptions options = null, |
| 58 | + Stream contents = null) |
| 59 | + { |
| 60 | + objectName ??= GetNewObjectName(); |
| 61 | + ShareFileClient fileClient = container.GetRootDirectoryClient().GetFileClient(objectName); |
| 62 | + if (createResource) |
| 63 | + { |
| 64 | + if (!objectLength.HasValue) |
| 65 | + { |
| 66 | + throw new InvalidOperationException($"Cannot create share file without size specified. Either set {nameof(createResource)} to false or specify a {nameof(objectLength)}."); |
| 67 | + } |
| 68 | + await fileClient.CreateAsync(objectLength.Value); |
| 69 | + |
| 70 | + if (contents != default) |
| 71 | + { |
| 72 | + await fileClient.UploadAsync(contents); |
| 73 | + } |
| 74 | + } |
| 75 | + Uri sourceUri = fileClient.GenerateSasUri(Sas.ShareFileSasPermissions.All, Recording.UtcNow.AddDays(1)); |
| 76 | + return InstrumentClient(new ShareFileClient(sourceUri, GetOptions())); |
| 77 | + } |
| 78 | + |
| 79 | + protected override Task<ShareFileClient> GetSourceObjectClientAsync( |
| 80 | + ShareClient container, |
| 81 | + long? objectLength = null, |
| 82 | + bool createResource = false, |
| 83 | + string objectName = null, |
| 84 | + ShareClientOptions options = null, |
| 85 | + Stream contents = null) |
| 86 | + => CreateFileClientAsync( |
| 87 | + container, |
| 88 | + objectLength, |
| 89 | + createResource, |
| 90 | + objectName, |
| 91 | + options, |
| 92 | + contents); |
| 93 | + |
| 94 | + protected override StorageResourceItem GetSourceStorageResourceItem(ShareFileClient objectClient) |
| 95 | + => new ShareFileStorageResource(objectClient); |
| 96 | + |
| 97 | + protected override Task<Stream> SourceOpenReadAsync(ShareFileClient objectClient) |
| 98 | + => objectClient.OpenReadAsync(); |
| 99 | + |
| 100 | + protected override Task<ShareFileClient> GetDestinationObjectClientAsync( |
| 101 | + ShareClient container, |
| 102 | + long? objectLength = null, |
| 103 | + bool createResource = false, |
| 104 | + string objectName = null, |
| 105 | + ShareClientOptions options = null, |
| 106 | + Stream contents = null) |
| 107 | + => CreateFileClientAsync( |
| 108 | + container, |
| 109 | + objectLength, |
| 110 | + createResource, |
| 111 | + objectName, |
| 112 | + options, |
| 113 | + contents); |
| 114 | + |
| 115 | + protected override StorageResourceItem GetDestinationStorageResourceItem(ShareFileClient objectClient) |
| 116 | + => new ShareFileStorageResource(objectClient); |
| 117 | + |
| 118 | + protected override Task<Stream> DestinationOpenReadAsync(ShareFileClient objectClient) |
| 119 | + => objectClient.OpenReadAsync(); |
| 120 | + |
| 121 | + public ShareClientOptions GetOptions() |
| 122 | + { |
| 123 | + var options = new ShareClientOptions(_serviceVersion) |
| 124 | + { |
| 125 | + Diagnostics = { IsLoggingEnabled = true }, |
| 126 | + Retry = |
| 127 | + { |
| 128 | + Mode = RetryMode.Exponential, |
| 129 | + MaxRetries = Constants.MaxReliabilityRetries, |
| 130 | + Delay = TimeSpan.FromSeconds(Mode == RecordedTestMode.Playback ? 0.01 : 1), |
| 131 | + MaxDelay = TimeSpan.FromSeconds(Mode == RecordedTestMode.Playback ? 0.1 : 60) |
| 132 | + }, |
| 133 | + }; |
| 134 | + if (Mode != RecordedTestMode.Live) |
| 135 | + { |
| 136 | + options.AddPolicy(new RecordedClientRequestIdPolicy(Recording), HttpPipelinePosition.PerCall); |
| 137 | + } |
| 138 | + |
| 139 | + return InstrumentClientOptions(options); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments