Skip to content

Commit

Permalink
Initialize container manager based on whether the ContainerHooksPath …
Browse files Browse the repository at this point in the history
…is set (#2317)

* Added tests around checking if correct manager's Initialize method has been called

* repaired missing initialization on container action handler
  • Loading branch information
nikola-jokic authored Dec 16, 2022
1 parent f9e2fa9 commit 04761e5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/Runner.Worker/ContainerOperationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ public class ContainerOperationProvider : RunnerService, IContainerOperationProv
public override void Initialize(IHostContext hostContext)
{
base.Initialize(hostContext);
_dockerManager = HostContext.GetService<IDockerCommandManager>();
_containerHookManager = HostContext.GetService<IContainerHookManager>();
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable(Constants.Hooks.ContainerHooksPath)))
{
_dockerManager = HostContext.GetService<IDockerCommandManager>();
}
else
{
_containerHookManager = HostContext.GetService<IContainerHookManager>();
}
}

public async Task StartContainersAsync(IExecutionContext executionContext, object data)
Expand Down
13 changes: 11 additions & 2 deletions src/Runner.Worker/Handlers/ContainerActionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,17 @@ public async Task RunAsync(ActionRunStage stage)
// Update the env dictionary.
AddInputsToEnvironment();

var dockerManager = HostContext.GetService<IDockerCommandManager>();
var containerHookManager = HostContext.GetService<IContainerHookManager>();
IDockerCommandManager dockerManager = null;
IContainerHookManager containerHookManager = null;
if (FeatureManager.IsContainerHooksEnabled(ExecutionContext.Global.Variables))
{
containerHookManager = HostContext.GetService<IContainerHookManager>();
}
else
{
dockerManager = HostContext.GetService<IDockerCommandManager>();
}

string dockerFile = null;

// container image haven't built/pull
Expand Down
41 changes: 40 additions & 1 deletion src/Test/L0/Worker/ContainerOperationProviderL0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,46 @@ public async void RunServiceContainersHealthcheck_healthyServiceContainerWithout

}

[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public void InitializeWithCorrectManager()
{
containers.Add(new ContainerInfo() { ContainerImage = "ubuntu:16.04" });
_hc = new TestHostContext(this, "Test");
_ec = new Mock<IExecutionContext>();
serverQueue = new Mock<IJobServerQueue>();
pagingLogger = new Mock<IPagingLogger>();

containerOperationProvider = new ContainerOperationProvider();

_hc.SetSingleton<IJobServerQueue>(serverQueue.Object);
_hc.SetSingleton<IPagingLogger>(pagingLogger.Object);


_ec.Setup(x => x.Global).Returns(new GlobalContext());

Environment.SetEnvironmentVariable(Constants.Hooks.ContainerHooksPath, "/tmp/k8s/index.js");
_dockerManager = new Mock<IDockerCommandManager>();
_dockerManager.Setup(x => x.Initialize(_hc)).Throws(new Exception("Docker manager's Initialize should not be called"));

_containerHookManager = new Mock<IContainerHookManager>();
_hc.SetSingleton<IDockerCommandManager>(_dockerManager.Object);
_hc.SetSingleton<IContainerHookManager>(_containerHookManager.Object);

containerOperationProvider.Initialize(_hc);

Environment.SetEnvironmentVariable(Constants.Hooks.ContainerHooksPath, null);
_containerHookManager = new Mock<IContainerHookManager>();
_containerHookManager.Setup(x => x.Initialize(_hc)).Throws(new Exception("Container hook manager's Initialize should not be called"));

_dockerManager = new Mock<IDockerCommandManager>();
_hc.SetSingleton<IDockerCommandManager>(_dockerManager.Object);
_hc.SetSingleton<IContainerHookManager>(_containerHookManager.Object);

containerOperationProvider.Initialize(_hc);
}

private void Setup([CallerMemberName] string testName = "")
{
containers.Add(new ContainerInfo() { ContainerImage = "ubuntu:16.04" });
Expand All @@ -111,7 +151,6 @@ private void Setup([CallerMemberName] string testName = "")
_containerHookManager = new Mock<IContainerHookManager>();
containerOperationProvider = new ContainerOperationProvider();

_hc.SetSingleton<IDockerCommandManager>(_dockerManager.Object);
_hc.SetSingleton<IJobServerQueue>(serverQueue.Object);
_hc.SetSingleton<IPagingLogger>(pagingLogger.Object);

Expand Down

0 comments on commit 04761e5

Please sign in to comment.