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);
            }
        }
Esempio n. 2
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;
            }
        }
Esempio n. 3
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);
                }
            }
        }
        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;
            }
        }