Repository navigation
Capture KurrentDB version during service registration #769
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
HealthMonitoring/HealthMonitoring.Tests/Monitoring/KurrentDbVersionTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| using System.Net; | ||
| using System.Net.Http; | ||
| using HealthMonitoring.Domain; | ||
| using HealthMonitoring.Monitoring; | ||
| using Microsoft.Extensions.Logging.Abstractions; | ||
|
|
||
| namespace HealthMonitoring.Tests.Monitoring; | ||
|
|
||
| public sealed class KurrentDbVersionTests | ||
| { | ||
| [Theory] | ||
| [InlineData("25.0.0.1673-build.1", "KurrentDB 25.0.0")] | ||
| [InlineData("25.1", "KurrentDB 25.1")] | ||
| public void Formats_kurrentdb_versions(string rawVersion, string expected) | ||
| { | ||
| Assert.Equal(expected, KurrentDbVersionFormatter.Format(rawVersion)); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(null)] | ||
| [InlineData("")] | ||
| [InlineData("not-a-version")] | ||
| public void Returns_null_for_an_unusable_kurrentdb_version(string? rawVersion) | ||
| { | ||
| Assert.Null(KurrentDbVersionFormatter.Format(rawVersion)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Reads_and_formats_the_info_endpoint_version() | ||
| { | ||
| var handler = new StubHandler(HttpStatusCode.OK, "{\"esVersion\":\"25.0.0.1673-build.1\"}"); | ||
| var probe = new KurrentDbVersionProbe(new StubHttpClientFactory(handler)); | ||
| var service = new MonitoredService | ||
| { | ||
| ConnectionString = "esdb://admin:changeit@localhost:2113?tls=false", | ||
| RequestTimeout = TimeSpan.FromSeconds(5) | ||
| }; | ||
|
|
||
| var version = await probe.GetVersionAsync(service, CancellationToken.None); | ||
|
|
||
| Assert.Equal("25.0.0.1673-build.1", version); | ||
| Assert.Equal("http://localhost:2113/info", handler.Request!.RequestUri!.ToString()); | ||
| Assert.Equal("Basic YWRtaW46Y2hhbmdlaXQ=", handler.Request.Headers.Authorization!.ToString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Registration_version_resolution_preserves_fallback_when_info_is_unavailable() | ||
| { | ||
| var service = new MonitoredService { MonitorType = MonitorType.KurrentDb }; | ||
| var probe = new FailingVersionProbe(); | ||
| var resolver = new KurrentDbRegistrationVersionResolver(probe, NullLogger<KurrentDbRegistrationVersionResolver>.Instance); | ||
|
|
||
| var version = await resolver.ResolveAsync(service, "KurrentDB 24.0.0", CancellationToken.None); | ||
|
|
||
| Assert.Equal("KurrentDB 24.0.0", version); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Registration_version_resolution_returns_a_human_readable_version() | ||
| { | ||
| var service = new MonitoredService { MonitorType = MonitorType.KurrentDb }; | ||
| var resolver = new KurrentDbRegistrationVersionResolver(new VersionProbe("25.0.0.1673-build.1"), NullLogger<KurrentDbRegistrationVersionResolver>.Instance); | ||
|
|
||
| var version = await resolver.ResolveAsync(service, "reported-by-caller", CancellationToken.None); | ||
|
|
||
| Assert.Equal("KurrentDB 25.0.0", version); | ||
| } | ||
|
|
||
| private sealed class VersionProbe(string version) : IKurrentDbVersionProbe | ||
| { | ||
| public Task<string?> GetVersionAsync(MonitoredService service, CancellationToken cancellationToken) => Task.FromResult<string?>(version); | ||
| } | ||
|
|
||
| private sealed class FailingVersionProbe : IKurrentDbVersionProbe | ||
| { | ||
| public Task<string?> GetVersionAsync(MonitoredService service, CancellationToken cancellationToken) => | ||
| Task.FromException<string?>(new HttpRequestException("info endpoint unavailable")); | ||
| } | ||
|
|
||
| private sealed class StubHandler(HttpStatusCode status, string body) : HttpMessageHandler | ||
| { | ||
| public HttpRequestMessage? Request { get; private set; } | ||
|
|
||
| protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
| { | ||
| Request = request; | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
| return Task.FromResult(new HttpResponseMessage(status) { Content = new StringContent(body) }); | ||
| } | ||
| } | ||
|
|
||
| private sealed class StubHttpClientFactory(HttpMessageHandler handler) : IHttpClientFactory | ||
| { | ||
| public HttpClient CreateClient(string name) | ||
| { | ||
| _ = name; | ||
| return new(handler, disposeHandler: false); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a TLS KurrentDB connection relies on
tlsVerifyCert=falsein its connection string orIgnoreCertificateErrorsto accept a self-signed/internal certificate, the KurrentDB client can connect successfully, but this defaultHttpClientstill performs normal certificate validation. The/inforequest therefore fails and the resolver falls back, leaving a newly registered service without an automatically captured version despite its connection being valid; configure the HTTP handler consistently with the service's certificate-verification settings.Useful? React with 👍 / 👎.