Skip to content

Commit

Permalink
Use job execution context instead of step for adding summary attachme…
Browse files Browse the repository at this point in the history
…nts (actions#1667)

* use job exec context to queue/attach summaries

* step summary tests: use job ctx and verify against server queue
  • Loading branch information
robherley authored and kamilrakoczy committed Aug 8, 2022
1 parent 607e6d0 commit c059972
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/Runner.Worker/FileCommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
72 changes: 60 additions & 12 deletions src/Test/L0/Worker/CreateStepSummaryCommandL0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IExecutionContext> _executionContext;
private Mock<IJobServerQueue> _jobServerQueue;
private ExecutionContext _jobExecutionContext;
private List<Tuple<DTWebApi.Issue, string>> _issues;
private Variables _variables;
private string _rootDirectory;
Expand All @@ -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<string>(), It.IsAny<string>()), Times.Never());

_jobServerQueue.Verify(x => x.QueueFileUpload(It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()), Times.Never());
Assert.Equal(0, _issues.Count);
}
}
Expand All @@ -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<string>(), It.IsAny<string>()), Times.Never());
_jobServerQueue.Verify(x => x.QueueFileUpload(It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()), Times.Never());
Assert.Equal(0, _issues.Count);
}
}
Expand All @@ -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<string>(), It.IsAny<string>()), Times.Never());
_jobServerQueue.Verify(x => x.QueueFileUpload(It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()), Times.Never());
Assert.Equal(0, _issues.Count);
}
}
Expand All @@ -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<string>(), It.IsAny<string>()), Times.Never());
_jobServerQueue.Verify(x => x.QueueFileUpload(It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()), Times.Never());
Assert.Equal(0, _issues.Count);
}
}
Expand All @@ -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<string>(), It.IsAny<string>()), Times.Never());
_jobServerQueue.Verify(x => x.QueueFileUpload(It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()), Times.Never());
Assert.Equal(0, _issues.Count);
}
}
Expand All @@ -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<string>(), It.IsAny<string>()), Times.Never());
_jobServerQueue.Verify(x => x.QueueFileUpload(It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()), Times.Never());
Assert.Equal(1, _issues.Count);
}
}
Expand All @@ -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<Guid>(), It.IsAny<Guid>(), ChecksAttachmentType.StepSummary, _executionContext.Object.Id.ToString(), stepSummaryFile + "-scrubbed", It.IsAny<bool>()), Times.Once());
Assert.Equal(0, _issues.Count);
}
}
Expand Down Expand Up @@ -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<Guid>(), It.IsAny<Guid>(), ChecksAttachmentType.StepSummary, _executionContext.Object.Id.ToString(), scrubbedFile, It.IsAny<bool>()), Times.Once());
Assert.Equal(0, _issues.Count);
}
}
Expand All @@ -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<Tuple<DTWebApi.Issue, string>>();

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<string, VariableValue>(), new List<MaskHint>(), new Pipelines.JobResources(), new Pipelines.ContextData.DictionaryContextData(), new Pipelines.WorkspaceOptions(), new List<Pipelines.ActionStep>(), 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<IJobServerQueue>();
_jobServerQueue.Setup(x => x.QueueTimelineRecordUpdate(It.IsAny<Guid>(), It.IsAny<TimelineRecord>()));
hostContext.SetSingleton(_jobServerQueue.Object);

// Configuration store (required singleton)
var configurationStore = new Mock<IConfigurationStore>();
configurationStore.Setup(x => x.GetSettings()).Returns(new RunnerSettings());
hostContext.SetSingleton(configurationStore.Object);

// Paging Logger (required singleton)
var pagingLogger = new Mock<IPagingLogger>();
hostContext.EnqueueInstance(pagingLogger.Object);

// Trace
_trace = hostContext.GetTrace();

// Variables to test for secret scrubbing & FF options
_variables = new Variables(hostContext, new Dictionary<string, VariableValue>
{
{ "MySecretName", new VariableValue("My secret value", true) },
Expand All @@ -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<IExecutionContext>();
_executionContext.Setup(x => x.Global)
.Returns(new GlobalContext
Expand All @@ -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();
Expand All @@ -238,4 +286,4 @@ private TestHostContext Setup([CallerMemberName] string name = "", string featur
return hostContext;
}
}
}
}

0 comments on commit c059972

Please sign in to comment.