diff --git a/src/Runner.Worker/FileCommandManager.cs b/src/Runner.Worker/FileCommandManager.cs index f45528a5c17..b9470fba85f 100644 --- a/src/Runner.Worker/FileCommandManager.cs +++ b/src/Runner.Worker/FileCommandManager.cs @@ -317,7 +317,8 @@ public void ProcessCommand(IExecutionContext context, string filePath, Container var attachmentName = context.Id.ToString(); Trace.Info($"Queueing file ({filePath}) for attachment upload ({attachmentName})"); - context.QueueAttachFile(ChecksAttachmentType.StepSummary, attachmentName, scrubbedFilePath); + // Attachments must be added to the parent context (job), not the current context (step) + context.Root.QueueAttachFile(ChecksAttachmentType.StepSummary, attachmentName, scrubbedFilePath); } catch (Exception e) { diff --git a/src/Test/L0/Worker/CreateStepSummaryCommandL0.cs b/src/Test/L0/Worker/CreateStepSummaryCommandL0.cs index f75fb8e0170..3ce96b87efe 100644 --- a/src/Test/L0/Worker/CreateStepSummaryCommandL0.cs +++ b/src/Test/L0/Worker/CreateStepSummaryCommandL0.cs @@ -10,12 +10,15 @@ using Xunit; using DTWebApi = GitHub.DistributedTask.WebApi; using GitHub.DistributedTask.WebApi; +using Pipelines = GitHub.DistributedTask.Pipelines; namespace GitHub.Runner.Common.Tests.Worker { public sealed class CreateStepSummaryCommandL0 { private Mock _executionContext; + private Mock _jobServerQueue; + private ExecutionContext _jobExecutionContext; private List> _issues; private Variables _variables; private string _rootDirectory; @@ -30,9 +33,11 @@ public void CreateStepSummaryCommand_FeatureDisabled() using (var hostContext = Setup(featureFlagState: "false")) { var stepSummaryFile = Path.Combine(_rootDirectory, "feature-off"); + + _createStepCommand.ProcessCommand(_executionContext.Object, stepSummaryFile, null); + _jobExecutionContext.Complete(); - _executionContext.Verify(e => e.QueueAttachFile(ChecksAttachmentType.StepSummary, It.IsAny(), It.IsAny()), Times.Never()); - + _jobServerQueue.Verify(x => x.QueueFileUpload(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); Assert.Equal(0, _issues.Count); } } @@ -45,8 +50,9 @@ public void CreateStepSummaryCommand_FileNull() using (var hostContext = Setup()) { _createStepCommand.ProcessCommand(_executionContext.Object, null, null); + _jobExecutionContext.Complete(); - _executionContext.Verify(e => e.QueueAttachFile(ChecksAttachmentType.StepSummary, It.IsAny(), It.IsAny()), Times.Never()); + _jobServerQueue.Verify(x => x.QueueFileUpload(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); Assert.Equal(0, _issues.Count); } } @@ -61,8 +67,9 @@ public void CreateStepSummaryCommand_DirectoryNotFound() var stepSummaryFile = Path.Combine(_rootDirectory, "directory-not-found", "env"); _createStepCommand.ProcessCommand(_executionContext.Object, stepSummaryFile, null); + _jobExecutionContext.Complete(); - _executionContext.Verify(e => e.QueueAttachFile(ChecksAttachmentType.StepSummary, It.IsAny(), It.IsAny()), Times.Never()); + _jobServerQueue.Verify(x => x.QueueFileUpload(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); Assert.Equal(0, _issues.Count); } } @@ -77,8 +84,9 @@ public void CreateStepSummaryCommand_FileNotFound() var stepSummaryFile = Path.Combine(_rootDirectory, "file-not-found"); _createStepCommand.ProcessCommand(_executionContext.Object, stepSummaryFile, null); + _jobExecutionContext.Complete(); - _executionContext.Verify(e => e.QueueAttachFile(ChecksAttachmentType.StepSummary, It.IsAny(), It.IsAny()), Times.Never()); + _jobServerQueue.Verify(x => x.QueueFileUpload(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); Assert.Equal(0, _issues.Count); } } @@ -94,8 +102,9 @@ public void CreateStepSummaryCommand_EmptyFile() File.Create(stepSummaryFile).Dispose(); _createStepCommand.ProcessCommand(_executionContext.Object, stepSummaryFile, null); + _jobExecutionContext.Complete(); - _executionContext.Verify(e => e.QueueAttachFile(ChecksAttachmentType.StepSummary, It.IsAny(), It.IsAny()), Times.Never()); + _jobServerQueue.Verify(x => x.QueueFileUpload(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); Assert.Equal(0, _issues.Count); } } @@ -111,8 +120,9 @@ public void CreateStepSummaryCommand_LargeFile() File.WriteAllBytes(stepSummaryFile, new byte[128 * 1024 + 1]); _createStepCommand.ProcessCommand(_executionContext.Object, stepSummaryFile, null); + _jobExecutionContext.Complete(); - _executionContext.Verify(e => e.QueueAttachFile(ChecksAttachmentType.StepSummary, It.IsAny(), It.IsAny()), Times.Never()); + _jobServerQueue.Verify(x => x.QueueFileUpload(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); Assert.Equal(1, _issues.Count); } } @@ -134,8 +144,9 @@ public void CreateStepSummaryCommand_Simple() WriteContent(stepSummaryFile, content); _createStepCommand.ProcessCommand(_executionContext.Object, stepSummaryFile, null); + _jobExecutionContext.Complete(); - _executionContext.Verify(e => e.QueueAttachFile(ChecksAttachmentType.StepSummary, _executionContext.Object.Id.ToString(), stepSummaryFile + "-scrubbed"), Times.Once()); + _jobServerQueue.Verify(x => x.QueueFileUpload(It.IsAny(), It.IsAny(), ChecksAttachmentType.StepSummary, _executionContext.Object.Id.ToString(), stepSummaryFile + "-scrubbed", It.IsAny()), Times.Once()); Assert.Equal(0, _issues.Count); } } @@ -166,8 +177,9 @@ public void CreateStepSummaryCommand_ScrubSecrets() var scrubbedFileContents = File.ReadAllText(scrubbedFile); Assert.DoesNotContain("ThisIsMySecretPassword!", scrubbedFileContents); Assert.DoesNotContain("ghs_verysecuretoken", scrubbedFileContents); + _jobExecutionContext.Complete(); - _executionContext.Verify(e => e.QueueAttachFile(ChecksAttachmentType.StepSummary, _executionContext.Object.Id.ToString(), scrubbedFile), Times.Once()); + _jobServerQueue.Verify(x => x.QueueFileUpload(It.IsAny(), It.IsAny(), ChecksAttachmentType.StepSummary, _executionContext.Object.Id.ToString(), scrubbedFile, It.IsAny()), Times.Once()); Assert.Equal(0, _issues.Count); } } @@ -189,13 +201,43 @@ private void WriteContent( private TestHostContext Setup([CallerMemberName] string name = "", string featureFlagState = "true") { + var hostContext = new TestHostContext(this, name); + _issues = new List>(); - var hostContext = new TestHostContext(this, name); + // Setup a job request + TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); + TimelineReference timeline = new TimelineReference(); + Guid jobId = Guid.NewGuid(); + string jobName = "Summary Job"; + var jobRequest = new Pipelines.AgentJobRequestMessage(plan, timeline, jobId, jobName, jobName, null, null, null, new Dictionary(), new List(), new Pipelines.JobResources(), new Pipelines.ContextData.DictionaryContextData(), new Pipelines.WorkspaceOptions(), new List(), null, null, null, null); + jobRequest.Resources.Repositories.Add(new Pipelines.RepositoryResource() + { + Alias = Pipelines.PipelineConstants.SelfAlias, + Id = "github", + Version = "sha1" + }); + jobRequest.ContextData["github"] = new Pipelines.ContextData.DictionaryContextData(); + jobRequest.Variables["ACTIONS_STEP_DEBUG"] = "true"; + + // Server queue for job + _jobServerQueue = new Mock(); + _jobServerQueue.Setup(x => x.QueueTimelineRecordUpdate(It.IsAny(), It.IsAny())); + hostContext.SetSingleton(_jobServerQueue.Object); + + // Configuration store (required singleton) + var configurationStore = new Mock(); + configurationStore.Setup(x => x.GetSettings()).Returns(new RunnerSettings()); + hostContext.SetSingleton(configurationStore.Object); + + // Paging Logger (required singleton) + var pagingLogger = new Mock(); + hostContext.EnqueueInstance(pagingLogger.Object); // Trace _trace = hostContext.GetTrace(); + // Variables to test for secret scrubbing & FF options _variables = new Variables(hostContext, new Dictionary { { "MySecretName", new VariableValue("My secret value", true) }, @@ -209,7 +251,12 @@ private TestHostContext Setup([CallerMemberName] string name = "", string featur _rootDirectory = Path.Combine(workDirectory, nameof(CreateStepSummaryCommandL0)); Directory.CreateDirectory(_rootDirectory); - // Execution context + // Job execution context + _jobExecutionContext = new ExecutionContext(); + _jobExecutionContext.Initialize(hostContext); + _jobExecutionContext.InitializeJob(jobRequest, System.Threading.CancellationToken.None); + + // Step execution context _executionContext = new Mock(); _executionContext.Setup(x => x.Global) .Returns(new GlobalContext @@ -230,6 +277,7 @@ private TestHostContext Setup([CallerMemberName] string name = "", string featur { _trace.Info($"{tag}{message}"); }); + _executionContext.SetupGet(x => x.Root).Returns(_jobExecutionContext); //CreateStepSummaryCommand _createStepCommand = new CreateStepSummaryCommand(); @@ -238,4 +286,4 @@ private TestHostContext Setup([CallerMemberName] string name = "", string featur return hostContext; } } -} +} \ No newline at end of file