Skip to content

Commit

Permalink
Check service exit code if there is no healtcheck configured
Browse files Browse the repository at this point in the history
  • Loading branch information
JoannaaKL committed Sep 16, 2022
1 parent 1778f8f commit fec24e8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/Runner.Worker/ContainerOperationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,13 @@ private async Task<string> ContainerHealthcheck(IExecutionContext executionConte
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))
Expand Down
61 changes: 57 additions & 4 deletions src/Test/L0/Worker/ContainerOperationProviderL0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public sealed class ContainerOperationProviderL0
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>();

Expand All @@ -35,7 +37,7 @@ public async void RunServiceContainersHealthcheck_UnhealthyServiceContainer_Asse
{
//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
Expand All @@ -47,6 +49,7 @@ public async void RunServiceContainersHealthcheck_UnhealthyServiceContainer_Asse

//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());
}
}

Expand All @@ -57,27 +60,77 @@ public async void RunServiceContainersHealthcheck_UnhealthyServiceContainer_Asse
{
//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()
{
//Arrange
Setup();
_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_healthyServiceContainer_AssertSucceededTask()
public async void RunServiceContainersHealthcheck_ServiceContainerWithoutHealthcheckAndWithOkExitStatus_AssertSucceededTask()
{

string exitCode = "--format=\"{{print .State.ExitCode}}\"";
//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(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));
}

}

Expand Down

0 comments on commit fec24e8

Please sign in to comment.