public void InitializeJob_should_set_JobSettings() { using (TestHostContext hc = CreateTestContext()) using (var ec = new Agent.Worker.ExecutionContext()) { // Arrange: Create a job request message. TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); TimelineReference timeline = new TimelineReference(); JobEnvironment environment = new JobEnvironment(); environment.SystemConnection = new ServiceEndpoint(); List <TaskInstance> tasks = new List <TaskInstance>(); Guid JobId = Guid.NewGuid(); string jobName = "some job name"; var jobRequest = Pipelines.AgentJobRequestMessageUtil.Convert(new AgentJobRequestMessage(plan, timeline, JobId, jobName, jobName, environment, tasks)); // Arrange: Setup the paging logger. var pagingLogger = new Mock <IPagingLogger>(); hc.EnqueueInstance(pagingLogger.Object); ec.Initialize(hc); // Act. ec.InitializeJob(jobRequest, CancellationToken.None); // Assert. Assert.NotNull(ec.JobSettings); Assert.Equal(Boolean.FalseString, ec.JobSettings[WellKnownJobSettings.HasMultipleCheckouts]); } }
public void InitializeJob_LogsWarningsFromVariables() { using (TestHostContext hc = CreateTestContext()) { // Arrange: Create a job request message. TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); TimelineReference timeline = new TimelineReference(); JobEnvironment environment = new JobEnvironment(); environment.SystemConnection = new ServiceEndpoint(); environment.Variables["v1"] = "v1-$(v2)"; environment.Variables["v2"] = "v2-$(v1)"; List <TaskInstance> tasks = new List <TaskInstance>(); Guid JobId = Guid.NewGuid(); string jobName = "some job name"; var jobRequest = new AgentJobRequestMessage(plan, timeline, JobId, jobName, environment, tasks); // Arrange: Setup the paging logger. var pagingLogger = new Mock <IPagingLogger>(); hc.EnqueueInstance(pagingLogger.Object); var ec = new Agent.Worker.ExecutionContext(); ec.Initialize(hc); // Act. ec.InitializeJob(jobRequest, CancellationToken.None); // Assert. pagingLogger.Verify(x => x.Write(It.Is <string>(y => y.IndexOf("##[warning]") >= 0)), Times.Exactly(2)); } }
public void InitializeJob_should_mark_primary_repository_in_multicheckout() { // Note: the primary repository is defined as the first repository that is checked out in the job using (TestHostContext hc = CreateTestContext()) using (var ec = new Agent.Worker.ExecutionContext()) { // Arrange: Create a job request message. TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); TimelineReference timeline = new TimelineReference(); JobEnvironment environment = new JobEnvironment(); environment.SystemConnection = new ServiceEndpoint(); List <TaskInstance> tasks = new List <TaskInstance>(); tasks.Add(new TaskInstance() { Id = Pipelines.PipelineConstants.CheckoutTask.Id, Version = Pipelines.PipelineConstants.CheckoutTask.Version, Inputs = { { Pipelines.PipelineConstants.CheckoutTaskInputs.Repository, "repo2" } } }); tasks.Add(new TaskInstance() { Id = Pipelines.PipelineConstants.CheckoutTask.Id, Version = Pipelines.PipelineConstants.CheckoutTask.Version, Inputs = { { Pipelines.PipelineConstants.CheckoutTaskInputs.Repository, "repo3" } } }); Guid JobId = Guid.NewGuid(); string jobName = "some job name"; var jobRequest = Pipelines.AgentJobRequestMessageUtil.Convert(new AgentJobRequestMessage(plan, timeline, JobId, jobName, jobName, environment, tasks)); var repo1 = new Pipelines.RepositoryResource() { Alias = "self" }; var repo2 = new Pipelines.RepositoryResource() { Alias = "repo2" }; var repo3 = new Pipelines.RepositoryResource() { Alias = "repo3" }; jobRequest.Resources.Repositories.Add(repo1); jobRequest.Resources.Repositories.Add(repo2); jobRequest.Resources.Repositories.Add(repo3); // Arrange: Setup the paging logger. var pagingLogger = new Mock <IPagingLogger>(); hc.EnqueueInstance(pagingLogger.Object); ec.Initialize(hc); // Act. ec.InitializeJob(jobRequest, CancellationToken.None); // Assert. Assert.NotNull(ec.JobSettings); Assert.Equal(Boolean.TrueString, ec.JobSettings[WellKnownJobSettings.HasMultipleCheckouts]); Assert.Equal("repo2", ec.JobSettings[WellKnownJobSettings.FirstRepositoryCheckedOut]); Assert.Equal(Boolean.FalseString, repo1.Properties.Get <string>(RepositoryUtil.IsPrimaryRepository, Boolean.FalseString)); Assert.Equal(Boolean.TrueString, repo2.Properties.Get <string>(RepositoryUtil.IsPrimaryRepository, Boolean.FalseString)); Assert.Equal(Boolean.FalseString, repo3.Properties.Get <string>(RepositoryUtil.IsPrimaryRepository, Boolean.FalseString)); } }
public void TranslatePathForStepTarget_should_convert_path_only_for_containers(bool isCheckout, string jobTarget, string stepTarget) { // Note: the primary repository is defined as the first repository that is checked out in the job using (TestHostContext hc = CreateTestContext()) using (var ec = new Agent.Worker.ExecutionContext()) { ec.Initialize(hc); // Arrange: Create a container. var pipeContainer = new Pipelines.ContainerResource { Alias = "container" }; pipeContainer.Properties.Set <string>("image", "someimage"); // Arrange: Create a job request message. TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); TimelineReference timeline = new TimelineReference(); JobEnvironment environment = new JobEnvironment(); environment.SystemConnection = new ServiceEndpoint(); List <Pipelines.JobStep> steps = new List <Pipelines.JobStep>(); steps.Add(new Pipelines.TaskStep { Target = new Pipelines.StepTarget { Target = stepTarget }, Reference = new Pipelines.TaskStepDefinitionReference() }); var resources = new Pipelines.JobResources(); resources.Containers.Add(pipeContainer); Guid JobId = Guid.NewGuid(); string jobName = "some job name"; var jobRequest = new Pipelines.AgentJobRequestMessage(plan, timeline, JobId, jobName, jobName, jobTarget, new Dictionary <string, string>(), new Dictionary <string, VariableValue>(), new List <MaskHint>(), resources, new Pipelines.WorkspaceOptions(), steps); // Arrange var pagingLogger = new Mock <IPagingLogger>(); hc.EnqueueInstance(pagingLogger.Object); // Act. ec.InitializeJob(jobRequest, CancellationToken.None); ec.SetStepTarget(steps[0].Target); ec.Variables.Set(Constants.Variables.Task.SkipTranslatorForCheckout, isCheckout.ToString()); string stringBeforeTranslation = hc.GetDirectory(WellKnownDirectory.Work); string stringAfterTranslation = ec.TranslatePathForStepTarget(stringBeforeTranslation); // Assert. if ((stepTarget == "container") || (isCheckout is false && jobTarget == "container" && stepTarget == null)) { string stringContainer = "C:\\__w"; if (ec.StepTarget().ExecutionOS != PlatformUtil.OS.Windows) { stringContainer = "/__w"; } Assert.Equal(stringContainer, stringAfterTranslation); }
private JobRequestMessage CreateJobRequestMessage() { TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); TimelineReference timeline = null; JobEnvironment environment = new JobEnvironment(); List<TaskInstance> tasks = new List<TaskInstance>(); Guid JobId = Guid.NewGuid(); var jobRequest = new JobRequestMessage(plan, timeline, JobId, "someJob", environment, tasks); return jobRequest; }
private Pipelines.AgentJobRequestMessage CreateJobRequestMessage() { TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); TimelineReference timeline = null; JobEnvironment environment = new JobEnvironment(); List <TaskInstance> tasks = new List <TaskInstance>(); Guid JobId = Guid.NewGuid(); var jobRequest = new AgentJobRequestMessage(plan, timeline, JobId, "someJob", "someJob", environment, tasks); return(Pipelines.AgentJobRequestMessageUtil.Convert(jobRequest)); }
private JobRequestMessage CreateJobRequestMessage(string jobName) { TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); TimelineReference timeline = null; JobEnvironment environment = new JobEnvironment(); environment.Variables[Constants.Variables.System.Culture] = "en-US"; List<TaskInstance> tasks = new List<TaskInstance>(); Guid JobId = Guid.NewGuid(); var jobRequest = new JobRequestMessage(plan, timeline, JobId, jobName, environment, tasks); return jobRequest; }
private AgentJobRequestMessage CreateJobRequestMessage(string jobName) { TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); TimelineReference timeline = null; JobEnvironment environment = new JobEnvironment(); List <TaskInstance> tasks = new List <TaskInstance>(); Guid JobId = Guid.NewGuid(); var jobRequest = new AgentJobRequestMessage(plan, timeline, JobId, jobName, environment, tasks); return(jobRequest as AgentJobRequestMessage); }
public void SidecarContainers_VerifyNotJobContainers() { using (TestHostContext hc = CreateTestContext()) using (var ec = new Agent.Worker.ExecutionContext()) { ec.Initialize(hc); var pipeContainer = new Pipelines.ContainerResource { Alias = "container" }; var pipeContainerSidecar = new Pipelines.ContainerResource { Alias = "sidecar" }; var pipeContainerExtra = new Pipelines.ContainerResource { Alias = "extra" }; pipeContainer.Properties.Set <string>("image", "someimage"); pipeContainerSidecar.Properties.Set <string>("image", "someimage"); pipeContainerExtra.Properties.Set <string>("image", "someimage"); // Arrange: Create a job request message. TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); TimelineReference timeline = new TimelineReference(); JobEnvironment environment = new JobEnvironment(); environment.SystemConnection = new ServiceEndpoint(); List <Pipelines.JobStep> steps = new List <Pipelines.JobStep>(); steps.Add(new Pipelines.TaskStep { Reference = new Pipelines.TaskStepDefinitionReference() }); var resources = new Pipelines.JobResources(); resources.Containers.Add(pipeContainer); resources.Containers.Add(pipeContainerSidecar); resources.Containers.Add(pipeContainerExtra); Guid JobId = Guid.NewGuid(); string jobName = "some job name"; var sidecarContainers = new Dictionary <string, string>(); sidecarContainers.Add("sidecar", "sidecar"); var jobRequest = new Pipelines.AgentJobRequestMessage(plan, timeline, JobId, jobName, jobName, null, sidecarContainers, new Dictionary <string, VariableValue>(), new List <MaskHint>(), resources, new Pipelines.WorkspaceOptions(), steps); // Arrange: Setup command manager var pagingLogger = new Mock <IPagingLogger>(); hc.EnqueueInstance(pagingLogger.Object); // Act. ec.InitializeJob(jobRequest, CancellationToken.None); // Assert. Assert.Equal(2, ec.Containers.Count()); Assert.Equal(1, ec.SidecarContainers.Count()); Assert.False(ec.SidecarContainers.First().IsJobContainer); } }
private JobRequestMessage CreateJobRequestMessage(string jobName) { TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); TimelineReference timeline = null; JobEnvironment environment = new JobEnvironment(); environment.Variables[Constants.Variables.System.Culture] = "en-US"; List <TaskInstance> tasks = new List <TaskInstance>(); Guid JobId = Guid.NewGuid(); var jobRequest = new JobRequestMessage(plan, timeline, JobId, jobName, environment, tasks); return(jobRequest); }
private JobRequestMessage CreateJobRequestMessage() { TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); TimelineReference timeline = new TimelineReference(); JobEnvironment environment = new JobEnvironment(); environment.SystemConnection = new ServiceEndpoint(); environment.Variables["v1"] = "v1"; List <TaskInstance> tasks = new List <TaskInstance>(); Guid JobId = Guid.NewGuid(); string jobName = "some job name"; return(new AgentJobRequestMessage(plan, timeline, JobId, jobName, environment, tasks)); }
public void StepTarget_RestrictedCommands_Host() { using (TestHostContext hc = CreateTestContext()) using (var ec = new Agent.Worker.ExecutionContext()) { ec.Initialize(hc); var pipeContainer = new Pipelines.ContainerResource { Alias = "container" }; pipeContainer.Properties.Set <string>("image", "someimage"); // Arrange: Create a job request message. TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); TimelineReference timeline = new TimelineReference(); JobEnvironment environment = new JobEnvironment(); environment.SystemConnection = new ServiceEndpoint(); List <Pipelines.JobStep> steps = new List <Pipelines.JobStep>(); steps.Add(new Pipelines.TaskStep { Target = new Pipelines.StepTarget { Target = "host", Commands = "restricted" }, Reference = new Pipelines.TaskStepDefinitionReference() }); var resources = new Pipelines.JobResources(); resources.Containers.Add(pipeContainer); Guid JobId = Guid.NewGuid(); string jobName = "some job name"; var jobRequest = new Pipelines.AgentJobRequestMessage(plan, timeline, JobId, jobName, jobName, null, new Dictionary <string, string>(), new Dictionary <string, VariableValue>(), new List <MaskHint>(), resources, new Pipelines.WorkspaceOptions(), steps); // Arrange: Setup command manager var commandMock = new Mock <IWorkerCommandManager>(); hc.SetSingleton(commandMock.Object); var pagingLogger = new Mock <IPagingLogger>(); hc.EnqueueInstance(pagingLogger.Object); // Act. ec.InitializeJob(jobRequest, CancellationToken.None); ec.SetStepTarget(steps[0].Target); // Assert. Assert.IsType <HostInfo>(ec.StepTarget()); commandMock.Verify(x => x.SetCommandRestrictionPolicy(It.IsAny <AttributeBasedWorkerCommandRestrictionPolicy>())); } }
public void InitializeJob_LogsWarningsFromVariables() { using (TestHostContext hc = new TestHostContext(this)) { // Arrange: Create a job request message. TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); TimelineReference timeline = new TimelineReference(); JobEnvironment environment = new JobEnvironment(); environment.SystemConnection = new ServiceEndpoint(); environment.Variables["v1"] = "v1-$(v2)"; environment.Variables["v2"] = "v2-$(v1)"; List <TaskInstance> tasks = new List <TaskInstance>(); Guid JobId = Guid.NewGuid(); string jobName = "some job name"; var jobRequest = new AgentJobRequestMessage(plan, timeline, JobId, jobName, environment, tasks); // Arrange: Setup the configation store. var configurationStore = new Mock <IConfigurationStore>(); configurationStore.Setup(x => x.GetSettings()).Returns(new AgentSettings()); hc.SetSingleton(configurationStore.Object); // Arrange: Setup the secret masker. var secretMasker = new Mock <ISecretMasker>(); secretMasker.Setup(x => x.MaskSecrets(It.IsAny <string>())) .Returns((string x) => x); hc.SetSingleton(secretMasker.Object); // Arrange: Setup the paging logger. var pagingLogger = new Mock <IPagingLogger>(); hc.EnqueueInstance(pagingLogger.Object); // Arrange: Setup the proxy configation. var proxy = new Mock <IProxyConfiguration>(); hc.SetSingleton(proxy.Object); // Arrange: Create the execution context. hc.SetSingleton(new Mock <IJobServerQueue>().Object); var ec = new Microsoft.VisualStudio.Services.Agent.Worker.ExecutionContext(); ec.Initialize(hc); // Act. ec.InitializeJob(jobRequest, CancellationToken.None); // Assert. pagingLogger.Verify(x => x.Write(It.Is <string>(y => y.IndexOf("##[warning]") >= 0)), Times.Exactly(2)); } }
private JobRequestMessage CreateJobRequestMessage(string jobName) { TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); TimelineReference timeline = null; JobEnvironment environment = new JobEnvironment(); var serviceEndpoint = new ServiceEndpoint(); serviceEndpoint.Authorization = new EndpointAuthorization(); serviceEndpoint.Authorization.Parameters.Add("nullValue", null); environment.Endpoints.Add(serviceEndpoint); environment.Variables[Constants.Variables.System.Culture] = "en-US"; List <TaskInstance> tasks = new List <TaskInstance>(); Guid JobId = Guid.NewGuid(); var jobRequest = new AgentJobRequestMessage(plan, timeline, JobId, jobName, environment, tasks); return(jobRequest); }
public void AddIssue_CountWarningsErrors() { using (TestHostContext hc = CreateTestContext()) using (var ec = new Agent.Worker.ExecutionContext()) { // Arrange: Create a job request message. TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); TimelineReference timeline = new TimelineReference(); JobEnvironment environment = new JobEnvironment(); environment.SystemConnection = new ServiceEndpoint(); List <TaskInstance> tasks = new List <TaskInstance>(); Guid JobId = Guid.NewGuid(); string jobName = "some job name"; var jobRequest = Pipelines.AgentJobRequestMessageUtil.Convert(new AgentJobRequestMessage(plan, timeline, JobId, jobName, jobName, environment, tasks)); // Arrange: Setup the paging logger. var pagingLogger = new Mock <IPagingLogger>(); var jobServerQueue = new Mock <IJobServerQueue>(); jobServerQueue.Setup(x => x.QueueTimelineRecordUpdate(It.IsAny <Guid>(), It.IsAny <TimelineRecord>())); hc.EnqueueInstance(pagingLogger.Object); hc.SetSingleton(jobServerQueue.Object); ec.Initialize(hc); // Act. ec.InitializeJob(jobRequest, CancellationToken.None); ec.AddIssue(new Issue() { Type = IssueType.Error, Message = "error" }); ec.AddIssue(new Issue() { Type = IssueType.Error, Message = "error" }); ec.AddIssue(new Issue() { Type = IssueType.Error, Message = "error" }); ec.AddIssue(new Issue() { Type = IssueType.Error, Message = "error" }); ec.AddIssue(new Issue() { Type = IssueType.Error, Message = "error" }); ec.AddIssue(new Issue() { Type = IssueType.Error, Message = "error" }); ec.AddIssue(new Issue() { Type = IssueType.Error, Message = "error" }); ec.AddIssue(new Issue() { Type = IssueType.Error, Message = "error" }); ec.AddIssue(new Issue() { Type = IssueType.Error, Message = "error" }); ec.AddIssue(new Issue() { Type = IssueType.Error, Message = "error" }); ec.AddIssue(new Issue() { Type = IssueType.Error, Message = "error" }); ec.AddIssue(new Issue() { Type = IssueType.Error, Message = "error" }); ec.AddIssue(new Issue() { Type = IssueType.Error, Message = "error" }); ec.AddIssue(new Issue() { Type = IssueType.Error, Message = "error" }); ec.AddIssue(new Issue() { Type = IssueType.Error, Message = "error" }); ec.AddIssue(new Issue() { Type = IssueType.Warning, Message = "warning" }); ec.AddIssue(new Issue() { Type = IssueType.Warning, Message = "warning" }); ec.AddIssue(new Issue() { Type = IssueType.Warning, Message = "warning" }); ec.AddIssue(new Issue() { Type = IssueType.Warning, Message = "warning" }); ec.AddIssue(new Issue() { Type = IssueType.Warning, Message = "warning" }); ec.AddIssue(new Issue() { Type = IssueType.Warning, Message = "warning" }); ec.AddIssue(new Issue() { Type = IssueType.Warning, Message = "warning" }); ec.AddIssue(new Issue() { Type = IssueType.Warning, Message = "warning" }); ec.AddIssue(new Issue() { Type = IssueType.Warning, Message = "warning" }); ec.AddIssue(new Issue() { Type = IssueType.Warning, Message = "warning" }); ec.AddIssue(new Issue() { Type = IssueType.Warning, Message = "warning" }); ec.AddIssue(new Issue() { Type = IssueType.Warning, Message = "warning" }); ec.AddIssue(new Issue() { Type = IssueType.Warning, Message = "warning" }); ec.AddIssue(new Issue() { Type = IssueType.Warning, Message = "warning" }); ec.Complete(); // Assert. jobServerQueue.Verify(x => x.QueueTimelineRecordUpdate(It.IsAny <Guid>(), It.Is <TimelineRecord>(t => t.ErrorCount == 15)), Times.AtLeastOnce); jobServerQueue.Verify(x => x.QueueTimelineRecordUpdate(It.IsAny <Guid>(), It.Is <TimelineRecord>(t => t.WarningCount == 14)), Times.AtLeastOnce); jobServerQueue.Verify(x => x.QueueTimelineRecordUpdate(It.IsAny <Guid>(), It.Is <TimelineRecord>(t => t.Issues.Where(i => i.Type == IssueType.Error).Count() == 10)), Times.AtLeastOnce); jobServerQueue.Verify(x => x.QueueTimelineRecordUpdate(It.IsAny <Guid>(), It.Is <TimelineRecord>(t => t.Issues.Where(i => i.Type == IssueType.Warning).Count() == 10)), Times.AtLeastOnce); } }
public async Task RunIPCEndToEnd() { using (var server = new ProcessChannel()) { JobRequestMessage result = null; TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); TimelineReference timeline = null; JobEnvironment environment = new JobEnvironment(); List <TaskInstance> tasks = new List <TaskInstance>(); Guid JobId = Guid.NewGuid(); var jobRequest = new JobRequestMessage(plan, timeline, JobId, "someJob", environment, tasks); Process jobProcess; server.StartServer((p1, p2) => { string clientFileName = $"Test{IOUtil.ExeExtension}"; jobProcess = new Process(); jobProcess.StartInfo.FileName = clientFileName; jobProcess.StartInfo.Arguments = "spawnclient " + p1 + " " + p2; jobProcess.EnableRaisingEvents = true; jobProcess.Start(); }); var cs = new CancellationTokenSource(); await server.SendAsync(MessageType.NewJobRequest, JsonUtility.ToString(jobRequest), cs.Token); var packetReceiveTask = server.ReceiveAsync(cs.Token); Task[] taskToWait = { packetReceiveTask, Task.Delay(5000) }; await Task.WhenAny(taskToWait); bool timedOut = !packetReceiveTask.IsCompleted; // Wait until response is received if (timedOut) { cs.Cancel(); try { await packetReceiveTask; } catch (OperationCanceledException) { // Ignore OperationCanceledException and TaskCanceledException exceptions } catch (AggregateException errors) { // Ignore OperationCanceledException and TaskCanceledException exceptions errors.Handle(e => e is OperationCanceledException); } } else { result = JsonUtility.FromString <JobRequestMessage>(packetReceiveTask.Result.Body); } // Wait until response is received if (timedOut) { Assert.True(false, "Test timed out."); } else { Assert.True(jobRequest.JobId.Equals(result.JobId) && jobRequest.JobName.Equals(result.JobName)); } } }
private TestHostContext CreateTestContext(CancellationTokenSource _tokenSource, [CallerMemberName] String testName = "") { var hc = new TestHostContext(this, testName); _jobEc = new Agent.Worker.ExecutionContext(); _taskManager = new Mock <ITaskManager>(); _jobServerQueue = new Mock <IJobServerQueue>(); _config = new Mock <IConfigurationStore>(); _logger = new Mock <IPagingLogger>(); _proxy = new Mock <IVstsAgentWebProxy>(); _cert = new Mock <IAgentCertificateManager>(); _express = new Mock <IExpressionManager>(); _containerProvider = new Mock <IContainerOperationProvider>(); _logPlugin = new Mock <IAgentLogPlugin>(); TaskRunner step1 = new TaskRunner(); TaskRunner step2 = new TaskRunner(); TaskRunner step3 = new TaskRunner(); TaskRunner step4 = new TaskRunner(); TaskRunner step5 = new TaskRunner(); TaskRunner step6 = new TaskRunner(); TaskRunner step7 = new TaskRunner(); TaskRunner step8 = new TaskRunner(); TaskRunner step9 = new TaskRunner(); TaskRunner step10 = new TaskRunner(); TaskRunner step11 = new TaskRunner(); TaskRunner step12 = new TaskRunner(); _logger.Setup(x => x.Setup(It.IsAny <Guid>(), It.IsAny <Guid>())); var settings = new AgentSettings { AgentId = 1, AgentName = "agent1", ServerUrl = "https://test.visualstudio.com", WorkFolder = "_work", }; _config.Setup(x => x.GetSettings()) .Returns(settings); _proxy.Setup(x => x.ProxyAddress) .Returns(string.Empty); 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 <TaskInstance> tasks = new List <TaskInstance>() { new TaskInstance() { InstanceId = Guid.NewGuid(), DisplayName = "task1", }, new TaskInstance() { InstanceId = Guid.NewGuid(), DisplayName = "task2", }, new TaskInstance() { InstanceId = Guid.NewGuid(), DisplayName = "task3", }, new TaskInstance() { InstanceId = Guid.NewGuid(), DisplayName = "task4", }, new TaskInstance() { InstanceId = Guid.NewGuid(), DisplayName = "task5", }, new TaskInstance() { InstanceId = Guid.NewGuid(), DisplayName = "task6", }, new TaskInstance() { InstanceId = Guid.NewGuid(), DisplayName = "task7", }, }; Guid JobId = Guid.NewGuid(); _message = Pipelines.AgentJobRequestMessageUtil.Convert(new AgentJobRequestMessage(plan, timeline, JobId, testName, testName, environment, tasks)); _taskManager.Setup(x => x.DownloadAsync(It.IsAny <IExecutionContext>(), It.IsAny <IEnumerable <Pipelines.TaskStep> >())) .Returns(Task.CompletedTask); _taskManager.Setup(x => x.Load(It.Is <Pipelines.TaskStep>(t => t.DisplayName == "task1"))) .Returns(new Definition() { Data = new DefinitionData() { PreJobExecution = null, Execution = new ExecutionData(), PostJobExecution = null, }, }); _taskManager.Setup(x => x.Load(It.Is <Pipelines.TaskStep>(t => t.DisplayName == "task2"))) .Returns(new Definition() { Data = new DefinitionData() { PreJobExecution = new ExecutionData(), Execution = new ExecutionData(), PostJobExecution = new ExecutionData(), }, }); _taskManager.Setup(x => x.Load(It.Is <Pipelines.TaskStep>(t => t.DisplayName == "task3"))) .Returns(new Definition() { Data = new DefinitionData() { PreJobExecution = new ExecutionData(), Execution = null, PostJobExecution = new ExecutionData(), }, }); _taskManager.Setup(x => x.Load(It.Is <Pipelines.TaskStep>(t => t.DisplayName == "task4"))) .Returns(new Definition() { Data = new DefinitionData() { PreJobExecution = new ExecutionData(), Execution = null, PostJobExecution = null, }, }); _taskManager.Setup(x => x.Load(It.Is <Pipelines.TaskStep>(t => t.DisplayName == "task5"))) .Returns(new Definition() { Data = new DefinitionData() { PreJobExecution = null, Execution = null, PostJobExecution = new ExecutionData(), }, }); _taskManager.Setup(x => x.Load(It.Is <Pipelines.TaskStep>(t => t.DisplayName == "task6"))) .Returns(new Definition() { Data = new DefinitionData() { PreJobExecution = new ExecutionData(), Execution = new ExecutionData(), PostJobExecution = null, }, }); _taskManager.Setup(x => x.Load(It.Is <Pipelines.TaskStep>(t => t.DisplayName == "task7"))) .Returns(new Definition() { Data = new DefinitionData() { PreJobExecution = null, Execution = new ExecutionData(), PostJobExecution = new ExecutionData(), }, }); hc.SetSingleton(_taskManager.Object); hc.SetSingleton(_config.Object); hc.SetSingleton(_jobServerQueue.Object); hc.SetSingleton(_proxy.Object); hc.SetSingleton(_cert.Object); hc.SetSingleton(_express.Object); hc.SetSingleton(_containerProvider.Object); hc.SetSingleton(_logPlugin.Object); hc.EnqueueInstance <IPagingLogger>(_logger.Object); // jobcontext logger hc.EnqueueInstance <IPagingLogger>(_logger.Object); // init step logger hc.EnqueueInstance <IPagingLogger>(_logger.Object); // step 1 hc.EnqueueInstance <IPagingLogger>(_logger.Object); hc.EnqueueInstance <IPagingLogger>(_logger.Object); hc.EnqueueInstance <IPagingLogger>(_logger.Object); hc.EnqueueInstance <IPagingLogger>(_logger.Object); hc.EnqueueInstance <IPagingLogger>(_logger.Object); hc.EnqueueInstance <IPagingLogger>(_logger.Object); hc.EnqueueInstance <IPagingLogger>(_logger.Object); hc.EnqueueInstance <IPagingLogger>(_logger.Object); hc.EnqueueInstance <IPagingLogger>(_logger.Object); hc.EnqueueInstance <IPagingLogger>(_logger.Object); hc.EnqueueInstance <IPagingLogger>(_logger.Object); // step 12 hc.EnqueueInstance <ITaskRunner>(step1); hc.EnqueueInstance <ITaskRunner>(step2); hc.EnqueueInstance <ITaskRunner>(step3); hc.EnqueueInstance <ITaskRunner>(step4); hc.EnqueueInstance <ITaskRunner>(step5); hc.EnqueueInstance <ITaskRunner>(step6); hc.EnqueueInstance <ITaskRunner>(step7); hc.EnqueueInstance <ITaskRunner>(step8); hc.EnqueueInstance <ITaskRunner>(step9); hc.EnqueueInstance <ITaskRunner>(step10); hc.EnqueueInstance <ITaskRunner>(step11); hc.EnqueueInstance <ITaskRunner>(step12); _jobEc.Initialize(hc); _jobEc.InitializeJob(_message, _tokenSource.Token); return(hc); }
private TestHostContext CreateTestContext([CallerMemberName] String testName = "") { var hc = new TestHostContext(this, testName); _jobEc = new Agent.Worker.ExecutionContext(); _config = new Mock <IConfigurationStore>(); _extensions = new Mock <IExtensionManager>(); _jobExtension = new Mock <IJobExtension>(); _jobServer = new Mock <IJobServer>(); _jobServerQueue = new Mock <IJobServerQueue>(); _proxyConfig = new Mock <IVstsAgentWebProxy>(); _cert = new Mock <IAgentCertificateManager>(); _taskServer = new Mock <ITaskServer>(); _stepRunner = new Mock <IStepsRunner>(); _logger = new Mock <IPagingLogger>(); _temp = new Mock <ITempDirectoryManager>(); _diagnosticLogManager = new Mock <IDiagnosticLogManager>(); if (_tokenSource != null) { _tokenSource.Dispose(); _tokenSource = null; } _tokenSource = new CancellationTokenSource(); var expressionManager = new ExpressionManager(); expressionManager.Initialize(hc); hc.SetSingleton <IExpressionManager>(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() { Url = new Uri("https://test.visualstudio.com"), Authorization = new EndpointAuthorization() { Scheme = "Test", } }; environment.SystemConnection.Authorization.Parameters["AccessToken"] = "token"; List <TaskInstance> tasks = new List <TaskInstance>(); Guid JobId = Guid.NewGuid(); _message = new AgentJobRequestMessage(plan, timeline, JobId, testName, testName, environment, tasks); _extensions.Setup(x => x.GetExtensions <IJobExtension>()). Returns(new[] { _jobExtension.Object }.ToList()); _initResult.PreJobSteps.Clear(); _initResult.JobSteps.Clear(); _initResult.PostJobStep.Clear(); _jobExtension.Setup(x => x.InitializeJob(It.IsAny <IExecutionContext>(), It.IsAny <AgentJobRequestMessage>())). Returns(Task.FromResult(_initResult)); _jobExtension.Setup(x => x.HostType) .Returns <string>(null); _proxyConfig.Setup(x => x.ProxyAddress) .Returns(string.Empty); var settings = new AgentSettings { 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 <Guid>(), It.IsAny <Guid>())); hc.SetSingleton(_config.Object); hc.SetSingleton(_jobServer.Object); hc.SetSingleton(_jobServerQueue.Object); hc.SetSingleton(_proxyConfig.Object); hc.SetSingleton(_cert.Object); hc.SetSingleton(_taskServer.Object); hc.SetSingleton(_stepRunner.Object); hc.SetSingleton(_extensions.Object); hc.SetSingleton(_temp.Object); hc.SetSingleton(_diagnosticLogManager.Object); hc.EnqueueInstance <IExecutionContext>(_jobEc); hc.EnqueueInstance <IPagingLogger>(_logger.Object); return(hc); }
public async Task RunIPCEndToEnd() { using (var server = new ProcessChannel()) { JobRequestMessage result = null; TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); TimelineReference timeline = null; JobEnvironment environment = new JobEnvironment(); List<TaskInstance> tasks = new List<TaskInstance>(); Guid JobId = Guid.NewGuid(); var jobRequest = new JobRequestMessage(plan, timeline, JobId, "someJob", environment, tasks); Process jobProcess; server.StartServer((p1, p2) => { string clientFileName = $"Test{IOUtil.ExeExtension}"; jobProcess = new Process(); jobProcess.StartInfo.FileName = clientFileName; jobProcess.StartInfo.Arguments = "spawnclient " + p1 + " " + p2; jobProcess.EnableRaisingEvents = true; jobProcess.Start(); }); var cs = new CancellationTokenSource(); await server.SendAsync(MessageType.NewJobRequest, JsonUtility.ToString(jobRequest), cs.Token); var packetReceiveTask = server.ReceiveAsync(cs.Token); Task[] taskToWait = { packetReceiveTask, Task.Delay(30*1000) }; await Task.WhenAny(taskToWait); bool timedOut = !packetReceiveTask.IsCompleted; // Wait until response is received if (timedOut) { cs.Cancel(); try { await packetReceiveTask; } catch (OperationCanceledException) { // Ignore OperationCanceledException and TaskCanceledException exceptions } catch (AggregateException errors) { // Ignore OperationCanceledException and TaskCanceledException exceptions errors.Handle(e => e is OperationCanceledException); } } else { result = JsonUtility.FromString<JobRequestMessage>(packetReceiveTask.Result.Body); } // Wait until response is received if (timedOut) { Assert.True(false, "Test timed out."); } else { Assert.True(jobRequest.JobId.Equals(result.JobId) && jobRequest.JobName.Equals(result.JobName)); } } }