|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Runtime.CompilerServices; |
| 4 | +using Moq; |
| 5 | +using Xunit; |
| 6 | +using GitHub.Runner.Worker; |
| 7 | +using GitHub.Runner.Worker.Handlers; |
| 8 | +using GitHub.DistributedTask.Pipelines; |
| 9 | +using GitHub.DistributedTask.WebApi; |
| 10 | + |
| 11 | +namespace GitHub.Runner.Common.Tests.Worker |
| 12 | +{ |
| 13 | + public sealed class HandlerFactoryL0 |
| 14 | + { |
| 15 | + private Mock<IExecutionContext> _ec; |
| 16 | + private TestHostContext CreateTestContext([CallerMemberName] string testName = "") |
| 17 | + { |
| 18 | + var hostContext = new TestHostContext(this, testName); |
| 19 | + _ec = new Mock<IExecutionContext>(); |
| 20 | + _ec.SetupAllProperties(); |
| 21 | + _ec.Object.Initialize(hostContext); |
| 22 | + var handler = new Mock<INodeScriptActionHandler>(); |
| 23 | + handler.SetupAllProperties(); |
| 24 | + hostContext.EnqueueInstance(handler.Object); |
| 25 | + //hostContext.EnqueueInstance(new ActionCommandManager() as IActionCommandManager); |
| 26 | + |
| 27 | + return hostContext; |
| 28 | + } |
| 29 | + |
| 30 | + [Theory] |
| 31 | + [Trait("Level", "L0")] |
| 32 | + [Trait("Category", "Worker")] |
| 33 | + [InlineData("node12", "", "", "", "node12")] |
| 34 | + [InlineData("node12", "true", "", "", "node16")] |
| 35 | + [InlineData("node12", "true", "", "true", "node12")] |
| 36 | + [InlineData("node12", "true", "true", "", "node12")] |
| 37 | + [InlineData("node12", "true", "true", "true", "node12")] |
| 38 | + [InlineData("node12", "true", "false", "true", "node16")] // workflow overrides env |
| 39 | + [InlineData("node16", "", "", "", "node16")] |
| 40 | + [InlineData("node16", "true", "", "", "node16")] |
| 41 | + [InlineData("node16", "true", "", "true", "node16")] |
| 42 | + [InlineData("node16", "true", "true", "", "node16")] |
| 43 | + [InlineData("node16", "true", "true", "true", "node16")] |
| 44 | + [InlineData("node16", "true", "false", "true", "node16")] |
| 45 | + public void IsNodeVersionUpgraded(string inputVersion, string serverFeatureFlag, string workflowOptOut, string machineOptOut, string expectedVersion) |
| 46 | + { |
| 47 | + using (TestHostContext hc = CreateTestContext()) |
| 48 | + { |
| 49 | + // Arrange. |
| 50 | + var hf = new HandlerFactory(); |
| 51 | + hf.Initialize(hc); |
| 52 | + |
| 53 | + // Server Feature Flag |
| 54 | + var variables = new Dictionary<string, VariableValue>(); |
| 55 | + if (!string.IsNullOrEmpty(serverFeatureFlag)) |
| 56 | + { |
| 57 | + variables["DistributedTask.ForceGithubJavascriptActionsToNode16"] = serverFeatureFlag; |
| 58 | + } |
| 59 | + Variables serverVariables = new Variables(hc, variables); |
| 60 | + |
| 61 | + // Workflow opt-out |
| 62 | + var workflowVariables = new Dictionary<string, string>(); |
| 63 | + if (!string.IsNullOrEmpty(workflowOptOut)) |
| 64 | + { |
| 65 | + workflowVariables[Constants.Variables.Actions.AllowActionsUseUnsecureNodeVersion] = workflowOptOut; |
| 66 | + } |
| 67 | + |
| 68 | + // Machine opt-out |
| 69 | + if (!string.IsNullOrEmpty(machineOptOut)) |
| 70 | + { |
| 71 | + Environment.SetEnvironmentVariable(Constants.Variables.Actions.AllowActionsUseUnsecureNodeVersion, machineOptOut); |
| 72 | + } |
| 73 | + |
| 74 | + _ec.Setup(x => x.Global).Returns(new GlobalContext() |
| 75 | + { |
| 76 | + Variables = serverVariables, |
| 77 | + EnvironmentVariables = workflowVariables |
| 78 | + }); |
| 79 | + |
| 80 | + |
| 81 | + // Act. |
| 82 | + var data = new NodeJSActionExecutionData(); |
| 83 | + data.NodeVersion = inputVersion; |
| 84 | + var handler = hf.Create( |
| 85 | + _ec.Object, |
| 86 | + new ScriptReference(), |
| 87 | + new Mock<IStepHost>().Object, |
| 88 | + data, |
| 89 | + new Dictionary<string, string>(), |
| 90 | + new Dictionary<string, string>(), |
| 91 | + new Variables(hc, new Dictionary<string, VariableValue>()), "", new List<JobExtensionRunner>() |
| 92 | + ) as INodeScriptActionHandler; |
| 93 | + |
| 94 | + // Assert. |
| 95 | + Assert.Equal(expectedVersion, handler.Data.NodeVersion); |
| 96 | + Environment.SetEnvironmentVariable(Constants.Variables.Actions.AllowActionsUseUnsecureNodeVersion, null); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments