Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Storage] [DataMovement] Added Download Share Directory Tests #39645

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@
<ItemGroup>
<Compile Include="$(AzureStorageDataMovementTestSharedSources)TestEventsRaised.cs" LinkBase="Shared\DataMovement" />
<Compile Include="$(AzureStorageDataMovementTestSharedSources)DisposingLocalDirectory.cs" LinkBase="Shared\DataMovement" />
<Compile Include="$(AzureStorageDataMovementTestSharedSources)TransferValidator.cs" LinkBase="Shared\DataMovement" />
<Compile Include="$(AzureStorageDataMovementTestSharedSources)TransferValidator.Local.cs" LinkBase="Shared\DataMovement" />
<Compile Include="$(AzureStorageDataMovementTestSharedSources)StartTransferUploadTestBase.cs" LinkBase="Shared\DataMovement" />
<Compile Include="$(AzureStorageDataMovementTestSharedSources)StartTransferCopyTestBase.cs" LinkBase="Shared\DataMovement" />
<Compile Include="$(AzureStorageDataMovementTestSharedSources)StartTransferDownloadTestBase.cs" LinkBase="Shared\DataMovement" />
<Compile Include="$(AzureStorageDataMovementTestSharedSources)StartTransferDirectoryDownloadTestBase.cs" LinkBase="Shared\DataMovement" />
</ItemGroup>
<ItemGroup>
<Content Include="Resources\**">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Storage.DataMovement.Tests;
using Azure.Storage.Files.Shares;
using Azure.Storage.Files.Shares.Tests;
using Azure.Storage.Test.Shared;

