Check service exit code if there is no healtcheck configured

This commit is contained in:
JoannaaKL
2022-09-16 11:09:55 +00:00
parent 1778f8f022
commit fec24e8341
2 changed files with 64 additions and 6 deletions

View File

@@ -435,8 +435,13 @@ namespace GitHub.Runner.Worker
string serviceHealth = (await _dockerManager.DockerInspect(context: executionContext, dockerObject: container.ContainerId, options: healthCheck)).FirstOrDefault();
if (string.IsNullOrEmpty(serviceHealth))
{
string exitCode = "--format=\"{{print .State.ExitCode}}\"";
string serviceExitCode = (await _dockerManager.DockerInspect(context: executionContext, dockerObject: container.ContainerId, options: exitCode)).FirstOrDefault();
// Container has no healthcheck but didn't exit with an error code
if ("0".Equals(serviceExitCode)) return "healthy";
// Container has no HEALTHCHECK
return String.Empty;
else return "unhealthy";
}
var retryCount = 0;
while (string.Equals(serviceHealth, "starting", StringComparison.OrdinalIgnoreCase))

View File

@@ -24,7 +24,9 @@ namespace GitHub.Runner.Common.Tests.Worker
private Mock<IPagingLogger> pagingLogger;
private List<string> healthyDockerStatus = new List<string> { "healthy" };
private List<string> unhealthyDockerStatus = new List<string> { "unhealthy" };
private List<string> emptyDockerStatus = new List<string> { "" };
private List<string> dockerLogs = new List<string> { "log1", "log2", "log3" };
string healthCheck = "--format=\"{{if .Config.Healthcheck}}{{print .State.Health.Status}}{{end}}\"";
List<ContainerInfo> containers = new List<ContainerInfo>();
@@ -35,7 +37,7 @@ namespace GitHub.Runner.Common.Tests.Worker
{
//Arrange
Setup();
_dockerManager.Setup(x => x.DockerInspect(_ec.Object, It.IsAny<string>(), It.IsAny<string>())).Returns(Task.FromResult(unhealthyDockerStatus));
_dockerManager.Setup(x => x.DockerInspect(_ec.Object, It.IsAny<string>(), healthCheck)).Returns(Task.FromResult(unhealthyDockerStatus));
//Act
try
@@ -47,6 +49,7 @@ namespace GitHub.Runner.Common.Tests.Worker
//Assert
Assert.Equal(TaskResult.Failed, _ec.Object.Result ?? TaskResult.Failed);
_dockerManager.Verify(x => x.DockerInspect(_ec.Object, It.IsAny<string>(), It.IsAny<string>()), Times.Once());
}
}
@@ -57,27 +60,77 @@ namespace GitHub.Runner.Common.Tests.Worker
{
//Arrange
Setup();
_dockerManager.Setup(x => x.DockerInspect(_ec.Object, It.IsAny<string>(), It.IsAny<string>())).Returns(Task.FromResult(unhealthyDockerStatus));
_dockerManager.Setup(x => x.DockerInspect(_ec.Object, It.IsAny<string>(), healthCheck)).Returns(Task.FromResult(unhealthyDockerStatus));
//Act and Assert
await Assert.ThrowsAsync<InvalidOperationException>(() => containerOperationProvider.RunContainersHealthcheck(_ec.Object, containers));
_dockerManager.Verify(x => x.DockerInspect(_ec.Object, It.IsAny<string>(), It.IsAny<string>()), Times.Once());
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async void RunServiceContainersHealthcheck_healthyServiceContainer_AssertSucceededTask()
public async void RunServiceContainersHealthcheck_HealthyServiceContainer_AssertSucceededTask()
{
//Arrange
Setup();
_dockerManager.Setup(x => x.DockerInspect(_ec.Object, It.IsAny<string>(), It.IsAny<string>())).Returns(Task.FromResult(healthyDockerStatus));
_dockerManager.Setup(x => x.DockerInspect(_ec.Object, It.IsAny<string>(), healthCheck)).Returns(Task.FromResult(healthyDockerStatus));
//Act
await containerOperationProvider.RunContainersHealthcheck(_ec.Object, containers);
//Assert
Assert.Equal(TaskResult.Succeeded, _ec.Object.Result ?? TaskResult.Succeeded);
_dockerManager.Verify(x => x.DockerInspect(_ec.Object, It.IsAny<string>(), It.IsAny<string>()), Times.Once());
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async void RunServiceContainersHealthcheck_ServiceContainerWithoutHealthcheckAndWithOkExitStatus_AssertSucceededTask()
{
string exitCode = "--format=\"{{print .State.ExitCode}}\"";
//Arrange
Setup();
_dockerManager.Setup(x => x.DockerInspect(_ec.Object, It.IsAny<string>(), healthCheck)).Returns(Task.FromResult(emptyDockerStatus));
_dockerManager.Setup(x => x.DockerInspect(_ec.Object, It.IsAny<string>(), exitCode)).Returns(Task.FromResult(new List<string> { "0" }));
//Act
await containerOperationProvider.RunContainersHealthcheck(_ec.Object, containers);
//Assert
Assert.Equal(TaskResult.Succeeded, _ec.Object.Result ?? TaskResult.Succeeded);
_dockerManager.Verify(x => x.DockerInspect(_ec.Object, It.IsAny<string>(), It.IsAny<string>()), Times.Exactly(2));
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public async void RunServiceContainersHealthcheck_ServiceContainerWithoutHealthcheckAndWithErrorExitStatus_AssertSucceededTask()
{
string exitCode = "--format=\"{{print .State.ExitCode}}\"";
//Arrange
Setup();
_dockerManager.Setup(x => x.DockerInspect(_ec.Object, It.IsAny<string>(), healthCheck)).Returns(Task.FromResult(emptyDockerStatus));
_dockerManager.Setup(x => x.DockerInspect(_ec.Object, It.IsAny<string>(), exitCode)).Returns(Task.FromResult(new List<string> { "127" }));
//Act
try
{
await containerOperationProvider.RunContainersHealthcheck(_ec.Object, containers);
}
catch (InvalidOperationException)
{
//Assert
Assert.Equal(TaskResult.Failed, _ec.Object.Result ?? TaskResult.Failed);
_dockerManager.Verify(x => x.DockerInspect(_ec.Object, It.IsAny<string>(), It.IsAny<string>()), Times.Exactly(2));
}
}