Skip to content

Commit

Permalink
Fix Loggers, Metrics and Hypermedia when Manangement:Endpoint:Path is…
Browse files Browse the repository at this point in the history
… set to / (#470)
  • Loading branch information
Hananiel Sarella authored Oct 16, 2020
1 parent 39abe43 commit 0f5750c
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public Links Invoke(string baseUrl)
{
if (!string.IsNullOrEmpty(opt.Id) && !links._links.ContainsKey(opt.Id))
{
links._links.Add(opt.Id, new Link(baseUrl + "/" + opt.Path));
var linkPath = $"{baseUrl.TrimEnd('/')}/{opt.Path}";
links._links.Add(opt.Id, new Link(linkPath));
}
else if (links._links.ContainsKey(opt.Id))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected internal async Task HandleLoggersRequestAsync(HttpContext context)
}
else
{
paths.AddRange(_mgmtOptions.Select(opt => $"{opt.Path}/{_endpoint.Path}"));
paths.AddRange(_mgmtOptions.Select(opt => $"{opt.Path}/{_endpoint.Path}".Replace("//", "/")));
}

foreach (var path in paths)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected internal string GetMetricName(HttpRequest request)
return GetMetricName(request, _endpoint.Path);
}

var paths = new List<string>(_mgmtOptions.Select(opt => $"{opt.Path}/{_endpoint.Id}"));
var paths = new List<string>(_mgmtOptions.Select(opt => $"{opt.Path}/{_endpoint.Id}".Replace("//", "/")));
foreach (var path in paths)
{
var metricName = GetMetricName(request, path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,28 @@ public async void HypermediaEndpointMiddleware_ServiceContractNotBroken()
Assert.Equal("{\"type\":\"steeltoe\",\"_links\":{\"info\":{\"href\":\"http://localhost/actuator/info\",\"templated\":false},\"self\":{\"href\":\"http://localhost/actuator\",\"templated\":false}}}", json);
}

[Fact]
public async void HypermediaEndpointMiddleware_Returns_Expected_When_ManagementPath_Is_Slash()
{
var settings = new Dictionary<string, string>(appSettings);
appSettings.Add("Management:Endpoints:Path", "/");

// arrange a server and client
var builder = new WebHostBuilder()
.UseStartup<Startup>()
.ConfigureAppConfiguration((builderContext, config) => config.AddInMemoryCollection(appSettings));

using var server = new TestServer(builder);
var client = server.CreateClient();

// send the request
var result = await client.GetAsync("http://localhost/");
var json = await result.Content.ReadAsStringAsync();

// assert
Assert.Equal("{\"type\":\"steeltoe\",\"_links\":{\"info\":{\"href\":\"http://localhost/info\",\"templated\":false},\"self\":{\"href\":\"http://localhost/\",\"templated\":false}}}", json);
}

[Fact]
public void ActuatorHypermediaEndpointMiddleware_PathAndVerbMatching_ReturnsExpected()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,34 @@ public async void LoggersActuator_AcceptsPost()
Assert.Equal("ERROR", parsedObject.loggers.Default.effectiveLevel.ToString());
}

[Fact]
public async void LoggersActuator_AcceptsPost_When_ManagementPath_Is_Slash()
{
var appSettings = new Dictionary<string, string>(AppSettings);
appSettings["management:endpoints:path"] = "/";
appSettings.Add("Management:Endpoints:Actuator:Exposure:Include:0", "*");

var builder = new WebHostBuilder()
.UseStartup<Startup>()
.ConfigureAppConfiguration((builderContext, config) => config.AddInMemoryCollection(appSettings))
.ConfigureLogging((context, loggingBuilder) =>
{
loggingBuilder.AddConfiguration(context.Configuration.GetSection("Logging"));
loggingBuilder.AddDynamicConsole();
});

using var server = new TestServer(builder);
var client = server.CreateClient();
HttpContent content = new StringContent("{\"configuredLevel\":\"ERROR\"}");
var changeResult = await client.PostAsync("http://localhost/loggers/Default", content);
Assert.Equal(HttpStatusCode.OK, changeResult.StatusCode);

var validationResult = await client.GetAsync("http://localhost/loggers");
var json = await validationResult.Content.ReadAsStringAsync();
dynamic parsedObject = JsonConvert.DeserializeObject(json);
Assert.Equal("ERROR", parsedObject.loggers.Default.effectiveLevel.ToString());
}

[Fact]
public async void LoggersActuator_UpdateNameSpace_UpdatesChildren()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
using OpenCensus.Stats.Measures;
using OpenCensus.Tags;
using Steeltoe.Management.Census.Stats;
using Steeltoe.Management.Endpoint.Hypermedia;
using Steeltoe.Management.Endpoint.Test;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Xunit;

namespace Steeltoe.Management.Endpoint.Metrics.Test
Expand Down Expand Up @@ -88,6 +90,30 @@ public void GetMetricName_ReturnsExpected()
Assert.Null(middle.GetMetricName(context3.Request));
}

[Fact]
public void GetMetricName_ReturnsExpected_When_ManagementPath_Is_Slash()
{
var opts = new MetricsEndpointOptions();

var mopts = TestHelper.GetManagementOptions(opts);
var stats = new OpenCensusStats();

var amOpts = mopts.Where(m => m.GetType() == typeof(ActuatorManagementOptions)).FirstOrDefault() as ActuatorManagementOptions;
amOpts.Path = "/";

var ep = new MetricsEndpoint(opts, stats);
var middle = new MetricsEndpointMiddleware(null, ep, mopts);

var context1 = CreateRequest("GET", "/metrics");
Assert.Null(middle.GetMetricName(context1.Request));

var context2 = CreateRequest("GET", "/metrics/Foo.Bar.Class");
Assert.Equal("Foo.Bar.Class", middle.GetMetricName(context2.Request));

var context3 = CreateRequest("GET", "/metrics", "?tag=key:value&tag=key1:value1");
Assert.Null(middle.GetMetricName(context3.Request));
}

[Fact]
public async void HandleMetricsRequestAsync_GetMetricsNames_ReturnsExpected()
{
Expand Down

0 comments on commit 0f5750c

Please sign in to comment.