Add a functional test to verify RazorCompileOnBuild

* Verify precompiled views are used
* Merge developer exception page tests with dotnet run tests
* Turn logging down for EF DbCommands to make logs less noisy
This commit is contained in:
Pranav K 2018-02-25 20:47:04 -08:00
parent 617005f04c
commit cc0eaf9a76
15 changed files with 155 additions and 82 deletions

View File

@ -57,6 +57,9 @@ namespace MusicStore
var logLevel = string.Equals(environment, "Development", StringComparison.Ordinal) ? LogLevel.Information : LogLevel.Warning;
factory.SetMinimumLevel(logLevel);
// Turn off Info logging for EF commands
factory.AddFilter("Microsoft.EntityFrameworkCore.Database.Command", LogLevel.Warning);
});
var host = builder.Build();

View File

@ -18,4 +18,4 @@
</a>
</li>
}
</ul>
</ul>

View File

@ -1,4 +1,4 @@
@inject IOptions<AppSettings> AppSettings
@inject IOptions<AppSettings> AppSettings
<!DOCTYPE html>
<html>
<head>
@ -51,6 +51,7 @@
<footer class="navbar navbar-fixed-bottom navbar-default text-center">
<p><a href="https://www.github.com/aspnet/MusicStore">www.github.com/aspnet/MusicStore</a></p>
<small>@Html.ActionLink("admin", "Index", "StoreManager", new { area = "Admin" })</small>
<small>This view served from @GetType().Assembly.FullName</small>
</footer>
</div>
@ -71,4 +72,4 @@
@RenderSection("scripts", required: false)
</body>
</html>
</html>

View File

@ -7,7 +7,7 @@ namespace E2ETests
{
public class Helpers
{
public static string GetApplicationPath(ApplicationType applicationType)
public static string GetApplicationPath()
{
var solutionDirectory = TestPathUtilities.GetSolutionRootDirectory("MusicStore");
return Path.GetFullPath(Path.Combine(solutionDirectory, "samples", "MusicStore"));
@ -60,4 +60,4 @@ namespace E2ETests
throw new ArgumentException($"Unknown runtime flavor '{flavor}.");
}
}
}
}

View File

@ -0,0 +1,85 @@
// 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.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;
using Xunit;
using Xunit.Abstractions;
namespace E2ETests
{
public class DotnetRunTestRunner : LoggedTest
{
public DotnetRunTestRunner(ITestOutputHelper output)
: base(output)
{
}
public async Task RunTests(
ServerType serverType,
RuntimeFlavor runtimeFlavor,
ApplicationType applicationType,
RuntimeArchitecture runtimeArchitecture)
{
var testName = $"DotnetRunTests_{serverType}_{runtimeFlavor}_{applicationType}";
using (StartLog(out var loggerFactory, testName))
{
var logger = loggerFactory.CreateLogger("DotnetRunTests");
var musicStoreDbName = DbUtils.GetUniqueName();
var applicationPath = Helpers.GetApplicationPath();
var deploymentParameters = new DeploymentParameters(
applicationPath, serverType, runtimeFlavor, runtimeArchitecture)
{
PublishApplicationBeforeDeployment = false,
TargetFramework = Helpers.GetTargetFramework(runtimeFlavor),
Configuration = Helpers.GetCurrentBuildConfiguration(),
EnvironmentName = "Development",
ApplicationType = applicationType,
UserAdditionalCleanup = parameters =>
{
DbUtils.DropDatabase(musicStoreDbName, logger);
},
EnvironmentVariables =
{
{ MusicStoreConfig.ConnectionStringKey, DbUtils.CreateConnectionString(musicStoreDbName) },
},
};
using (var deployer = ApplicationDeployerFactory.Create(deploymentParameters, loggerFactory))
{
var deploymentResult = await deployer.DeployAsync();
var httpClientHandler = new HttpClientHandler { UseDefaultCredentials = true };
var httpClient = deploymentResult.CreateHttpClient(httpClientHandler);
var response = await RetryHelper.RetryRequest(
() => httpClient.GetAsync(string.Empty), logger, deploymentResult.HostShutdownToken);
Assert.False(response == null, "Response object is null because the client could not " +
"connect to the server after multiple retries");
var validator = new Validator(httpClient, httpClientHandler, logger, deploymentResult);
logger.LogInformation("Verifying home page");
// Verify HomePage should validate that we're using precompiled views.
await validator.VerifyHomePage(response);
// Verify developer exception page
logger.LogInformation("Verifying developer exception page");
response = await RetryHelper.RetryRequest(
() => httpClient.GetAsync("PageThatThrows"), logger, cancellationToken: deploymentResult.HostShutdownToken);
await validator.VerifyDeveloperExceptionPage(response);
logger.LogInformation("Variation completed successfully.");
}
}
}
}
}

View File

