From 80194511f517930a7a0053ae65ceb95c4154cd2c Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Thu, 24 Aug 2017 14:27:41 -0700 Subject: [PATCH] Add test that uses site extension and bundled aspnetcore version (#91) --- build/repo.targets | 6 +- .../AzureFixture.cs | 2 +- .../TemplateFunctionalTests.cs | 89 +++++++++++++++++-- .../AppServicesWithSiteExtensions.json | 2 +- .../TestCommand.cs | 11 ++- 5 files changed, 96 insertions(+), 14 deletions(-) diff --git a/build/repo.targets b/build/repo.targets index 9460d8b735..ff5b43edad 100644 --- a/build/repo.targets +++ b/build/repo.targets @@ -2,7 +2,7 @@ $(RepositoryRoot)src\Microsoft.AspNetCore.AzureAppServices.TestBundle\ - https://dotnet.myget.org/F/aspnetcore-ci-dev/ + https://dotnet.myget.org/F/aspnetcore-ci-dev/ master coherent @@ -30,9 +30,9 @@ + TimeoutSeconds="600"/> diff --git a/test/Microsoft.AspNetCore.AzureAppServices.FunctionalTests/AzureFixture.cs b/test/Microsoft.AspNetCore.AzureAppServices.FunctionalTests/AzureFixture.cs index 5f6dd9e656..9a63781058 100644 --- a/test/Microsoft.AspNetCore.AzureAppServices.FunctionalTests/AzureFixture.cs +++ b/test/Microsoft.AspNetCore.AzureAppServices.FunctionalTests/AzureFixture.cs @@ -68,7 +68,7 @@ namespace Microsoft.AspNetCore.AzureAppServices.FunctionalTests .Create(); } - private static string GetRequiredEnvironmentVariable(string name) + public static string GetRequiredEnvironmentVariable(string name) { var authFile = Environment.GetEnvironmentVariable(name); if (string.IsNullOrEmpty(authFile)) diff --git a/test/Microsoft.AspNetCore.AzureAppServices.FunctionalTests/TemplateFunctionalTests.cs b/test/Microsoft.AspNetCore.AzureAppServices.FunctionalTests/TemplateFunctionalTests.cs index 5f97e5e6a9..633fd63249 100644 --- a/test/Microsoft.AspNetCore.AzureAppServices.FunctionalTests/TemplateFunctionalTests.cs +++ b/test/Microsoft.AspNetCore.AzureAppServices.FunctionalTests/TemplateFunctionalTests.cs @@ -1,10 +1,15 @@ // 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.Net.Http; +using System.Reflection; using System.Runtime.CompilerServices; using System.Threading.Tasks; +using System.Xml.Linq; using Microsoft.Azure.Management.AppService.Fluent; using Microsoft.Azure.Management.AppService.Fluent.Models; using Xunit; @@ -35,20 +40,15 @@ namespace Microsoft.AspNetCore.AzureAppServices.FunctionalTests using (var logger = GetLogger(testId)) { - Assert.NotNull(_fixture.Azure); - var site = await _fixture.Deploy("Templates\\BasicAppServices.json", baseName: testId); var testDirectory = GetTestDirectory(testId); - var dotnet = DotNet(logger, testDirectory); - var result = await dotnet.ExecuteAsync("new " + template); - result.AssertSuccess(); + await dotnet.ExecuteAndAssertAsync("new " + template); await site.BuildPublishProfileAsync(testDirectory.FullName); - result = await dotnet.ExecuteAsync("publish /p:PublishProfile=Profile"); - result.AssertSuccess(); + await dotnet.ExecuteAndAssertAsync("publish /p:PublishProfile=Profile"); using (var httpClient = site.CreateClient()) { @@ -59,6 +59,81 @@ namespace Microsoft.AspNetCore.AzureAppServices.FunctionalTests } } + [Theory] + [InlineData("web", "Hello World!")] + [InlineData("razor", "Learn how to build ASP.NET apps that can run anywhere.")] + [InlineData("mvc", "Learn how to build ASP.NET apps that can run anywhere.")] + public async Task DotnetNewWebRunsWebAppOnLatestRuntime(string template, string expected) + { + var testId = nameof(DotnetNewWebRunsWebAppOnLatestRuntime) + template; + + using (var logger = GetLogger(testId)) + { + var site = await _fixture.Deploy("Templates\\AppServicesWithSiteExtensions.json", + baseName: testId, + additionalArguments: new Dictionary + { + { "extensionFeed", AzureFixture.GetRequiredEnvironmentVariable("SiteExtensionFeed") }, + { "extensionName", "AspNetCoreTestBundle" }, + { "extensionVersion", GetAssemblyInformationalVersion() }, + }); + + var testDirectory = GetTestDirectory(testId); + var dotnet = DotNet(logger, testDirectory); + + await dotnet.ExecuteAndAssertAsync("new " + template); + + FixAspNetCoreVersion(testDirectory); + + await dotnet.ExecuteAndAssertAsync("restore"); + + await site.BuildPublishProfileAsync(testDirectory.FullName); + + await dotnet.ExecuteAndAssertAsync("publish /p:PublishProfile=Profile"); + + using (var httpClient = site.CreateClient()) + { + var getResult = await httpClient.GetAsync("/"); + getResult.EnsureSuccessStatusCode(); + Assert.Contains(expected, await getResult.Content.ReadAsStringAsync()); + } + } + } + + private static void FixAspNetCoreVersion(DirectoryInfo testDirectory) + { + // TODO: Temporary workaround for broken templates in latest CLI + + var csproj = testDirectory.GetFiles("*.csproj").Single().FullName; + var projectContents = XDocument.Load(csproj); + var packageReference = projectContents + .Descendants("PackageReference") + .Single(element => (string) element.Attribute("Include") == "Microsoft.AspNetCore.All"); + + // Detect what version of aspnet core was shipped with this CLI installation + var aspnetCoreVersion = + new DirectoryInfo( + Path.Combine( + Path.GetDirectoryName(TestCommand.DotnetPath), + "store", "x86", "netcoreapp2.0", "microsoft.aspnetcore")) + .GetDirectories() + .Single() + .Name; + + packageReference.Attribute("Version").Value = aspnetCoreVersion; + projectContents.Save(csproj); + } + + private string GetAssemblyInformationalVersion() + { + var assemblyInformationalVersionAttribute = typeof(TemplateFunctionalTests).Assembly.GetCustomAttribute(); + if (assemblyInformationalVersionAttribute == null) + { + throw new InvalidOperationException("Tests assembly lacks AssemblyInformationalVersionAttribute"); + } + return assemblyInformationalVersionAttribute.InformationalVersion; + } + private TestLogger GetLogger([CallerMemberName] string callerName = null) { _fixture.TestLog.StartTestLog(_outputHelper, nameof(TemplateFunctionalTests), out var factory, callerName); diff --git a/test/Microsoft.AspNetCore.AzureAppServices.FunctionalTests/Templates/AppServicesWithSiteExtensions.json b/test/Microsoft.AspNetCore.AzureAppServices.FunctionalTests/Templates/AppServicesWithSiteExtensions.json index 9f0d6c8528..af14fbad61 100644 --- a/test/Microsoft.AspNetCore.AzureAppServices.FunctionalTests/Templates/AppServicesWithSiteExtensions.json +++ b/test/Microsoft.AspNetCore.AzureAppServices.FunctionalTests/Templates/AppServicesWithSiteExtensions.json @@ -32,7 +32,7 @@ }, "resources": [ { - "type": "extensions", + "type": "siteextensions", "name": "[parameters('extensionName')]", "apiVersion": "2015-08-01", "location": "West US 2", diff --git a/test/Microsoft.AspNetCore.AzureAppServices.FunctionalTests/TestCommand.cs b/test/Microsoft.AspNetCore.AzureAppServices.FunctionalTests/TestCommand.cs index d45eb59366..4eec47076b 100644 --- a/test/Microsoft.AspNetCore.AzureAppServices.FunctionalTests/TestCommand.cs +++ b/test/Microsoft.AspNetCore.AzureAppServices.FunctionalTests/TestCommand.cs @@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.AzureAppServices.FunctionalTests { public class TestCommand { - private string _dotnetPath = GetDotnetPath(); + public static string DotnetPath { get; } = GetDotnetPath(); private static string GetDotnetPath() { @@ -77,6 +77,13 @@ namespace Microsoft.AspNetCore.AzureAppServices.FunctionalTests return await ExecuteAsyncInternal(resolvedCommand, args); } + public virtual async Task ExecuteAndAssertAsync(string args = "") + { + var result = await ExecuteAsync(args); + result.AssertSuccess(); + return result; + } + private async Task ExecuteAsyncInternal(string executable, string args) { var stdOut = new List(); @@ -200,7 +207,7 @@ namespace Microsoft.AspNetCore.AzureAppServices.FunctionalTests { if (executable == "dotnet") { - executable = _dotnetPath; + executable = DotnetPath; return; }