namespace Azure.Storage.DataMovement.Files.Shares.Tests
{
[ShareClientTestFixture]
public class ShareDirectoryStartTransferDownloadTests
: StartTransferDirectoryDownloadTestBase<
ShareServiceClient,
ShareClient,
ShareClientOptions,
StorageTestEnvironment>
{
private const string _fileResourcePrefix = "test-file-";
private const string _dirResourcePrefix = "test-file-";
private const string _expectedOverwriteExceptionMessage = "Cannot overwrite file.";

public ShareDirectoryStartTransferDownloadTests(
bool async,
ShareClientOptions.ServiceVersion serviceVersion)
: base(async, _expectedOverwriteExceptionMessage, _fileResourcePrefix, _dirResourcePrefix, null /* RecordedTestMode.Record /* to re-record */)
{
ClientBuilder = ClientBuilderExtensions.GetNewShareClientBuilder(Tenants, serviceVersion);
}

protected override async Task<IDisposingContainer<ShareClient>> GetDisposingContainerAsync(ShareServiceClient service = null, string containerName = null)
=> await ClientBuilder.GetTestShareAsync(service, containerName);

protected override async Task CreateObjectClientAsync(
ShareClient container,
long? objectLength,
string objectName,
bool createResource = false,
ShareClientOptions options = null,
Stream contents = null,
CancellationToken cancellationToken = default)
{
objectName ??= GetNewObjectName();
if (!objectLength.HasValue)
{
throw new InvalidOperationException($"Cannot create share file without size specified. Specify {nameof(objectLength)}.");
}
ShareFileClient fileClient = container.GetRootDirectoryClient().GetFileClient(objectName);
await fileClient.CreateAsync(objectLength.Value, cancellationToken: cancellationToken);

if (contents != default)
{
await fileClient.UploadAsync(contents, cancellationToken: cancellationToken);
}
}

protected override TransferValidator.ListFilesAsync GetSourceLister(ShareClient container, string prefix)
=> TransferValidator.GetShareFilesLister(container, prefix);

protected override StorageResourceContainer GetStorageResourceContainer(ShareClient container, string directoryPath)
{
ShareDirectoryClient directory = string.IsNullOrEmpty(directoryPath) ?
container.GetRootDirectoryClient() :
container.GetDirectoryClient(directoryPath);
return new ShareDirectoryStorageResourceContainer(directory, new ShareFileStorageResourceOptions());
}

protected override async Task SetupSourceDirectoryAsync(
ShareClient container,
string directoryPath,
List<(string PathName, int Size)> fileSizes,
CancellationToken cancellationToken)
{
ShareDirectoryClient parentDirectory = string.IsNullOrEmpty(directoryPath) ?
container.GetRootDirectoryClient() :
container.GetDirectoryClient(directoryPath);
if (!string.IsNullOrEmpty(directoryPath))
{
await parentDirectory.CreateIfNotExistsAsync(cancellationToken: cancellationToken);
}
HashSet<string> subDirectoryNames = new() { directoryPath };
foreach ((string filePath, long size) in fileSizes)
{
CancellationHelper.ThrowIfCancellationRequested(cancellationToken);

// Check if the parent subdirectory is already created,
// if not create it before making the files
int fileNameIndex = filePath.LastIndexOf('/');
string subDirectoryName = fileNameIndex > 0 ? filePath.Substring(0, fileNameIndex) : "";
string fileName = fileNameIndex > 0 ? filePath.Substring(fileNameIndex + 1) : filePath;

// Create parent subdirectory if it does not currently exist.
ShareDirectoryClient subdirectory = string.IsNullOrEmpty(subDirectoryName) ?
container.GetRootDirectoryClient() :
container.GetDirectoryClient(subDirectoryName);

if (!string.IsNullOrEmpty(subDirectoryName) &&
!subDirectoryNames.Contains(subDirectoryName))
{
await subdirectory.CreateIfNotExistsAsync(cancellationToken: cancellationToken);
subDirectoryNames.Add(subDirectoryName);
}

using (Stream data = await CreateLimitedMemoryStream(size))
{
ShareFileClient fileClient = subdirectory.GetFileClient(fileName);
await fileClient.CreateAsync(size, cancellationToken: cancellationToken);
if (size > 0)
{
await fileClient.UploadAsync(data, cancellationToken: cancellationToken);
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Azure.Storage.DataMovement.Tests;
using Azure.Storage.Files.Shares;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Azure.Storage.Files.Shares;
using Azure.Storage.Files.Shares.Models;

namespace Azure.Storage.DataMovement.Tests
{
public partial class TransferValidator
{
public class ShareResourceEnumerationItem : IResourceEnumerationItem
{
private readonly ShareFileClient _client;

public string RelativePath { get; }

public ShareResourceEnumerationItem(ShareFileClient client, string relativePath)
{
_client = client;
RelativePath = relativePath;
}

public async Task<Stream> OpenReadAsync(CancellationToken cancellationToken)
{
return await _client.OpenReadAsync(cancellationToken: cancellationToken);
}
}

public static ListFilesAsync GetShareFilesLister(ShareClient container, string prefix)
{
async Task<List<IResourceEnumerationItem>> ListFiles(CancellationToken cancellationToken)
{
List<IResourceEnumerationItem> result = new();
ShareDirectoryClient directory = string.IsNullOrEmpty(prefix) ?
container.GetRootDirectoryClient() :
container.GetDirectoryClient(prefix);

Queue<ShareDirectoryClient> toScan = new();
toScan.Enqueue(directory);

while (toScan.Count > 0)
{
ShareDirectoryClient current = toScan.Dequeue();
await foreach (ShareFileItem item in current.GetFilesAndDirectoriesAsync(
cancellationToken: cancellationToken).ConfigureAwait(false))
{
if (item.IsDirectory)
{
ShareDirectoryClient subdir = current.GetSubdirectoryClient(item.Name);
toScan.Enqueue(subdir);
}
else
{
string relativePath = "";
if (string.IsNullOrEmpty(current.Path))
{
relativePath = item.Name;
}
else if (string.IsNullOrEmpty(prefix))
{
relativePath = string.Join("/", current.Path, item.Name);
}
else
{
relativePath =
prefix != current.Name ?
string.Join("/", current.Path.Substring(prefix.Length + 1), item.Name) :
item.Name;
}
result.Add(new ShareResourceEnumerationItem(current.GetFileClient(item.Name), relativePath));
}
}
}
return result;
}
return ListFiles;
}

public static ListFilesAsync GetFileListerSingle(ShareFileClient file, string relativePath)
{
Task<List<IResourceEnumerationItem>> ListFiles(CancellationToken cancellationToken)
{
return Task.FromResult(new List<IResourceEnumerationItem>
{
new ShareResourceEnumerationItem(file, relativePath)
});
}
return ListFiles;
}
}
}
Loading
Loading