@ -0,0 +1,39 @@
// 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.Threading.Tasks;
using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.AspNetCore.Testing.xunit;
using Xunit;
using Xunit.Abstractions;
namespace E2ETests
{
[Trait("E2Etests", "DotnetRun")]
public class DotnetRunTests_X64
{
private readonly DotnetRunTestRunner _testRunner;
public DotnetRunTests_X64(ITestOutputHelper output)
{
_testRunner = new DotnetRunTestRunner(output);
}
[Fact]
public Task DotnetRunTests_X64_Kestrel_CoreClr()
{
return RunTests(ServerType.Kestrel, RuntimeFlavor.CoreClr, ApplicationType.Portable);
}
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)]
public Task DotnetRunTests_X64_Kestrel_Clr()
{
// CLR must be published as standalone to perform rid specific deployment
return RunTests(ServerType.Kestrel, RuntimeFlavor.Clr, ApplicationType.Standalone);
}
private Task RunTests(ServerType serverType, RuntimeFlavor runtimeFlavor, ApplicationType applicationType)
=> _testRunner.RunTests(serverType, runtimeFlavor, applicationType, RuntimeArchitecture.x64);
}
}

View File

@ -95,4 +95,4 @@ namespace E2ETests
_logger.LogInformation("Middleware events were fired successfully");
}
}
}
}

View File

@ -110,8 +110,8 @@ namespace E2ETests
//Helpers.ThrowIfResponseStatusNotOk(runtimeResponse, _logger);
}
//var runtimeInfo = await runtimeResponse.Content.ReadAsStringAsync();
//_logger.LogInformation(runtimeInfo);
// Verify the app is using precompiled views
Assert.Contains("MusicStore.Views, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", responseContent);
}
public async Task VerifyNtlmHomePage(HttpResponseMessage response)
@ -130,8 +130,8 @@ namespace E2ETests
public async Task VerifyDeveloperExceptionPage(HttpResponseMessage response)
{
var responseContent = await response.Content.ReadAsStringAsync();
Assert.Contains(responseContent, "PageThatThrows.cshtml");
Assert.Contains(responseContent, "@{ throw new InvalidOperationException(); }");
Assert.Contains("PageThatThrows.cshtml", responseContent);
Assert.Contains("@{ throw new InvalidOperationException(); }", responseContent);
}
public void ValidateLayoutPage(string responseContent)
@ -143,6 +143,17 @@ namespace E2ETests
Assert.Contains("<li class=\"divider\"></li>", responseContent, StringComparison.OrdinalIgnoreCase);
}
public async Task VerifyRuntimeCompiledHomePage(HttpResponseMessage response)
{
var responseContent = await response.Content.ReadAsStringAsync();
// Smoke test to make sure the page is served correctly.
Assert.Contains("<title>Home Page ASP.NET MVC Music Store</title>", responseContent);
// Verify the app is using a runtime compiled view
Assert.DoesNotContain("MusicStore.Views, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", responseContent);
}
public async Task VerifyStaticContentServed()
{
_logger.LogInformation("Validating if static contents are served..");

View File

@ -68,7 +68,7 @@ namespace E2ETests
var logger = loggerFactory.CreateLogger("NtlmAuthenticationTest");
var musicStoreDbName = DbUtils.GetUniqueName();
var deploymentParameters = new DeploymentParameters(Helpers.GetApplicationPath(applicationType), serverType, runtimeFlavor, architecture)
var deploymentParameters = new DeploymentParameters(Helpers.GetApplicationPath(), serverType, runtimeFlavor, architecture)
{
PublishApplicationBeforeDeployment = true,
PreservePublishedApplicationForDebugging = Helpers.PreservePublishedApplicationForDebugging,

View File

@ -46,7 +46,7 @@ namespace E2ETests
var logger = loggerFactory.CreateLogger("OpenIdConnectTestSuite");
var musicStoreDbName = DbUtils.GetUniqueName();
var deploymentParameters = new DeploymentParameters(Helpers.GetApplicationPath(applicationType), serverType, runtimeFlavor, architecture)
var deploymentParameters = new DeploymentParameters(Helpers.GetApplicationPath(), serverType, runtimeFlavor, architecture)
{
PublishApplicationBeforeDeployment = true,
PreservePublishedApplicationForDebugging = Helpers.PreservePublishedApplicationForDebugging,

View File

@ -31,7 +31,7 @@ namespace E2ETests
var musicStoreDbName = DbUtils.GetUniqueName();
var deploymentParameters = new DeploymentParameters(
Helpers.GetApplicationPath(applicationType), serverType, runtimeFlavor, runtimeArchitecture)
Helpers.GetApplicationPath(), serverType, runtimeFlavor, runtimeArchitecture)
{
PublishApplicationBeforeDeployment = true,
PreservePublishedApplicationForDebugging = Helpers.PreservePublishedApplicationForDebugging,
@ -85,58 +85,5 @@ namespace E2ETests
}
}
}
public async Task RunDeveloperTests(
ServerType serverType,
RuntimeFlavor runtimeFlavor,
ApplicationType applicationType,
RuntimeArchitecture runtimeArchitecture)
{
var testName = $"PublishAndRunTests_{serverType}_{runtimeFlavor}_{applicationType}";
using (StartLog(out var loggerFactory, testName))
{
var logger = loggerFactory.CreateLogger("Publish_And_Run_Development");
var musicStoreDbName = DbUtils.GetUniqueName();
var deploymentParameters = new DeploymentParameters(
Helpers.GetApplicationPath(applicationType), serverType, runtimeFlavor, runtimeArchitecture)
{
PublishApplicationBeforeDeployment = true,
PreservePublishedApplicationForDebugging = Helpers.PreservePublishedApplicationForDebugging,
TargetFramework = Helpers.GetTargetFramework(runtimeFlavor),
Configuration = Helpers.GetCurrentBuildConfiguration(),
EnvironmentName = "Development",
ApplicationType = applicationType,
UserAdditionalCleanup = parameters =>
{
DbUtils.DropDatabase(musicStoreDbName, logger);
}
};
// Override the connection strings using environment based configuration
deploymentParameters.EnvironmentVariables
.Add(new KeyValuePair<string, string>(
MusicStoreConfig.ConnectionStringKey,
DbUtils.CreateConnectionString(musicStoreDbName)));
using (var deployer = ApplicationDeployerFactory.Create(deploymentParameters, loggerFactory))
{
var deploymentResult = await deployer.DeployAsync();
var httpClientHandler = new HttpClientHandler { UseDefaultCredentials = true };
var httpClient = deploymentResult.CreateHttpClient(httpClientHandler);
// Request to base address and check if various parts of the body are rendered &
// measure the cold startup time.
// Add retry logic since tests are flaky on mono due to connection issues
var validator = new Validator(httpClient, httpClientHandler, logger, deploymentResult);
var response = await RetryHelper.RetryRequest(() => httpClient.GetAsync("PageThatThrows"), logger, cancellationToken: deploymentResult.HostShutdownToken);
logger.LogInformation("Verifying developer exception page");
await validator.VerifyDeveloperExceptionPage(response);
logger.LogInformation("Variation completed successfully.");
}
}
}
}
}

