public void IsAutoSwapOngoingOrEnabledTests() { var deploymentSettingsMock = new Mock<IDeploymentSettingsManager>(); var enviromentMock = new Mock<IEnvironment>(); var traceFactoryMock = new Mock<ITraceFactory>(); traceFactoryMock.Setup(s => s.GetTracer()).Returns(Mock.Of<ITracer>()); enviromentMock.Setup(e => e.LocksPath).Returns(@"x:\foo"); var handler = new AutoSwapHandler( Mock.Of<IDeploymentStatusManager>(), enviromentMock.Object, deploymentSettingsMock.Object, traceFactoryMock.Object); Assert.False(handler.IsAutoSwapEnabled(), "Autoswap should NOT be enabled"); Assert.False(handler.IsAutoSwapOngoing(), "Should not be any autoswap, since it is not enabled"); deploymentSettingsMock.Setup( s => s.GetValue(It.Is<string>(v => "WEBSITE_SWAP_SLOTNAME".StartsWith(v)), It.IsAny<bool>()) ).Returns("someslot"); handler = new AutoSwapHandler( Mock.Of<IDeploymentStatusManager>(), enviromentMock.Object, deploymentSettingsMock.Object, traceFactoryMock.Object); Assert.True(handler.IsAutoSwapEnabled(), "Autoswap should be enabled"); var fileSystemMock = new Mock<IFileSystem>(); var fileInfoMock = new Mock<IFileInfoFactory>(); var fileInfoBaseMock = new Mock<FileInfoBase>(); fileSystemMock.Setup(f => f.FileInfo).Returns(fileInfoMock.Object); fileInfoMock.Setup(f => f.FromFileName(It.IsAny<string>())).Returns(fileInfoBaseMock.Object); fileInfoBaseMock.Setup(f => f.Exists).Returns(false); fileInfoBaseMock.Setup(f => f.LastWriteTimeUtc).Returns(DateTime.UtcNow); FileSystemHelpers.Instance = fileSystemMock.Object; Assert.False(handler.IsAutoSwapOngoing(), "Should not be any autoswap, since autoswap lock is not acquired by process."); fileInfoBaseMock.Setup(f => f.Exists).Returns(true); fileInfoBaseMock.Setup(f => f.LastWriteTimeUtc).Returns(DateTime.UtcNow.AddMinutes(-3)); Assert.False(handler.IsAutoSwapOngoing(), "Should not be any autoswap, since autoswap lock is acquired over 2 mintues ago."); fileInfoBaseMock.Setup(f => f.Exists).Returns(true); fileInfoBaseMock.Setup(f => f.LastWriteTimeUtc).Returns(DateTime.UtcNow); Assert.True(handler.IsAutoSwapOngoing(), "Autoswap is ongoing, since autoswap lock is acquired within 2 mintues"); }
private static int PerformDeploy( string appRoot, string wapTargets, string deployer, string lockPath, IEnvironment env, IDeploymentSettingsManager settingsManager, TraceLevel level, ITracer tracer, ITraceFactory traceFactory, IOperationLock deploymentLock) { System.Environment.SetEnvironmentVariable("GIT_DIR", null, System.EnvironmentVariableTarget.Process); // Skip SSL Certificate Validate OperationClient.SkipSslValidationIfNeeded(); // Adjust repo path env.RepositoryPath = Path.Combine(env.SiteRootPath, settingsManager.GetRepositoryPath()); string statusLockPath = Path.Combine(lockPath, Constants.StatusLockFile); string hooksLockPath = Path.Combine(lockPath, Constants.HooksLockFile); IOperationLock statusLock = new LockFile(statusLockPath, traceFactory); IOperationLock hooksLock = new LockFile(hooksLockPath, traceFactory); IBuildPropertyProvider buildPropertyProvider = new BuildPropertyProvider(); ISiteBuilderFactory builderFactory = new SiteBuilderFactory(buildPropertyProvider, env); var logger = new ConsoleLogger(); IRepository gitRepository; if (settingsManager.UseLibGit2SharpRepository()) { gitRepository = new LibGit2SharpRepository(env, settingsManager, traceFactory); } else { gitRepository = new GitExeRepository(env, settingsManager, traceFactory); } IServerConfiguration serverConfiguration = new ServerConfiguration(); IAnalytics analytics = new Analytics(settingsManager, serverConfiguration, traceFactory); IWebHooksManager hooksManager = new WebHooksManager(tracer, env, hooksLock); IDeploymentStatusManager deploymentStatusManager = new DeploymentStatusManager(env, analytics, statusLock); IAutoSwapHandler autoSwapHander = new AutoSwapHandler(env, settingsManager, traceFactory); var functionManager = new FunctionManager(env, traceFactory); IDeploymentManager deploymentManager = new DeploymentManager(builderFactory, env, traceFactory, analytics, settingsManager, deploymentStatusManager, deploymentLock, GetLogger(env, level, logger), hooksManager, functionManager); var step = tracer.Step(XmlTracer.ExecutingExternalProcessTrace, new Dictionary<string, string> { { "type", "process" }, { "path", "kudu.exe" }, { "arguments", appRoot + " " + wapTargets } }); using (step) { try { deploymentManager.DeployAsync(gitRepository, changeSet: null, deployer: deployer, clean: false) .Wait(); if (autoSwapHander.IsAutoSwapEnabled()) { string branch = settingsManager.GetBranch(); ChangeSet changeSet = gitRepository.GetChangeSet(branch); IDeploymentStatusFile statusFile = deploymentStatusManager.Open(changeSet.Id); if (statusFile != null && statusFile.Status == DeployStatus.Success) { autoSwapHander.HandleAutoSwap(changeSet.Id, deploymentManager.GetLogger(changeSet.Id), tracer).Wait(); } } } catch (Exception e) { tracer.TraceError(e); System.Console.Error.WriteLine(e.GetBaseException().Message); System.Console.Error.WriteLine(Resources.Log_DeploymentError); return 1; } } if (logger.HasErrors) { System.Console.Error.WriteLine(Resources.Log_DeploymentError); return 1; } return 0; }