|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Azure.Storage.Files.Shares.Models; |
| 11 | +using Azure.Storage.Test; |
| 12 | +using Azure.Storage.Tests; |
| 13 | +using Moq; |
| 14 | +using NUnit.Framework; |
| 15 | + |
| 16 | +namespace Azure.Storage.DataMovement.Files.Shares.Tests |
| 17 | +{ |
| 18 | + public class RehydrateShareResourceTests |
| 19 | + { |
| 20 | + public const string ShareProviderId = "share"; |
| 21 | + |
| 22 | + private static byte[] GetBytes(StorageResourceCheckpointDataInternal checkpointData) |
| 23 | + { |
| 24 | + using MemoryStream stream = new(); |
| 25 | + checkpointData.SerializeInternal(stream); |
| 26 | + return stream.ToArray(); |
| 27 | + } |
| 28 | + |
| 29 | + private static Mock<DataTransferProperties> GetProperties( |
| 30 | + string transferId, |
| 31 | + string sourcePath, |
| 32 | + string destinationPath, |
| 33 | + string sourceProviderId, |
| 34 | + string destinationProviderId, |
| 35 | + bool isContainer, |
| 36 | + ShareFileSourceCheckpointData sourceCheckpointData, |
| 37 | + ShareFileDestinationCheckpointData destinationCheckpointData) |
| 38 | + { |
| 39 | + var mock = new Mock<DataTransferProperties>(MockBehavior.Strict); |
| 40 | + mock.Setup(p => p.TransferId).Returns(transferId); |
| 41 | + mock.Setup(p => p.SourceUri).Returns(new Uri(sourcePath)); |
| 42 | + mock.Setup(p => p.DestinationUri).Returns(new Uri(destinationPath)); |
| 43 | + mock.Setup(p => p.SourceProviderId).Returns(sourceProviderId); |
| 44 | + mock.Setup(p => p.DestinationProviderId).Returns(destinationProviderId); |
| 45 | + mock.Setup(p => p.SourceCheckpointData).Returns(GetBytes(sourceCheckpointData)); |
| 46 | + mock.Setup(p => p.DestinationCheckpointData).Returns(GetBytes(destinationCheckpointData)); |
| 47 | + mock.Setup(p => p.IsContainer).Returns(isContainer); |
| 48 | + return mock; |
| 49 | + } |
| 50 | + |
| 51 | + [Test] |
| 52 | + public async Task RehydrateFile( |
| 53 | + [Values(true, false)] bool isSource) |
| 54 | + { |
| 55 | + string transferId = Guid.NewGuid().ToString(); |
| 56 | + string sourcePath = "https://storageaccount.file.core.windows.net/share/dir1/file1"; |
| 57 | + string destinationPath = "https://storageaccount.file.core.windows.net/share/dir2/file2"; |
| 58 | + string originalPath = isSource ? sourcePath : destinationPath; |
| 59 | + |
| 60 | + DataTransferProperties transferProperties = GetProperties( |
| 61 | + transferId, |
| 62 | + sourcePath, |
| 63 | + destinationPath, |
| 64 | + ShareProviderId, |
| 65 | + ShareProviderId, |
| 66 | + isContainer: false, |
| 67 | + new ShareFileSourceCheckpointData(), |
| 68 | + new ShareFileDestinationCheckpointData(null, null, null, null)).Object; |
| 69 | + |
| 70 | + StorageResource storageResource = isSource |
| 71 | + ? await new ShareFilesStorageResourceProvider().FromSourceInternalHookAsync(transferProperties) |
| 72 | + : await new ShareFilesStorageResourceProvider().FromDestinationInternalHookAsync(transferProperties); |
| 73 | + |
| 74 | + Assert.That(originalPath, Is.EqualTo(storageResource.Uri.AbsoluteUri)); |
| 75 | + Assert.That(storageResource, Is.TypeOf<ShareFileStorageResource>()); |
| 76 | + } |
| 77 | + |
| 78 | + [Test] |
| 79 | + public async Task RehydrateFile_DestinationOptions() |
| 80 | + { |
| 81 | + string transferId = Guid.NewGuid().ToString(); |
| 82 | + string sourcePath = "https://storageaccount.file.core.windows.net/share/dir1/file1"; |
| 83 | + string destinationPath = "https://storageaccount.file.core.windows.net/share/dir2/file2"; |
| 84 | + |
| 85 | + Random r = new(); |
| 86 | + ShareFileDestinationCheckpointData originalDestinationData = new( |
| 87 | + new ShareFileHttpHeaders |
| 88 | + { |
| 89 | + ContentType = "text/plain", |
| 90 | + ContentEncoding = new string[] { "gzip" }, |
| 91 | + ContentLanguage = new string[] { "en-US" }, |
| 92 | + ContentDisposition = "inline", |
| 93 | + CacheControl = "no-cache", |
| 94 | + }, |
| 95 | + new Dictionary<string, string> |
| 96 | + { |
| 97 | + { r.NextString(8), r.NextString(8) } |
| 98 | + }, |
| 99 | + new Dictionary<string, string> |
| 100 | + { |
| 101 | + { r.NextString(8), r.NextString(8) } |
| 102 | + }, |
| 103 | + new FileSmbProperties |
| 104 | + { |
| 105 | + FileAttributes = NtfsFileAttributes.Archive, |
| 106 | + FilePermissionKey = r.NextString(8), |
| 107 | + FileLastWrittenOn = DateTimeOffset.Now, |
| 108 | + FileChangedOn = DateTimeOffset.Now, |
| 109 | + FileCreatedOn = DateTimeOffset.Now, |
| 110 | + }); |
| 111 | + DataTransferProperties transferProperties = GetProperties( |
| 112 | + transferId, |
| 113 | + sourcePath, |
| 114 | + destinationPath, |
| 115 | + ShareProviderId, |
| 116 | + ShareProviderId, |
| 117 | + isContainer: false, |
| 118 | + new ShareFileSourceCheckpointData(), |
| 119 | + originalDestinationData).Object; |
| 120 | + |
| 121 | + ShareFileStorageResource storageResource = (ShareFileStorageResource) |
| 122 | + await new ShareFilesStorageResourceProvider().FromDestinationInternalHookAsync(transferProperties); |
| 123 | + |
| 124 | + Assert.That(destinationPath, Is.EqualTo(storageResource.Uri.AbsoluteUri)); |
| 125 | + Assert.That(storageResource, Is.TypeOf<ShareFileStorageResource>()); |
| 126 | + Assert.That(storageResource._options.HttpHeaders.ContentType, Is.EqualTo(originalDestinationData.ContentHeaders.ContentType)); |
| 127 | + Assert.That(storageResource._options.HttpHeaders.ContentEncoding, Is.EqualTo(originalDestinationData.ContentHeaders.ContentEncoding)); |
| 128 | + Assert.That(storageResource._options.HttpHeaders.ContentLanguage, Is.EqualTo(originalDestinationData.ContentHeaders.ContentLanguage)); |
| 129 | + Assert.That(storageResource._options.HttpHeaders.ContentDisposition, Is.EqualTo(originalDestinationData.ContentHeaders.ContentDisposition)); |
| 130 | + Assert.That(storageResource._options.HttpHeaders.CacheControl, Is.EqualTo(originalDestinationData.ContentHeaders.CacheControl)); |
| 131 | + Assert.That(storageResource._options.FileMetadata, Is.EqualTo(originalDestinationData.FileMetadata)); |
| 132 | + Assert.That(storageResource._options.DirectoryMetadata, Is.EqualTo(originalDestinationData.DirectoryMetadata)); |
| 133 | + Assert.That(storageResource._options.SmbProperties.FileAttributes, Is.EqualTo(originalDestinationData.SmbProperties.FileAttributes)); |
| 134 | + Assert.That(storageResource._options.SmbProperties.FilePermissionKey, Is.EqualTo(originalDestinationData.SmbProperties.FilePermissionKey)); |
| 135 | + Assert.That(storageResource._options.SmbProperties.FileCreatedOn, Is.EqualTo(originalDestinationData.SmbProperties.FileCreatedOn)); |
| 136 | + Assert.That(storageResource._options.SmbProperties.FileLastWrittenOn, Is.EqualTo(originalDestinationData.SmbProperties.FileLastWrittenOn)); |
| 137 | + Assert.That(storageResource._options.SmbProperties.FileChangedOn, Is.EqualTo(originalDestinationData.SmbProperties.FileChangedOn)); |
| 138 | + } |
| 139 | + |
| 140 | + [Test] |
| 141 | + public async Task RehydrateDirectory( |
| 142 | + [Values(true, false)] bool isSource) |
| 143 | + { |
| 144 | + string transferId = Guid.NewGuid().ToString(); |
| 145 | + List<string> sourcePaths = new List<string>(); |
| 146 | + string sourcePath = "https://storageaccount.file.core.windows.net/share/dir1"; |
| 147 | + List<string> destinationPaths = new List<string>(); |
| 148 | + string destinationPath = "https://storageaccount.file.core.windows.net/share/dir2"; |
| 149 | + string originalPath = isSource ? sourcePath : destinationPath; |
| 150 | + int jobPartCount = 10; |
| 151 | + for (int i = 0; i < jobPartCount; i++) |
| 152 | + { |
| 153 | + string childPath = DataProvider.GetNewString(5); |
| 154 | + sourcePaths.Add(string.Join("/", sourcePath, childPath)); |
| 155 | + destinationPaths.Add(string.Join("/", destinationPath, childPath)); |
| 156 | + } |
| 157 | + |
| 158 | + DataTransferProperties transferProperties = GetProperties( |
| 159 | + transferId, |
| 160 | + sourcePath, |
| 161 | + destinationPath, |
| 162 | + ShareProviderId, |
| 163 | + ShareProviderId, |
| 164 | + isContainer: true, |
| 165 | + new ShareFileSourceCheckpointData(), |
| 166 | + new ShareFileDestinationCheckpointData(null, null, null, null)).Object; |
| 167 | + |
| 168 | + StorageResource storageResource = isSource |
| 169 | + ? await new ShareFilesStorageResourceProvider().FromSourceInternalHookAsync(transferProperties) |
| 170 | + : await new ShareFilesStorageResourceProvider().FromDestinationInternalHookAsync(transferProperties); |
| 171 | + |
| 172 | + Assert.That(originalPath, Is.EqualTo(storageResource.Uri.AbsoluteUri)); |
| 173 | + Assert.That(storageResource, Is.TypeOf<ShareDirectoryStorageResourceContainer>()); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments