예제 #1
0
        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;
        }
예제 #2
0
        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();
        }
예제 #3
0
        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);
        }
예제 #4
0
        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);
                }
            }
        }
예제 #5
0
        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;
        }
예제 #6
0
        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();
        }
예제 #7
0
        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);
        }
예제 #8
0
        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"));
        }
예제 #9
0
        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);
        }
예제 #10
0
        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));
        }
예제 #11
0
        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();
        }
예제 #12
0
        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);
        }
예제 #13
0
        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();
        }
예제 #14
0
        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}");
        }
예제 #16
0
        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
            };
        }
예제 #17
0
    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);
        }
    }
예제 #18
0
 public void ForAssembly_ReturnsSameInstanceForSameAssembly()
 {
     Assert.Same(
         AssemblyTestLog.ForAssembly(ThisAssembly),
         AssemblyTestLog.ForAssembly(ThisAssembly));
 }
예제 #19
0
        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));
                }
            }
        }
예제 #20
0
 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));
 }