From 1822f2c66c4d3f2b02c3d7cf671070061d4a03ac Mon Sep 17 00:00:00 2001 From: Francisco-Gamino Date: Wed, 16 Mar 2022 17:31:32 -0700 Subject: [PATCH 1/3] Add AutomaticUpgradesAreDisabled message --- src/resources/PowerShellWorkerStrings.resx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/resources/PowerShellWorkerStrings.resx b/src/resources/PowerShellWorkerStrings.resx index 0315a4d4..e98da03f 100644 --- a/src/resources/PowerShellWorkerStrings.resx +++ b/src/resources/PowerShellWorkerStrings.resx @@ -322,4 +322,7 @@ The Function app uses PowerShell Core 6. Your Function app is still supported, but this version of PowerShell reached end of life, so it is strongly recommended that you migrate the Function app to PowerShell 7. For more details, see https://aka.ms/functions-powershell-6-to-7. + + Automatic upgrades are disabled in PowerShell 6 function apps. To enable this functionality back, please migrate your function app to PowerShell 7. For more details, see https://aka.ms/functions-powershell-6-to-7. + \ No newline at end of file From e2f77241811cf15536f2722f34ecc5f04cbda5eb Mon Sep 17 00:00:00 2001 From: Francisco-Gamino Date: Wed, 16 Mar 2022 23:43:47 -0700 Subject: [PATCH 2/3] Disable optional upgrades --- .../BackgroundDependencySnapshotMaintainer.cs | 13 +++++++++++++ src/DependencyManagement/DependencyManager.cs | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/DependencyManagement/BackgroundDependencySnapshotMaintainer.cs b/src/DependencyManagement/BackgroundDependencySnapshotMaintainer.cs index deba2e90..b42faacd 100644 --- a/src/DependencyManagement/BackgroundDependencySnapshotMaintainer.cs +++ b/src/DependencyManagement/BackgroundDependencySnapshotMaintainer.cs @@ -20,6 +20,9 @@ internal class BackgroundDependencySnapshotMaintainer : IBackgroundDependencySna private TimeSpan MaxBackgroundUpgradePeriod { get; } = PowerShellWorkerConfiguration.GetTimeSpan("MDMaxBackgroundUpgradePeriod") ?? TimeSpan.FromDays(7); + private bool EnableAutomaticUpgrades { get; } = + PowerShellWorkerConfiguration.GetBoolean("MDEnableAutomaticUpgrades") ?? false; + private readonly IDependencyManagerStorage _storage; private readonly IDependencySnapshotInstaller _installer; private readonly IDependencySnapshotPurger _purger; @@ -42,6 +45,16 @@ public void Start(string currentSnapshotPath, DependencyManifestEntry[] dependen _purger.SetCurrentlyUsedSnapshot(currentSnapshotPath, logger); + if (!EnableAutomaticUpgrades) + { + logger.Log( + isUserOnlyLog: false, + RpcLog.Types.Level.Warning, + PowerShellWorkerStrings.AutomaticUpgradesAreDisabled); + + return; + } + _installAndPurgeTimer = new Timer( _ => InstallAndPurgeSnapshots(PowerShell.Create, logger), state: null, diff --git a/src/DependencyManagement/DependencyManager.cs b/src/DependencyManagement/DependencyManager.cs index cece111d..7d9b0a43 100644 --- a/src/DependencyManagement/DependencyManager.cs +++ b/src/DependencyManagement/DependencyManager.cs @@ -41,6 +41,9 @@ internal class DependencyManager : IDisposable private Task _dependencyInstallationTask; + private bool EnableAutomaticUpgrades { get; } = + PowerShellWorkerConfiguration.GetBoolean("MDEnableAutomaticUpgrades") ?? false; + #endregion public DependencyManager( @@ -198,6 +201,16 @@ internal Exception InstallFunctionAppDependencies(PowerShell firstPwsh, Func Date: Fri, 18 Mar 2022 12:26:29 -0700 Subject: [PATCH 3/3] Update test --- .../DependencyManagerTests.cs | 61 +++++++++++-------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/test/Unit/DependencyManagement/DependencyManagerTests.cs b/test/Unit/DependencyManagement/DependencyManagerTests.cs index b4eb5ecb..36d5ea45 100644 --- a/test/Unit/DependencyManagement/DependencyManagerTests.cs +++ b/test/Unit/DependencyManagement/DependencyManagerTests.cs @@ -163,40 +163,49 @@ public void StartDependencyInstallationIfNeeded_InstallsSnapshotInForeground_Whe [Fact] public void StartDependencyInstallationIfNeeded_InvokesBackgroundMaintainer_WhenAcceptableDependenciesAlreadyInstalled() { - _mockInstalledDependenciesLocator.Setup(_ => _.GetPathWithAcceptableDependencyVersionsInstalled()) - .Returns("AlreadyInstalled"); - _mockStorage.Setup(_ => _.GetDependencies()).Returns(GetAnyNonEmptyDependencyManifestEntries()); + try + { + Environment.SetEnvironmentVariable("MDEnableAutomaticUpgrades", "true"); - var firstPowerShellRunspace = PowerShell.Create(); - Func powerShellFactory = PowerShell.Create; + _mockInstalledDependenciesLocator.Setup(_ => _.GetPathWithAcceptableDependencyVersionsInstalled()) + .Returns("AlreadyInstalled"); + _mockStorage.Setup(_ => _.GetDependencies()).Returns(GetAnyNonEmptyDependencyManifestEntries()); - _mockStorage.Setup(_ => _.SnapshotExists("AlreadyInstalled")).Returns(true); + var firstPowerShellRunspace = PowerShell.Create(); + Func powerShellFactory = PowerShell.Create; - _mockBackgroundDependencySnapshotMaintainer.Setup( - _ => _.InstallAndPurgeSnapshots(It.IsAny>(), It.IsAny())) - .Returns("NewSnapshot"); + _mockStorage.Setup(_ => _.SnapshotExists("AlreadyInstalled")).Returns(true); - using (var dependencyManager = CreateDependencyManagerWithMocks()) - { - dependencyManager.Initialize(_mockLogger.Object); - dependencyManager.StartDependencyInstallationIfNeeded(firstPowerShellRunspace, powerShellFactory, _mockLogger.Object); - var hadToWait = dependencyManager.WaitForDependenciesAvailability(() => _mockLogger.Object); + _mockBackgroundDependencySnapshotMaintainer.Setup( + _ => _.InstallAndPurgeSnapshots(It.IsAny>(), It.IsAny())) + .Returns("NewSnapshot"); - Assert.False(hadToWait); - Assert.Equal("NewSnapshot", dependencyManager.WaitForBackgroundDependencyInstallationTaskCompletion()); + using (var dependencyManager = CreateDependencyManagerWithMocks()) + { + dependencyManager.Initialize(_mockLogger.Object); + dependencyManager.StartDependencyInstallationIfNeeded(firstPowerShellRunspace, powerShellFactory, _mockLogger.Object); + var hadToWait = dependencyManager.WaitForDependenciesAvailability(() => _mockLogger.Object); - _mockBackgroundDependencySnapshotMaintainer.Verify( - _ => _.InstallAndPurgeSnapshots(powerShellFactory, _mockLogger.Object), + Assert.False(hadToWait); + Assert.Equal("NewSnapshot", dependencyManager.WaitForBackgroundDependencyInstallationTaskCompletion()); + + _mockBackgroundDependencySnapshotMaintainer.Verify( + _ => _.InstallAndPurgeSnapshots(powerShellFactory, _mockLogger.Object), + Times.Once); + } + + _mockLogger.Verify( + _ => _.Log( + false, + LogLevel.Trace, + It.Is(message => message.Contains(PowerShellWorkerStrings.AcceptableFunctionAppDependenciesAlreadyInstalled)), + It.IsAny()), Times.Once); } - - _mockLogger.Verify( - _ => _.Log( - false, - LogLevel.Trace, - It.Is(message => message.Contains(PowerShellWorkerStrings.AcceptableFunctionAppDependenciesAlreadyInstalled)), - It.IsAny()), - Times.Once); + finally + { + Environment.SetEnvironmentVariable("MDEnableAutomaticUpgrades", null); + } } [Fact]