public void AddHeapDumpActuator_AddsCorrectServices()
        {
            if (EndpointServiceCollectionExtensions.IsHeapDumpSupported())
            {
                var services    = new ServiceCollection();
                var appSettings = new Dictionary <string, string>()
                {
                    ["management:endpoints:enabled"]               = "false",
                    ["management:endpoints:path"]                  = "/cloudfoundryapplication",
                    ["management:endpoints:heapdump:enabled"]      = "false",
                    ["management:endpoints:heapdump:HeapDumpType"] = "Normal"
                };
                var configurationBuilder = new ConfigurationBuilder();
                configurationBuilder.AddInMemoryCollection(appSettings);
                var config = configurationBuilder.Build();

                services.AddHeapDumpActuator(config);

                var serviceProvider = services.BuildServiceProvider();
                var options         = serviceProvider.GetService <IHeapDumpOptions>();
                Assert.NotNull(options);
                Assert.Equal("Normal", options.HeapDumpType);
                var repo = serviceProvider.GetService <IHeapDumper>();
                Assert.NotNull(repo);
                var ep = serviceProvider.GetService <HeapDumpEndpoint>();
                Assert.NotNull(ep);
            }
        }
        public void AddHealthContributors_AddsServices()
        {
            ServiceCollection services = new ServiceCollection();

            EndpointServiceCollectionExtensions.AddHealthContributors(services, typeof(TestContributor));
            var serviceProvider = services.BuildServiceProvider();
            var contribs        = serviceProvider.GetServices <IHealthContributor>();

            Assert.NotNull(contribs);
            var contribsList = contribs.ToList();

            Assert.Single(contribsList);
        }
        public void AddEnvActuator_ThrowsOnNulls()
        {
            IServiceCollection services  = null;
            IServiceCollection services2 = new ServiceCollection();
            IConfigurationRoot config    = null;

            var ex = Assert.Throws <ArgumentNullException>(() => EndpointServiceCollectionExtensions.AddEnvActuator(services, config));

            Assert.Contains(nameof(services), ex.Message);
            var ex2 = Assert.Throws <ArgumentNullException>(() => EndpointServiceCollectionExtensions.AddEnvActuator(services2, config));

            Assert.Contains(nameof(config), ex2.Message);
        }
Пример #4
0
        public void AddCloudFoundryActuator_ThrowsOnNulls()
        {
            // Arrange
            IServiceCollection services  = null;
            IServiceCollection services2 = new ServiceCollection();
            IConfigurationRoot config    = null;

            // Act and Assert
            var ex = Assert.Throws <ArgumentNullException>(() => EndpointServiceCollectionExtensions.AddHypermediaActuator(services, config));

            Assert.Contains(nameof(services), ex.Message);
            var ex2 = Assert.Throws <ArgumentNullException>(() => EndpointServiceCollectionExtensions.AddHypermediaActuator(services2, config));

            Assert.Contains(nameof(config), ex2.Message);
        }
Пример #5
0
        public void AddHealthActuator_ThrowsOnNulls()
        {
            IServiceCollection services = new ServiceCollection();
            var config = new ConfigurationBuilder().Build();
            IHealthAggregator aggregator = null;

            var ex = Assert.Throws <ArgumentNullException>(() => EndpointServiceCollectionExtensions.AddHealthActuator(null));

            Assert.Equal("services", ex.ParamName);
            var ex2 = Assert.Throws <ArgumentNullException>(() => services.AddHealthActuator());

            Assert.Equal("config", ex2.ParamName);
            var ex3 = Assert.Throws <ArgumentNullException>(() => services.AddHealthActuator(config, aggregator));

            Assert.Contains(nameof(aggregator), ex3.Message);
        }
        public void AddHealthActuator_ThrowsOnNulls()
        {
            // Arrange
            IServiceCollection services2 = new ServiceCollection();
            var config2 = new ConfigurationBuilder().Build();
            IHealthAggregator aggregator = null;

            // Act and Assert
            var ex = Assert.Throws <ArgumentNullException>(() => EndpointServiceCollectionExtensions.AddHealthActuator(null));

            Assert.Contains("services", ex.Message);
            var ex2 = Assert.Throws <InvalidOperationException>(() => EndpointServiceCollectionExtensions.AddHealthActuator(services2));

            Assert.Equal("No service for type 'Microsoft.Extensions.Configuration.IConfiguration' has been registered.", ex2.Message);
            var ex3 = Assert.Throws <ArgumentNullException>(() => EndpointServiceCollectionExtensions.AddHealthActuator(services2, config2, aggregator));

            Assert.Contains(nameof(aggregator), ex3.Message);
        }
        public void AddHealthActuator_ThrowsOnNulls()
        {
            // Arrange
            IServiceCollection services   = null;
            IServiceCollection services2  = new ServiceCollection();
            IConfigurationRoot config     = null;
            IConfigurationRoot config2    = new ConfigurationBuilder().Build();
            IHealthAggregator  aggregator = null;

            // Act and Assert
            var ex = Assert.Throws <ArgumentNullException>(() => EndpointServiceCollectionExtensions.AddHealthActuator(services, config));

            Assert.Contains(nameof(services), ex.Message);
            var ex2 = Assert.Throws <ArgumentNullException>(() => EndpointServiceCollectionExtensions.AddHealthActuator(services2, config));

            Assert.Contains(nameof(config), ex2.Message);
            var ex3 = Assert.Throws <ArgumentNullException>(() => EndpointServiceCollectionExtensions.AddHealthActuator(services2, config2, aggregator));

            Assert.Contains(nameof(aggregator), ex3.Message);
        }
