public IISTestSiteFixture() { var logging = AssemblyTestLog.ForAssembly(typeof(IISTestSiteFixture).Assembly); var deploymentParameters = new IISDeploymentParameters(Helpers.GetInProcessTestSitesPath(), DeployerSelector.ServerType, RuntimeFlavor.CoreClr, RuntimeArchitecture.x64) { TargetFramework = Tfm.NetCoreApp22, AncmVersion = AncmVersion.AspNetCoreModuleV2, HostingModel = HostingModel.InProcess, PublishApplicationBeforeDeployment = true, }; _forwardingProvider = new ForwardingProvider(); var loggerFactory = logging.CreateLoggerFactory(null, nameof(IISTestSiteFixture)); loggerFactory.AddProvider(_forwardingProvider); _deployer = IISApplicationDeployerFactory.Create(deploymentParameters, loggerFactory); DeploymentResult = _deployer.DeployAsync().Result; Client = DeploymentResult.HttpClient; BaseUri = DeploymentResult.ApplicationBaseUri; ShutdownToken = DeploymentResult.HostShutdownToken; }
public ServerFixture() { var testLog = AssemblyTestLog.ForAssembly(typeof(ServerFixture <TStartup>).Assembly); _logToken = testLog.StartTestLog(null, $"{nameof(ServerFixture<TStartup>)}_{typeof(TStartup).Name}", out _loggerFactory, "ServerFixture"); _logger = _loggerFactory.CreateLogger <ServerFixture <TStartup> >(); StartServer(); }
public ServerFixture() { var testLog = AssemblyTestLog.ForAssembly(typeof(ServerFixture <TStartup>).Assembly); _logToken = testLog.StartTestLog(null, $"{nameof(ServerFixture<TStartup>)}_{typeof(TStartup).Name}", out _loggerFactory, "ServerFixture"); _logger = _loggerFactory.CreateLogger <ServerFixture <TStartup> >(); Url = "http://localhost:" + GetNextPort(); StartServer(Url); }
public void TestLogWritesToGlobalLogFile() { // Because this test writes to a file, it is a functional test and should be logged // but it's also testing the test logging facility. So this is pretty meta ;) var tempDir = Path.Combine(Path.GetTempPath(), $"TestLogging_{Guid.NewGuid().ToString("N")}"); try { using (StartLog(out var loggerFactory)) { var logger = loggerFactory.CreateLogger("Test"); using (var testAssemblyLog = AssemblyTestLog.Create("FakeTestAssembly", tempDir)) { logger.LogInformation("Created test log in {baseDirectory}", tempDir); using (testAssemblyLog.StartTestLog(output: null, className: "FakeTestAssembly.FakeTestClass", loggerFactory: out var testLoggerFactory, minLogLevel: LogLevel.Trace, testName: "FakeTestName")) { var testLogger = testLoggerFactory.CreateLogger("TestLogger"); testLogger.LogInformation("Information!"); testLogger.LogTrace("Trace!"); } } logger.LogInformation("Finished test log in {baseDirectory}", tempDir); } var globalLogPath = Path.Combine(tempDir, "FakeTestAssembly", RuntimeInformation.FrameworkDescription.TrimStart('.'), "global.log"); var testLog = Path.Combine(tempDir, "FakeTestAssembly", RuntimeInformation.FrameworkDescription.TrimStart('.'), "FakeTestClass", $"FakeTestName.log"); Assert.True(File.Exists(globalLogPath), $"Expected global log file {globalLogPath} to exist"); Assert.True(File.Exists(testLog), $"Expected test log file {testLog} to exist"); var globalLogContent = MakeConsistent(File.ReadAllText(globalLogPath)); var testLogContent = MakeConsistent(File.ReadAllText(testLog)); Assert.Equal(@"[GlobalTestLog] [Information] Global Test Logging initialized. Set the 'ASPNETCORE_TEST_LOG_DIR' Environment Variable in order to create log files on disk. [GlobalTestLog] [Information] Starting test ""FakeTestName"" [GlobalTestLog] [Information] Finished test ""FakeTestName"" in DURATION ", globalLogContent, ignoreLineEndingDifferences: true); Assert.Equal(@"[TestLifetime] [Information] Starting test ""FakeTestName"" [TestLogger] [Information] Information! [TestLogger] [Verbose] Trace! [TestLifetime] [Information] Finished test ""FakeTestName"" in DURATION ", testLogContent, ignoreLineEndingDifferences: true); } finally { if (Directory.Exists(tempDir)) { Directory.Delete(tempDir, recursive: true); } } }
public IISTestSiteFixture(Action <IISDeploymentParameters> configure) { var logging = AssemblyTestLog.ForAssembly(typeof(IISTestSiteFixture).Assembly); _loggerFactory = logging.CreateLoggerFactory(null, nameof(IISTestSiteFixture)); _forwardingProvider = new ForwardingProvider(); _loggerFactory.AddProvider(_forwardingProvider); _configure = configure; }
public ErrorPageTests( MvcTestFixture <ErrorPageMiddlewareWebSite.Startup> fixture, ITestOutputHelper testOutputHelper) { _assemblyTestLog = AssemblyTestLog.ForAssembly(GetType().Assembly); var loggerProvider = _assemblyTestLog.CreateLoggerFactory(testOutputHelper, GetType().Name); var factory = fixture.Factories.FirstOrDefault() ?? fixture.WithWebHostBuilder(b => b.UseStartup <ErrorPageMiddlewareWebSite.Startup>()); Client = factory .WithWebHostBuilder(builder => builder.ConfigureLogging(l => l.Services.AddSingleton <ILoggerFactory>(loggerProvider))) .CreateDefaultClient(); }
public ServerFixture() { _asyncLoggerProvider = new AsyncForwardingLoggerProvider(); var testLog = AssemblyTestLog.ForAssembly(typeof(TStartup).Assembly); _logToken = testLog.StartTestLog(null, $"{nameof(ServerFixture<TStartup>)}_{typeof(TStartup).Name}", out _loggerFactory, "ServerFixture"); _loggerFactory.AddProvider(_asyncLoggerProvider); _logger = _loggerFactory.CreateLogger <ServerFixture <TStartup> >(); // We're using 127.0.0.1 instead of localhost to ensure that we use IPV4 across different OSes Url = "http://127.0.0.1:" + GetNextPort(); StartServer(Url); }
public ErrorPageTests( MvcTestFixture <ErrorPageMiddlewareWebSite.Startup> fixture, ITestOutputHelper testOutputHelper) { _assemblyTestLog = AssemblyTestLog.ForAssembly(GetType().Assembly); var loggerProvider = _assemblyTestLog.CreateLoggerFactory(testOutputHelper, GetType().Name); var factory = fixture.Factories.FirstOrDefault() ?? fixture.WithWebHostBuilder(b => b.UseStartup <ErrorPageMiddlewareWebSite.Startup>()); Client = factory .WithWebHostBuilder(builder => builder.ConfigureLogging(l => l.Services.AddSingleton <ILoggerFactory>(loggerProvider))) .CreateDefaultClient(); // These tests want to verify runtime compilation and formatting in the HTML of the error page Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html")); }
public RedisServerFixture() { // Docker is not available on the machine, tests using this fixture // should be using SkipIfDockerNotPresentAttribute and will be skipped. if (Docker.Default == null) { return; } var testLog = AssemblyTestLog.ForAssembly(typeof(RedisServerFixture <TStartup>).Assembly); _logToken = testLog.StartTestLog(null, $"{nameof(RedisServerFixture<TStartup>)}_{typeof(TStartup).Name}", out _loggerFactory, LogLevel.Trace, "RedisServerFixture"); _logger = _loggerFactory.CreateLogger <RedisServerFixture <TStartup> >(); Docker.Default.Start(_logger); }
public void TestLogWritesToITestOutputHelper() { var output = new TestTestOutputHelper(); var assemblyLog = AssemblyTestLog.Create("NonExistant.Test.Assembly", baseDirectory: null); using (assemblyLog.StartTestLog(output, "NonExistant.Test.Class", out var loggerFactory)) { var logger = loggerFactory.CreateLogger("TestLogger"); logger.LogInformation("Information!"); } Assert.Equal(@"| TestLifetime Information: Starting test TestLogWritesToITestOutputHelper | TestLogger Information: Information! | TestLifetime Information: Finished test TestLogWritesToITestOutputHelper in DURATION ", MakeConsistent(output.Output)); }
public RabbitMQServerFixture() { if (Docker.Default == null) { return; } var testLog = AssemblyTestLog.ForAssembly(typeof(RabbitMQServerFixture <TStartup>).Assembly); _logToken = testLog.StartTestLog(null, $"{nameof(RabbitMQServerFixture<TStartup>)}_{typeof(TStartup).Name}", out _loggerFactory, LogLevel.Trace, "RabbitMQServerFixture"); _logger = _loggerFactory.CreateLogger <RabbitMQServerFixture <TStartup> >(); Docker.Default.Start(_logger); FirstServer = StartServer(); SecondServer = StartServer(); }
public void TestLogWritesToITestOutputHelper() { var output = new TestTestOutputHelper(); var assemblyLog = AssemblyTestLog.Create("NonExistant.Test.Assembly", baseDirectory: null); using (assemblyLog.StartTestLog(output, "NonExistant.Test.Class", out var loggerFactory)) { var logger = loggerFactory.CreateLogger("TestLogger"); logger.LogInformation("Information!"); // Trace is disabled by default logger.LogTrace("Trace!"); } Assert.Equal(@"[TIMESTAMP] TestLifetime Information: Starting test TestLogWritesToITestOutputHelper [TIMESTAMP] TestLogger Information: Information! [TIMESTAMP] TestLifetime Information: Finished test TestLogWritesToITestOutputHelper in DURATION ", MakeConsistent(output.Output), ignoreLineEndingDifferences: true); }
public ServerFixture(ILoggerFactory loggerFactory) { _logSinkProvider = new LogSinkProvider(); if (loggerFactory == null) { var testLog = AssemblyTestLog.ForAssembly(typeof(TStartup).Assembly); _logToken = testLog.StartTestLog(null, $"{nameof(ServerFixture<TStartup>)}_{typeof(TStartup).Name}", out _loggerFactory, "ServerFixture"); } else { _loggerFactory = loggerFactory; } _logger = _loggerFactory.CreateLogger <ServerFixture <TStartup> >(); StartServer(); }
public void TestLogWritesToGlobalLogFile() { // Because this test writes to a file, it is a functional test and should be logged // but it's also testing the test logging facility. So this is pretty meta ;) using (StartLog(out var loggerFactory)) { var logger = loggerFactory.CreateLogger("Test"); var tempDir = Path.Combine(Path.GetTempPath(), $"TestLogging_{Guid.NewGuid().ToString("N")}"); using (var testAssemblyLog = AssemblyTestLog.Create("FakeTestAssembly", tempDir)) { logger.LogInformation("Created test log in {baseDirectory}", tempDir); using (testAssemblyLog.StartTestLog(output: null, className: "FakeTestAssembly.FakeTestClass", loggerFactory: out var testLoggerFactory, testName: "FakeTestName")) { var testLogger = testLoggerFactory.CreateLogger("TestLogger"); testLogger.LogInformation("Information!"); } } logger.LogInformation("Finished test log in {baseDirectory}", tempDir); var globalLogPath = Path.Combine(tempDir, "FakeTestAssembly", "global.log"); var testLog = Path.Combine(tempDir, "FakeTestAssembly", "FakeTestClass", $"FakeTestName.log"); Assert.True(File.Exists(globalLogPath), $"Expected global log file {globalLogPath} to exist"); Assert.True(File.Exists(testLog), $"Expected test log file {testLog} to exist"); var globalLogContent = MakeConsistent(File.ReadAllText(globalLogPath)); logger.LogInformation($"Global Log Content:{Environment.NewLine}{{content}}", globalLogContent); var testLogContent = MakeConsistent(File.ReadAllText(testLog)); logger.LogInformation($"Test Log Content:{Environment.NewLine}{{content}}", testLogContent); Assert.Equal(@"[GlobalTestLog] [Information] Global Test Logging initialized. Set the 'ASPNETCORE_TEST_LOG_DIR' Environment Variable in order to create log files on disk. [GlobalTestLog] [Information] Starting test ""FakeTestName"" [GlobalTestLog] [Information] Finished test ""FakeTestName"" in DURATION ", globalLogContent); Assert.Equal(@"[TestLifetime] [Information] Starting test ""FakeTestName"" [TestLogger] [Information] Information! [TestLifetime] [Information] Finished test ""FakeTestName"" in DURATION ", testLogContent); } }
public StoreSetupFixture() { if (!Store.IsEnabled()) { return; } var loggerName = nameof(StoreSetupFixture); var testLog = AssemblyTestLog.ForAssembly(typeof(StoreSetupFixture).Assembly); ILoggerFactory loggerFactory; _logToken = testLog.StartTestLog(null, loggerName, out loggerFactory, testName: loggerName); _logger = loggerFactory.CreateLogger <StoreSetupFixture>(); _store = new Store(loggerFactory); StoreDirectory = _store.CreateStore(); _logger.LogInformation($"Store was setup at {StoreDirectory}"); }
internal IISTestSiteFixture(Action <IISDeploymentParameters> configure) { var logging = AssemblyTestLog.ForAssembly(typeof(IISTestSiteFixture).Assembly); _loggerFactory = logging.CreateLoggerFactory(null, nameof(IISTestSiteFixture)); _forwardingProvider = new ForwardingProvider(); _loggerFactory.AddProvider(_forwardingProvider); _configure = configure; DeploymentParameters = new IISDeploymentParameters() { RuntimeArchitecture = RuntimeArchitecture.x64, RuntimeFlavor = RuntimeFlavor.CoreClr, TargetFramework = Tfm.Default, HostingModel = HostingModel.InProcess, PublishApplicationBeforeDeployment = true, ApplicationPublisher = new PublishedApplicationPublisher(Helpers.GetInProcessTestSitesName()), ServerType = DeployerSelector.ServerType }; }
public virtual void Initialize(TestContext context, MethodInfo methodInfo, object[] testMethodArguments, ITestOutputHelper testOutputHelper) { try { TestOutputHelper = testOutputHelper; var classType = GetType(); var logLevelAttribute = methodInfo.GetCustomAttribute <LogLevelAttribute>() ?? methodInfo.DeclaringType.GetCustomAttribute <LogLevelAttribute>() ?? methodInfo.DeclaringType.Assembly.GetCustomAttribute <LogLevelAttribute>(); // internal for testing ResolvedTestClassName = context.FileOutput.TestClassName; _testLog = AssemblyTestLog .ForAssembly(classType.GetTypeInfo().Assembly) .StartTestLog( TestOutputHelper, context.FileOutput.TestClassName, out var loggerFactory, logLevelAttribute?.LogLevel ?? LogLevel.Debug, out var resolvedTestName, out var logDirectory, context.FileOutput.TestName); ResolvedLogOutputDirectory = logDirectory; ResolvedTestMethodName = resolvedTestName; LoggerFactory = loggerFactory; Logger = loggerFactory.CreateLogger(classType); } catch (Exception e) { _initializationException = ExceptionDispatchInfo.Capture(e); } }
public void ForAssembly_ReturnsSameInstanceForSameAssembly() { Assert.Same( AssemblyTestLog.ForAssembly(ThisAssembly), AssemblyTestLog.ForAssembly(ThisAssembly)); }
public async Task GrpcTemplate() { // Setup AssemblyTestLog var assemblyLog = AssemblyTestLog.Create(Assembly.GetExecutingAssembly(), baseDirectory: Project.ArtifactsLogDir); using var testLog = assemblyLog.StartTestLog(Output, nameof(GrpcTemplateTest), out var loggerFactory); var logger = loggerFactory.CreateLogger("TestLogger"); Project = await ProjectFactory.GetOrCreateProject("grpc", Output); var createResult = await Project.RunDotNetNewAsync("grpc"); Assert.True(0 == createResult.ExitCode, ErrorMessages.GetFailedProcessMessage("create/restore", Project, createResult)); var publishResult = await Project.RunDotNetPublishAsync(); Assert.True(0 == publishResult.ExitCode, ErrorMessages.GetFailedProcessMessage("publish", Project, publishResult)); var buildResult = await Project.RunDotNetBuildAsync(); Assert.True(0 == buildResult.ExitCode, ErrorMessages.GetFailedProcessMessage("build", Project, buildResult)); var isOsx = RuntimeInformation.IsOSPlatform(OSPlatform.OSX); var isWindowsOld = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Environment.OSVersion.Version < new Version(6, 2); var unsupported = isOsx || isWindowsOld; using (var serverProcess = Project.StartBuiltProjectAsync(hasListeningUri: !unsupported, logger: logger)) { // These templates are HTTPS + HTTP/2 only which is not supported on Mac due to missing ALPN support. // https://github.com/dotnet/aspnetcore/issues/11061 if (isOsx) { serverProcess.Process.WaitForExit(assertSuccess: false); Assert.True(serverProcess.Process.HasExited, "built"); Assert.Contains("System.NotSupportedException: HTTP/2 over TLS is not supported on macOS due to missing ALPN support.", ErrorMessages.GetFailedProcessMessageOrEmpty("Run built service", Project, serverProcess.Process)); } else if (isWindowsOld) { serverProcess.Process.WaitForExit(assertSuccess: false); Assert.True(serverProcess.Process.HasExited, "built"); Assert.Contains("System.NotSupportedException: HTTP/2 over TLS is not supported on Windows 7 due to missing ALPN support.", ErrorMessages.GetFailedProcessMessageOrEmpty("Run built service", Project, serverProcess.Process)); } else { Assert.False( serverProcess.Process.HasExited, ErrorMessages.GetFailedProcessMessageOrEmpty("Run built service", Project, serverProcess.Process)); } } using (var aspNetProcess = Project.StartPublishedProjectAsync(hasListeningUri: !unsupported)) { // These templates are HTTPS + HTTP/2 only which is not supported on Mac due to missing ALPN support. // https://github.com/dotnet/aspnetcore/issues/11061 if (isOsx) { aspNetProcess.Process.WaitForExit(assertSuccess: false); Assert.True(aspNetProcess.Process.HasExited, "published"); Assert.Contains("System.NotSupportedException: HTTP/2 over TLS is not supported on macOS due to missing ALPN support.", ErrorMessages.GetFailedProcessMessageOrEmpty("Run published service", Project, aspNetProcess.Process)); } else if (isWindowsOld) { aspNetProcess.Process.WaitForExit(assertSuccess: false); Assert.True(aspNetProcess.Process.HasExited, "published"); Assert.Contains("System.NotSupportedException: HTTP/2 over TLS is not supported on Windows 7 due to missing ALPN support.", ErrorMessages.GetFailedProcessMessageOrEmpty("Run published service", Project, aspNetProcess.Process)); } else { Assert.False( aspNetProcess.Process.HasExited, ErrorMessages.GetFailedProcessMessageOrEmpty("Run published service", Project, aspNetProcess.Process)); } } }
public IDisposable StartLog(out ILoggerFactory loggerFactory, LogLevel minLogLevel, [CallerMemberName] string testName = null) { return(AssemblyTestLog.ForAssembly(GetType().GetTypeInfo().Assembly).StartTestLog(TestOutputHelper, GetType().FullName, out loggerFactory, minLogLevel, testName)); }