feat(docker): Gracefully reload Caddy configuration
All checks were successful
Caddy Manager CI build / docker (push) Successful in 11m33s

This commit is contained in:
2026-07-26 11:39:59 +07:00
parent 284036b549
commit d0687c9134
10 changed files with 200 additions and 22 deletions

View File

@@ -1,5 +1,6 @@
using CaddyManager.Contracts.Configurations.Docker;
using CaddyManager.Contracts.Configurations;
using CaddyManager.Contracts.Docker;
using CaddyManager.Services.Docker;
namespace CaddyManager.Tests.Services.Docker;
@@ -161,6 +162,63 @@ public class DockerServiceTests
// Assert
config.CaddyContainerName.Should().Be("caddy");
config.DockerHost.Should().Be("unix:///var/run/docker.sock");
config.CaddyConfigPathInContainer.Should().Be("/etc/caddy/Caddyfile");
}
/// <summary>
/// Tests that a graceful reload against a container that does not exist reports a failure instead of throwing.
/// Setup: Points the service at a container name that is guaranteed not to be running.
/// Expectation: The service returns an unsuccessful <see cref="CaddyReloadResponse"/> carrying an explanatory
/// message, so the UI can surface the problem rather than the caller having to handle exceptions.
/// </summary>
[Fact]
public async Task ReloadCaddyContainerAsync_WithNonExistentContainer_ReturnsFailureResponse()
{
// Arrange
_mockConfigurationsService
.Setup(x => x.Get<DockerServiceConfiguration>())
.Returns(new DockerServiceConfiguration
{
CaddyContainerName = "non-existent-container",
DockerHost = "unix:///var/run/docker.sock"
});
var service = new DockerService(_mockConfigurationsService.Object);
// Act
var response = await service.ReloadCaddyContainerAsync();
// Assert
response.Success.Should().BeFalse();
response.Message.Should().NotBeNullOrWhiteSpace();
}
/// <summary>
/// Tests that a graceful reload against an unreachable Docker daemon reports a failure instead of throwing.
/// Setup: Points the service at a Docker host that cannot be contacted.
/// Expectation: The connection error is captured in the response message, keeping the failure handling uniform
/// regardless of whether Docker, the container, or Caddy itself is the cause.
/// </summary>
[Fact]
public async Task ReloadCaddyContainerAsync_WithUnreachableDockerHost_ReturnsFailureResponse()
{
// Arrange
_mockConfigurationsService
.Setup(x => x.Get<DockerServiceConfiguration>())
.Returns(new DockerServiceConfiguration
{
CaddyContainerName = "test-caddy",
DockerHost = "tcp://unreachable-host:2376"
});
var service = new DockerService(_mockConfigurationsService.Object);
// Act
var response = await service.ReloadCaddyContainerAsync();
// Assert
response.Success.Should().BeFalse();
response.Message.Should().NotBeNullOrWhiteSpace();
}
/// <summary>