public async Task HandleAutoSwapTests() { string deploymentId = Guid.Empty.ToString(); var deploymentSettingsMock = new Mock <IDeploymentSettingsManager>(); var enviromentMock = new Mock <IEnvironment>(); var deploymentStatusManagerMock = new Mock <IDeploymentStatusManager>(); var tracerMock = new Mock <ITracer>(); var deploymentContextMock = new DeploymentContext() { Logger = Mock.Of <ILogger>(), Tracer = tracerMock.Object }; enviromentMock.Setup(e => e.LocksPath).Returns(@"x:\foo"); deploymentStatusManagerMock.Setup(d => d.ActiveDeploymentId).Returns(deploymentId); var handler = new AutoSwapHandler( deploymentStatusManagerMock.Object, enviromentMock.Object, deploymentSettingsMock.Object, Mock.Of <ITraceFactory>()); TestTracer.Trace("Autoswap will not happen, since it is not enabled."); await handler.HandleAutoSwap(deploymentId, deploymentContextMock); TestTracer.Trace("Autoswap will not happen, since there is no JWT token."); System.Environment.SetEnvironmentVariable(Constants.SiteRestrictedJWT, null); deploymentSettingsMock.Setup( s => s.GetValue(It.Is <string>(v => "WEBSITE_SWAP_SLOTNAME".StartsWith(v)), It.IsAny <bool>()) ).Returns("someslot"); handler = new AutoSwapHandler( deploymentStatusManagerMock.Object, enviromentMock.Object, deploymentSettingsMock.Object, Mock.Of <ITraceFactory>()); var fileSystemMock = new Mock <IFileSystem>(); var fileInfoMock = new Mock <IFileInfoFactory>(); var fileInfoBaseMock = new Mock <FileInfoBase>(); FileSystemHelpers.Instance = fileSystemMock.Object; 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(true); fileInfoBaseMock.Setup(f => f.LastWriteTimeUtc).Returns(DateTime.UtcNow); await handler.HandleAutoSwap(deploymentId, deploymentContextMock); try { string jwtToken = Guid.NewGuid().ToString(); string hostName = "foo.scm.bar"; System.Environment.SetEnvironmentVariable(Constants.SiteRestrictedJWT, jwtToken); System.Environment.SetEnvironmentVariable(Constants.HttpHost, hostName); TestTracer.Trace("Autoswap will not happen, since there deploymet id not changed"); await handler.HandleAutoSwap(deploymentId, deploymentContextMock); tracerMock.Verify(l => l.Trace("AutoSwap is not enabled", It.IsAny <IDictionary <string, string> >()), Times.Once); tracerMock.Verify(l => l.Trace("AutoSwap is not enabled", It.IsAny <IDictionary <string, string> >()), Times.Once); tracerMock.Verify(l => l.Trace(string.Format(CultureInfo.InvariantCulture, "Deployment haven't changed, no need for auto swap: {0}", deploymentId), It.IsAny <IDictionary <string, string> >()), Times.Once); TestTracer.Trace("Autoswap will be triggered"); string newDeploymentId = Guid.NewGuid().ToString(); string autoSwapRequestUrl = null; string bearerToken = null; OperationClient.ClientHandler = new TestMessageHandler((HttpRequestMessage requestMessage) => { autoSwapRequestUrl = requestMessage.RequestUri.AbsoluteUri; bearerToken = requestMessage.Headers.GetValues("Authorization").First(); return(new HttpResponseMessage(HttpStatusCode.OK)); }); await handler.HandleAutoSwap(newDeploymentId, deploymentContextMock); Assert.NotNull(autoSwapRequestUrl); Assert.True(autoSwapRequestUrl.StartsWith("https://foo.scm.bar/operations/autoswap?slot=someslot&operationId=AUTOSWAP")); Assert.NotNull(bearerToken); Assert.Equal("Bearer " + jwtToken, bearerToken); } finally { System.Environment.SetEnvironmentVariable(Constants.SiteRestrictedJWT, null); System.Environment.SetEnvironmentVariable(Constants.HttpHost, null); OperationClient.ClientHandler = null; } }
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(); 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); }