-
Notifications
You must be signed in to change notification settings - Fork 10k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move config operations from helpers to functions on the deployment pa…
…rameters (#1063)
- Loading branch information
Showing
21 changed files
with
375 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/Microsoft.AspNetCore.Server.IntegrationTesting.IIS/IISDeployerBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using System.Xml.Linq; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS | ||
{ | ||
public abstract class IISDeployerBase : ApplicationDeployer | ||
{ | ||
public IISDeploymentParameters IISDeploymentParameters { get; } | ||
|
||
public IISDeployerBase(IISDeploymentParameters deploymentParameters, ILoggerFactory loggerFactory) | ||
: base(deploymentParameters, loggerFactory) | ||
{ | ||
IISDeploymentParameters = deploymentParameters; | ||
} | ||
|
||
public void RunWebConfigActions() | ||
{ | ||
if (IISDeploymentParameters == null) | ||
{ | ||
return; | ||
} | ||
|
||
if (!DeploymentParameters.PublishApplicationBeforeDeployment) | ||
{ | ||
throw new InvalidOperationException("Cannot modify web.config file if no published output."); | ||
} | ||
|
||
var path = Path.Combine(DeploymentParameters.PublishedApplicationRootPath, "web.config"); | ||
var webconfig = XDocument.Load(path); | ||
var xElement = webconfig.Descendants("system.webServer").Single(); | ||
foreach (var action in IISDeploymentParameters.WebConfigActionList) | ||
{ | ||
action.Invoke(xElement); | ||
} | ||
|
||
webconfig.Save(path); | ||
} | ||
|
||
public string RunServerConfigActions(string serverConfigString) | ||
{ | ||
if (IISDeploymentParameters == null) | ||
{ | ||
return serverConfigString; | ||
} | ||
|
||
var serverConfig = XDocument.Parse(serverConfigString); | ||
var xElement = serverConfig.Descendants("configuration").FirstOrDefault(); | ||
|
||
foreach (var action in IISDeploymentParameters.ServerConfigActionList) | ||
{ | ||
action.Invoke(xElement); | ||
} | ||
return xElement.ToString(); | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/Microsoft.AspNetCore.Server.IntegrationTesting.IIS/IISDeploymentParameterExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// 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.IO; | ||
using System.Linq; | ||
using System.Xml.Linq; | ||
|
||
namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS | ||
{ | ||
public static class IISDeploymentParameterExtensions | ||
{ | ||
public static void AddDebugLogToWebConfig(this IISDeploymentParameters parameters, string filename) | ||
{ | ||
parameters.HandlerSettings["debugLevel"] = "4"; | ||
parameters.HandlerSettings["debugFile"] = filename; | ||
} | ||
|
||
public static void AddServerConfigAction(this IISDeploymentParameters parameters, Action<XElement> action) | ||
{ | ||
parameters.ServerConfigActionList.Add(action); | ||
} | ||
|
||
public static void ModifyWebConfig(this IISDeploymentParameters parameters, Action<XElement> transform) | ||
{ | ||
parameters.WebConfigActionList.Add(transform); | ||
} | ||
|
||
public static void ModifyAspNetCoreSectionInWebConfig(this IISDeploymentParameters parameters, string key, string value) | ||
=> ModifyAttributeInWebConfig(parameters, key, value, section: "aspNetCore"); | ||
|
||
|
||
public static void ModifyAttributeInWebConfig(this IISDeploymentParameters parameters, string key, string value, string section) | ||
{ | ||
parameters.WebConfigActionList.Add((element) => | ||
{ | ||
element.Descendants(section).SingleOrDefault().SetAttributeValue(key, value); | ||
}); | ||
} | ||
|
||
public static void AddHttpsToServerConfig(this IISDeploymentParameters parameters) | ||
{ | ||
parameters.ServerConfigActionList.Add( | ||
element => | ||
{ | ||
element.Descendants("binding") | ||
.Single() | ||
.SetAttributeValue("protocol", "https"); | ||
element.Descendants("access") | ||
.Single() | ||
.SetAttributeValue("sslFlags", "Ssl, SslNegotiateCert"); | ||
}); | ||
} | ||
|
||
public static void AddWindowsAuthToServerConfig(this IISDeploymentParameters parameters) | ||
{ | ||
parameters.ServerConfigActionList.Add( | ||
element => | ||
{ | ||
element.Descendants("windowsAuthentication") | ||
.Single() | ||
.SetAttributeValue("enabled", "true"); | ||
}); | ||
} | ||
|
||
public static void ModifyHandlerSectionInWebConfig(this IISDeploymentParameters parameters, string key, string value) | ||
{ | ||
parameters.WebConfigActionList.Add(element => | ||
{ | ||
element.Descendants("handlers") | ||
.FirstOrDefault() | ||
.Descendants("add") | ||
.FirstOrDefault() | ||
.SetAttributeValue(key, value); | ||
}); | ||
} | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
src/Microsoft.AspNetCore.Server.IntegrationTesting.IIS/IISDeploymentParameters.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// 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.Linq; | ||
using System.Xml.Linq; | ||
|
||
namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS | ||
{ | ||
public class IISDeploymentParameters : DeploymentParameters | ||
{ | ||
public IISDeploymentParameters() : base() | ||
{ | ||
WebConfigActionList = CreateDefaultWebConfigActionList(); | ||
} | ||
|
||
public IISDeploymentParameters(TestVariant variant) | ||
: base(variant) | ||
{ | ||
WebConfigActionList = CreateDefaultWebConfigActionList(); | ||
} | ||
|
||
public IISDeploymentParameters( | ||
string applicationPath, | ||
ServerType serverType, | ||
RuntimeFlavor runtimeFlavor, | ||
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; | ||
WebConfigActionList = tempParameters.WebConfigActionList; | ||
ServerConfigActionList = tempParameters.ServerConfigActionList; | ||
WebConfigBasedEnvironmentVariables = tempParameters.WebConfigBasedEnvironmentVariables; | ||
HandlerSettings = tempParameters.HandlerSettings; | ||
} | ||
} | ||
|
||
private IList<Action<XElement>> CreateDefaultWebConfigActionList() | ||
{ | ||
return new List<Action<XElement>>() { AddWebConfigEnvironmentVariables(), AddHandlerSettings() }; | ||
} | ||
|
||
public IList<Action<XElement>> WebConfigActionList { get; } | ||
|
||
public IList<Action<XElement>> ServerConfigActionList { get; } = new List<Action<XElement>>(); | ||
|
||
public IDictionary<string, string> WebConfigBasedEnvironmentVariables { get; set; } = new Dictionary<string, string>(); | ||
|
||
public IDictionary<string, string> HandlerSettings { get; set; } = new Dictionary<string, string>(); | ||
|
||
private Action<XElement> AddWebConfigEnvironmentVariables() | ||
{ | ||
return xElement => | ||
{ | ||
if (WebConfigBasedEnvironmentVariables.Count == 0) | ||
{ | ||
return; | ||
} | ||
var element = xElement.Descendants("environmentVariables").SingleOrDefault(); | ||
if (element == null) | ||
{ | ||
element = new XElement("environmentVariables"); | ||
xElement.Descendants("aspNetCore").SingleOrDefault().Add(element); | ||
} | ||
foreach (var envVar in WebConfigBasedEnvironmentVariables) | ||
{ | ||
CreateOrSetElement(element, envVar.Key, envVar.Value, "environmentVariable"); | ||
} | ||
}; | ||
} | ||
|
||
private Action<XElement> AddHandlerSettings() | ||
{ | ||
return xElement => | ||
{ | ||
if (HandlerSettings.Count == 0) | ||
{ | ||
return; | ||
} | ||
var element = xElement.Descendants("handlerSettings").SingleOrDefault(); | ||
if (element == null) | ||
{ | ||
element = new XElement("handlerSettings"); | ||
xElement.Descendants("aspNetCore").SingleOrDefault().Add(element); | ||
} | ||
foreach (var handlerSetting in HandlerSettings) | ||
{ | ||
CreateOrSetElement(element, handlerSetting.Key, handlerSetting.Value, "handlerSetting"); | ||
} | ||
}; | ||
} | ||
|
||
private static void CreateOrSetElement(XElement rootElement, string name, string value, string elementName) | ||
{ | ||
if (rootElement.Descendants() | ||
.Attributes() | ||
.Where(attribute => attribute.Value == name) | ||
.Any()) | ||
{ | ||
return; | ||
} | ||
var element = new XElement(elementName); | ||
element.SetAttributeValue("name", name); | ||
element.SetAttributeValue("value", value); | ||
rootElement.Add(element); | ||
} | ||
} | ||
} |
Oops, something went wrong.