From 04761e535339bbcb94fae5efe264b8d9a704967a Mon Sep 17 00:00:00 2001 From: Nikola Jokic Date: Fri, 16 Dec 2022 15:40:49 +0100 Subject: [PATCH] Initialize container manager based on whether the ContainerHooksPath is set (#2317) * Added tests around checking if correct manager's Initialize method has been called * repaired missing initialization on container action handler --- .../ContainerOperationProvider.cs | 10 ++++- .../Handlers/ContainerActionHandler.cs | 13 +++++- .../L0/Worker/ContainerOperationProviderL0.cs | 41 ++++++++++++++++++- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/Runner.Worker/ContainerOperationProvider.cs b/src/Runner.Worker/ContainerOperationProvider.cs index 79cc8cf76..6e5b12047 100644 --- a/src/Runner.Worker/ContainerOperationProvider.cs +++ b/src/Runner.Worker/ContainerOperationProvider.cs @@ -33,8 +33,14 @@ namespace GitHub.Runner.Worker public override void Initialize(IHostContext hostContext) { base.Initialize(hostContext); - _dockerManager = HostContext.GetService(); - _containerHookManager = HostContext.GetService(); + if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable(Constants.Hooks.ContainerHooksPath))) + { + _dockerManager = HostContext.GetService(); + } + else + { + _containerHookManager = HostContext.GetService(); + } } public async Task StartContainersAsync(IExecutionContext executionContext, object data) diff --git a/src/Runner.Worker/Handlers/ContainerActionHandler.cs b/src/Runner.Worker/Handlers/ContainerActionHandler.cs index 1fe205f40..c1d57de11 100644 --- a/src/Runner.Worker/Handlers/ContainerActionHandler.cs +++ b/src/Runner.Worker/Handlers/ContainerActionHandler.cs @@ -38,8 +38,17 @@ namespace GitHub.Runner.Worker.Handlers // Update the env dictionary. AddInputsToEnvironment(); - var dockerManager = HostContext.GetService(); - var containerHookManager = HostContext.GetService(); + IDockerCommandManager dockerManager = null; + IContainerHookManager containerHookManager = null; + if (FeatureManager.IsContainerHooksEnabled(ExecutionContext.Global.Variables)) + { + containerHookManager = HostContext.GetService(); + } + else + { + dockerManager = HostContext.GetService(); + } + string dockerFile = null; // container image haven't built/pull diff --git a/src/Test/L0/Worker/ContainerOperationProviderL0.cs b/src/Test/L0/Worker/ContainerOperationProviderL0.cs index 1cec0d4da..4971baef8 100644 --- a/src/Test/L0/Worker/ContainerOperationProviderL0.cs +++ b/src/Test/L0/Worker/ContainerOperationProviderL0.cs @@ -99,6 +99,46 @@ namespace GitHub.Runner.Common.Tests.Worker } + [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(); + serverQueue = new Mock(); + pagingLogger = new Mock(); + + containerOperationProvider = new ContainerOperationProvider(); + + _hc.SetSingleton(serverQueue.Object); + _hc.SetSingleton(pagingLogger.Object); + + + _ec.Setup(x => x.Global).Returns(new GlobalContext()); + + Environment.SetEnvironmentVariable(Constants.Hooks.ContainerHooksPath, "/tmp/k8s/index.js"); + _dockerManager = new Mock(); + _dockerManager.Setup(x => x.Initialize(_hc)).Throws(new Exception("Docker manager's Initialize should not be called")); + + _containerHookManager = new Mock(); + _hc.SetSingleton(_dockerManager.Object); + _hc.SetSingleton(_containerHookManager.Object); + + containerOperationProvider.Initialize(_hc); + + Environment.SetEnvironmentVariable(Constants.Hooks.ContainerHooksPath, null); + _containerHookManager = new Mock(); + _containerHookManager.Setup(x => x.Initialize(_hc)).Throws(new Exception("Container hook manager's Initialize should not be called")); + + _dockerManager = new Mock(); + _hc.SetSingleton(_dockerManager.Object); + _hc.SetSingleton(_containerHookManager.Object); + + containerOperationProvider.Initialize(_hc); + } + private void Setup([CallerMemberName] string testName = "") { containers.Add(new ContainerInfo() { ContainerImage = "ubuntu:16.04" }); @@ -111,7 +151,6 @@ namespace GitHub.Runner.Common.Tests.Worker _containerHookManager = new Mock(); containerOperationProvider = new ContainerOperationProvider(); - _hc.SetSingleton(_dockerManager.Object); _hc.SetSingleton(serverQueue.Object); _hc.SetSingleton(pagingLogger.Object);