diff --git a/src/Runner.Worker/ContainerOperationProvider.cs b/src/Runner.Worker/ContainerOperationProvider.cs index 0b15d54c6..0a76f8aca 100644 --- a/src/Runner.Worker/ContainerOperationProvider.cs +++ b/src/Runner.Worker/ContainerOperationProvider.cs @@ -396,7 +396,7 @@ namespace GitHub.Runner.Worker } } - private async Task Healthcheck(IExecutionContext executionContext, ContainerInfo container){ + public 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(); if (string.IsNullOrEmpty(serviceHealth)) @@ -416,7 +416,7 @@ namespace GitHub.Runner.Worker return serviceHealth; } - private async Task ContainerHealthcheckLogs(IExecutionContext executionContext, ContainerInfo container, string serviceHealth) + public async Task ContainerHealthcheckLogs(IExecutionContext executionContext, ContainerInfo container, string serviceHealth) { if (string.Equals(serviceHealth, "healthy", StringComparison.OrdinalIgnoreCase)) diff --git a/src/Test/L0/TestHostContext.cs b/src/Test/L0/TestHostContext.cs index ce1ec01bc..b31f228ca 100644 --- a/src/Test/L0/TestHostContext.cs +++ b/src/Test/L0/TestHostContext.cs @@ -12,7 +12,7 @@ using System.Collections.Generic; using GitHub.DistributedTask.Logging; using System.Net.Http.Headers; using GitHub.Runner.Sdk; - +using GitHub.Runner.Worker.Container; namespace GitHub.Runner.Common.Tests { public sealed class TestHostContext : IHostContext, IDisposable @@ -26,6 +26,7 @@ namespace GitHub.Runner.Common.Tests private string _suiteName; private string _testName; private Tracing _trace; + private AssemblyLoadContext _loadContext; private string _tempDirectoryRoot = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("D")); private StartupType _startupType; diff --git a/src/Test/L0/Worker/ContainerOperationProviderL0.cs b/src/Test/L0/Worker/ContainerOperationProviderL0.cs index f4df2c83a..61379aebb 100644 --- a/src/Test/L0/Worker/ContainerOperationProviderL0.cs +++ b/src/Test/L0/Worker/ContainerOperationProviderL0.cs @@ -1,44 +1,106 @@ -using System; using GitHub.Runner.Worker; using GitHub.Runner.Worker.Container; -using Moq; using Xunit; +using Moq; +using GitHub.Runner.Worker.Container.ContainerHooks; +using System.Threading.Tasks; +using System.Collections.Generic; +using System; +using System.IO; +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading; + namespace GitHub.Runner.Common.Tests.Worker { + public sealed class ContainerOperationProviderL0 { - // private Mock _commandManager; - // private ContainerOperationProvider operationProvider; + + private TestHostContext _hc; + private Mock _ec; + private Mock _dockerManager; + private Mock _containerHookManager; + + private ContainerOperationProvider containerOperationProvider; + + private List healthyDockerStatus = new List { "healthy" }; + private List dockerLogs = new List { "log1", "log2", "log3" }; [Fact] [Trait("Level", "L0")] [Trait("Category", "Worker")] - public void EnablePluginInternalCommand() + public async void Healthchecktest_healthyDocker() { - - // _ec.Setup(x => x.Write(It.IsAny(), It.IsAny())) - // .Returns((string tag, string line) => - // { - // hc.GetTrace().Info($"{tag} {line}"); - // return 1; - // }); - // _ec.Setup(x => x.AddIssue(It.IsAny(), It.IsAny())) - // .Callback((Issue issue, string message) => - // { - // hc.GetTrace().Info($"{issue.Type} {issue.Message} {message ?? string.Empty}"); - // }); + Setup(); - // _commandManager.EnablePluginInternalCommand(); + var containerInfo = new ContainerInfo() { ContainerImage = "ubuntu:16.04" }; - // Assert.True(_commandManager.TryProcessCommand(_ec.Object, "##[internal-set-repo-path repoFullName=actions/runner;workspaceRepo=true]somepath", null)); + _dockerManager.Setup(x => x.DockerInspect(_ec.Object, It.IsAny(), It.IsAny())).Returns(Task.FromResult(healthyDockerStatus)); + var result = await containerOperationProvider.Healthcheck(_ec.Object, containerInfo); + + _dockerManager.Verify(dm => dm.DockerInspectLogs(It.IsAny(), It.IsAny()), Times.Never()); - // _pipelineDirectoryManager.Verify(x => x.UpdateRepositoryDirectory(_ec.Object, "actions/runner", "somepath", true), Times.Once); - } - // private TestHostContext CreateTestContext([CallerMemberName] string testName = "") { - // return null; - // } + [Fact] + [Trait("Level", "L0")] + [Trait("Category", "Worker")] + public async void Healthchecktest_dockerError() + { + Setup(); + + var containerInfo = new ContainerInfo() { ContainerImage = "ubuntu:16.04", ContainerId = "1234" }; + + _dockerManager.Setup(x => x.DockerInspectLogs(_ec.Object, containerInfo.ContainerId)).Returns(Task.FromResult(dockerLogs)); + + await Assert.ThrowsAsync(() => containerOperationProvider.ContainerHealthcheckLogs(_ec.Object, containerInfo, "error")); + + } + + [Fact] + [Trait("Level", "L0")] + [Trait("Category", "Worker")] + public async void Healthchecktest_dockerError_inspectLogs() + { + Setup(); + + var containerInfo = new ContainerInfo() { ContainerImage = "ubuntu:16.04", ContainerId = "1234" }; + + _dockerManager.Setup(x => x.DockerInspectLogs(_ec.Object, containerInfo.ContainerId)).Returns(Task.FromResult(dockerLogs)); + + try + { + await containerOperationProvider.ContainerHealthcheckLogs(_ec.Object, containerInfo, "error"); + + } + catch (InvalidOperationException) + { + //TODO validate the log message written to the _ec + } + + } + + private void Setup([CallerMemberName] string testName = "") + { + _hc = new TestHostContext(this, "name"); + _ec = new Mock(); + + _dockerManager = new Mock(); + _containerHookManager = new Mock(); + containerOperationProvider = new ContainerOperationProvider(); + + _hc.SetSingleton(_dockerManager.Object); + _hc.SetSingleton(_containerHookManager.Object); + + var list = new List(); + list.Add("result"); + + _ec.Setup(x => x.Global).Returns(new GlobalContext()); + + containerOperationProvider.Initialize(_hc); + } } } \ No newline at end of file