mirror of
https://github.com/actions/runner.git
synced 2025-12-10 20:36:49 +00:00
Adding Unit test to ContainerOperationProvider
This commit is contained in:
@@ -396,7 +396,7 @@ namespace GitHub.Runner.Worker
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<string> Healthcheck(IExecutionContext executionContext, ContainerInfo container){
|
||||
public async Task<string> 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))
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<DockerCommandManager> _commandManager;
|
||||
// private ContainerOperationProvider operationProvider;
|
||||
|
||||
private TestHostContext _hc;
|
||||
private Mock<IExecutionContext> _ec;
|
||||
private Mock<IDockerCommandManager> _dockerManager;
|
||||
private Mock<IContainerHookManager> _containerHookManager;
|
||||
|
||||
private ContainerOperationProvider containerOperationProvider;
|
||||
|
||||
private List<string> healthyDockerStatus = new List<string> { "healthy" };
|
||||
private List<string> dockerLogs = new List<string> { "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<string>(), It.IsAny<string>()))
|
||||
// .Returns((string tag, string line) =>
|
||||
// {
|
||||
// hc.GetTrace().Info($"{tag} {line}");
|
||||
// return 1;
|
||||
// });
|
||||
// _ec.Setup(x => x.AddIssue(It.IsAny<Issue>(), It.IsAny<string>()))
|
||||
// .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<string>(), It.IsAny<string>())).Returns(Task.FromResult(healthyDockerStatus));
|
||||
var result = await containerOperationProvider.Healthcheck(_ec.Object, containerInfo);
|
||||
|
||||
_dockerManager.Verify(dm => dm.DockerInspectLogs(It.IsAny<IExecutionContext>(), It.IsAny<string>()), 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<InvalidOperationException>(() => 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<IExecutionContext>();
|
||||
|
||||
_dockerManager = new Mock<IDockerCommandManager>();
|
||||
_containerHookManager = new Mock<IContainerHookManager>();
|
||||
containerOperationProvider = new ContainerOperationProvider();
|
||||
|
||||
_hc.SetSingleton<IDockerCommandManager>(_dockerManager.Object);
|
||||
_hc.SetSingleton<IContainerHookManager>(_containerHookManager.Object);
|
||||
|
||||
var list = new List<string>();
|
||||
list.Add("result");
|
||||
|
||||
_ec.Setup(x => x.Global).Returns(new GlobalContext());
|
||||
|
||||
containerOperationProvider.Initialize(_hc);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user