Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust S3 Plugin for HealthCheck Configuration #61

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('/'));
}
}
}