Throw when web config action is added without publish (#1234)

This commit is contained in:
Pavel Krymets 2018-08-14 15:08:20 -07:00 committed by GitHub
parent 0d91dde270
commit 1104564797
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 120 additions and 119 deletions

View File

@ -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<Action<XElement, string>> 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

View File

@ -16,18 +16,20 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS
{
public IISDeploymentParameters IISDeploymentParameters { get; }
protected List<Action<XElement, string>> DefaultWebConfigActions { get; } = new List<Action<XElement, string>>();
protected List<Action<XElement, string>> DefaultServerConfigActions { get; } = new List<Action<XElement, string>>();
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<Action<XElement, string>> 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<Action<XElement, string>> 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);
}
}
}
}

View File

@ -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<Action<XElement, string>> CreateDefaultWebConfigActionList()
{
return new List<Action<XElement, string>>() { AddWebConfigEnvironmentVariables(), AddHandlerSettings() };
}
public IList<Action<XElement, string>> WebConfigActionList { get; }
public IList<Action<XElement, string>> WebConfigActionList { get; } = new List<Action<XElement, string>>();
public IList<Action<XElement, string>> ServerConfigActionList { get; } = new List<Action<XElement, string>>();
@ -61,49 +51,5 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS
public bool GracefulShutdown { get; set; }
private Action<XElement, string> 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<XElement, string> 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);
}
};
}
}
}

View File

@ -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<Action<XElement, string>> 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)
{