From 826cec2775e00b2ce8bf454bd4e86afc5b25fe16 Mon Sep 17 00:00:00 2001 From: JoannaaKL Date: Wed, 14 Sep 2022 08:10:03 +0000 Subject: [PATCH] Refactor healthcheck logic to separate method to enable unit testing. --- .../ContainerOperationProvider.cs | 9 +++- .../L0/Worker/ContainerOperationProviderL0.cs | 54 +++++++++++++++---- 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/src/Runner.Worker/ContainerOperationProvider.cs b/src/Runner.Worker/ContainerOperationProvider.cs index d77a50c70..b23ae3886 100644 --- a/src/Runner.Worker/ContainerOperationProvider.cs +++ b/src/Runner.Worker/ContainerOperationProvider.cs @@ -99,6 +99,11 @@ namespace GitHub.Runner.Worker await StartContainerAsync(executionContext, container); } + await RunContainersHealthcheck(executionContext, containers); + } + + public async Task RunContainersHealthcheck(IExecutionContext executionContext, List containers) + { executionContext.Output("##[group]Waiting for all services to be ready"); var unhealthyContainers = new List(); @@ -423,7 +428,7 @@ namespace GitHub.Runner.Worker } } - public async Task Healthcheck(IExecutionContext executionContext, ContainerInfo container) + private async Task Healthcheck(IExecutionContext executionContext, ContainerInfo container) { string healthCheck = "--format=\"{{if .Config.Healthcheck}}{{print .State.Health.Status}}{{end}}\""; string serviceHealth = (await _dockerManager.DockerInspect(context: executionContext, dockerObject: container.ContainerId, options: healthCheck)).FirstOrDefault(); @@ -444,7 +449,7 @@ namespace GitHub.Runner.Worker return serviceHealth; } - public async Task ContainerErrorLogs(IExecutionContext executionContext, ContainerInfo container) + private async Task ContainerErrorLogs(IExecutionContext executionContext, ContainerInfo container) { await _dockerManager.DockerLogs(context: executionContext, containerId: container.ContainerId); executionContext.Error($"Failed to initialize container {container.ContainerImage}"); diff --git a/src/Test/L0/Worker/ContainerOperationProviderL0.cs b/src/Test/L0/Worker/ContainerOperationProviderL0.cs index 7bd567544..032bc3351 100644 --- a/src/Test/L0/Worker/ContainerOperationProviderL0.cs +++ b/src/Test/L0/Worker/ContainerOperationProviderL0.cs @@ -6,6 +6,8 @@ using GitHub.Runner.Worker.Container.ContainerHooks; using System.Threading.Tasks; using System.Collections.Generic; using System.Runtime.CompilerServices; +using GitHub.DistributedTask.WebApi; +using System; namespace GitHub.Runner.Common.Tests.Worker { @@ -18,37 +20,69 @@ namespace GitHub.Runner.Common.Tests.Worker private Mock _dockerManager; private Mock _containerHookManager; private ContainerOperationProvider containerOperationProvider; - private Mock serverQueue; - private Mock pagingLogger; - private List healthyDockerStatus = new List { "healthy" }; + private List unhealthyDockerStatus = new List { "unhealthy" }; private List dockerLogs = new List { "log1", "log2", "log3" }; - private ContainerInfo containerInfo; + List containers = new List(); [Fact] [Trait("Level", "L0")] [Trait("Category", "Worker")] - public async void Healthchecktest_healthyDocker() + public async void RunServiceContainersHealthcheck_UnhealthyServiceContainer_AssertExceptionThrown() + { + //Arrange + Setup(); + _dockerManager.Setup(x => x.DockerInspect(_ec.Object, It.IsAny(), It.IsAny())).Returns(Task.FromResult(unhealthyDockerStatus)); + + //Act and Assert + await Assert.ThrowsAsync(() => containerOperationProvider.RunContainersHealthcheck(_ec.Object, containers)); + + } + + [Fact] + [Trait("Level", "L0")] + [Trait("Category", "Worker")] + public async void RunServiceContainersHealthcheck_UnhealthyServiceContainer_AssertFailedTask() + { + //Arrange + Setup(); + _dockerManager.Setup(x => x.DockerInspect(_ec.Object, It.IsAny(), It.IsAny())).Returns(Task.FromResult(unhealthyDockerStatus)); + + //Act + try + { + await containerOperationProvider.RunContainersHealthcheck(_ec.Object, containers); + + } + catch (InvalidOperationException) { } + + //Assert + Assert.Equal(TaskResult.Failed, _ec.Object.Result ?? TaskResult.Failed); + + } + + [Fact] + [Trait("Level", "L0")] + [Trait("Category", "Worker")] + public async void RunServiceContainersHealthcheck_healthyServiceContainer_AssertSucceededTask() { //Arrange Setup(); _dockerManager.Setup(x => x.DockerInspect(_ec.Object, It.IsAny(), It.IsAny())).Returns(Task.FromResult(healthyDockerStatus)); //Act - var result = await containerOperationProvider.Healthcheck(_ec.Object, containerInfo); + await containerOperationProvider.RunContainersHealthcheck(_ec.Object, containers); //Assert - _dockerManager.Verify(dm => dm.DockerLogs(It.IsAny(), It.IsAny()), Times.Never()); + Assert.Equal(TaskResult.Succeeded, _ec.Object.Result ?? TaskResult.Succeeded); } - - private void Setup([CallerMemberName] string testName = "") { - containerInfo = new ContainerInfo() { ContainerImage = "ubuntu:16.04" }; + containers.Add(new ContainerInfo() { ContainerImage = "ubuntu:16.04" }); _hc = new TestHostContext(this, "name"); _ec = new Mock(); serverQueue = new Mock();