Tells apart a slave that is down from one that does not know us
The integrity check mapped four outcomes onto a single null: no response, a 404, a rejected key, and a genuine answer. Only "rejected key" is recoverable, and it was being reported as "unreachable" and never repaired — so an instance registered against the wrong URL stayed broken until someone edited the database by hand. That is exactly what happened locally. GetRegisteredMasterUrlAsync now returns an outcome alongside the URL. Unauthorized triggers registration; 404 is reported as "this host does not serve the master/slave protocol", which names the actual mistake instead of hiding it behind a generic contact failure; unreachable and server errors behave as before. Registering on a rejected key cannot hijack a slave that belongs to another master: the slave accepts a registration only when it has none, and refuses any key that does not match an existing one. So it succeeds exactly in the case worth recovering and fails harmlessly otherwise. That guarantee lives on the slave, so the test asserting the refusal now says out loud that the master depends on it. Found while diagnosing a status push that failed against a frontend URL. Small and contained, so fixed here rather than filed as tech debt. 372 tests pass, up from 366. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HHoJpxYXzHACSQguHrC5fw
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using FluentAssertions;
|
||||
using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
@@ -258,7 +258,7 @@ public class CmsInstanceServiceTests
|
||||
var instance = ActiveInstance();
|
||||
_repo.GetActiveAsync().Returns([instance]);
|
||||
_protector.Unprotect("encrypted-key").Returns("plain");
|
||||
_slaveClient.GetRegisteredMasterUrlAsync(Arg.Any<string>(), Arg.Any<string>()).Returns((string?)null);
|
||||
_slaveClient.GetRegisteredMasterUrlAsync(Arg.Any<string>(), Arg.Any<string>()).Returns(RegisteredMasterUrlResult.Unreachable);
|
||||
_httpContextAccessor.HttpContext.Returns((HttpContext?)null);
|
||||
|
||||
await CreateSut().VerifyIntegrityAsync();
|
||||
@@ -272,7 +272,7 @@ public class CmsInstanceServiceTests
|
||||
var instance = ActiveInstance();
|
||||
_repo.GetActiveAsync().Returns([instance]);
|
||||
_protector.Unprotect("encrypted-key").Returns("plain");
|
||||
_slaveClient.GetRegisteredMasterUrlAsync(Arg.Any<string>(), Arg.Any<string>()).Returns("https://old-master.test");
|
||||
_slaveClient.GetRegisteredMasterUrlAsync(Arg.Any<string>(), Arg.Any<string>()).Returns(RegisteredMasterUrlResult.Ok("https://old-master.test"));
|
||||
_slaveClient.RegisterMasterAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>()).Returns(true);
|
||||
_httpContextAccessor.HttpContext.Returns((HttpContext?)null);
|
||||
|
||||
@@ -288,7 +288,7 @@ public class CmsInstanceServiceTests
|
||||
instance.LastIntegrityCheckFailedAt = DateTimeOffset.UtcNow.AddHours(-1);
|
||||
_repo.GetActiveAsync().Returns([instance]);
|
||||
_protector.Unprotect("encrypted-key").Returns("plain");
|
||||
_slaveClient.GetRegisteredMasterUrlAsync(Arg.Any<string>(), Arg.Any<string>()).Returns("https://master.test");
|
||||
_slaveClient.GetRegisteredMasterUrlAsync(Arg.Any<string>(), Arg.Any<string>()).Returns(RegisteredMasterUrlResult.Ok("https://master.test"));
|
||||
_httpContextAccessor.HttpContext.Returns((HttpContext?)null);
|
||||
|
||||
await CreateSut().VerifyIntegrityAsync();
|
||||
@@ -304,7 +304,7 @@ public class CmsInstanceServiceTests
|
||||
instance.DisableMessage = "Onderhoud";
|
||||
_repo.GetActiveAsync().Returns([instance]);
|
||||
_protector.Unprotect("encrypted-key").Returns("plain");
|
||||
_slaveClient.GetRegisteredMasterUrlAsync(Arg.Any<string>(), Arg.Any<string>()).Returns("https://master.test");
|
||||
_slaveClient.GetRegisteredMasterUrlAsync(Arg.Any<string>(), Arg.Any<string>()).Returns(RegisteredMasterUrlResult.Ok("https://master.test"));
|
||||
_slaveClient.PushStatusAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<string?>()).Returns(true);
|
||||
_httpContextAccessor.HttpContext.Returns((HttpContext?)null);
|
||||
|
||||
@@ -314,6 +314,85 @@ public class CmsInstanceServiceTests
|
||||
_repo.Received().Update(Arg.Is<CmsInstance>(i => i.LastStatusPushedAt.HasValue));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The state this branch exists for: the slave answers but does not recognise us, which
|
||||
/// happens when the original registration call went to the wrong URL. It used to be reported
|
||||
/// as "unreachable" and never repaired, so the instance stayed broken until someone edited the
|
||||
/// database by hand.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task VerifyIntegrityAsync_Registers_WhenSlaveDoesNotRecogniseThisMaster()
|
||||
{
|
||||
var instance = ActiveInstance();
|
||||
_repo.GetActiveAsync().Returns([instance]);
|
||||
_protector.Unprotect("encrypted-key").Returns("plain");
|
||||
_slaveClient.GetRegisteredMasterUrlAsync(Arg.Any<string>(), Arg.Any<string>())
|
||||
.Returns(RegisteredMasterUrlResult.Unauthorized);
|
||||
_slaveClient.RegisterMasterAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>()).Returns(true);
|
||||
_httpContextAccessor.HttpContext.Returns((HttpContext?)null);
|
||||
|
||||
await CreateSut().VerifyIntegrityAsync();
|
||||
|
||||
await _slaveClient.Received(1).RegisterMasterAsync("https://slave.test", "plain", "https://master.test");
|
||||
_repo.Received().Update(Arg.Is<CmsInstance>(i => i.LastIntegrityCheckFailedAt == null && i.LastContactedAt.HasValue));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A slave that belongs to a different master refuses the registration, and the master must
|
||||
/// accept that rather than keep hammering. The refusal itself is the slave's job — see
|
||||
/// MasterAvailabilityServiceTests — this asserts the master honours it.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task VerifyIntegrityAsync_StaysFailed_WhenRegistrationIsRefused()
|
||||
{
|
||||
var instance = ActiveInstance();
|
||||
_repo.GetActiveAsync().Returns([instance]);
|
||||
_protector.Unprotect("encrypted-key").Returns("plain");
|
||||
_slaveClient.GetRegisteredMasterUrlAsync(Arg.Any<string>(), Arg.Any<string>())
|
||||
.Returns(RegisteredMasterUrlResult.Unauthorized);
|
||||
_slaveClient.RegisterMasterAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>()).Returns(false);
|
||||
_httpContextAccessor.HttpContext.Returns((HttpContext?)null);
|
||||
|
||||
await CreateSut().VerifyIntegrityAsync();
|
||||
|
||||
_repo.Received().Update(Arg.Is<CmsInstance>(i => i.LastIntegrityCheckFailedAt.HasValue));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A URL pointing at a frontend rather than an instance's API. Registering again would 404
|
||||
/// just the same, so it must not be attempted — the URL is wrong and only a human can fix it.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task VerifyIntegrityAsync_DoesNotRegister_WhenHostDoesNotSpeakTheProtocol()
|
||||
{
|
||||
var instance = ActiveInstance();
|
||||
_repo.GetActiveAsync().Returns([instance]);
|
||||
_protector.Unprotect("encrypted-key").Returns("plain");
|
||||
_slaveClient.GetRegisteredMasterUrlAsync(Arg.Any<string>(), Arg.Any<string>())
|
||||
.Returns(RegisteredMasterUrlResult.NotAProtocolEndpoint);
|
||||
_httpContextAccessor.HttpContext.Returns((HttpContext?)null);
|
||||
|
||||
await CreateSut().VerifyIntegrityAsync();
|
||||
|
||||
await _slaveClient.DidNotReceive().RegisterMasterAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>());
|
||||
_repo.Received().Update(Arg.Is<CmsInstance>(i => i.LastIntegrityCheckFailedAt.HasValue));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task VerifyIntegrityAsync_DoesNotRegister_WhenSlaveIsUnreachable()
|
||||
{
|
||||
var instance = ActiveInstance();
|
||||
_repo.GetActiveAsync().Returns([instance]);
|
||||
_protector.Unprotect("encrypted-key").Returns("plain");
|
||||
_slaveClient.GetRegisteredMasterUrlAsync(Arg.Any<string>(), Arg.Any<string>())
|
||||
.Returns(RegisteredMasterUrlResult.Unreachable);
|
||||
_httpContextAccessor.HttpContext.Returns((HttpContext?)null);
|
||||
|
||||
await CreateSut().VerifyIntegrityAsync();
|
||||
|
||||
await _slaveClient.DidNotReceive().RegisterMasterAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>());
|
||||
}
|
||||
|
||||
private static CmsInstance ActiveInstance() => new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using FluentAssertions;
|
||||
using FluentAssertions;
|
||||
using SlpModularCms.Modules.Master.Services;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
@@ -71,7 +71,7 @@ public class SlaveApiClientTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetRegisteredMasterUrlAsync_ReturnsMasterUrl_WhenResponseIsSuccess()
|
||||
public async Task GetRegisteredMasterUrlAsync_ReturnsOkWithMasterUrl_WhenResponseIsSuccess()
|
||||
{
|
||||
var json = JsonSerializer.Serialize(new { MasterUrl = MasterUrl });
|
||||
var handler = new FakeHttpMessageHandler(HttpStatusCode.OK, json);
|
||||
@@ -79,18 +79,50 @@ public class SlaveApiClientTests
|
||||
|
||||
var result = await sut.GetRegisteredMasterUrlAsync(SlaveUrl, ApiKey);
|
||||
|
||||
result.Should().Be(MasterUrl);
|
||||
result.Outcome.Should().Be(SlaveContactOutcome.Ok);
|
||||
result.MasterUrl.Should().Be(MasterUrl);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The slave answered and refused the key. Distinct from unreachable, because it is the one
|
||||
/// failure the integrity check can repair on its own.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task GetRegisteredMasterUrlAsync_ReturnsNull_WhenResponseIsFailure()
|
||||
public async Task GetRegisteredMasterUrlAsync_ReturnsUnauthorized_WhenSlaveRejectsTheKey()
|
||||
{
|
||||
var handler = new FakeHttpMessageHandler(HttpStatusCode.Unauthorized);
|
||||
var sut = CreateSut(handler);
|
||||
|
||||
var result = await sut.GetRegisteredMasterUrlAsync(SlaveUrl, ApiKey);
|
||||
|
||||
result.Outcome.Should().Be(SlaveContactOutcome.Unauthorized);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Something is listening but does not serve /api/v1/master/*. Almost always a URL pointing at
|
||||
/// a frontend instead of an instance's API — worth its own outcome, because reporting it as a
|
||||
/// generic contact failure hides the actual mistake.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task GetRegisteredMasterUrlAsync_ReturnsNotAProtocolEndpoint_OnNotFound()
|
||||
{
|
||||
var handler = new FakeHttpMessageHandler(HttpStatusCode.NotFound);
|
||||
var sut = CreateSut(handler);
|
||||
|
||||
var result = await sut.GetRegisteredMasterUrlAsync(SlaveUrl, ApiKey);
|
||||
|
||||
result.Should().BeNull();
|
||||
result.Outcome.Should().Be(SlaveContactOutcome.NotAProtocolEndpoint);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetRegisteredMasterUrlAsync_ReturnsUnreachable_OnServerError()
|
||||
{
|
||||
var handler = new FakeHttpMessageHandler(HttpStatusCode.InternalServerError);
|
||||
var sut = CreateSut(handler);
|
||||
|
||||
var result = await sut.GetRegisteredMasterUrlAsync(SlaveUrl, ApiKey);
|
||||
|
||||
result.Outcome.Should().Be(SlaveContactOutcome.Unreachable);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
Reference in New Issue
Block a user