From 3842779379a0ef4c995dc120709e53b02661899f Mon Sep 17 00:00:00 2001 From: Justin Kotalik Date: Tue, 23 Oct 2018 11:23:36 -0700 Subject: [PATCH] Check what current processes are running to see if dotnet.exe/iisexpress.exe is being stopped. (#1530) --- .../Inprocess/StartupTests.cs | 2 +- test/Common.FunctionalTests/LogFileTests.cs | 20 ++++----------- .../Utilities/Helpers.cs | 25 +++++++++++++++++++ .../Utilities/LogFileTestBase.cs | 2 +- .../Inprocess/StdOutRedirectionTests.cs | 6 ++--- 5 files changed, 35 insertions(+), 20 deletions(-) diff --git a/test/Common.FunctionalTests/Inprocess/StartupTests.cs b/test/Common.FunctionalTests/Inprocess/StartupTests.cs index fd77a400e3..5ca1733443 100644 --- a/test/Common.FunctionalTests/Inprocess/StartupTests.cs +++ b/test/Common.FunctionalTests/Inprocess/StartupTests.cs @@ -137,7 +137,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests Assert.True(File.Exists(Path.Combine(deploymentResult.ContentRoot, "InProcessWebSite.exe"))); Assert.False(File.Exists(Path.Combine(deploymentResult.ContentRoot, "hostfxr.dll"))); - Assert.Contains("InProcessWebSite.exe", File.ReadAllText(Path.Combine(deploymentResult.ContentRoot, "web.config"))); + Assert.Contains("InProcessWebSite.exe", Helpers.ReadAllTextFromFile(Path.Combine(deploymentResult.ContentRoot, "web.config"), Logger)); await deploymentResult.AssertStarts(); } diff --git a/test/Common.FunctionalTests/LogFileTests.cs b/test/Common.FunctionalTests/LogFileTests.cs index 17f9def2d7..829a736b6e 100644 --- a/test/Common.FunctionalTests/LogFileTests.cs +++ b/test/Common.FunctionalTests/LogFileTests.cs @@ -13,26 +13,15 @@ using Xunit; namespace Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests { [Collection(PublishedSitesCollection.Name)] - public class LoggingTests : IISFunctionalTestBase + public class LoggingTests : LogFileTestBase { private readonly PublishedSitesFixture _fixture; - private readonly string _logFolderPath; public LoggingTests(PublishedSitesFixture fixture) { - _logFolderPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); _fixture = fixture; } - public override void Dispose() - { - base.Dispose(); - if (Directory.Exists(_logFolderPath)) - { - Directory.Delete(_logFolderPath, true); - } - } - public static TestMatrix TestVariants => TestMatrix.ForServers(DeployerSelector.ServerType) .WithTfms(Tfm.NetCoreApp22) @@ -65,7 +54,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests StopServer(); - var contents = File.ReadAllText(Helpers.GetExpectedLogName(deploymentResult, _logFolderPath)); + var contents = Helpers.ReadAllTextFromFile(Helpers.GetExpectedLogName(deploymentResult, _logFolderPath), Logger); Assert.Contains("TEST MESSAGE", contents); } @@ -158,7 +147,8 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests var response = await deploymentResult.HttpClient.GetAsync("/"); StopServer(); - var logContents = File.ReadAllText(firstTempFile); + var logContents = Helpers.ReadAllTextFromFile(firstTempFile, Logger); + Assert.Contains("Switching debug log files to", logContents); AssertLogs(secondTempFile); @@ -202,7 +192,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests StopServer(); - var contents = File.ReadAllText(Helpers.GetExpectedLogName(deploymentResult, logFolderPath)); + var contents = Helpers.ReadAllTextFromFile(Helpers.GetExpectedLogName(deploymentResult, logFolderPath), Logger); Assert.Contains("彡⾔", contents); if (variant.HostingModel == HostingModel.InProcess) diff --git a/test/Common.FunctionalTests/Utilities/Helpers.cs b/test/Common.FunctionalTests/Utilities/Helpers.cs index 4918a12781..733030dbc8 100644 --- a/test/Common.FunctionalTests/Utilities/Helpers.cs +++ b/test/Common.FunctionalTests/Utilities/Helpers.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Net.Http; @@ -209,5 +210,29 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests Path.Combine(deploymentResult.DeploymentParameters.PublishedApplicationRootPath, "aspnetcore-debug.log"), "Running test allowed log file to be empty." + Environment.NewLine); } + + public static string ReadAllTextFromFile(string filename, ILogger logger) + { + try + { + return File.ReadAllText(filename); + } + catch (Exception ex) + { + // check if there is a dotnet.exe, iisexpress.exe, or w3wp.exe processes still running. + var hostingProcesses = Process.GetProcessesByName("dotnet") + .Concat(Process.GetProcessesByName("iisexpress")) + .Concat(Process.GetProcessesByName("w3wp")); + + logger.LogError($"Could not read file content. Exception message {ex.Message}"); + logger.LogError("Current hosting exes running:"); + + foreach (var hostingProcess in hostingProcesses) + { + logger.LogError($"{hostingProcess.ProcessName} pid: {hostingProcess.Id} hasExited: {hostingProcess.HasExited.ToString()}"); + } + throw ex; + } + } } } diff --git a/test/Common.FunctionalTests/Utilities/LogFileTestBase.cs b/test/Common.FunctionalTests/Utilities/LogFileTestBase.cs index 395ddfd40f..17885f3547 100644 --- a/test/Common.FunctionalTests/Utilities/LogFileTestBase.cs +++ b/test/Common.FunctionalTests/Utilities/LogFileTestBase.cs @@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.Server.IIS.FunctionalTests.Utilities public string GetLogFileContent(IISDeploymentResult deploymentResult) { - return File.ReadAllText(Helpers.GetExpectedLogName(deploymentResult, _logFolderPath)); + return Helpers.ReadAllTextFromFile(Helpers.GetExpectedLogName(deploymentResult, _logFolderPath), Logger); } } } diff --git a/test/IIS.Shared.FunctionalTests/Inprocess/StdOutRedirectionTests.cs b/test/IIS.Shared.FunctionalTests/Inprocess/StdOutRedirectionTests.cs index 9c89249d1e..35f10b13ab 100644 --- a/test/IIS.Shared.FunctionalTests/Inprocess/StdOutRedirectionTests.cs +++ b/test/IIS.Shared.FunctionalTests/Inprocess/StdOutRedirectionTests.cs @@ -61,7 +61,7 @@ namespace IIS.FunctionalTests.Inprocess StopServer(); - var contents = File.ReadAllText(Helpers.GetExpectedLogName(deploymentResult, _logFolderPath)); + var contents = Helpers.ReadAllTextFromFile(Helpers.GetExpectedLogName(deploymentResult, _logFolderPath), Logger); var expectedString = "The specified framework 'Microsoft.NETCore.App', version '2.9.9' was not found."; EventLogHelpers.VerifyEventLogEvent(deploymentResult, expectedString); Assert.Contains(expectedString, contents); @@ -88,7 +88,7 @@ namespace IIS.FunctionalTests.Inprocess StopServer(); var fileInDirectory = Directory.GetFiles(_logFolderPath).Single(); - var contents = File.ReadAllText(fileInDirectory); + var contents = Helpers.ReadAllTextFromFile(fileInDirectory, Logger); EventLogHelpers.VerifyEventLogEvent(deploymentResult, "Invoked hostfxr"); Assert.Contains("Invoked hostfxr", contents); } @@ -141,7 +141,7 @@ namespace IIS.FunctionalTests.Inprocess StopServer(); var fileInDirectory = Directory.GetFiles(_logFolderPath).First(); - var contents = File.ReadAllText(fileInDirectory); + var contents = Helpers.ReadAllTextFromFile(fileInDirectory, Logger); EventLogHelpers.VerifyEventLogEvent(deploymentResult, "Invoked hostfxr"); Assert.Contains("Invoked hostfxr", contents);