diff --git a/global.json b/global.json index 983ba0401e..dd6bbaf48b 100644 --- a/global.json +++ b/global.json @@ -1,3 +1,3 @@ { - "projects": ["src"] + "projects": ["src","c:/github/testing/src","/home/kiran/github/testing/src"] } diff --git a/src/Microsoft.AspNet.Server.Testing/Deployers/ApplicationDeployer.cs b/src/Microsoft.AspNet.Server.Testing/Deployers/ApplicationDeployer.cs index 2c0d8f1742..c7a303affe 100644 --- a/src/Microsoft.AspNet.Server.Testing/Deployers/ApplicationDeployer.cs +++ b/src/Microsoft.AspNet.Server.Testing/Deployers/ApplicationDeployer.cs @@ -3,10 +3,12 @@ using System; using System.Diagnostics; +using System.Linq; using System.IO; using System.Text; using System.Text.RegularExpressions; using System.Threading; +using Microsoft.AspNet.Testing; using Microsoft.Extensions.Logging; namespace Microsoft.AspNet.Server.Testing @@ -16,8 +18,14 @@ namespace Microsoft.AspNet.Server.Testing /// public abstract class ApplicationDeployer : IApplicationDeployer { + /// + /// Example: runtimes/dnx-coreclr-win-x64.1.0.0-rc1-15844/bin + /// protected string ChosenRuntimePath { get; set; } + /// + /// Examples: dnx-coreclr-win-x64.1.0.0-rc1-15844, dnx-mono.1.0.0-rc1-15844 + /// protected string ChosenRuntimeName { get; set; } protected DeploymentParameters DeploymentParameters { get; private set; } @@ -26,6 +34,59 @@ namespace Microsoft.AspNet.Server.Testing protected Stopwatch StopWatch { get; private set; } = new Stopwatch(); + protected string OSPrefix + { + get + { + if (TestPlatformHelper.IsLinux) + { + return "linux"; + } + else if (TestPlatformHelper.IsMac) + { + return "darwin"; + } + else if (TestPlatformHelper.IsWindows) + { + return "win"; + } + else + { + throw new InvalidOperationException("Unrecognized operating system"); + } + } + } + + protected string DnuCommandName + { + get + { + if (TestPlatformHelper.IsWindows) + { + return "dnu.cmd"; + } + else + { + return "dnu"; + } + } + } + + protected string DnxCommandName + { + get + { + if (TestPlatformHelper.IsWindows) + { + return "dnx.exe"; + } + else + { + return "dnx"; + } + } + } + public abstract DeploymentResult Deploy(); public ApplicationDeployer( @@ -38,31 +99,44 @@ namespace Microsoft.AspNet.Server.Testing protected string PopulateChosenRuntimeInformation() { - var runtimePath = Process.GetCurrentProcess().MainModule.FileName; - Logger.LogInformation(string.Empty); - Logger.LogInformation($"Current runtime path is : {runtimePath}"); + // ex: runtimes/dnx-coreclr-win-x64.1.0.0-rc1-15844/bin + var currentRuntimeBinPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName); + Logger.LogInformation($"Current runtime path is : {currentRuntimeBinPath}"); - var replaceStr = new StringBuilder(). - Append("dnx"). - Append((DeploymentParameters.RuntimeFlavor == RuntimeFlavor.CoreClr) ? "-coreclr" : "-clr"). - Append("-win"). - Append((DeploymentParameters.RuntimeArchitecture == RuntimeArchitecture.x86) ? "-x86" : "-x64"). - ToString(); + var targetRuntimeName = new StringBuilder() + .Append("dnx") + .Append((DeploymentParameters.RuntimeFlavor == RuntimeFlavor.CoreClr) ? "-coreclr" : "-clr") + .Append($"-{OSPrefix}") + .Append((DeploymentParameters.RuntimeArchitecture == RuntimeArchitecture.x86) ? "-x86" : "-x64") + .ToString(); - runtimePath = Regex.Replace(runtimePath, "dnx-(clr|coreclr)-win-(x86|x64)", replaceStr, RegexOptions.IgnoreCase); - ChosenRuntimePath = Path.GetDirectoryName(runtimePath); - - var runtimeDirectoryInfo = new DirectoryInfo(ChosenRuntimePath); - if (!runtimeDirectoryInfo.Exists) + string targetRuntimeBinPath; + // Ex: When current runtime is Mono and the tests are being run for CoreClr + if (currentRuntimeBinPath.Contains("dnx-mono")) { - throw new Exception( - string.Format("Requested runtime at location '{0}' does not exist. Please make sure it is installed before running test.", - runtimeDirectoryInfo.FullName)); + targetRuntimeBinPath = currentRuntimeBinPath.Replace("dnx-mono", targetRuntimeName); + } + else + { + targetRuntimeBinPath = Regex.Replace( + currentRuntimeBinPath, + "dnx-(clr|coreclr)-(win|linux|darwin)-(x86|x64)", + targetRuntimeName, + RegexOptions.IgnoreCase); } - ChosenRuntimeName = runtimeDirectoryInfo.Parent.Name; - Logger.LogInformation(string.Empty); - Logger.LogInformation($"Changing to use runtime : {ChosenRuntimeName}"); + var targetRuntimeBinDir = new DirectoryInfo(targetRuntimeBinPath); + if (targetRuntimeBinDir == null || !targetRuntimeBinDir.Exists) + { + throw new Exception($"Requested runtime at location '{targetRuntimeBinPath}' does not exist.Please make sure it is installed before running test."); + } + + ChosenRuntimePath = targetRuntimeBinDir.FullName; + ChosenRuntimeName = targetRuntimeBinDir.Parent.Name; + DeploymentParameters.DnxRuntime = ChosenRuntimeName; + + Logger.LogInformation($"Chosen runtime path is {ChosenRuntimePath}"); + return ChosenRuntimeName; } @@ -75,7 +149,7 @@ namespace Microsoft.AspNet.Server.Testing var parameters = $"publish {DeploymentParameters.ApplicationPath} -o {DeploymentParameters.PublishedApplicationRootPath}" + $" --runtime {DeploymentParameters.DnxRuntime} {noSource} --iis-command {command}"; - var dnuPath = Path.Combine(ChosenRuntimePath, "dnu.cmd"); + var dnuPath = Path.Combine(ChosenRuntimePath, DnuCommandName); Logger.LogInformation($"Executing command {dnuPath} {parameters}"); var startInfo = new ProcessStartInfo @@ -115,7 +189,7 @@ namespace Microsoft.AspNet.Server.Testing { try { - // We've originally published the application in a temp folder. We need to delete it. + // We've originally published the application in a temp folder. We need to delete it. Directory.Delete(DeploymentParameters.PublishedApplicationRootPath, true); } catch (Exception exception) diff --git a/src/Microsoft.AspNet.Server.Testing/Deployers/MonoDeployer.cs b/src/Microsoft.AspNet.Server.Testing/Deployers/MonoDeployer.cs index 93913099f2..00e1d9b999 100644 --- a/src/Microsoft.AspNet.Server.Testing/Deployers/MonoDeployer.cs +++ b/src/Microsoft.AspNet.Server.Testing/Deployers/MonoDeployer.cs @@ -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.IO; using System.Linq; @@ -36,13 +37,20 @@ namespace Microsoft.AspNet.Server.Testing throw new Exception("Runtime not detected on the machine."); } + var runtimeBinDir = new DirectoryInfo(runtimeBin); + ChosenRuntimePath = runtimeBinDir.FullName; + ChosenRuntimeName = runtimeBinDir.Parent.Name; + DeploymentParameters.DnxRuntime = ChosenRuntimeName; + if (DeploymentParameters.PublishApplicationBeforeDeployment) { // We use full path to runtime to pack. - DeploymentParameters.DnxRuntime = new DirectoryInfo(runtimeBin).Parent.FullName; DnuPublish(); } + DeploymentParameters.EnvironmentVariables + .Add(new KeyValuePair("DNX_APPBASE", DeploymentParameters.ApplicationPath)); + // Launch the host process. var hostExitToken = StartMonoHost(); @@ -62,29 +70,36 @@ namespace Microsoft.AspNet.Server.Testing throw new InvalidOperationException("kestrel is the only valid ServerType for Mono"); } + var dnxPath = Path.Combine(ChosenRuntimePath, DnxCommandName); var dnxArgs = $"-p \"{DeploymentParameters.ApplicationPath}\" kestrel --server.urls {DeploymentParameters.ApplicationBaseUriHint}"; - - Logger.LogInformation("Executing command: dnx {dnxArgs}", dnxArgs); + Logger.LogInformation($"Executing command {dnxPath} {dnxArgs}"); var startInfo = new ProcessStartInfo { - FileName = "dnx", + FileName = dnxPath, Arguments = dnxArgs, UseShellExecute = false, CreateNoWindow = true, + RedirectStandardError = true, + RedirectStandardOutput = true, + // Trying a work around for https://github.com/aspnet/Hosting/issues/140. RedirectStandardInput = true }; - _hostProcess = Process.Start(startInfo); + AddEnvironmentVariablesToProcess(startInfo); + + _hostProcess = new Process() { StartInfo = startInfo }; + _hostProcess.ErrorDataReceived += (sender, dataArgs) => { Logger.LogError(dataArgs.Data ?? string.Empty); }; + _hostProcess.OutputDataReceived += (sender, dataArgs) => { Logger.LogInformation(dataArgs.Data ?? string.Empty); }; _hostProcess.EnableRaisingEvents = true; var hostExitTokenSource = new CancellationTokenSource(); _hostProcess.Exited += (sender, e) => { - Logger.LogError("Host process {processName} exited with code {exitCode}.", startInfo.FileName, _hostProcess.ExitCode); TriggerHostShutdown(hostExitTokenSource); }; - - Logger.LogInformation("Started {0}. Process Id : {1}", _hostProcess.MainModule.FileName, _hostProcess.Id); + _hostProcess.Start(); + _hostProcess.BeginErrorReadLine(); + _hostProcess.BeginOutputReadLine(); if (_hostProcess.HasExited) { @@ -92,6 +107,7 @@ namespace Microsoft.AspNet.Server.Testing throw new Exception("Failed to start host"); } + Logger.LogInformation("Started {fileName}. Process Id : {processId}", startInfo.FileName, _hostProcess.Id); return hostExitTokenSource.Token; } diff --git a/src/Microsoft.AspNet.Server.Testing/Deployers/SelfHostDeployer.cs b/src/Microsoft.AspNet.Server.Testing/Deployers/SelfHostDeployer.cs index d9a67a6f89..1d2c2b1ed7 100644 --- a/src/Microsoft.AspNet.Server.Testing/Deployers/SelfHostDeployer.cs +++ b/src/Microsoft.AspNet.Server.Testing/Deployers/SelfHostDeployer.cs @@ -5,6 +5,7 @@ using System; using System.Diagnostics; using System.IO; using System.Threading; +using Microsoft.AspNet.Testing; using Microsoft.Extensions.Logging; namespace Microsoft.AspNet.Server.Testing @@ -50,11 +51,11 @@ namespace Microsoft.AspNet.Server.Testing var commandName = DeploymentParameters.Command; if (string.IsNullOrEmpty(commandName)) { - commandName = DeploymentParameters.ServerType == ServerType.WebListener ? "web" : "kestrel"; + commandName = DeploymentParameters.ServerType == ServerType.WebListener ? "weblistener" : "kestrel"; } - var dnxPath = Path.Combine(ChosenRuntimePath, "dnx.exe"); + var dnxPath = Path.Combine(ChosenRuntimePath, DnxCommandName); var dnxArgs = $"-p \"{DeploymentParameters.ApplicationPath}\" {commandName} --server.urls {DeploymentParameters.ApplicationBaseUriHint}"; - Logger.LogInformation("Executing {dnxexe} {dnxArgs}", dnxPath, dnxArgs); + Logger.LogInformation($"Executing {dnxPath} {dnxArgs}"); var startInfo = new ProcessStartInfo { diff --git a/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs b/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs index 159c2775b5..e640180a97 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs @@ -281,7 +281,8 @@ namespace Microsoft.AspNet.Hosting } [ConditionalTheory] - [OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)] + [OSSkipCondition(OperatingSystems.Linux)] + [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(@"sub/sub2\sub3\", @"sub/sub2/sub3/")] public void MapPath_Windows_Facts(string virtualPath, string expectedSuffix) {