diff --git a/src/Microsoft.AspNetCore.Server.IntegrationTesting.IIS/IISDeployer.cs b/src/Microsoft.AspNetCore.Server.IntegrationTesting.IIS/IISDeployer.cs index 54efe1f3fe..ca0ced4218 100644 --- a/src/Microsoft.AspNetCore.Server.IntegrationTesting.IIS/IISDeployer.cs +++ b/src/Microsoft.AspNetCore.Server.IntegrationTesting.IIS/IISDeployer.cs @@ -87,22 +87,9 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS IISDeploymentParameters.HandlerSettings["debugFile"] = _debugLogFile; } - if (DeploymentParameters.ApplicationType == ApplicationType.Portable) - { - DefaultWebConfigActions.Add( - WebConfigHelpers.AddOrModifyAspNetCoreSection( - "processPath", - DotNetCommands.GetDotNetExecutable(DeploymentParameters.RuntimeArchitecture))); - } - - DotnetPublish(); var contentRoot = DeploymentParameters.PublishedApplicationRootPath; - DefaultWebConfigActions.Add(WebConfigHelpers.AddOrModifyHandlerSection( - key: "modules", - value: DeploymentParameters.AncmVersion.ToString())); - RunWebConfigActions(contentRoot); var uri = TestUriHelper.BuildTestUri(ServerType.IIS, DeploymentParameters.ApplicationBaseUriHint); @@ -121,6 +108,26 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS } } + protected override IEnumerable> GetWebConfigActions() + { + if (DeploymentParameters.ApplicationType == ApplicationType.Portable) + { + yield return WebConfigHelpers.AddOrModifyAspNetCoreSection( + "processPath", + DotNetCommands.GetDotNetExecutable(DeploymentParameters.RuntimeArchitecture)); + } + + yield return WebConfigHelpers.AddOrModifyHandlerSection( + key: "modules", + value: DeploymentParameters.AncmVersion.ToString()); + + + foreach (var action in base.GetWebConfigActions()) + { + yield return action; + } + } + private void GetLogsFromFile() { try diff --git a/src/Microsoft.AspNetCore.Server.IntegrationTesting.IIS/IISDeployerBase.cs b/src/Microsoft.AspNetCore.Server.IntegrationTesting.IIS/IISDeployerBase.cs index f511f9f5f1..614d68b145 100644 --- a/src/Microsoft.AspNetCore.Server.IntegrationTesting.IIS/IISDeployerBase.cs +++ b/src/Microsoft.AspNetCore.Server.IntegrationTesting.IIS/IISDeployerBase.cs @@ -16,18 +16,20 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS { public IISDeploymentParameters IISDeploymentParameters { get; } - protected List> DefaultWebConfigActions { get; } = new List>(); - - protected List> DefaultServerConfigActions { get; } = new List>(); - public IISDeployerBase(IISDeploymentParameters deploymentParameters, ILoggerFactory loggerFactory) : base(deploymentParameters, loggerFactory) { IISDeploymentParameters = deploymentParameters; } - public void RunWebConfigActions(string contentRoot) + protected void RunWebConfigActions(string contentRoot) { + var actions = GetWebConfigActions(); + if (!actions.Any()) + { + return; + } + if (!DeploymentParameters.PublishApplicationBeforeDeployment) { throw new InvalidOperationException("Cannot modify web.config file if no published output."); @@ -36,37 +38,46 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS var path = Path.Combine(DeploymentParameters.PublishedApplicationRootPath, "web.config"); var webconfig = XDocument.Load(path); - foreach (var action in DefaultWebConfigActions) + foreach (var action in actions) { action.Invoke(webconfig.Root, contentRoot); } - if (IISDeploymentParameters != null) - { - foreach (var action in IISDeploymentParameters.WebConfigActionList) - { - action.Invoke(webconfig.Root, contentRoot); - } - } - webconfig.Save(path); } + protected virtual IEnumerable> GetWebConfigActions() + { + if (IISDeploymentParameters.HandlerSettings.Any()) + { + yield return AddHandlerSettings; + } + + if (IISDeploymentParameters.WebConfigBasedEnvironmentVariables.Any()) + { + yield return AddWebConfigEnvironmentVariables; + } + + foreach (var action in IISDeploymentParameters.WebConfigActionList) + { + yield return action; + } + } + + protected virtual IEnumerable> GetServerConfigActions() + { + foreach (var action in IISDeploymentParameters.ServerConfigActionList) + { + yield return action; + } + } public void RunServerConfigActions(XElement config, string contentRoot) { - foreach (var action in DefaultServerConfigActions) + foreach (var action in GetServerConfigActions()) { action.Invoke(config, contentRoot); } - - if (IISDeploymentParameters != null) - { - foreach (var action in IISDeploymentParameters.ServerConfigActionList) - { - action.Invoke(config, contentRoot); - } - } } protected string GetAncmLocation(AncmVersion version) @@ -85,5 +96,33 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS return ancmFile; } + + private void AddWebConfigEnvironmentVariables(XElement element, string contentRoot) + { + var environmentVariables = element + .RequiredElement("system.webServer") + .RequiredElement("aspNetCore") + .GetOrAdd("environmentVariables"); + + foreach (var envVar in IISDeploymentParameters.WebConfigBasedEnvironmentVariables) + { + environmentVariables.GetOrAdd("environmentVariable", "name", envVar.Key) + .SetAttributeValue("value", envVar.Value); + } + } + + private void AddHandlerSettings(XElement element, string contentRoot) + { + var handlerSettings = element + .RequiredElement("system.webServer") + .RequiredElement("aspNetCore") + .GetOrAdd("handlerSettings"); + + foreach (var handlerSetting in IISDeploymentParameters.HandlerSettings) + { + handlerSettings.GetOrAdd("handlerSetting", "name", handlerSetting.Key) + .SetAttributeValue("value", handlerSetting.Value); + } + } } } diff --git a/src/Microsoft.AspNetCore.Server.IntegrationTesting.IIS/IISDeploymentParameters.cs b/src/Microsoft.AspNetCore.Server.IntegrationTesting.IIS/IISDeploymentParameters.cs index fc8fd99efc..30e3f13c25 100644 --- a/src/Microsoft.AspNetCore.Server.IntegrationTesting.IIS/IISDeploymentParameters.cs +++ b/src/Microsoft.AspNetCore.Server.IntegrationTesting.IIS/IISDeploymentParameters.cs @@ -11,13 +11,11 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS { public IISDeploymentParameters() : base() { - WebConfigActionList = CreateDefaultWebConfigActionList(); } public IISDeploymentParameters(TestVariant variant) : base(variant) { - WebConfigActionList = CreateDefaultWebConfigActionList(); } public IISDeploymentParameters( @@ -27,14 +25,11 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS RuntimeArchitecture runtimeArchitecture) : base(applicationPath, serverType, runtimeFlavor, runtimeArchitecture) { - WebConfigActionList = CreateDefaultWebConfigActionList(); } public IISDeploymentParameters(DeploymentParameters parameters) : base(parameters) { - WebConfigActionList = CreateDefaultWebConfigActionList(); - if (parameters is IISDeploymentParameters) { var tempParameters = (IISDeploymentParameters)parameters; @@ -46,12 +41,7 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS } } - private IList> CreateDefaultWebConfigActionList() - { - return new List>() { AddWebConfigEnvironmentVariables(), AddHandlerSettings() }; - } - - public IList> WebConfigActionList { get; } + public IList> WebConfigActionList { get; } = new List>(); public IList> ServerConfigActionList { get; } = new List>(); @@ -61,49 +51,5 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS public bool GracefulShutdown { get; set; } - private Action AddWebConfigEnvironmentVariables() - { - return (element, _) => - { - if (WebConfigBasedEnvironmentVariables.Count == 0) - { - return; - } - - var environmentVariables = element - .RequiredElement("system.webServer") - .RequiredElement("aspNetCore") - .GetOrAdd("environmentVariables"); - - - foreach (var envVar in WebConfigBasedEnvironmentVariables) - { - environmentVariables.GetOrAdd("environmentVariable", "name", envVar.Key) - .SetAttributeValue("value", envVar.Value); - } - }; - } - - private Action AddHandlerSettings() - { - return (element, _) => - { - if (HandlerSettings.Count == 0) - { - return; - } - - var handlerSettings = element - .RequiredElement("system.webServer") - .RequiredElement("aspNetCore") - .GetOrAdd("handlerSettings"); - - foreach (var handlerSetting in HandlerSettings) - { - handlerSettings.GetOrAdd("handlerSetting", "name", handlerSetting.Key) - .SetAttributeValue("value", handlerSetting.Value); - } - }; - } } } diff --git a/src/Microsoft.AspNetCore.Server.IntegrationTesting.IIS/IISExpressDeployer.cs b/src/Microsoft.AspNetCore.Server.IntegrationTesting.IIS/IISExpressDeployer.cs index cd1fbc69f2..c1bc7a26e1 100644 --- a/src/Microsoft.AspNetCore.Server.IntegrationTesting.IIS/IISExpressDeployer.cs +++ b/src/Microsoft.AspNetCore.Server.IntegrationTesting.IIS/IISExpressDeployer.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; @@ -281,17 +282,7 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS if (DeploymentParameters.PublishApplicationBeforeDeployment) { - // For published apps, prefer the content in the web.config, but update it. - DefaultWebConfigActions.Add(WebConfigHelpers.AddOrModifyAspNetCoreSection( - key: "hostingModel", - value: DeploymentParameters.HostingModel == HostingModel.InProcess ? "inprocess" : "")); - - DefaultWebConfigActions.Add(WebConfigHelpers.AddOrModifyHandlerSection( - key: "modules", - value: DeploymentParameters.AncmVersion.ToString())); - ModifyDotNetExePathInWebConfig(); serverConfig = RemoveRedundantElements(serverConfig); - RunWebConfigActions(contentRoot); } else { @@ -300,6 +291,8 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS serverConfig = ReplacePlaceholder(serverConfig, "[AspNetCoreModule]", DeploymentParameters.AncmVersion.ToString()); } + RunWebConfigActions(contentRoot); + var config = XDocument.Parse(serverConfig); RunServerConfigActions(config.Root, contentRoot); serverConfig = config.ToString(); @@ -320,6 +313,40 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS return content; } + protected override IEnumerable> GetWebConfigActions() + { + if (IISDeploymentParameters.PublishApplicationBeforeDeployment) + { + // For published apps, prefer the content in the web.config, but update it. + yield return WebConfigHelpers.AddOrModifyAspNetCoreSection( + key: "hostingModel", + value: DeploymentParameters.HostingModel == HostingModel.InProcess ? "inprocess" : ""); + + yield return WebConfigHelpers.AddOrModifyHandlerSection( + key: "modules", + value: DeploymentParameters.AncmVersion.ToString()); + + // We assume the x64 dotnet.exe is on the path so we need to provide an absolute path for x86 scenarios. + // Only do it for scenarios that rely on dotnet.exe (Core, portable, etc.). + if (DeploymentParameters.RuntimeFlavor == RuntimeFlavor.CoreClr + && DeploymentParameters.ApplicationType == ApplicationType.Portable + && DotNetCommands.IsRunningX86OnX64(DeploymentParameters.RuntimeArchitecture)) + { + var executableName = DotNetCommands.GetDotNetExecutable(DeploymentParameters.RuntimeArchitecture); + if (!File.Exists(executableName)) + { + throw new Exception($"Unable to find '{executableName}'.'"); + } + yield return WebConfigHelpers.AddOrModifyAspNetCoreSection("processPath", executableName); + } + } + + foreach (var action in base.GetWebConfigActions()) + { + yield return action; + } + } + private string ModifyANCMPathInConfig(string replaceFlag, AncmVersion version, string serverConfig) { if (serverConfig.Contains(replaceFlag)) @@ -451,24 +478,6 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS } } - private void ModifyDotNetExePathInWebConfig() - { - // We assume the x64 dotnet.exe is on the path so we need to provide an absolute path for x86 scenarios. - // Only do it for scenarios that rely on dotnet.exe (Core, portable, etc.). - if (DeploymentParameters.RuntimeFlavor == RuntimeFlavor.CoreClr - && DeploymentParameters.ApplicationType == ApplicationType.Portable - && DotNetCommands.IsRunningX86OnX64(DeploymentParameters.RuntimeArchitecture)) - { - var executableName = DotNetCommands.GetDotNetExecutable(DeploymentParameters.RuntimeArchitecture); - if (!File.Exists(executableName)) - { - throw new Exception($"Unable to find '{executableName}'.'"); - } - DefaultWebConfigActions.Add( - WebConfigHelpers.AddOrModifyAspNetCoreSection("processPath", executableName)); - } - } - // These elements are duplicated in the web.config if you publish. Remove them from the host.config. private string RemoveRedundantElements(string serverConfig) {