Skip to content

Commit

Permalink
test: to unix line endings
Browse files Browse the repository at this point in the history
  • Loading branch information
phnx47 committed Jun 15, 2024
1 parent eb5df43 commit 350fbef
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
14 changes: 14 additions & 0 deletions tests/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace Prometheus.Client.HealthChecks.Tests;

public static class Extensions
{
private const string _unix = "\n";
private const string _nonUnix = "\r\n";

public static string ToUnixLineEndings(this string s)
{
return Environment.NewLine == _unix ? s : s.Replace(_nonUnix, _unix);
}
}
9 changes: 2 additions & 7 deletions tests/Prometheus.Client.HealthChecks.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks Condition="'$(OS)' == 'Unix'">net6.0;net8.0</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Unix'">net462;net6.0;net8.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<InvariantGlobalization>true</InvariantGlobalization>
<NoWarn>$(NoWarn);CS0618</NoWarn>
</PropertyGroup>
<PropertyGroup Condition="'$(OS)' == 'Unix'">
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
</PropertyGroup>
<PropertyGroup Condition="'$(OS)' != 'Unix'">
<TargetFrameworks>net462;net6.0;net8.0</TargetFrameworks>
<DefineConstants>$(DefineConstants);Windows</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
Expand Down
35 changes: 17 additions & 18 deletions tests/PrometheusHealthCheckPublisherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class PrometheusHealthCheckPublisherTests
{
private readonly ICollectorRegistry _registry;
private readonly PrometheusHealthCheckPublisherOptions _options;
private PrometheusHealthCheckPublisher _publisher;

public PrometheusHealthCheckPublisherTests()
{
Expand All @@ -27,7 +28,7 @@ public PrometheusHealthCheckPublisherTests()
[Fact]
public void Publisher_Register_Metrics()
{
var publisher = new PrometheusHealthCheckPublisher(_options);
_publisher = new PrometheusHealthCheckPublisher(_options);
Assert.True(_registry.TryGet(PrometheusHealthCheckPublisherOptions.DefaultStatusMetricName, out _));
Assert.True(_registry.TryGet(PrometheusHealthCheckPublisherOptions.DefaultDurationMetricName, out _));
}
Expand All @@ -40,8 +41,7 @@ public void Publisher_Register_Metrics_With_CustomNames(string statusMetricName,
{
_options.StatusMetricName = statusMetricName;
_options.DurationMetricName = durationMetricName;

var publisher = new PrometheusHealthCheckPublisher(_options);
_publisher = new PrometheusHealthCheckPublisher(_options);
Assert.True(_registry.TryGet(statusMetricName, out _));
Assert.True(_registry.TryGet(durationMetricName, out _));
}
Expand All @@ -53,33 +53,32 @@ public void Publisher_Register_Metrics_With_CustomNames(string statusMetricName,
[InlineData("key", HealthStatus.Degraded, 1)]
public async Task Publisher_Publish_Correct_Result(string key, HealthStatus status, int durationSec)
{
const string tmpl = """
# HELP [[durationMetricName]] Shows duration of the health check execution in seconds
# TYPE [[durationMetricName]] gauge
[[durationMetricName]]{name="[[key]]"} [[duration]]
# HELP [[statusMetricName]] Shows raw health check status (0 = Unhealthy, 1 = Degraded, 2 = Healthy)
# TYPE [[statusMetricName]] gauge
[[statusMetricName]]{name="[[key]]"} [[status]]
var expected = """
# HELP [[durationMetricName]] Shows duration of the health check execution in seconds
# TYPE [[durationMetricName]] gauge
[[durationMetricName]]{name="[[key]]"} [[duration]]
# HELP [[statusMetricName]] Shows raw health check status (0 = Unhealthy, 1 = Degraded, 2 = Healthy)
# TYPE [[statusMetricName]] gauge
[[statusMetricName]]{name="[[key]]"} [[status]]
""";

""";
var expected = tmpl
expected = expected
.Replace("[[durationMetricName]]", PrometheusHealthCheckPublisherOptions.DefaultDurationMetricName)
.Replace("[[statusMetricName]]", PrometheusHealthCheckPublisherOptions.DefaultStatusMetricName)
.Replace("[[key]]", key)
.Replace("[[status]]", ((int)status).ToString())
.Replace("[[duration]]", durationSec.ToString());
.Replace("[[duration]]", durationSec.ToString())
.ToUnixLineEndings();

#if Windows
expected = expected.Replace("\r\n", "\n");
#endif
var publisher = new PrometheusHealthCheckPublisher(_options);
_publisher = new PrometheusHealthCheckPublisher(_options);

var entries = new Dictionary<string, HealthReportEntry>
{
{ key, new HealthReportEntry(status, string.Empty, TimeSpan.FromSeconds(durationSec), null, null) }
};
var report = new HealthReport(entries, TimeSpan.FromSeconds(1));
await publisher.PublishAsync(report, CancellationToken.None);
await _publisher.PublishAsync(report, CancellationToken.None);

using var stream = new MemoryStream();
await ScrapeHandler.ProcessAsync(_registry, stream);
Expand Down

0 comments on commit 350fbef

Please sign in to comment.