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 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 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]