Пример #8
0
        public async Task HandleHeapDumpRequestAsync_ReturnsExpected()
        {
            if (EndpointServiceCollectionExtensions.IsHeapDumpSupported())
            {
                var opts  = new HeapDumpEndpointOptions();
                var mopts = new ActuatorManagementOptions();
                mopts.EndpointOptions.Add(opts);

                IServiceCollection serviceCollection = new ServiceCollection();
                serviceCollection.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Trace));
                var loggerFactory = serviceCollection.BuildServiceProvider().GetService <ILoggerFactory>();

                var logger1 = loggerFactory.CreateLogger <WindowsHeapDumper>();
                var logger2 = loggerFactory.CreateLogger <HeapDumpEndpoint>();
                var logger3 = loggerFactory.CreateLogger <HeapDumpEndpointMiddleware>();
                var logger4 = loggerFactory.CreateLogger <HeapDumper>();

                // WindowsHeapDumper should be used with .NET Core 3.1 on Windows. HeapDumper should be used with Linux and .NET 5 on Windows
                var obs = (Platform.IsWindows && RuntimeInformation.FrameworkDescription.StartsWith(".NET Core", StringComparison.InvariantCultureIgnoreCase))
                            ? new WindowsHeapDumper(opts, logger: logger1)
                            : !Platform.IsOSX
                                ? (IHeapDumper) new HeapDumper(opts, logger: logger4)
                                : throw new InvalidOperationException("Unsupported Platfornm");

                var ep      = new HeapDumpEndpoint(opts, obs, logger2);
                var middle  = new HeapDumpEndpointMiddleware(null, ep, mopts, logger3);
                var context = CreateRequest("GET", "/heapdump");
                await middle.HandleHeapDumpRequestAsync(context);

                context.Response.Body.Seek(0, SeekOrigin.Begin);
                var buffer = new byte[1024];
                await context.Response.Body.ReadAsync(buffer, 0, 1024);

                Assert.NotEqual(0, buffer[0]);
            }
            else
            {
                return;
            }
        }
Пример #9
0
        public async void HeapDumpActuator_ReturnsExpectedData()
        {
            if (EndpointServiceCollectionExtensions.IsHeapDumpSupported())
            {
                var builder = new WebHostBuilder()
                              .UseStartup <Startup>()
                              .ConfigureAppConfiguration((builderContext, config) => config.AddInMemoryCollection(AppSettings))
                              .ConfigureLogging((webhostContext, loggingBuilder) =>
                {
                    loggingBuilder.AddConfiguration(webhostContext.Configuration);
                    loggingBuilder.AddDynamicConsole();
                });
                using (var server = new TestServer(builder))
                {
                    var client = server.CreateClient();
                    var result = await client.GetAsync("http://localhost/cloudfoundryapplication/heapdump");

                    Assert.Equal(HttpStatusCode.OK, result.StatusCode);

                    Assert.True(result.Content.Headers.Contains("Content-Type"));
                    var contentType = result.Content.Headers.GetValues("Content-Type");
                    Assert.Equal("application/octet-stream", contentType.Single());
                    Assert.True(result.Content.Headers.Contains("Content-Disposition"));

                    var tempFile = Path.GetTempFileName();
                    var fs       = new FileStream(tempFile, FileMode.Create);
                    var input    = await result.Content.ReadAsStreamAsync();

                    await input.CopyToAsync(fs);

                    fs.Close();

                    var fs2 = File.Open(tempFile, FileMode.Open);
                    Assert.NotEqual(0, fs2.Length);
                    fs2.Close();
                    File.Delete(tempFile);
                }
            }
        }
Пример #10
0
        public async void HandleHeapDumpRequestAsync_ReturnsExpected()
        {
            if (EndpointServiceCollectionExtensions.IsHeapDumpSupported())
            {
                var opts  = new HeapDumpEndpointOptions();
                var mopts = new ActuatorManagementOptions();
                mopts.EndpointOptions.Add(opts);

                IServiceCollection serviceCollection = new ServiceCollection();
                serviceCollection.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Trace));
                var loggerFactory = serviceCollection.BuildServiceProvider().GetService <ILoggerFactory>();

                var logger1 = loggerFactory.CreateLogger <WindowsHeapDumper>();
                var logger2 = loggerFactory.CreateLogger <HeapDumpEndpoint>();
                var logger3 = loggerFactory.CreateLogger <HeapDumpEndpointMiddleware>();
                var logger4 = loggerFactory.CreateLogger <LinuxHeapDumper>();

                var obs = Platform.IsWindows ? (IHeapDumper) new WindowsHeapDumper(opts, logger: logger1)
                                : Platform.IsLinux ? (IHeapDumper) new LinuxHeapDumper(opts, logger: logger4)
                                : throw new InvalidOperationException("Unsupported Platfornm");

                var ep      = new HeapDumpEndpoint(opts, obs, logger2);
                var middle  = new HeapDumpEndpointMiddleware(null, ep, mopts, logger3);
                var context = CreateRequest("GET", "/heapdump");
                await middle.HandleHeapDumpRequestAsync(context);

                context.Response.Body.Seek(0, SeekOrigin.Begin);
                var buffer = new byte[1024];
                await context.Response.Body.ReadAsync(buffer, 0, 1024);

                Assert.NotEqual(0, buffer[0]);
            }
            else
            {
                return;
            }
        }