Skip to content
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
16 changes: 14 additions & 2 deletions src/DependencyManagement/ManagedDependenciesPathDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ internal static class ManagedDependenciesPathDetector
{
// Environment variables to help figure out if we are running in Azure.
private const string AzureWebsiteInstanceId = "WEBSITE_INSTANCE_ID";
private const string ContainerName = "CONTAINER_NAME";

private const string HomeDriveName = "HOME";
private const string DataFolderName = "data";

Expand All @@ -28,8 +30,8 @@ internal static class ManagedDependenciesPathDetector
/// </summary>
public static string GetManagedDependenciesPath(string functionAppRootPath)
{
// If we are running in Azure use the 'HOME\Data' path.
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable(AzureWebsiteInstanceId)))
// If we are running in Azure App Service or Linux Consumption use the 'HOME\data' path.
if (IsAppService() || IsLinuxConsumption())
{
var homeDriveVariable = Environment.GetEnvironmentVariable(HomeDriveName);
if (string.IsNullOrEmpty(homeDriveVariable))
Expand All @@ -46,5 +48,15 @@ public static string GetManagedDependenciesPath(string functionAppRootPath)
string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.DoNotVerify);
return Path.Combine(appDataFolder, AzureFunctionsFolderName, functionAppName, ManagedDependenciesFolderName);
}

private static bool IsAppService()
{
return !string.IsNullOrEmpty(Environment.GetEnvironmentVariable(AzureWebsiteInstanceId));
}

private static bool IsLinuxConsumption()
{
return !IsAppService() && !string.IsNullOrEmpty(Environment.GetEnvironmentVariable(ContainerName));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

namespace Microsoft.Azure.Functions.PowerShellWorker.Test.DependencyManagement
{
using System;
using System.IO;
using Xunit;
using Microsoft.Azure.Functions.PowerShellWorker.DependencyManagement;

public class ManagedDependenciesPathDetectorTests
{
[Theory]
[InlineData("CONTAINER_NAME", "MyContainerName", true)]
[InlineData("WEBSITE_INSTANCE_ID", "MyInstanceId", true)]
[InlineData(null, null, false)]
public void ValidateManagedDependenciesPath(string name, string value, bool setEnvironmentVariable)
{
const string HomeDriveName = "HOME";
const string FunctionName = "MyFunction";

string expectedPath = null;

if (setEnvironmentVariable)
{
Environment.SetEnvironmentVariable(name, value);
Environment.SetEnvironmentVariable(HomeDriveName, "home");

const string DataFolderName = "data";
const string ManagedDependenciesFolderName = "ManagedDependencies";
expectedPath = Path.Combine(HomeDriveName, DataFolderName, ManagedDependenciesFolderName);
}
else
{
const string ManagedDependenciesFolderName = "ManagedDependencies";
const string AzureFunctionsFolderName = "AzureFunctions";
string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.DoNotVerify);
expectedPath = Path.Combine(appDataFolder, AzureFunctionsFolderName, FunctionName, ManagedDependenciesFolderName);
}

var functionAppRoot = Path.Join(Path.GetTempPath(), FunctionName);

try
{
var managedDependenciesPath = ManagedDependenciesPathDetector.GetManagedDependenciesPath(functionAppRoot);
var dependenciesPathIsValid = managedDependenciesPath.StartsWith(expectedPath, StringComparison.CurrentCultureIgnoreCase);
Assert.True(dependenciesPathIsValid);
}
finally
{
if (setEnvironmentVariable)
{
Environment.SetEnvironmentVariable(name, null);
Environment.SetEnvironmentVariable(HomeDriveName, null);
}

if (Directory.Exists(functionAppRoot))
{
Directory.Delete(functionAppRoot);
}
}
}
}
}