Skip to content

Commit 8c78013

Browse files
Set ManagedDependenciesPath for both AppService and Linux Consumption environments (#633)
* Set the Managed Dependencies path when running in Azure App Service or Linux Consumption. * Add unit test to validate setting the ManagedDependenciesPath.
1 parent 91df0e5 commit 8c78013

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

src/DependencyManagement/ManagedDependenciesPathDetector.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ internal static class ManagedDependenciesPathDetector
1212
{
1313
// Environment variables to help figure out if we are running in Azure.
1414
private const string AzureWebsiteInstanceId = "WEBSITE_INSTANCE_ID";
15+
private const string ContainerName = "CONTAINER_NAME";
16+
1517
private const string HomeDriveName = "HOME";
1618
private const string DataFolderName = "data";
1719

@@ -28,8 +30,8 @@ internal static class ManagedDependenciesPathDetector
2830
/// </summary>
2931
public static string GetManagedDependenciesPath(string functionAppRootPath)
3032
{
31-
// If we are running in Azure use the 'HOME\Data' path.
32-
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable(AzureWebsiteInstanceId)))
33+
// If we are running in Azure App Service or Linux Consumption use the 'HOME\data' path.
34+
if (IsAppService() || IsLinuxConsumption())
3335
{
3436
var homeDriveVariable = Environment.GetEnvironmentVariable(HomeDriveName);
3537
if (string.IsNullOrEmpty(homeDriveVariable))
@@ -46,5 +48,15 @@ public static string GetManagedDependenciesPath(string functionAppRootPath)
4648
string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.DoNotVerify);
4749
return Path.Combine(appDataFolder, AzureFunctionsFolderName, functionAppName, ManagedDependenciesFolderName);
4850
}
51+
52+
private static bool IsAppService()
53+
{
54+
return !string.IsNullOrEmpty(Environment.GetEnvironmentVariable(AzureWebsiteInstanceId));
55+
}
56+
57+
private static bool IsLinuxConsumption()
58+
{
59+
return !IsAppService() && !string.IsNullOrEmpty(Environment.GetEnvironmentVariable(ContainerName));
60+
}
4961
}
5062
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

Comments
 (0)