|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using Moq; |
| 5 | +using Tes.Runner.Models; |
| 6 | +using Tes.Runner.Storage; |
| 7 | +using Tes.Runner.Transfer; |
| 8 | + |
| 9 | +namespace Tes.Runner.Test.Storage |
| 10 | +{ |
| 11 | + [TestClass] |
| 12 | + [TestCategory("Unit")] |
| 13 | + public class FileOperationResolverTests |
| 14 | + { |
| 15 | + private ResolutionPolicyHandler resolutionPolicyHandler = null!; |
| 16 | + private Mock<IFileInfoProvider> fileInfoProvider = null!; |
| 17 | + private FileOutput singleFileOutput = null!; |
| 18 | + private FileOutput directoryFileOutput = null!; |
| 19 | + private FileOutput patternFileOutput = null!; |
| 20 | + |
| 21 | + private FileInput singleFileInput = null!; |
| 22 | + |
| 23 | + [TestInitialize] |
| 24 | + public void SetUp() |
| 25 | + { |
| 26 | + resolutionPolicyHandler = new ResolutionPolicyHandler(); |
| 27 | + fileInfoProvider = new Mock<IFileInfoProvider>(); |
| 28 | + |
| 29 | + fileInfoProvider.Setup(x => x.GetExpandedFileName(It.IsAny<string>())).Returns<string>(x => x); |
| 30 | + fileInfoProvider.Setup(x => x.FileExists(It.IsAny<string>())).Returns(true); |
| 31 | + |
| 32 | + singleFileInput = new FileInput |
| 33 | + { |
| 34 | + Path = "/foo/bar", |
| 35 | + SourceUrl = "https://foo.bar/cont/foo/bar?sig=sasToken", |
| 36 | + SasStrategy = SasResolutionStrategy.None, |
| 37 | + }; |
| 38 | + |
| 39 | + singleFileOutput = new FileOutput |
| 40 | + { |
| 41 | + Path = "/foo/bar", |
| 42 | + TargetUrl = "https://foo.bar/cont/foo/bar?sig=sasToken", |
| 43 | + SasStrategy = SasResolutionStrategy.None, |
| 44 | + FileType = FileType.File |
| 45 | + }; |
| 46 | + |
| 47 | + directoryFileOutput = new FileOutput |
| 48 | + { |
| 49 | + Path = "/root", |
| 50 | + TargetUrl = "https://foo.bar/cont?sig=sasToken", |
| 51 | + SasStrategy = SasResolutionStrategy.None, |
| 52 | + FileType = FileType.Directory |
| 53 | + }; |
| 54 | + |
| 55 | + patternFileOutput = new FileOutput |
| 56 | + { |
| 57 | + Path = "/data/*.foo", |
| 58 | + TargetUrl = "https://foo.bar/cont?sig=sasToken", |
| 59 | + SasStrategy = SasResolutionStrategy.None, |
| 60 | + PathPrefix = "/prefix", |
| 61 | + FileType = FileType.File |
| 62 | + }; |
| 63 | + } |
| 64 | + |
| 65 | + [TestMethod] |
| 66 | + public async Task ResolveOutputsAsync_FileOutputProvided_FileOperationIsResolved() |
| 67 | + { |
| 68 | + var nodeTask = new NodeTask() |
| 69 | + { |
| 70 | + Outputs = new List<FileOutput> |
| 71 | + { |
| 72 | + singleFileOutput |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + var fileOperationInfoResolver = new FileOperationResolver(nodeTask, resolutionPolicyHandler, fileInfoProvider.Object); |
| 77 | + var resolvedOutputs = await fileOperationInfoResolver.ResolveOutputsAsync(); |
| 78 | + |
| 79 | + Assert.AreEqual(1, resolvedOutputs?.Count); |
| 80 | + Assert.IsTrue(resolvedOutputs?.Any(r => r.FullFilePath.Equals(singleFileOutput.Path, StringComparison.InvariantCultureIgnoreCase))); |
| 81 | + Assert.IsTrue(resolvedOutputs?.Any(r => r.TargetUri.ToString().Equals(singleFileOutput.TargetUrl, StringComparison.InvariantCultureIgnoreCase))); |
| 82 | + } |
| 83 | + |
| 84 | + [TestMethod] |
| 85 | + public async Task ResolveOutputsAsync_PatternOutputProvided_FileOperationsAreResolved() |
| 86 | + { |
| 87 | + var nodeTask = new NodeTask() |
| 88 | + { |
| 89 | + Outputs = new List<FileOutput> |
| 90 | + { |
| 91 | + patternFileOutput |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + fileInfoProvider.Setup(x => x.GetFilesBySearchPattern(It.IsAny<string>(), It.IsAny<string>())) |
| 96 | + .Returns(new List<string> { "/prefix/data/foo.foo", "/prefix/data/bar.foo" }.ToArray); |
| 97 | + |
| 98 | + var fileOperationInfoResolver = new FileOperationResolver(nodeTask, resolutionPolicyHandler, fileInfoProvider.Object); |
| 99 | + var resolvedOutputs = await fileOperationInfoResolver.ResolveOutputsAsync(); |
| 100 | + |
| 101 | + Assert.AreEqual(2, resolvedOutputs?.Count); |
| 102 | + Assert.IsTrue(resolvedOutputs!.Any(r => r.FullFilePath.Equals("/prefix/data/foo.foo", StringComparison.OrdinalIgnoreCase))); |
| 103 | + Assert.IsTrue(resolvedOutputs!.Any(r => r.FullFilePath.Equals("/prefix/data/bar.foo", StringComparison.OrdinalIgnoreCase))); |
| 104 | + Assert.IsTrue(resolvedOutputs!.Any(r => r.TargetUri.ToString().Equals(@"https://foo.bar/cont/data/foo.foo?sig=sasToken", StringComparison.OrdinalIgnoreCase))); |
| 105 | + Assert.IsTrue(resolvedOutputs!.Any(r => r.TargetUri.ToString().Equals(@"https://foo.bar/cont/data/bar.foo?sig=sasToken", StringComparison.OrdinalIgnoreCase))); |
| 106 | + } |
| 107 | + |
| 108 | + [TestMethod] |
| 109 | + public async Task ResolveOutputsAsync_DirectoryOutputProvided_TargetUrlDoesNotContainRootDirInPathProperty() |
| 110 | + { |
| 111 | + var nodeTask = new NodeTask() |
| 112 | + { |
| 113 | + Outputs = new List<FileOutput> |
| 114 | + { |
| 115 | + directoryFileOutput |
| 116 | + } |
| 117 | + }; |
| 118 | + var rootDir = directoryFileOutput.Path; |
| 119 | + var file1 = "/dir1/file1.tmp"; |
| 120 | + var file2 = "/dir1/dir2/file2.tmp"; |
| 121 | + |
| 122 | + fileInfoProvider.Setup(x => x.GetAllFilesInDirectory(It.IsAny<string>())) |
| 123 | + .Returns(new List<string> { $"{rootDir}{file1}", $"{rootDir}{file2}" }.ToArray); |
| 124 | + |
| 125 | + var fileOperationInfoResolver = new FileOperationResolver(nodeTask, resolutionPolicyHandler, fileInfoProvider.Object); |
| 126 | + var resolvedOutputs = await fileOperationInfoResolver.ResolveOutputsAsync(); |
| 127 | + |
| 128 | + |
| 129 | + Assert.AreEqual(2, resolvedOutputs?.Count); |
| 130 | + Assert.IsTrue(resolvedOutputs!.Any(r => r.FullFilePath.Equals($"{rootDir}{file1}", StringComparison.OrdinalIgnoreCase))); |
| 131 | + Assert.IsTrue(resolvedOutputs!.Any(r => r.FullFilePath.Equals($"{rootDir}{file2}", StringComparison.OrdinalIgnoreCase))); |
| 132 | + Assert.IsTrue(resolvedOutputs!.Any(r => r.TargetUri.AbsoluteUri.ToString().Equals($@"https://foo.bar/cont{file1}?sig=sasToken", StringComparison.OrdinalIgnoreCase))); |
| 133 | + Assert.IsTrue(resolvedOutputs!.Any(r => r.TargetUri.AbsoluteUri.ToString().Equals($@"https://foo.bar/cont{file2}?sig=sasToken", StringComparison.OrdinalIgnoreCase))); |
| 134 | + } |
| 135 | + |
| 136 | + [TestMethod] |
| 137 | + public async Task ResolveOutputsAsync_PatternAndFileOutputProvided_FileOperationsAreResolved() |
| 138 | + { |
| 139 | + var nodeTask = new NodeTask() |
| 140 | + { |
| 141 | + Outputs = new List<FileOutput> |
| 142 | + { |
| 143 | + patternFileOutput, |
| 144 | + singleFileOutput |
| 145 | + } |
| 146 | + }; |
| 147 | + |
| 148 | + fileInfoProvider.Setup(x => x.GetFilesBySearchPattern(It.IsAny<string>(), It.IsAny<string>())) |
| 149 | + .Returns(new List<string> { "/prefix/data/foo.foo", "/prefix/data/bar.foo" }.ToArray); |
| 150 | + |
| 151 | + var fileOperationInfoResolver = new FileOperationResolver(nodeTask, resolutionPolicyHandler, fileInfoProvider.Object); |
| 152 | + var resolvedOutputs = await fileOperationInfoResolver.ResolveOutputsAsync(); |
| 153 | + |
| 154 | + Assert.AreEqual(3, resolvedOutputs?.Count); |
| 155 | + Assert.IsTrue(resolvedOutputs!.Any(r => r.FullFilePath.Equals("/prefix/data/foo.foo", StringComparison.OrdinalIgnoreCase))); |
| 156 | + Assert.IsTrue(resolvedOutputs!.Any(r => r.FullFilePath.Equals("/prefix/data/bar.foo", StringComparison.OrdinalIgnoreCase))); |
| 157 | + Assert.IsTrue(resolvedOutputs!.Any(r => r.FullFilePath.Equals("/prefix/data/bar.foo", StringComparison.OrdinalIgnoreCase))); |
| 158 | + Assert.IsTrue(resolvedOutputs!.Any(r => r.TargetUri.ToString().Equals(@"https://foo.bar/cont/data/foo.foo?sig=sasToken", StringComparison.OrdinalIgnoreCase))); |
| 159 | + Assert.IsTrue(resolvedOutputs!.Any(r => r.TargetUri.ToString().Equals(@"https://foo.bar/cont/data/bar.foo?sig=sasToken", StringComparison.OrdinalIgnoreCase))); |
| 160 | + Assert.IsTrue(resolvedOutputs?.Any(r => r.FullFilePath.Equals(singleFileOutput.Path, StringComparison.InvariantCultureIgnoreCase))); |
| 161 | + Assert.IsTrue(resolvedOutputs?.Any(r => r.TargetUri.ToString().Equals(singleFileOutput.TargetUrl, StringComparison.InvariantCultureIgnoreCase))); |
| 162 | + } |
| 163 | + |
| 164 | + [TestMethod] |
| 165 | + public async Task ResolveInputsAsync_FileInputProvided_FileOperationsAreResolved() |
| 166 | + { |
| 167 | + var nodeTask = new NodeTask() |
| 168 | + { |
| 169 | + Inputs = new List<FileInput> |
| 170 | + { |
| 171 | + singleFileInput |
| 172 | + } |
| 173 | + }; |
| 174 | + |
| 175 | + var fileOperationInfoResolver = new FileOperationResolver(nodeTask, resolutionPolicyHandler, fileInfoProvider.Object); |
| 176 | + |
| 177 | + var resolvedInputs = await fileOperationInfoResolver.ResolveInputsAsync(); |
| 178 | + |
| 179 | + Assert.AreEqual(1, resolvedInputs?.Count); |
| 180 | + Assert.IsTrue(resolvedInputs?.Any(r => r.FullFilePath.Equals(singleFileInput.Path, StringComparison.InvariantCultureIgnoreCase))); |
| 181 | + Assert.IsTrue(resolvedInputs?.Any(r => r.SourceUrl.ToString().Equals(singleFileInput.SourceUrl, StringComparison.InvariantCultureIgnoreCase))); |
| 182 | + } |
| 183 | + |
| 184 | + } |
| 185 | +} |
0 commit comments