Skip to content

Commit

Permalink
Merge pull request #61 from lukas-kd/feature/s3_configuration_healthc…
Browse files Browse the repository at this point in the history
…heck

Adjust S3 Plugin for HealthCheck Configuration
  • Loading branch information
tst-sia authored Oct 22, 2024
2 parents cca75b3 + c3c2a78 commit 6e51c26
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
name: dotnet-pr
on:
workflow_dispatch:
pull_request:
pull_request_target:
branches: [ "main" ]
permissions:
contents: read
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public object ReadYaml(IParser parser, Type type)
public void WriteYaml(IEmitter emitter, object value, Type type)
{
var val = value!.GetType().GetProperty("Value")!.GetGetMethod()!
.Invoke(value, Array.Empty<object>())!
.Invoke(value, [])!
.ToString()!;
emitter.Emit(new Scalar(null, null, val, ScalarStyle.Plain, true, false));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Nullable>disable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
Expand Down
11 changes: 6 additions & 5 deletions PiBox.Plugins/Persistence/S3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ Configure your appsettings.yml with these properties

```yaml
s3:
Endpoint: ""
AccessKey: ""
SecretKey: ""
Region: ""
UseSsl: true
endpoint: ""
accessKey: ""
secretKey: ""
region: ""
useSsl: true
healthCheckPath: ""
```
## Usage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public class S3Configuration
public string SecretKey { get; set; } = "";
public string Region { get; set; } = "";
public bool UseSsl { get; set; }
public string HealthCheckPath { get; set; } = "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public void ConfigureHealthChecks(IHealthChecksBuilder healthChecksBuilder)
{
var urlScheme = _configuration.UseSsl ? "https" : "http";
var url = $"{urlScheme}://{_configuration.Endpoint}";
if (!string.IsNullOrEmpty(_configuration.HealthCheckPath))
url += $"/{_configuration.HealthCheckPath.TrimStart('/')}";
healthChecksBuilder.AddUrlGroup(new Uri(url), "s3", HealthStatus.Unhealthy, new[] { HealthCheckTag.Readiness.Value });
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using FluentAssertions;
using HealthChecks.Uris;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Minio;
using NSubstitute;
using NUnit.Framework;
using PiBox.Plugins.Persistence.Abstractions;
using PiBox.Testing;
using PiBox.Testing.Extensions;

namespace PiBox.Plugins.Persistence.S3.Tests
{
Expand Down Expand Up @@ -63,8 +65,38 @@ public void PluginConfiguresHealthChecks()
var healthChecksBuilder = Substitute.For<IHealthChecksBuilder>();
healthChecksBuilder.Services.Returns(new ServiceCollection());
_plugin.ConfigureHealthChecks(healthChecksBuilder);
AssertUriHealthCheck(healthChecksBuilder);
}

[Test]
public void PluginConfiguresHealthChecksWithPath()
{
var healthChecksBuilder = Substitute.For<IHealthChecksBuilder>();
healthChecksBuilder.Services.Returns(new ServiceCollection());
_configuration.HealthCheckPath = "/minio/health/live";
_plugin.ConfigureHealthChecks(healthChecksBuilder);
AssertUriHealthCheck(healthChecksBuilder);
}

private void AssertUriHealthCheck(IHealthChecksBuilder healthChecksBuilder)
{
healthChecksBuilder.Received(1)
.Add(Arg.Is<HealthCheckRegistration>(h => h.Name == "s3"));
var registration = healthChecksBuilder.ReceivedCalls().Last().GetArguments()![0] as HealthCheckRegistration;
registration.Should().NotBeNull();
var sp = new ServiceCollection()
.AddSingleton(Substitute.For<IHttpClientFactory>())
.BuildServiceProvider();
var healthCheck = registration!.Factory.Invoke(sp);
var uriHealthCheck = healthCheck.Should().BeOfType<UriHealthCheck>().Subject;
uriHealthCheck.Should().NotBeNull();
var options = uriHealthCheck.GetInaccessibleValue<UriHealthCheckOptions>("_options");
options.Should().NotBeNull();
var uriOptions = options.GetInaccessibleValue<List<UriOptions>>("UrisOptions");
uriOptions.Should().NotBeNull().And.HaveCount(1);
var uriOption = uriOptions.Single();
uriOption.Uri.Authority.Should().Be(_configuration.Endpoint);
uriOption.Uri.AbsolutePath.Should().Be("/" + _configuration.HealthCheckPath.TrimStart('/'));
}
}
}

0 comments on commit 6e51c26

Please sign in to comment.