View File

@ -44,19 +44,6 @@ namespace E2ETests
return RunTests(ServerType.Kestrel, RuntimeFlavor.CoreClr, ApplicationType.Portable);
}
[Fact]
public Task PublishAndRunDevelopmentTests_X64_Kestrel_CoreClr_Portable()
{
return _testRunner.RunTests(ServerType.Kestrel, RuntimeFlavor.CoreClr, ApplicationType.Portable, RuntimeArchitecture.x64);
}
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)]
public Task PublishAndRunDevelopmentTests_X64_Kestrel_Clr_Portable()
{
return _testRunner.RunTests(ServerType.Kestrel, RuntimeFlavor.Clr, ApplicationType.Portable, RuntimeArchitecture.x64);
}
[Fact]
public Task PublishAndRunTests_X64_Kestrel_CoreClr_Standalone()
{

View File

@ -30,7 +30,7 @@ namespace E2ETests
var musicStoreDbName = DbUtils.GetUniqueName();
var deploymentParameters = new DeploymentParameters(
Helpers.GetApplicationPath(applicationType), serverType, runtimeFlavor, architecture)
Helpers.GetApplicationPath(), serverType, runtimeFlavor, architecture)
{
EnvironmentName = "SocialTesting",
ServerConfigTemplateContent = (serverType == ServerType.IISExpress) ? File.ReadAllText("Http.config") : null,

View File

@ -236,7 +236,7 @@ namespace E2ETests
{
var logger = loggerFactory.CreateLogger(nameof(SmokeTestsOnNanoServerUsingStandaloneRuntime));
var deploymentParameters = new RemoteWindowsDeploymentParameters(
Helpers.GetApplicationPath(applicationType),
Helpers.GetApplicationPath(),
_remoteDeploymentConfig.DotnetRuntimePathOnShare,
serverType,
RuntimeFlavor.CoreClr,

View File

@ -30,7 +30,7 @@ namespace E2ETests.SmokeTestsUsingStore
var musicStoreDbName = DbUtils.GetUniqueName();
var deploymentParameters = new DeploymentParameters(
Helpers.GetApplicationPath(ApplicationType.Portable), serverType, RuntimeFlavor.CoreClr, RuntimeArchitecture.x64)
Helpers.GetApplicationPath(), serverType, RuntimeFlavor.CoreClr, RuntimeArchitecture.x64)
{
EnvironmentName = "SocialTesting",
SiteName = "MusicStoreTestSiteUsingStore",