diff --git a/ServerTests.sln b/ServerTests.sln index f26151af6f..8f87a50c85 100644 --- a/ServerTests.sln +++ b/ServerTests.sln @@ -1,6 +1,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26228.10 +VisualStudioVersion = 15.0.26403.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{49AB8AAA-8160-48DF-A18B-78F51E54E02A}" ProjectSection(SolutionItems) = preProject diff --git a/test/ServerComparison.FunctionalTests/HelloWorldTest.cs b/test/ServerComparison.FunctionalTests/HelloWorldTest.cs index d5724031af..5557e053e8 100644 --- a/test/ServerComparison.FunctionalTests/HelloWorldTest.cs +++ b/test/ServerComparison.FunctionalTests/HelloWorldTest.cs @@ -6,6 +6,7 @@ using System.Net.Http; using System.Runtime.CompilerServices; using System.Threading.Tasks; using Microsoft.AspNetCore.Server.IntegrationTesting; +using Microsoft.AspNetCore.Server.IntegrationTesting.xunit; using Microsoft.AspNetCore.Testing.xunit; using Microsoft.DotNet.PlatformAbstractions; using Microsoft.Extensions.Logging; @@ -15,13 +16,10 @@ using Xunit.Sdk; namespace ServerComparison.FunctionalTests { - public class HelloWorldTests + public class HelloWorldTests : LoggedTest { - private readonly ITestOutputHelper _output; - - public HelloWorldTests(ITestOutputHelper output) + public HelloWorldTests(ITestOutputHelper output) : base(output) { - _output = output; } // Tests disabled on x86 because of https://github.com/aspnet/Hosting/issues/601 @@ -60,13 +58,10 @@ namespace ServerComparison.FunctionalTests public async Task HelloWorld(ServerType serverType, RuntimeFlavor runtimeFlavor, RuntimeArchitecture architecture, ApplicationType applicationType, [CallerMemberName] string testName = null) { testName = $"{testName}_{serverType}_{runtimeFlavor}_{architecture}_{applicationType}"; - var loggerFactory = TestLoggingUtilities.SetUpLogging(_output, testName); - var logger = loggerFactory.CreateLogger("TestHarness"); - Console.WriteLine($"Starting test: {testName}"); - logger.LogInformation("Starting test: {testName}", testName); - - using (logger.BeginScope("HelloWorld")) + using (StartLog(out var loggerFactory, testName)) { + var logger = loggerFactory.CreateLogger("HelloWorld"); + var deploymentParameters = new DeploymentParameters(Helpers.GetApplicationPath(applicationType), serverType, runtimeFlavor, architecture) { EnvironmentName = "HelloWorld", // Will pick the Start class named 'StartupHelloWorld', @@ -84,13 +79,11 @@ namespace ServerComparison.FunctionalTests using (var deployer = ApplicationDeployerFactory.Create(deploymentParameters, loggerFactory)) { var deploymentResult = await deployer.DeployAsync(); - var httpClientHandler = new HttpClientHandler(); - var httpClient = new HttpClient(new LoggingHandler(loggerFactory, httpClientHandler)) { BaseAddress = new Uri(deploymentResult.ApplicationBaseUri) }; // Request to base address and check if various parts of the body are rendered & measure the cold startup time. var response = await RetryHelper.RetryRequest(() => { - return httpClient.GetAsync(string.Empty); + return deploymentResult.HttpClient.GetAsync(string.Empty); }, logger, deploymentResult.HostShutdownToken); var responseText = await response.Content.ReadAsStringAsync(); @@ -106,9 +99,6 @@ namespace ServerComparison.FunctionalTests } } } - - Console.WriteLine($"Finished test: {testName}"); - logger.LogInformation("Finished test: {testName}", testName); } } } diff --git a/test/ServerComparison.FunctionalTests/LoggingHandler.cs b/test/ServerComparison.FunctionalTests/LoggingHandler.cs deleted file mode 100644 index 42027875e0..0000000000 --- a/test/ServerComparison.FunctionalTests/LoggingHandler.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System.Net.Http; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.Extensions.Logging; - -namespace ServerComparison.FunctionalTests -{ - internal class LoggingHandler : DelegatingHandler - { - private ILogger _logger; - - public LoggingHandler(ILoggerFactory loggerFactory, HttpMessageHandler innerHandler) : base(innerHandler) - { - _logger = loggerFactory.CreateLogger(); - } - - protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) - { - _logger.LogDebug("Sending {method} {url}", request.Method, request.RequestUri); - var response = await base.SendAsync(request, cancellationToken); - _logger.LogDebug("Received {statusCode} {reasonPhrase} {url}", response.StatusCode, response.ReasonPhrase, request.RequestUri); - return response; - } - } -} \ No newline at end of file diff --git a/test/ServerComparison.FunctionalTests/NtlmAuthenticationTest.cs b/test/ServerComparison.FunctionalTests/NtlmAuthenticationTest.cs index 05e520434a..cd89e8b571 100644 --- a/test/ServerComparison.FunctionalTests/NtlmAuthenticationTest.cs +++ b/test/ServerComparison.FunctionalTests/NtlmAuthenticationTest.cs @@ -8,6 +8,7 @@ using System.Net.Http; using System.Runtime.CompilerServices; using System.Threading.Tasks; using Microsoft.AspNetCore.Server.IntegrationTesting; +using Microsoft.AspNetCore.Server.IntegrationTesting.xunit; using Microsoft.AspNetCore.Testing.xunit; using Microsoft.DotNet.PlatformAbstractions; using Microsoft.Extensions.Logging; @@ -17,13 +18,10 @@ using Xunit.Sdk; namespace ServerComparison.FunctionalTests { - public class NtlmAuthenticationTests + public class NtlmAuthenticationTests : LoggedTest { - private readonly ITestOutputHelper _output; - - public NtlmAuthenticationTests(ITestOutputHelper output) + public NtlmAuthenticationTests(ITestOutputHelper output) : base(output) { - _output = output; } [ConditionalTheory, Trait("ServerComparison.FunctionalTests", "ServerComparison.FunctionalTests")] @@ -38,13 +36,10 @@ namespace ServerComparison.FunctionalTests public async Task NtlmAuthentication(ServerType serverType, RuntimeFlavor runtimeFlavor, RuntimeArchitecture architecture, ApplicationType applicationType) { var testName = $"NtlmAuthentication_{serverType}_{runtimeFlavor}_{architecture}_{applicationType}"; - var loggerFactory = TestLoggingUtilities.SetUpLogging(_output, testName); - var logger = loggerFactory.CreateLogger("TestHarness"); - Console.WriteLine($"Starting test: {testName}"); - logger.LogInformation("Starting test: {testName}", testName); - - using (logger.BeginScope("NtlmAuthenticationTest")) + using (StartLog(out var loggerFactory, testName)) { + var logger = loggerFactory.CreateLogger("NtlmAuthenticationTest"); + var deploymentParameters = new DeploymentParameters(Helpers.GetApplicationPath(applicationType), serverType, runtimeFlavor, architecture) { EnvironmentName = "NtlmAuthentication", // Will pick the Start class named 'StartupNtlmAuthentication' @@ -62,8 +57,7 @@ namespace ServerComparison.FunctionalTests using (var deployer = ApplicationDeployerFactory.Create(deploymentParameters, loggerFactory)) { var deploymentResult = await deployer.DeployAsync(); - var httpClientHandler = new HttpClientHandler(); - var httpClient = new HttpClient(new LoggingHandler(loggerFactory, httpClientHandler)) { BaseAddress = new Uri(deploymentResult.ApplicationBaseUri) }; + var httpClient = deploymentResult.HttpClient; // Request to base address and check if various parts of the body are rendered & measure the cold startup time. var response = await RetryHelper.RetryRequest(() => @@ -109,8 +103,9 @@ namespace ServerComparison.FunctionalTests Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode); logger.LogInformation("Enabling Default Credentials"); - httpClientHandler = new HttpClientHandler() { UseDefaultCredentials = true }; - httpClient = new HttpClient(httpClientHandler) { BaseAddress = new Uri(deploymentResult.ApplicationBaseUri) }; + + // Change the http client to one that uses default credentials + httpClient = deploymentResult.CreateHttpClient(new HttpClientHandler() { UseDefaultCredentials = true }); logger.LogInformation("Testing /AutoForbid"); response = await httpClient.GetAsync("/AutoForbid"); @@ -155,9 +150,6 @@ namespace ServerComparison.FunctionalTests } } } - - Console.WriteLine($"Finished test: {testName}"); - logger.LogInformation("Finished test: {testName}", testName); } } } diff --git a/test/ServerComparison.FunctionalTests/ResponseCompressionTests.cs b/test/ServerComparison.FunctionalTests/ResponseCompressionTests.cs index aca273bdaa..b0d7d6cf6f 100644 --- a/test/ServerComparison.FunctionalTests/ResponseCompressionTests.cs +++ b/test/ServerComparison.FunctionalTests/ResponseCompressionTests.cs @@ -11,6 +11,7 @@ using System.Net.Http; using System.Runtime.CompilerServices; using System.Threading.Tasks; using Microsoft.AspNetCore.Server.IntegrationTesting; +using Microsoft.AspNetCore.Server.IntegrationTesting.xunit; using Microsoft.AspNetCore.Testing.xunit; using Microsoft.DotNet.PlatformAbstractions; using Microsoft.Extensions.Logging; @@ -21,15 +22,13 @@ using Xunit.Sdk; namespace ServerComparison.FunctionalTests { - public class ResponseCompressionTests + public class ResponseCompressionTests : LoggedTest { // NGinx's default min size is 20 bytes private static readonly string HelloWorldBody = "Hello World;" + new string('a', 20); - private readonly ITestOutputHelper _output; - public ResponseCompressionTests(ITestOutputHelper output) + public ResponseCompressionTests(ITestOutputHelper output) : base(output) { - _output = output; } [ConditionalTheory] @@ -127,13 +126,10 @@ namespace ServerComparison.FunctionalTests public async Task ResponseCompression(ServerType serverType, RuntimeFlavor runtimeFlavor, RuntimeArchitecture architecture, Func scenario, ApplicationType applicationType, bool hostCompression, [CallerMemberName] string testName = null) { testName = $"{testName}_{serverType}_{runtimeFlavor}_{architecture}_{applicationType}"; - var loggerFactory = TestLoggingUtilities.SetUpLogging(_output, testName); - var logger = loggerFactory.CreateLogger("TestHarness"); - Console.WriteLine($"Starting test: {testName}"); - logger.LogInformation("Starting test: {testName}", testName); - - using (logger.BeginScope("ResponseCompression")) + using (StartLog(out var loggerFactory, testName)) { + var logger = loggerFactory.CreateLogger("ResponseCompression"); + var deploymentParameters = new DeploymentParameters(Helpers.GetApplicationPath(applicationType), serverType, runtimeFlavor, architecture) { EnvironmentName = "ResponseCompression", @@ -155,7 +151,7 @@ namespace ServerComparison.FunctionalTests var deploymentResult = await deployer.DeployAsync(); var httpClientHandler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.None }; Assert.True(httpClientHandler.SupportsAutomaticDecompression); - var httpClient = new HttpClient(new LoggingHandler(loggerFactory, httpClientHandler)) { BaseAddress = new Uri(deploymentResult.ApplicationBaseUri) }; + var httpClient = deploymentResult.CreateHttpClient(httpClientHandler); // Request to base address and check if various parts of the body are rendered & measure the cold startup time. var response = await RetryHelper.RetryRequest(() => @@ -178,9 +174,6 @@ namespace ServerComparison.FunctionalTests await scenario(httpClient, logger); } } - - Console.WriteLine($"Finished test: {testName}"); - logger.LogInformation("Finished test: {testName}", testName); } private static async Task CheckNoCompressionAsync(HttpClient client, ILogger logger) diff --git a/test/ServerComparison.FunctionalTests/ResponseTests.cs b/test/ServerComparison.FunctionalTests/ResponseTests.cs index b69fc9ee69..7fd466a51f 100644 --- a/test/ServerComparison.FunctionalTests/ResponseTests.cs +++ b/test/ServerComparison.FunctionalTests/ResponseTests.cs @@ -8,6 +8,7 @@ using System.Net.Http; using System.Runtime.CompilerServices; using System.Threading.Tasks; using Microsoft.AspNetCore.Server.IntegrationTesting; +using Microsoft.AspNetCore.Server.IntegrationTesting.xunit; using Microsoft.AspNetCore.Testing.xunit; using Microsoft.DotNet.PlatformAbstractions; using Microsoft.Extensions.Logging; @@ -18,13 +19,10 @@ using Xunit.Sdk; namespace ServerComparison.FunctionalTests { - public class ResponseTests + public class ResponseTests : LoggedTest { - private readonly ITestOutputHelper _output; - - public ResponseTests(ITestOutputHelper output) + public ResponseTests(ITestOutputHelper output) : base(output) { - _output = output; } [ConditionalTheory] @@ -165,13 +163,10 @@ namespace ServerComparison.FunctionalTests public async Task ResponseFormats(ServerType serverType, RuntimeFlavor runtimeFlavor, RuntimeArchitecture architecture, Func scenario, ApplicationType applicationType, [CallerMemberName] string testName = null) { testName = $"{testName}_{serverType}_{runtimeFlavor}_{architecture}_{applicationType}"; - var loggerFactory = TestLoggingUtilities.SetUpLogging(_output, testName); - var logger = loggerFactory.CreateLogger("TestHarness"); - Console.WriteLine($"Starting test: {testName}"); - logger.LogInformation("Starting test: {testName}", testName); - - using (logger.BeginScope("ResponseFormats")) + using (StartLog(out var loggerFactory, testName)) { + var logger = loggerFactory.CreateLogger("ResponseFormats"); + var deploymentParameters = new DeploymentParameters(Helpers.GetApplicationPath(applicationType), serverType, runtimeFlavor, architecture) { EnvironmentName = "Responses", @@ -189,13 +184,11 @@ namespace ServerComparison.FunctionalTests using (var deployer = ApplicationDeployerFactory.Create(deploymentParameters, loggerFactory)) { var deploymentResult = await deployer.DeployAsync(); - var httpClientHandler = new HttpClientHandler(); - var httpClient = new HttpClient(new LoggingHandler(loggerFactory, httpClientHandler)) { BaseAddress = new Uri(deploymentResult.ApplicationBaseUri) }; // Request to base address and check if various parts of the body are rendered & measure the cold startup time. var response = await RetryHelper.RetryRequest(() => { - return httpClient.GetAsync(string.Empty); + return deploymentResult.HttpClient.GetAsync(string.Empty); }, logger, deploymentResult.HostShutdownToken); var responseText = await response.Content.ReadAsStringAsync(); @@ -210,12 +203,9 @@ namespace ServerComparison.FunctionalTests throw; } - await scenario(httpClient, logger); + await scenario(deploymentResult.HttpClient, logger); } } - - Console.WriteLine($"Finished test: {testName}"); - logger.LogInformation("Finished test: {testName}", testName); } private static async Task CheckContentLengthAsync(HttpClient client, ILogger logger) diff --git a/test/ServerComparison.FunctionalTests/TestLoggingUtilities.cs b/test/ServerComparison.FunctionalTests/TestLoggingUtilities.cs deleted file mode 100644 index 8196bb92fd..0000000000 --- a/test/ServerComparison.FunctionalTests/TestLoggingUtilities.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; -using System.IO; -using System.Reflection; -using System.Runtime.CompilerServices; -using Microsoft.Extensions.Logging; -using Serilog; -using Xunit.Abstractions; - -namespace ServerComparison.FunctionalTests -{ - public static class TestLoggingUtilities - { - public static readonly string TestOutputRoot = Environment.GetEnvironmentVariable("ASPNETCORE_TEST_LOG_DIR"); - - public static ILoggerFactory SetUpLogging(ITestOutputHelper output, [CallerMemberName] string testName = null) - { - var loggerFactory = new LoggerFactory(); - loggerFactory.AddXunit(output, LogLevel.Debug); - - if (!string.IsNullOrEmpty(TestOutputRoot)) - { - var testClass = typeof(TTestClass).GetTypeInfo(); - var testOutputDir = Path.Combine(TestOutputRoot, testClass.Assembly.GetName().Name, testClass.FullName); - if (!Directory.Exists(testOutputDir)) - { - Directory.CreateDirectory(testOutputDir); - } - - var testOutputFile = Path.Combine(testOutputDir, $"{testName}.log"); - - if (File.Exists(testOutputFile)) - { - File.Delete(testOutputFile); - } - - var serilogger = new LoggerConfiguration() - .Enrich.FromLogContext() - .MinimumLevel.Verbose() - .WriteTo.File(testOutputFile, flushToDiskInterval: TimeSpan.FromSeconds(1)) - .CreateLogger(); - loggerFactory.AddSerilog(serilogger); - } - - return loggerFactory; - } - } -}