|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft. All rights reserved. |
| 3 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 4 | +// |
| 5 | + |
| 6 | +namespace Microsoft.Azure.Functions.PowerShellWorker.Test.DependencyManagement |
| 7 | +{ |
| 8 | + using System; |
| 9 | + using System.IO; |
| 10 | + using Xunit; |
| 11 | + using Microsoft.Azure.Functions.PowerShellWorker.DependencyManagement; |
| 12 | + |
| 13 | + public class ManagedDependenciesPathDetectorTests |
| 14 | + { |
| 15 | + [Theory] |
| 16 | + [InlineData("CONTAINER_NAME", "MyContainerName", true)] |
| 17 | + [InlineData("WEBSITE_INSTANCE_ID", "MyInstanceId", true)] |
| 18 | + [InlineData(null, null, false)] |
| 19 | + public void ValidateManagedDependenciesPath(string name, string value, bool setEnvironmentVariable) |
| 20 | + { |
| 21 | + const string HomeDriveName = "HOME"; |
| 22 | + const string FunctionName = "MyFunction"; |
| 23 | + |
| 24 | + string expectedPath = null; |
| 25 | + |
| 26 | + if (setEnvironmentVariable) |
| 27 | + { |
| 28 | + Environment.SetEnvironmentVariable(name, value); |
| 29 | + Environment.SetEnvironmentVariable(HomeDriveName, "home"); |
| 30 | + |
| 31 | + const string DataFolderName = "data"; |
| 32 | + const string ManagedDependenciesFolderName = "ManagedDependencies"; |
| 33 | + expectedPath = Path.Combine(HomeDriveName, DataFolderName, ManagedDependenciesFolderName); |
| 34 | + } |
| 35 | + else |
| 36 | + { |
| 37 | + const string ManagedDependenciesFolderName = "ManagedDependencies"; |
| 38 | + const string AzureFunctionsFolderName = "AzureFunctions"; |
| 39 | + string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.DoNotVerify); |
| 40 | + expectedPath = Path.Combine(appDataFolder, AzureFunctionsFolderName, FunctionName, ManagedDependenciesFolderName); |
| 41 | + } |
| 42 | + |
| 43 | + var functionAppRoot = Path.Join(Path.GetTempPath(), FunctionName); |
| 44 | + |
| 45 | + try |
| 46 | + { |
| 47 | + var managedDependenciesPath = ManagedDependenciesPathDetector.GetManagedDependenciesPath(functionAppRoot); |
| 48 | + var dependenciesPathIsValid = managedDependenciesPath.StartsWith(expectedPath, StringComparison.CurrentCultureIgnoreCase); |
| 49 | + Assert.True(dependenciesPathIsValid); |
| 50 | + } |
| 51 | + finally |
| 52 | + { |
| 53 | + if (setEnvironmentVariable) |
| 54 | + { |
| 55 | + Environment.SetEnvironmentVariable(name, null); |
| 56 | + Environment.SetEnvironmentVariable(HomeDriveName, null); |
| 57 | + } |
| 58 | + |
| 59 | + if (Directory.Exists(functionAppRoot)) |
| 60 | + { |
| 61 | + Directory.Delete(functionAppRoot); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments