using GitHub.DistributedTask.WebApi; using GitHub.Runner.Worker; using Moq; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Threading.Tasks; using Xunit; using System.Threading; using System.Collections.ObjectModel; using Pipelines = GitHub.DistributedTask.Pipelines; namespace GitHub.Runner.Common.Tests.Worker { public sealed class JobRunnerL0 { private IExecutionContext _jobEc; private JobRunner _jobRunner; private List _initResult = new List(); private Pipelines.AgentJobRequestMessage _message; private CancellationTokenSource _tokenSource; private Mock _jobServer; private Mock _jobServerQueue; private Mock _proxyConfig; private Mock _cert; private Mock _config; private Mock _extensions; private Mock _stepRunner; private Mock _jobExtension; private Mock _logger; private Mock _temp; private Mock _diagnosticLogManager; private TestHostContext CreateTestContext([CallerMemberName] String testName = "") { var hc = new TestHostContext(this, testName); _jobEc = new Runner.Worker.ExecutionContext(); _config = new Mock(); _extensions = new Mock(); _jobExtension = new Mock(); _jobServer = new Mock(); _jobServerQueue = new Mock(); _proxyConfig = new Mock(); _cert = new Mock(); _stepRunner = new Mock(); _logger = new Mock(); _temp = new Mock(); _diagnosticLogManager = new Mock(); if (_tokenSource != null) { _tokenSource.Dispose(); _tokenSource = null; } _tokenSource = new CancellationTokenSource(); var expressionManager = new ExpressionManager(); expressionManager.Initialize(hc); hc.SetSingleton(expressionManager); _jobRunner = new JobRunner(); _jobRunner.Initialize(hc); TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); TimelineReference timeline = new Timeline(Guid.NewGuid()); JobEnvironment environment = new JobEnvironment(); environment.Variables[Constants.Variables.System.Culture] = "en-US"; environment.SystemConnection = new ServiceEndpoint() { Name = WellKnownServiceEndpointNames.SystemVssConnection, Url = new Uri("https://test.visualstudio.com"), Authorization = new EndpointAuthorization() { Scheme = "Test", } }; environment.SystemConnection.Authorization.Parameters["AccessToken"] = "token"; List tasks = new List(); Guid JobId = Guid.NewGuid(); _message = Pipelines.AgentJobRequestMessageUtil.Convert(new AgentJobRequestMessage(plan, timeline, JobId, testName, testName, environment, tasks)); _message.Resources.Repositories.Add(new Pipelines.RepositoryResource() { Alias = Pipelines.PipelineConstants.SelfAlias, Id = "github", Version = "sha1" }); _message.ContextData.Add("github", new Pipelines.ContextData.DictionaryContextData()); _initResult.Clear(); _jobExtension.Setup(x => x.InitializeJob(It.IsAny(), It.IsAny())). Returns(Task.FromResult(_initResult)); _proxyConfig.Setup(x => x.ProxyAddress) .Returns(string.Empty); var settings = new RunnerSettings { AgentId = 1, AgentName = "agent1", ServerUrl = "https://test.visualstudio.com", WorkFolder = "_work", }; _config.Setup(x => x.GetSettings()) .Returns(settings); _logger.Setup(x => x.Setup(It.IsAny(), It.IsAny())); hc.SetSingleton(_config.Object); hc.SetSingleton(_jobServer.Object); hc.SetSingleton(_jobServerQueue.Object); hc.SetSingleton(_proxyConfig.Object); hc.SetSingleton(_cert.Object); hc.SetSingleton(_stepRunner.Object); hc.SetSingleton(_extensions.Object); hc.SetSingleton(_temp.Object); hc.SetSingleton(_diagnosticLogManager.Object); hc.EnqueueInstance(_jobEc); hc.EnqueueInstance(_logger.Object); hc.EnqueueInstance(_jobExtension.Object); return hc; } [Fact] [Trait("Level", "L0")] [Trait("Category", "Worker")] public async Task JobExtensionInitializeFailure() { using (TestHostContext hc = CreateTestContext()) { _jobExtension.Setup(x => x.InitializeJob(It.IsAny(), It.IsAny())) .Throws(new Exception()); await _jobRunner.RunAsync(_message, _tokenSource.Token); Assert.Equal(TaskResult.Failed, _jobEc.Result); _stepRunner.Verify(x => x.RunAsync(It.IsAny()), Times.Never); } } [Fact] [Trait("Level", "L0")] [Trait("Category", "Worker")] public async Task JobExtensionInitializeCancelled() { using (TestHostContext hc = CreateTestContext()) { _jobExtension.Setup(x => x.InitializeJob(It.IsAny(), It.IsAny())) .Throws(new OperationCanceledException()); _tokenSource.Cancel(); await _jobRunner.RunAsync(_message, _tokenSource.Token); Assert.Equal(TaskResult.Canceled, _jobEc.Result); _stepRunner.Verify(x => x.RunAsync(It.IsAny()), Times.Never); } } // TODO: Move these tests over to JobExtensionL0.cs // [Fact] // [Trait("Level", "L0")] // [Trait("Category", "Worker")] // public async Task UploadDiganosticLogIfEnvironmentVariableSet() // { // using (TestHostContext hc = CreateTestContext()) // { // _message.Variables[Constants.Variables.Actions.RunnerDebug] = "true"; // await _jobRunner.RunAsync(_message, _tokenSource.Token); // _diagnosticLogManager.Verify(x => // x.UploadDiagnosticLogsAsync( // It.IsAny(), // It.IsAny(), // It.IsAny()), // Times.Once); // } // } // [Fact] // [Trait("Level", "L0")] // [Trait("Category", "Worker")] // public async Task DontUploadDiagnosticLogIfEnvironmentVariableFalse() // { // using (TestHostContext hc = CreateTestContext()) // { // _message.Variables[Constants.Variables.Actions.RunnerDebug] = "false"; // await _jobRunner.RunAsync(_message, _tokenSource.Token); // _diagnosticLogManager.Verify(x => // x.UploadDiagnosticLogsAsync( // It.IsAny(), // It.IsAny(), // It.IsAny()), // Times.Never); // } // } // [Fact] // [Trait("Level", "L0")] // [Trait("Category", "Worker")] // public async Task DontUploadDiagnosticLogIfEnvironmentVariableMissing() // { // using (TestHostContext hc = CreateTestContext()) // { // await _jobRunner.RunAsync(_message, _tokenSource.Token); // _diagnosticLogManager.Verify(x => // x.UploadDiagnosticLogsAsync( // It.IsAny(), // It.IsAny(), // It.IsAny()), // Times.Never); // } // } } }