Fix L0 tests, add/update runner release yaml. (#214)

This commit is contained in:
Tingluo Huang
2019-12-09 16:11:00 -05:00
committed by GitHub
parent d81a7656a4
commit 3ed80b7c10
12 changed files with 995 additions and 1372 deletions

184
.github/workflows/release.yml vendored Normal file
View File

@@ -0,0 +1,184 @@
name: Runner CD
on:
push:
paths:
- src/runnerversion_block # Change this to src/runnerversion when we are ready.
jobs:
build:
strategy:
matrix:
runtime: [ linux-x64, linux-arm64, linux-arm, win-x64, osx-x64 ]
include:
- runtime: linux-x64
os: ubuntu-latest
devScript: ./dev.sh
- runtime: linux-arm64
os: ubuntu-latest
devScript: ./dev.sh
- runtime: linux-arm
os: ubuntu-latest
devScript: ./dev.sh
- runtime: osx-x64
os: macOS-latest
devScript: ./dev.sh
- runtime: win-x64
os: windows-latest
devScript: ./dev
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v1
# Build runner layout
- name: Build & Layout Release
run: |
${{ matrix.devScript }} layout Release ${{ matrix.runtime }}
working-directory: src
# Run tests
- name: L0
run: |
${{ matrix.devScript }} test
working-directory: src
if: matrix.runtime != 'linux-arm64' && matrix.runtime != 'linux-arm'
# Create runner package tar.gz/zip
- name: Package Release
if: github.event_name != 'pull_request'
run: |
${{ matrix.devScript }} package Release
working-directory: src
# Upload runner package tar.gz/zip as artifact.
# Since each package name is unique, so we don't need to put ${{matrix}} info into artifact name
- name: Publish Artifact
if: github.event_name != 'pull_request'
uses: actions/upload-artifact@v1
with:
name: runner-packages
path: _package
release:
needs: build
runs-on: linux-latest
steps:
# Download runner package tar.gz/zip produced by 'build' job
- name: Download Artifact
uses: actions/download-artifact@v1
with:
name: runner-packages
# Create ReleaseNote file
- name: Create ReleaseNote
id: releaseNote
uses: actions/github-script@0.3.0
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const fs = require('fs');
// Get runner version from ./src/runnerVersion file
const versionContent = await github.repos.getContents({
owner: '${{ github.event.repository.owner.name }}',
repo: '${{ github.event.repository.name }}',
path: 'src/runnerversion'
ref: ${{ github.sha }}
})
const runnerVersion = Buffer.from(versionContent.data.content, 'base64').toString()
console.log("Runner Version ' + runnerVersion)
core.setOutput('version', runnerVersion);
// Query GitHub release ensure version is bumped
const latestRelease = await github.repos.getLatestRelease({
owner: '${{ github.event.repository.owner.name }}',
repo: '${{ github.event.repository.name }}'
})
console.log(latestRelease.name)
const latestReleaseVersion = latestRelease.name.substring(1)
const vLatest = latestReleaseVersion.split('.')
const vNew = runnerVersion.split('.')
let versionBumped = true
for (let i = 0; i < 3; ++i) {
var v1 = parseInt(vLatest[i], 10);
var v2 = parseInt(vNew[i], 10);
if (v2 > v1) {
console.log(runnerVersion + " > " + latestReleaseVersion + "(Latest)")
break
}
if (v1 > v2) {
versionBumped = false
core.setFailed(runnerVersion + " < " + latestReleaseVersion + "(Latest)")
break
}
}
// Generate release note
if (versionBumped) {
const releaseNoteContent = await github.repos.getContents({
owner: '${{ github.event.repository.owner.name }}',
repo: '${{ github.event.repository.name }}',
path: 'releaseNote.md'
ref: ${{ github.sha }}
})
const releaseNote = Buffer.from(releaseNoteContent.data.content, 'base64').toString().replace("<RUNNER_VERSION>", runnerVersion)
console.log(releaseNote)
core.setOutput('note', releaseNote);
}
# Create GitHub release
- uses: actions/create-release@v1
id: createRelease
name: Create ${{ steps.releaseNote.outputs.version }} Runner Release
with:
tag_name: "v${{ steps.releaseNote.outputs.version }}"
release_name: "v${{ steps.releaseNote.outputs.version }}"
body: ${{ steps.releaseNote.outputs.note }}
prerelease: true
# Upload release assets
- name: Upload Release Asset (win-x64)
uses: actions/upload-release-asset@v1.0.1
with:
upload_url: ${{ steps.createRelease.outputs.upload_url }}
asset_path: ./actions-runner-win-x64-${{ steps.releaseNote.outputs.version }}.zip
asset_name: actions-runner-win-x64-${{ steps.releaseNote.outputs.version }}.zip
asset_content_type: application/octet-stream
- name: Upload Release Asset (linux-x64)
uses: actions/upload-release-asset@v1.0.1
with:
upload_url: ${{ steps.createRelease.outputs.upload_url }}
asset_path: ./actions-runner-linux-x64-${{ steps.releaseNote.outputs.version }}.zip
asset_name: actions-runner-linux-x64-${{ steps.releaseNote.outputs.version }}.zip
asset_content_type: application/octet-stream
- name: Upload Release Asset (mac-x64)
uses: actions/upload-release-asset@v1.0.1
with:
upload_url: ${{ steps.createRelease.outputs.upload_url }}
asset_path: ./actions-runner-mac-x64-${{ steps.releaseNote.outputs.version }}.zip
asset_name: actions-runner-mac-x64-${{ steps.releaseNote.outputs.version }}.zip
asset_content_type: application/octet-stream
- name: Upload Release Asset (linux-arm)
uses: actions/upload-release-asset@v1.0.1
with:
upload_url: ${{ steps.createRelease.outputs.upload_url }}
asset_path: ./actions-runner-linux-arm-${{ steps.releaseNote.outputs.version }}.zip
asset_name: actions-runner-linux-arm-${{ steps.releaseNote.outputs.version }}.zip
asset_content_type: application/octet-stream
- name: Upload Release Asset (linux-arm64)
uses: actions/upload-release-asset@v1.0.1
with:
upload_url: ${{ steps.createRelease.outputs.upload_url }}
asset_path: ./actions-runner-linux-arm64-${{ steps.releaseNote.outputs.version }}.zip
asset_name: actions-runner-linux-arm64-${{ steps.releaseNote.outputs.version }}.zip
asset_content_type: application/octet-stream

View File

@@ -221,15 +221,17 @@ stages:
$releaseCreated = Invoke-RestMethod @releaseParams $releaseCreated = Invoke-RestMethod @releaseParams
Write-Host $releaseCreated Write-Host $releaseCreated
$releaseId = $releaseCreated.id $releaseId = $releaseCreated.id
$assets = [System.IO.File]::ReadAllText("$(Build.SourcesDirectory)\assets.json").Replace("<RUNNER_VERSION>","$(ReleaseAgentVersion)") Get-ChildItem -LiteralPath "$(System.ArtifactsDirectory)/runners" | ForEach-Object {
Write-Host "Uploading $_ as GitHub release assets"
$assetsParams = @{ $assetsParams = @{
Uri = "https://uploads.github.com/repos/actions/runner/releases/$releaseId/assets?name=assets.json" Uri = "https://uploads.github.com/repos/actions/runner/releases/$releaseId/assets?name=$($_.Name)"
Method = 'POST'; Method = 'POST';
Headers = @{ Headers = @{
Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("github:$(GithubToken)")); Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("github:$(GithubToken)"));
} }
ContentType = 'application/octet-stream'; ContentType = 'application/octet-stream';
Body = [system.Text.Encoding]::UTF8.GetBytes($assets) Body = [System.IO.File]::ReadAllBytes($_.FullName)
} }
Invoke-RestMethod @assetsParams Invoke-RestMethod @assetsParams
}
displayName: Create agent release on Github displayName: Create agent release on Github

View File

@@ -50,6 +50,13 @@ namespace GitHub.Runner.Common
public void Error(Exception exception) public void Error(Exception exception)
{ {
Trace(TraceEventType.Error, exception.ToString()); Trace(TraceEventType.Error, exception.ToString());
var innerEx = exception.InnerException;
while (innerEx != null)
{
Trace(TraceEventType.Error, "#####################################################");
Trace(TraceEventType.Error, innerEx.ToString());
innerEx = innerEx.InnerException;
}
} }
// Do not remove the non-format overload. // Do not remove the non-format overload.

View File

@@ -15,7 +15,7 @@ namespace GitHub.Runner.Worker
[ServiceLocator(Default = typeof(DiagnosticLogManager))] [ServiceLocator(Default = typeof(DiagnosticLogManager))]
public interface IDiagnosticLogManager : IRunnerService public interface IDiagnosticLogManager : IRunnerService
{ {
Task UploadDiagnosticLogsAsync(IExecutionContext executionContext, void UploadDiagnosticLogs(IExecutionContext executionContext,
IExecutionContext parentContext, IExecutionContext parentContext,
Pipelines.AgentJobRequestMessage message, Pipelines.AgentJobRequestMessage message,
DateTime jobStartTimeUtc); DateTime jobStartTimeUtc);
@@ -31,9 +31,7 @@ namespace GitHub.Runner.Worker
public sealed class DiagnosticLogManager : RunnerService, IDiagnosticLogManager public sealed class DiagnosticLogManager : RunnerService, IDiagnosticLogManager
{ {
private static string DateTimeFormat = "yyyyMMdd-HHmmss"; private static string DateTimeFormat = "yyyyMMdd-HHmmss";
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously (method has async logic on only certain platforms) public void UploadDiagnosticLogs(IExecutionContext executionContext,
public async Task UploadDiagnosticLogsAsync(IExecutionContext executionContext,
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
IExecutionContext parentContext, IExecutionContext parentContext,
Pipelines.AgentJobRequestMessage message, Pipelines.AgentJobRequestMessage message,
DateTime jobStartTimeUtc) DateTime jobStartTimeUtc)

View File

@@ -19,7 +19,7 @@ namespace GitHub.Runner.Worker
public interface IJobExtension : IRunnerService public interface IJobExtension : IRunnerService
{ {
Task<List<IStep>> InitializeJob(IExecutionContext jobContext, Pipelines.AgentJobRequestMessage message); Task<List<IStep>> InitializeJob(IExecutionContext jobContext, Pipelines.AgentJobRequestMessage message);
Task FinalizeJob(IExecutionContext jobContext, Pipelines.AgentJobRequestMessage message, DateTime jobStartTimeUtc); void FinalizeJob(IExecutionContext jobContext, Pipelines.AgentJobRequestMessage message, DateTime jobStartTimeUtc);
} }
public sealed class JobExtension : RunnerService, IJobExtension public sealed class JobExtension : RunnerService, IJobExtension
@@ -42,7 +42,6 @@ namespace GitHub.Runner.Worker
List<IStep> preJobSteps = new List<IStep>(); List<IStep> preJobSteps = new List<IStep>();
List<IStep> jobSteps = new List<IStep>(); List<IStep> jobSteps = new List<IStep>();
List<IStep> postJobSteps = new List<IStep>();
using (var register = jobContext.CancellationToken.Register(() => { context.CancelToken(); })) using (var register = jobContext.CancellationToken.Register(() => { context.CancelToken(); }))
{ {
try try
@@ -231,7 +230,7 @@ namespace GitHub.Runner.Worker
} }
} }
public async Task FinalizeJob(IExecutionContext jobContext, Pipelines.AgentJobRequestMessage message, DateTime jobStartTimeUtc) public void FinalizeJob(IExecutionContext jobContext, Pipelines.AgentJobRequestMessage message, DateTime jobStartTimeUtc)
{ {
Trace.Entering(); Trace.Entering();
ArgUtil.NotNull(jobContext, nameof(jobContext)); ArgUtil.NotNull(jobContext, nameof(jobContext));
@@ -245,19 +244,6 @@ namespace GitHub.Runner.Worker
context.Start(); context.Start();
context.Debug("Starting: Complete job"); context.Debug("Starting: Complete job");
// Wait for agent log plugin process exits
// var logPlugin = HostContext.GetService<IAgentLogPlugin>();
// try
// {
// await logPlugin.WaitAsync(context);
// }
// catch (Exception ex)
// {
// // Log and ignore the error from log plugin finalization.
// Trace.Error($"Caught exception from log plugin finalization: {ex}");
// context.Output(ex.Message);
// }
if (context.Variables.GetBoolean(Constants.Variables.Actions.RunnerDebug) ?? false) if (context.Variables.GetBoolean(Constants.Variables.Actions.RunnerDebug) ?? false)
{ {
Trace.Info("Support log upload starting."); Trace.Info("Support log upload starting.");
@@ -267,7 +253,7 @@ namespace GitHub.Runner.Worker
try try
{ {
await diagnosticLogManager.UploadDiagnosticLogsAsync(executionContext: context, parentContext: jobContext, message: message, jobStartTimeUtc: jobStartTimeUtc); diagnosticLogManager.UploadDiagnosticLogs(executionContext: context, parentContext: jobContext, message: message, jobStartTimeUtc: jobStartTimeUtc);
Trace.Info("Support log upload complete."); Trace.Info("Support log upload complete.");
context.Output("Completed runner diagnostic log upload"); context.Output("Completed runner diagnostic log upload");

View File

@@ -179,7 +179,7 @@ namespace GitHub.Runner.Worker
finally finally
{ {
Trace.Info("Finalize job."); Trace.Info("Finalize job.");
await jobExtension.FinalizeJob(jobContext, message, jobStartTimeUtc); jobExtension.FinalizeJob(jobContext, message, jobStartTimeUtc);
} }
Trace.Info($"Job result after all job steps finish: {jobContext.Result ?? TaskResult.Succeeded}"); Trace.Info($"Job result after all job steps finish: {jobContext.Result ?? TaskResult.Succeeded}");

View File

@@ -13,50 +13,50 @@ namespace GitHub.Runner.Common.Tests
{ {
public sealed class ProcessInvokerL0 public sealed class ProcessInvokerL0
{ {
// #if OS_WINDOWS #if OS_WINDOWS
// [Fact] [Fact]
// [Trait("Level", "L0")] [Trait("Level", "L0")]
// [Trait("Category", "Common")] [Trait("Category", "Common")]
// public async Task DefaultsToCurrentSystemOemEncoding() public async Task DefaultsToCurrentSystemOemEncoding()
// { {
// // This test verifies that the additional code pages encoding provider is registered. // This test verifies that the additional code pages encoding provider is registered.
// // By default, only Unicode encodings, ASCII, and code page 28591 are supported. An // By default, only Unicode encodings, ASCII, and code page 28591 are supported. An
// // additional provider must be registered to support the full set of encodings that // additional provider must be registered to support the full set of encodings that
// // were included in Full .NET prior to 4.6. // were included in Full .NET prior to 4.6.
// // //
// // For example, on an en-US box, this is required for loading the encoding for the // For example, on an en-US box, this is required for loading the encoding for the
// // default console output code page '437'. Without loading the correct encoding for // default console output code page '437'. Without loading the correct encoding for
// // code page IBM437, some characters cannot be translated correctly, e.g. write 'ç' // code page IBM437, some characters cannot be translated correctly, e.g. write 'ç'
// // from powershell.exe. // from powershell.exe.
// using (TestHostContext hc = new TestHostContext(this)) using (TestHostContext hc = new TestHostContext(this))
// { {
// Tracing trace = hc.GetTrace(); Tracing trace = hc.GetTrace();
// var processInvoker = new ProcessInvokerWrapper(); var processInvoker = new ProcessInvokerWrapper();
// processInvoker.Initialize(hc); processInvoker.Initialize(hc);
// var stdout = new List<string>(); var stdout = new List<string>();
// var stderr = new List<string>(); var stderr = new List<string>();
// processInvoker.OutputDataReceived += (object sender, ProcessDataReceivedEventArgs e) => processInvoker.OutputDataReceived += (object sender, ProcessDataReceivedEventArgs e) =>
// { {
// stdout.Add(e.Data); stdout.Add(e.Data);
// }; };
// processInvoker.ErrorDataReceived += (object sender, ProcessDataReceivedEventArgs e) => processInvoker.ErrorDataReceived += (object sender, ProcessDataReceivedEventArgs e) =>
// { {
// stderr.Add(e.Data); stderr.Add(e.Data);
// }; };
// await processInvoker.ExecuteAsync( await processInvoker.ExecuteAsync(
// workingDirectory: "", workingDirectory: "",
// fileName: "powershell.exe", fileName: "powershell.exe",
// arguments: $@"-NoLogo -Sta -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ""Write-Host 'From STDOUT ''ç''' ; Write-Error 'From STDERR ''ç'''""", arguments: $@"-NoLogo -Sta -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ""Write-Host 'From STDOUT ''ç''' ; Write-Error 'From STDERR ''ç'''""",
// environment: null, environment: null,
// requireExitCodeZero: false, requireExitCodeZero: false,
// cancellationToken: CancellationToken.None); cancellationToken: CancellationToken.None);
// Assert.Equal(1, stdout.Count); Assert.Equal(1, stdout.Count);
// Assert.Equal("From STDOUT 'ç'", stdout[0]); Assert.Equal("From STDOUT 'ç'", stdout[0]);
// Assert.True(stderr.Count > 0); Assert.True(stderr.Count > 0);
// Assert.True(stderr[0].Contains("From STDERR 'ç'")); Assert.True(stderr[0].Contains("From STDERR 'ç'"));
// } }
// } }
// #endif #endif
[Fact] [Fact]
[Trait("Level", "L0")] [Trait("Level", "L0")]

View File

@@ -29,130 +29,6 @@ namespace GitHub.Runner.Common.Tests.Worker
private ActionManager _actionManager; private ActionManager _actionManager;
private string _workFolder; private string _workFolder;
// //Test how exceptions are propagated to the caller.
// [Fact]
// [Trait("Level", "L0")]
// [Trait("Category", "Worker")]
// public async void RetryNetworkException()
// {
// try
// {
// // Arrange.
// Setup();
// var pingTask = new Pipelines.TaskStep()
// {
// Enabled = true,
// Reference = new Pipelines.TaskStepDefinitionReference()
// {
// Name = "Ping",
// Version = "0.1.1",
// Id = Guid.NewGuid()
// }
// };
// var pingVersion = new TaskVersion(pingTask.Reference.Version);
// Exception expectedException = new System.Net.Http.HttpRequestException("simulated network error");
// _taskServer
// .Setup(x => x.GetTaskContentZipAsync(It.IsAny<Guid>(), It.IsAny<TaskVersion>(), It.IsAny<CancellationToken>()))
// .Returns((Guid taskId, TaskVersion taskVersion, CancellationToken token) =>
// {
// throw expectedException;
// });
// var tasks = new List<Pipelines.TaskStep>(new Pipelines.TaskStep[] { pingTask });
// //Act
// Exception actualException = null;
// try
// {
// await _actionManager.DownloadAsync(_ec.Object, tasks);
// }
// catch (Exception ex)
// {
// actualException = ex;
// }
// //Assert
// //verify task completed in less than 2sec and it is in failed state state
// Assert.Equal(expectedException, actualException);
// //assert download was invoked 3 times, because we retry on task download
// _taskServer
// .Verify(x => x.GetTaskContentZipAsync(It.IsAny<Guid>(), It.IsAny<TaskVersion>(), It.IsAny<CancellationToken>()), Times.Exactly(3));
// //see if the task.json was not downloaded
// Assert.Equal(
// 0,
// Directory.GetFiles(_hc.GetDirectory(WellKnownDirectory.Tasks), "*", SearchOption.AllDirectories).Length);
// }
// finally
// {
// Teardown();
// }
// }
// //Test how exceptions are propagated to the caller.
// [Fact]
// [Trait("Level", "L0")]
// [Trait("Category", "Worker")]
// public async void RetryStreamException()
// {
// try
// {
// // Arrange.
// Setup();
// var pingTask = new Pipelines.TaskStep()
// {
// Enabled = true,
// Reference = new Pipelines.TaskStepDefinitionReference()
// {
// Name = "Ping",
// Version = "0.1.1",
// Id = Guid.NewGuid()
// }
// };
// var pingVersion = new TaskVersion(pingTask.Reference.Version);
// Exception expectedException = new System.Net.Http.HttpRequestException("simulated network error");
// _taskServer
// .Setup(x => x.GetTaskContentZipAsync(It.IsAny<Guid>(), It.IsAny<TaskVersion>(), It.IsAny<CancellationToken>()))
// .Returns((Guid taskId, TaskVersion taskVersion, CancellationToken token) =>
// {
// return Task.FromResult<Stream>(new ExceptionStream());
// });
// var tasks = new List<Pipelines.TaskStep>(new Pipelines.TaskStep[] { pingTask });
// //Act
// Exception actualException = null;
// try
// {
// await _actionManager.DownloadAsync(_ec.Object, tasks);
// }
// catch (Exception ex)
// {
// actualException = ex;
// }
// //Assert
// //verify task completed in less than 2sec and it is in failed state state
// Assert.Equal("NotImplementedException", actualException.GetType().Name);
// //assert download was invoked 3 times, because we retry on task download
// _taskServer
// .Verify(x => x.GetTaskContentZipAsync(It.IsAny<Guid>(), It.IsAny<TaskVersion>(), It.IsAny<CancellationToken>()), Times.Exactly(3));
// //see if the task.json was not downloaded
// Assert.Equal(
// 0,
// Directory.GetFiles(_hc.GetDirectory(WellKnownDirectory.Tasks), "*", SearchOption.AllDirectories).Length);
// }
// finally
// {
// Teardown();
// }
// }
#if OS_LINUX #if OS_LINUX
[Fact] [Fact]
[Trait("Level", "L0")] [Trait("Level", "L0")]
@@ -233,31 +109,17 @@ namespace GitHub.Runner.Common.Tests.Worker
} }
} }
/*
[Fact] [Fact]
[Trait("Level", "L0")] [Trait("Level", "L0")]
[Trait("Category", "Worker")] [Trait("Category", "Worker")]
public async void PrepareActions_SkipDownloadActionFromGraphWhenCache() public async void PrepareActions_AlwaysClearActionsCache()
{ {
try try
{ {
//Arrange //Arrange
Setup(); Setup();
var actionId = Guid.NewGuid(); var actionId = Guid.NewGuid();
var actions = new List<Pipelines.ActionStep> var actions = new List<Pipelines.ActionStep>();
{
new Pipelines.ActionStep()
{
Name = "action",
Id = actionId,
Reference = new Pipelines.RepositoryPathReference()
{
Name = "notexist/no",
Ref = "notexist",
RepositoryType = "GitHub"
}
}
};
var watermarkFile = Path.Combine(_hc.GetDirectory(WellKnownDirectory.Actions), "notexist/no", "notexist.completed"); var watermarkFile = Path.Combine(_hc.GetDirectory(WellKnownDirectory.Actions), "notexist/no", "notexist.completed");
Directory.CreateDirectory(Path.GetDirectoryName(watermarkFile)); Directory.CreateDirectory(Path.GetDirectoryName(watermarkFile));
@@ -267,13 +129,15 @@ namespace GitHub.Runner.Common.Tests.Worker
//Act //Act
await _actionManager.PrepareActionsAsync(_ec.Object, actions); await _actionManager.PrepareActionsAsync(_ec.Object, actions);
// Make sure _actions folder get deleted
Assert.False(Directory.Exists(_hc.GetDirectory(WellKnownDirectory.Actions)));
} }
finally finally
{ {
Teardown(); Teardown();
} }
} }
*/
[Fact] [Fact]
[Trait("Level", "L0")] [Trait("Level", "L0")]
@@ -300,7 +164,9 @@ namespace GitHub.Runner.Common.Tests.Worker
}; };
//Act //Act
await _actionManager.PrepareActionsAsync(_ec.Object, actions); var steps = await _actionManager.PrepareActionsAsync(_ec.Object, actions);
Assert.True(steps.Count == 0);
} }
finally finally
{ {
@@ -308,7 +174,6 @@ namespace GitHub.Runner.Common.Tests.Worker
} }
} }
/*
#if OS_LINUX #if OS_LINUX
[Fact] [Fact]
[Trait("Level", "L0")] [Trait("Level", "L0")]
@@ -328,24 +193,20 @@ namespace GitHub.Runner.Common.Tests.Worker
Id = actionId, Id = actionId,
Reference = new Pipelines.RepositoryPathReference() Reference = new Pipelines.RepositoryPathReference()
{ {
Name = "actions/test", Name = "TingluoHuang/runner_L0",
Ref = "master", Ref = "repositoryactionwithdockerfile",
RepositoryType = "GitHub" RepositoryType = "GitHub"
} }
} }
}; };
var watermarkFile = Path.Combine(_hc.GetDirectory(WellKnownDirectory.Actions), "actions/test", "master.completed"); var actionDir = Path.Combine(_hc.GetDirectory(WellKnownDirectory.Actions), "TingluoHuang", "runner_L0", "repositoryactionwithdockerfile");
Directory.CreateDirectory(Path.GetDirectoryName(watermarkFile));
File.WriteAllText(watermarkFile, DateTime.UtcNow.ToString());
Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(watermarkFile), "master"));
File.WriteAllText(Path.Combine(Path.GetDirectoryName(watermarkFile), "master", "Dockerfile"), "Fake Dockerfile");
//Act //Act
var steps = await _actionManager.PrepareActionsAsync(_ec.Object, actions); var steps = await _actionManager.PrepareActionsAsync(_ec.Object, actions);
Assert.Equal((steps[0].Data as ContainerSetupInfo).StepIds[0], actionId); Assert.Equal((steps[0].Data as ContainerSetupInfo).StepIds[0], actionId);
Assert.Equal((steps[0].Data as ContainerSetupInfo).Container.WorkingDirectory, Path.Combine(Path.GetDirectoryName(watermarkFile), "master")); Assert.Equal((steps[0].Data as ContainerSetupInfo).Container.WorkingDirectory, actionDir);
Assert.Equal((steps[0].Data as ContainerSetupInfo).Container.Dockerfile, Path.Combine(Path.GetDirectoryName(watermarkFile), "master", "Dockerfile")); Assert.Equal((steps[0].Data as ContainerSetupInfo).Container.Dockerfile, Path.Combine(actionDir, "Dockerfile"));
} }
finally finally
{ {
@@ -371,26 +232,22 @@ namespace GitHub.Runner.Common.Tests.Worker
Id = actionId, Id = actionId,
Reference = new Pipelines.RepositoryPathReference() Reference = new Pipelines.RepositoryPathReference()
{ {
Name = "actions/test", Name = "TingluoHuang/runner_L0",
Ref = "master", Ref = "repositoryactionwithdockerfileinrelativepath",
Path = "images/cli", Path = "images/cli",
RepositoryType = "GitHub" RepositoryType = "GitHub"
} }
} }
}; };
var watermarkFile = Path.Combine(_hc.GetDirectory(WellKnownDirectory.Actions), "actions/test", "master.completed"); var actionDir = Path.Combine(_hc.GetDirectory(WellKnownDirectory.Actions), "TingluoHuang", "runner_L0", "repositoryactionwithdockerfileinrelativepath");
Directory.CreateDirectory(Path.GetDirectoryName(watermarkFile));
File.WriteAllText(watermarkFile, DateTime.UtcNow.ToString());
Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(watermarkFile), "master/images/cli"));
File.WriteAllText(Path.Combine(Path.GetDirectoryName(watermarkFile), "master/images/cli/Dockerfile"), "Fake Dockerfile");
//Act //Act
var steps = await _actionManager.PrepareActionsAsync(_ec.Object, actions); var steps = await _actionManager.PrepareActionsAsync(_ec.Object, actions);
Assert.Equal((steps[0].Data as ContainerSetupInfo).StepIds[0], actionId); Assert.Equal((steps[0].Data as ContainerSetupInfo).StepIds[0], actionId);
Assert.Equal((steps[0].Data as ContainerSetupInfo).Container.WorkingDirectory, Path.Combine(Path.GetDirectoryName(watermarkFile), "master")); Assert.Equal((steps[0].Data as ContainerSetupInfo).Container.WorkingDirectory, actionDir);
Assert.Equal((steps[0].Data as ContainerSetupInfo).Container.Dockerfile, Path.Combine(Path.GetDirectoryName(watermarkFile), "master", "images/cli", "Dockerfile")); Assert.Equal((steps[0].Data as ContainerSetupInfo).Container.Dockerfile, Path.Combine(actionDir, "images/cli", "Dockerfile"));
} }
finally finally
{ {
@@ -416,26 +273,20 @@ namespace GitHub.Runner.Common.Tests.Worker
Id = actionId, Id = actionId,
Reference = new Pipelines.RepositoryPathReference() Reference = new Pipelines.RepositoryPathReference()
{ {
Name = "notexist/no", Name = "TingluoHuang/runner_L0",
Ref = "notexist", Ref = "repositoryactionwithdockerfileinrelativepath",
RepositoryType = "GitHub" RepositoryType = "GitHub"
} }
} }
}; };
var watermarkFile = Path.Combine(_hc.GetDirectory(WellKnownDirectory.Actions), "notexist/no", "notexist.completed"); var actionDir = Path.Combine(_hc.GetDirectory(WellKnownDirectory.Actions), "TingluoHuang", "runner_L0", "repositoryactionwithdockerfileinrelativepath");
Directory.CreateDirectory(Path.GetDirectoryName(watermarkFile));
File.WriteAllText(watermarkFile, DateTime.UtcNow.ToString());
Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(watermarkFile), "notexist"));
File.Copy(Path.Combine(Environment.GetEnvironmentVariable("GITHUB_RUNNER_SRC_DIR"), "Test", TestDataFolderName, "dockerfileaction.yml"), Path.Combine(Path.GetDirectoryName(watermarkFile), "notexist", "action.yml"));
File.WriteAllText(Path.Combine(Path.GetDirectoryName(watermarkFile), "notexist/Dockerfile"), "Fake Dockerfile");
//Act //Act
var steps = await _actionManager.PrepareActionsAsync(_ec.Object, actions); var steps = await _actionManager.PrepareActionsAsync(_ec.Object, actions);
Assert.Equal((steps[0].Data as ContainerSetupInfo).StepIds[0], actionId); Assert.Equal((steps[0].Data as ContainerSetupInfo).StepIds[0], actionId);
Assert.Equal((steps[0].Data as ContainerSetupInfo).Container.WorkingDirectory, Path.Combine(Path.GetDirectoryName(watermarkFile), "notexist")); Assert.Equal((steps[0].Data as ContainerSetupInfo).Container.WorkingDirectory, actionDir);
Assert.Equal((steps[0].Data as ContainerSetupInfo).Container.Dockerfile, Path.Combine(Path.GetDirectoryName(watermarkFile), "notexist", "Dockerfile")); Assert.Equal((steps[0].Data as ContainerSetupInfo).Container.Dockerfile, Path.Combine(actionDir, "Dockerfile"));
} }
finally finally
{ {
@@ -461,27 +312,21 @@ namespace GitHub.Runner.Common.Tests.Worker
Id = actionId, Id = actionId,
Reference = new Pipelines.RepositoryPathReference() Reference = new Pipelines.RepositoryPathReference()
{ {
Name = "notexist/no", Name = "TingluoHuang/runner_L0",
Ref = "notexist", Ref = "RepositoryActionWithActionfile_DockerfileRelativePath",
RepositoryType = "GitHub" RepositoryType = "GitHub"
} }
} }
}; };
var watermarkFile = Path.Combine(_hc.GetDirectory(WellKnownDirectory.Actions), "notexist/no", "notexist.completed"); var actionDir = Path.Combine(_hc.GetDirectory(WellKnownDirectory.Actions), "TingluoHuang", "runner_L0", "RepositoryActionWithActionfile_DockerfileRelativePath");
Directory.CreateDirectory(Path.GetDirectoryName(watermarkFile));
File.WriteAllText(watermarkFile, DateTime.UtcNow.ToString());
Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(watermarkFile), "notexist"));
File.Copy(Path.Combine(Environment.GetEnvironmentVariable("GITHUB_RUNNER_SRC_DIR"), "Test", TestDataFolderName, "dockerfilerelativeaction.yml"), Path.Combine(Path.GetDirectoryName(watermarkFile), "notexist", "action.yml"));
Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(watermarkFile), "master/images"));
File.WriteAllText(Path.Combine(Path.GetDirectoryName(watermarkFile), "master/images/Dockerfile"), "Fake Dockerfile");
//Act //Act
var steps = await _actionManager.PrepareActionsAsync(_ec.Object, actions); var steps = await _actionManager.PrepareActionsAsync(_ec.Object, actions);
Assert.Equal((steps[0].Data as ContainerSetupInfo).StepIds[0], actionId); Assert.Equal((steps[0].Data as ContainerSetupInfo).StepIds[0], actionId);
Assert.Equal((steps[0].Data as ContainerSetupInfo).Container.WorkingDirectory, Path.Combine(Path.GetDirectoryName(watermarkFile), "notexist")); Assert.Equal((steps[0].Data as ContainerSetupInfo).Container.WorkingDirectory, actionDir);
Assert.Equal((steps[0].Data as ContainerSetupInfo).Container.Dockerfile, Path.Combine(Path.GetDirectoryName(watermarkFile), "notexist", "images/Dockerfile")); Assert.Equal((steps[0].Data as ContainerSetupInfo).Container.Dockerfile, Path.Combine(actionDir, "images/Dockerfile"));
} }
finally finally
{ {
@@ -507,18 +352,14 @@ namespace GitHub.Runner.Common.Tests.Worker
Id = actionId, Id = actionId,
Reference = new Pipelines.RepositoryPathReference() Reference = new Pipelines.RepositoryPathReference()
{ {
Name = "notexist/no", Name = "TingluoHuang/runner_L0",
Ref = "notexist", Ref = "RepositoryActionWithActionfile_DockerHubImage",
RepositoryType = "GitHub" RepositoryType = "GitHub"
} }
} }
}; };
var watermarkFile = Path.Combine(_hc.GetDirectory(WellKnownDirectory.Actions), "notexist/no", "notexist.completed"); var actionDir = Path.Combine(_hc.GetDirectory(WellKnownDirectory.Actions), "TingluoHuang", "runner_L0", "RepositoryActionWithActionfile_DockerHubImage");
Directory.CreateDirectory(Path.GetDirectoryName(watermarkFile));
File.WriteAllText(watermarkFile, DateTime.UtcNow.ToString());
Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(watermarkFile), "notexist"));
File.Copy(Path.Combine(Environment.GetEnvironmentVariable("GITHUB_RUNNER_SRC_DIR"), "Test", TestDataFolderName, "dockerhubaction.yml"), Path.Combine(Path.GetDirectoryName(watermarkFile), "notexist", "action.yml"));
//Act //Act
var steps = await _actionManager.PrepareActionsAsync(_ec.Object, actions); var steps = await _actionManager.PrepareActionsAsync(_ec.Object, actions);
@@ -550,25 +391,21 @@ namespace GitHub.Runner.Common.Tests.Worker
Id = actionId, Id = actionId,
Reference = new Pipelines.RepositoryPathReference() Reference = new Pipelines.RepositoryPathReference()
{ {
Name = "notexist/no", Name = "TingluoHuang/runner_L0",
Ref = "notexist", Ref = "repositoryactionwithactionfileanddockerfile",
RepositoryType = "GitHub" RepositoryType = "GitHub"
} }
} }
}; };
var watermarkFile = Path.Combine(_hc.GetDirectory(WellKnownDirectory.Actions), "notexist/no", "notexist.completed"); var actionDir = Path.Combine(_hc.GetDirectory(WellKnownDirectory.Actions), "TingluoHuang", "runner_L0", "repositoryactionwithactionfileanddockerfile");
Directory.CreateDirectory(Path.GetDirectoryName(watermarkFile));
File.WriteAllText(watermarkFile, DateTime.UtcNow.ToString());
Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(watermarkFile), "notexist"));
File.Copy(Path.Combine(Environment.GetEnvironmentVariable("GITHUB_RUNNER_SRC_DIR"), "Test", TestDataFolderName, "dockerhubaction.yml"), Path.Combine(Path.GetDirectoryName(watermarkFile), "notexist", "action.yml"));
File.WriteAllText(Path.Combine(Path.GetDirectoryName(watermarkFile), "notexist", "Dockerfile"), "Fake Dockerfile");
//Act //Act
var steps = await _actionManager.PrepareActionsAsync(_ec.Object, actions); var steps = await _actionManager.PrepareActionsAsync(_ec.Object, actions);
Assert.Equal((steps[0].Data as ContainerSetupInfo).StepIds[0], actionId); Assert.Equal((steps[0].Data as ContainerSetupInfo).StepIds[0], actionId);
Assert.Equal((steps[0].Data as ContainerSetupInfo).Container.Image, "ubuntu:18.04"); Assert.Equal((steps[0].Data as ContainerSetupInfo).Container.WorkingDirectory, actionDir);
Assert.Equal((steps[0].Data as ContainerSetupInfo).Container.Dockerfile, Path.Combine(actionDir, "Dockerfile"));
} }
finally finally
{ {
@@ -628,8 +465,8 @@ namespace GitHub.Runner.Common.Tests.Worker
Id = actionId4, Id = actionId4,
Reference = new Pipelines.RepositoryPathReference() Reference = new Pipelines.RepositoryPathReference()
{ {
Name = "notexist/no", Name = "TingluoHuang/runner_L0",
Ref = "notexist", Ref = "notpullorbuildimagesmultipletimes1",
RepositoryType = "GitHub" RepositoryType = "GitHub"
} }
}, },
@@ -639,8 +476,8 @@ namespace GitHub.Runner.Common.Tests.Worker
Id = actionId5, Id = actionId5,
Reference = new Pipelines.RepositoryPathReference() Reference = new Pipelines.RepositoryPathReference()
{ {
Name = "actions/test", Name = "TingluoHuang/runner_L0",
Ref = "master", Ref = "repositoryactionwithdockerfile",
RepositoryType = "GitHub" RepositoryType = "GitHub"
} }
}, },
@@ -650,8 +487,8 @@ namespace GitHub.Runner.Common.Tests.Worker
Id = actionId6, Id = actionId6,
Reference = new Pipelines.RepositoryPathReference() Reference = new Pipelines.RepositoryPathReference()
{ {
Name = "actions/test", Name = "TingluoHuang/runner_L0",
Ref = "release", Ref = "repositoryactionwithdockerfileinrelativepath",
RepositoryType = "GitHub" RepositoryType = "GitHub"
} }
}, },
@@ -661,8 +498,8 @@ namespace GitHub.Runner.Common.Tests.Worker
Id = actionId7, Id = actionId7,
Reference = new Pipelines.RepositoryPathReference() Reference = new Pipelines.RepositoryPathReference()
{ {
Name = "actions/test", Name = "TingluoHuang/runner_L0",
Ref = "release", Ref = "repositoryactionwithdockerfileinrelativepath",
RepositoryType = "GitHub" RepositoryType = "GitHub"
} }
}, },
@@ -672,38 +509,14 @@ namespace GitHub.Runner.Common.Tests.Worker
Id = actionId8, Id = actionId8,
Reference = new Pipelines.RepositoryPathReference() Reference = new Pipelines.RepositoryPathReference()
{ {
Name = "actions/test", Name = "TingluoHuang/runner_L0",
Ref = "master", Ref = "repositoryactionwithdockerfileinrelativepath",
Path = "images/cli", Path = "images/cli",
RepositoryType = "GitHub" RepositoryType = "GitHub"
} }
} }
}; };
var watermarkFile = Path.Combine(_hc.GetDirectory(WellKnownDirectory.Actions), "notexist/no", "notexist.completed");
Directory.CreateDirectory(Path.GetDirectoryName(watermarkFile));
File.WriteAllText(watermarkFile, DateTime.UtcNow.ToString());
Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(watermarkFile), "notexist"));
File.Copy(Path.Combine(Environment.GetEnvironmentVariable("GITHUB_RUNNER_SRC_DIR"), "Test", TestDataFolderName, "dockerhubaction.yml"), Path.Combine(Path.GetDirectoryName(watermarkFile), "notexist", "action.yml"));
watermarkFile = Path.Combine(_hc.GetDirectory(WellKnownDirectory.Actions), "actions/test", "master.completed");
Directory.CreateDirectory(Path.GetDirectoryName(watermarkFile));
File.WriteAllText(watermarkFile, DateTime.UtcNow.ToString());
Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(watermarkFile), "master"));
File.WriteAllText(Path.Combine(Path.GetDirectoryName(watermarkFile), "master", "Dockerfile"), "Fake Dockerfile");
watermarkFile = Path.Combine(_hc.GetDirectory(WellKnownDirectory.Actions), "actions/test", "release.completed");
Directory.CreateDirectory(Path.GetDirectoryName(watermarkFile));
File.WriteAllText(watermarkFile, DateTime.UtcNow.ToString());
Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(watermarkFile), "release"));
File.WriteAllText(Path.Combine(Path.GetDirectoryName(watermarkFile), "release", "Dockerfile"), "Fake Dockerfile");
watermarkFile = Path.Combine(_hc.GetDirectory(WellKnownDirectory.Actions), "actions/test", "master.completed");
Directory.CreateDirectory(Path.GetDirectoryName(watermarkFile));
File.WriteAllText(watermarkFile, DateTime.UtcNow.ToString());
Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(watermarkFile), "master/images/cli"));
File.WriteAllText(Path.Combine(Path.GetDirectoryName(watermarkFile), "master/images/cli/Dockerfile"), "Fake Dockerfile");
//Act //Act
var steps = await _actionManager.PrepareActionsAsync(_ec.Object, actions); var steps = await _actionManager.PrepareActionsAsync(_ec.Object, actions);
@@ -716,18 +529,22 @@ namespace GitHub.Runner.Common.Tests.Worker
Assert.True((steps[1].Data as ContainerSetupInfo).StepIds.Contains(actionId4)); Assert.True((steps[1].Data as ContainerSetupInfo).StepIds.Contains(actionId4));
Assert.Equal((steps[1].Data as ContainerSetupInfo).Container.Image, "ubuntu:18.04"); Assert.Equal((steps[1].Data as ContainerSetupInfo).Container.Image, "ubuntu:18.04");
var actionDir = Path.Combine(_hc.GetDirectory(WellKnownDirectory.Actions), "TingluoHuang", "runner_L0", "repositoryactionwithdockerfile");
Assert.Equal((steps[2].Data as ContainerSetupInfo).StepIds[0], actionId5); Assert.Equal((steps[2].Data as ContainerSetupInfo).StepIds[0], actionId5);
Assert.Equal((steps[2].Data as ContainerSetupInfo).Container.WorkingDirectory, Path.Combine(Path.GetDirectoryName(watermarkFile), "master")); Assert.Equal((steps[2].Data as ContainerSetupInfo).Container.WorkingDirectory, actionDir);
Assert.Equal((steps[2].Data as ContainerSetupInfo).Container.Dockerfile, Path.Combine(Path.GetDirectoryName(watermarkFile), "master", "Dockerfile")); Assert.Equal((steps[2].Data as ContainerSetupInfo).Container.Dockerfile, Path.Combine(actionDir, "Dockerfile"));
actionDir = Path.Combine(_hc.GetDirectory(WellKnownDirectory.Actions), "TingluoHuang", "runner_L0", "repositoryactionwithdockerfileinrelativepath");
Assert.True((steps[3].Data as ContainerSetupInfo).StepIds.Contains(actionId6)); Assert.True((steps[3].Data as ContainerSetupInfo).StepIds.Contains(actionId6));
Assert.True((steps[3].Data as ContainerSetupInfo).StepIds.Contains(actionId7)); Assert.True((steps[3].Data as ContainerSetupInfo).StepIds.Contains(actionId7));
Assert.Equal((steps[3].Data as ContainerSetupInfo).Container.WorkingDirectory, Path.Combine(Path.GetDirectoryName(watermarkFile), "release")); Assert.Equal((steps[3].Data as ContainerSetupInfo).Container.WorkingDirectory, actionDir);
Assert.Equal((steps[3].Data as ContainerSetupInfo).Container.Dockerfile, Path.Combine(Path.GetDirectoryName(watermarkFile), "release", "Dockerfile")); Assert.Equal((steps[3].Data as ContainerSetupInfo).Container.Dockerfile, Path.Combine(actionDir, "Dockerfile"));
Assert.Equal((steps[4].Data as ContainerSetupInfo).StepIds[0], actionId8); Assert.Equal((steps[4].Data as ContainerSetupInfo).StepIds[0], actionId8);
Assert.Equal((steps[4].Data as ContainerSetupInfo).Container.WorkingDirectory, Path.Combine(Path.GetDirectoryName(watermarkFile), "master")); Assert.Equal((steps[4].Data as ContainerSetupInfo).Container.WorkingDirectory, actionDir);
Assert.Equal((steps[4].Data as ContainerSetupInfo).Container.Dockerfile, Path.Combine(Path.GetDirectoryName(watermarkFile), "master", "images/cli", "Dockerfile")); Assert.Equal((steps[4].Data as ContainerSetupInfo).Container.Dockerfile, Path.Combine(actionDir, "images/cli", "Dockerfile"));
} }
finally finally
{ {
@@ -754,28 +571,24 @@ namespace GitHub.Runner.Common.Tests.Worker
Id = actionId, Id = actionId,
Reference = new Pipelines.RepositoryPathReference() Reference = new Pipelines.RepositoryPathReference()
{ {
Name = "notexist/no", Name = "actions/setup-node",
Ref = "notexist", Ref = "v1",
RepositoryType = "GitHub" RepositoryType = "GitHub"
} }
} }
}; };
var watermarkFile = Path.Combine(_hc.GetDirectory(WellKnownDirectory.Actions), "notexist/no", "notexist.completed");
Directory.CreateDirectory(Path.GetDirectoryName(watermarkFile));
File.WriteAllText(watermarkFile, DateTime.UtcNow.ToString());
Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(watermarkFile), "notexist"));
File.Copy(Path.Combine(Environment.GetEnvironmentVariable("GITHUB_RUNNER_SRC_DIR"), "Test", TestDataFolderName, "nodeaction.yml"), Path.Combine(Path.GetDirectoryName(watermarkFile), "notexist", "action.yml"));
//Act //Act
await _actionManager.PrepareActionsAsync(_ec.Object, actions); var steps = await _actionManager.PrepareActionsAsync(_ec.Object, actions);
// node.js based action doesn't need any extra steps to build/pull containers.
Assert.True(steps.Count == 0);
} }
finally finally
{ {
Teardown(); Teardown();
} }
} }
*/
[Fact] [Fact]
[Trait("Level", "L0")] [Trait("Level", "L0")]
@@ -1653,7 +1466,6 @@ runs:
}; };
} }
private void Setup([CallerMemberName] string name = "") private void Setup([CallerMemberName] string name = "")
{ {
_ecTokenSource?.Dispose(); _ecTokenSource?.Dispose();
@@ -1698,9 +1510,21 @@ runs:
}); });
_hc.SetSingleton<IConfigurationStore>(_configurationStore.Object); _hc.SetSingleton<IConfigurationStore>(_configurationStore.Object);
var pInvoker = new ProcessInvokerWrapper(); var pInvoker1 = new ProcessInvokerWrapper();
pInvoker.Initialize(_hc); pInvoker1.Initialize(_hc);
_hc.EnqueueInstance<IProcessInvoker>(pInvoker); var pInvoker2 = new ProcessInvokerWrapper();
pInvoker2.Initialize(_hc);
var pInvoker3 = new ProcessInvokerWrapper();
pInvoker3.Initialize(_hc);
var pInvoker4 = new ProcessInvokerWrapper();
pInvoker4.Initialize(_hc);
var pInvoker5 = new ProcessInvokerWrapper();
pInvoker5.Initialize(_hc);
_hc.EnqueueInstance<IProcessInvoker>(pInvoker1);
_hc.EnqueueInstance<IProcessInvoker>(pInvoker2);
_hc.EnqueueInstance<IProcessInvoker>(pInvoker3);
_hc.EnqueueInstance<IProcessInvoker>(pInvoker4);
_hc.EnqueueInstance<IProcessInvoker>(pInvoker5);
// Instance to test. // Instance to test.
_actionManager = new ActionManager(); _actionManager = new ActionManager();
@@ -1717,43 +1541,5 @@ runs:
Directory.Delete(_workFolder, recursive: true); Directory.Delete(_workFolder, recursive: true);
} }
} }
private class ExceptionStream : Stream
{
public override bool CanRead => throw new NotImplementedException();
public override bool CanSeek => throw new NotImplementedException();
public override bool CanWrite => throw new NotImplementedException();
public override long Length => throw new NotImplementedException();
public override long Position { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public override void Flush()
{
throw new NotImplementedException();
}
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
}
} }
} }

View File

@@ -1,386 +1,291 @@
// using GitHub.DistributedTask.WebApi; using GitHub.DistributedTask.WebApi;
// using GitHub.Runner.Worker; using GitHub.Runner.Worker;
// using Moq; using Moq;
// using System; using System;
// using System.Collections.Generic; using System.Collections.Generic;
// using System.Linq; using System.Linq;
// using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
// using System.Threading.Tasks; using System.Threading.Tasks;
// using Xunit; using Xunit;
// using System.Threading; using System.Threading;
// using Pipelines = GitHub.DistributedTask.Pipelines; using Pipelines = GitHub.DistributedTask.Pipelines;
// namespace GitHub.Runner.Common.Tests.Worker namespace GitHub.Runner.Common.Tests.Worker
// { {
// public sealed class JobExtensionL0 public sealed class JobExtensionL0
// { {
// private class TestJobExtension : JobExtension private IExecutionContext _jobEc;
// { private Pipelines.AgentJobRequestMessage _message;
// public override HostTypes HostType => HostTypes.None;
// public override Type ExtensionType => typeof(IJobExtension); private Mock<IPipelineDirectoryManager> _directoryManager;
private Mock<IActionManager> _actionManager;
private Mock<IJobServerQueue> _jobServerQueue;
private Mock<IRunnerCertificateManager> _cert;
private Mock<IConfigurationStore> _config;
private Mock<IPagingLogger> _logger;
private Mock<IExpressionManager> _express;
private Mock<IContainerOperationProvider> _containerProvider;
private Mock<IDiagnosticLogManager> _diagnosticLogManager;
// public override void ConvertLocalPath(IExecutionContext context, string localPath, out string repoName, out string sourcePath) private CancellationTokenSource _tokenSource;
// { private TestHostContext CreateTestContext([CallerMemberName] String testName = "")
// repoName = ""; {
// sourcePath = ""; var hc = new TestHostContext(this, testName);
// } _jobEc = new Runner.Worker.ExecutionContext();
_actionManager = new Mock<IActionManager>();
_jobServerQueue = new Mock<IJobServerQueue>();
_config = new Mock<IConfigurationStore>();
_logger = new Mock<IPagingLogger>();
_cert = new Mock<IRunnerCertificateManager>();
_express = new Mock<IExpressionManager>();
_containerProvider = new Mock<IContainerOperationProvider>();
_diagnosticLogManager = new Mock<IDiagnosticLogManager>();
_directoryManager = new Mock<IPipelineDirectoryManager>();
_directoryManager.Setup(x => x.PrepareDirectory(It.IsAny<IExecutionContext>(), It.IsAny<Pipelines.WorkspaceOptions>()))
.Returns(new TrackingConfig() { PipelineDirectory = "runner", WorkspaceDirectory = "runner/runner" });
// public override IStep GetExtensionPostJobStep(IExecutionContext jobContext) IActionRunner step1 = new ActionRunner();
// { IActionRunner step2 = new ActionRunner();
// return null; IActionRunner step3 = new ActionRunner();
// } IActionRunner step4 = new ActionRunner();
IActionRunner step5 = new ActionRunner();
// public override IStep GetExtensionPreJobStep(IExecutionContext jobContext) _logger.Setup(x => x.Setup(It.IsAny<Guid>(), It.IsAny<Guid>()));
// { var settings = new RunnerSettings
// return null; {
// } AgentId = 1,
AgentName = "runner",
ServerUrl = "https://pipelines.actions.githubusercontent.com/abcd",
WorkFolder = "_work",
};
// public override string GetRootedPath(IExecutionContext context, string path) _config.Setup(x => x.GetSettings())
// { .Returns(settings);
// return path;
// }
// public override void InitializeJobExtension(IExecutionContext context, IList<Pipelines.JobStep> steps, Pipelines.WorkspaceOptions workspace) if (_tokenSource != null)
// { {
// return; _tokenSource.Dispose();
// } _tokenSource = null;
// } }
// private IExecutionContext _jobEc; _tokenSource = new CancellationTokenSource();
// private Pipelines.AgentJobRequestMessage _message; TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference();
// private Mock<ITaskManager> _taskManager; TimelineReference timeline = new Timeline(Guid.NewGuid());
// private Mock<IAgentLogPlugin> _logPlugin;
// private Mock<IJobServerQueue> _jobServerQueue;
// private Mock<IVstsAgentWebProxy> _proxy;
// private Mock<IAgentCertificateManager> _cert;
// private Mock<IConfigurationStore> _config;
// private Mock<IPagingLogger> _logger;
// private Mock<IExpressionManager> _express;
// private Mock<IContainerOperationProvider> _containerProvider;
// private CancellationTokenSource _tokenSource;
// private TestHostContext CreateTestContext([CallerMemberName] String testName = "")
// {
// var hc = new TestHostContext(this, testName);
// _jobEc = new Runner.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(); List<Pipelines.ActionStep> steps = new List<Pipelines.ActionStep>()
// TaskRunner step2 = new TaskRunner(); {
// TaskRunner step3 = new TaskRunner(); new Pipelines.ActionStep()
// TaskRunner step4 = new TaskRunner(); {
// TaskRunner step5 = new TaskRunner(); Id = Guid.NewGuid(),
// TaskRunner step6 = new TaskRunner(); DisplayName = "action1",
// TaskRunner step7 = new TaskRunner(); },
// TaskRunner step8 = new TaskRunner(); new Pipelines.ActionStep()
// TaskRunner step9 = new TaskRunner(); {
// TaskRunner step10 = new TaskRunner(); Id = Guid.NewGuid(),
// TaskRunner step11 = new TaskRunner(); DisplayName = "action2",
// TaskRunner step12 = new TaskRunner(); },
new Pipelines.ActionStep()
{
Id = Guid.NewGuid(),
DisplayName = "action3",
},
new Pipelines.ActionStep()
{
Id = Guid.NewGuid(),
DisplayName = "action4",
},
new Pipelines.ActionStep()
{
Id = Guid.NewGuid(),
DisplayName = "action5",
}
};
// _logger.Setup(x => x.Setup(It.IsAny<Guid>(), It.IsAny<Guid>())); Guid jobId = Guid.NewGuid();
// var settings = new AgentSettings _message = new Pipelines.AgentJobRequestMessage(plan, timeline, jobId, "test", "test", null, null, null, new Dictionary<string, VariableValue>(), new List<MaskHint>(), new Pipelines.JobResources(), new Pipelines.ContextData.DictionaryContextData(), new Pipelines.WorkspaceOptions(), steps, null);
// { GitHubContext github = new GitHubContext();
// AgentId = 1, github["repository"] = new Pipelines.ContextData.StringContextData("actions/runner");
// AgentName = "agent1", _message.ContextData.Add("github", github);
// ServerUrl = "https://test.visualstudio.com",
// WorkFolder = "_work",
// };
// _config.Setup(x => x.GetSettings()) hc.SetSingleton(_actionManager.Object);
// .Returns(settings); hc.SetSingleton(_config.Object);
hc.SetSingleton(_jobServerQueue.Object);
hc.SetSingleton(_cert.Object);
hc.SetSingleton(_express.Object);
hc.SetSingleton(_containerProvider.Object);
hc.SetSingleton(_directoryManager.Object);
hc.SetSingleton(_diagnosticLogManager.Object);
hc.EnqueueInstance<IPagingLogger>(_logger.Object); // JobExecutionContext
hc.EnqueueInstance<IPagingLogger>(_logger.Object); // Initial Job
hc.EnqueueInstance<IPagingLogger>(_logger.Object); // step1
hc.EnqueueInstance<IPagingLogger>(_logger.Object); // step2
hc.EnqueueInstance<IPagingLogger>(_logger.Object); // step3
hc.EnqueueInstance<IPagingLogger>(_logger.Object); // step4
hc.EnqueueInstance<IPagingLogger>(_logger.Object); // step5
hc.EnqueueInstance<IPagingLogger>(_logger.Object); // prepare1
hc.EnqueueInstance<IPagingLogger>(_logger.Object); // prepare2
// _proxy.Setup(x => x.ProxyAddress) hc.EnqueueInstance<IActionRunner>(step1);
// .Returns(string.Empty); hc.EnqueueInstance<IActionRunner>(step2);
hc.EnqueueInstance<IActionRunner>(step3);
hc.EnqueueInstance<IActionRunner>(step4);
hc.EnqueueInstance<IActionRunner>(step5);
// if (_tokenSource != null) _jobEc.Initialize(hc);
// { _jobEc.InitializeJob(_message, _tokenSource.Token);
// _tokenSource.Dispose(); return hc;
// _tokenSource = null; }
// }
// _tokenSource = new CancellationTokenSource(); [Fact]
// TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); [Trait("Level", "L0")]
// TimelineReference timeline = new Timeline(Guid.NewGuid()); [Trait("Category", "Worker")]
// JobEnvironment environment = new JobEnvironment(); public async Task JobExtensionBuildStepsList()
// environment.Variables[Constants.Variables.System.Culture] = "en-US"; {
// environment.SystemConnection = new ServiceEndpoint() using (TestHostContext hc = CreateTestContext())
// { {
// Name = WellKnownServiceEndpointNames.SystemVssConnection, var jobExtension = new JobExtension();
// Url = new Uri("https://test.visualstudio.com"), jobExtension.Initialize(hc);
// Authorization = new EndpointAuthorization()
// {
// Scheme = "Test",
// }
// };
// environment.SystemConnection.Authorization.Parameters["AccessToken"] = "token";
// List<TaskInstance> tasks = new List<TaskInstance>() _actionManager.Setup(x => x.PrepareActionsAsync(It.IsAny<IExecutionContext>(), It.IsAny<IEnumerable<Pipelines.JobStep>>()))
// { .Returns(Task.FromResult(new List<JobExtensionRunner>()));
// 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(); List<IStep> result = await jobExtension.InitializeJob(_jobEc, _message);
// _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>>())) var trace = hc.GetTrace();
// .Returns(Task.CompletedTask);
// _taskManager.Setup(x => x.Load(It.Is<Pipelines.TaskStep>(t => t.DisplayName == "task1"))) trace.Info(string.Join(", ", result.Select(x => x.DisplayName)));
// .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); Assert.Equal(5, result.Count);
// 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); Assert.Equal("action1", result[0].DisplayName);
// hc.EnqueueInstance<ITaskRunner>(step2); Assert.Equal("action2", result[1].DisplayName);
// hc.EnqueueInstance<ITaskRunner>(step3); Assert.Equal("action3", result[2].DisplayName);
// hc.EnqueueInstance<ITaskRunner>(step4); Assert.Equal("action4", result[3].DisplayName);
// hc.EnqueueInstance<ITaskRunner>(step5); Assert.Equal("action5", result[4].DisplayName);
// 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); Assert.NotNull(result[0].ExecutionContext);
// _jobEc.InitializeJob(_message, _tokenSource.Token); Assert.NotNull(result[1].ExecutionContext);
// return hc; Assert.NotNull(result[2].ExecutionContext);
// } Assert.NotNull(result[3].ExecutionContext);
Assert.NotNull(result[4].ExecutionContext);
}
}
// [Fact] [Fact]
// [Trait("Level", "L0")] [Trait("Level", "L0")]
// [Trait("Category", "Worker")] [Trait("Category", "Worker")]
// public async Task JobExtensioBuildStepsList() public async Task JobExtensionBuildPreStepsList()
// { {
// using (TestHostContext hc = CreateTestContext()) using (TestHostContext hc = CreateTestContext())
// { {
// TestJobExtension testExtension = new TestJobExtension(); var jobExtension = new JobExtension();
// testExtension.Initialize(hc); jobExtension.Initialize(hc);
// List<IStep> result = await testExtension.InitializeJob(_jobEc, _message);
// var trace = hc.GetTrace(); _actionManager.Setup(x => x.PrepareActionsAsync(It.IsAny<IExecutionContext>(), It.IsAny<IEnumerable<Pipelines.JobStep>>()))
.Returns(Task.FromResult(new List<JobExtensionRunner>() { new JobExtensionRunner(null, "", "prepare1", null), new JobExtensionRunner(null, "", "prepare2", null) }));
// trace.Info(string.Join(", ", result.Select(x => x.DisplayName))); List<IStep> result = await jobExtension.InitializeJob(_jobEc, _message);
// Assert.Equal(12, result.Count); var trace = hc.GetTrace();
// Assert.Equal("task2", result[0].DisplayName); trace.Info(string.Join(", ", result.Select(x => x.DisplayName)));
// Assert.Equal("task3", result[1].DisplayName);
// Assert.Equal("task4", result[2].DisplayName);
// Assert.Equal("task6", result[3].DisplayName);
// Assert.Equal("task1", result[4].DisplayName);
// Assert.Equal("task2", result[5].DisplayName);
// Assert.Equal("task6", result[6].DisplayName);
// Assert.Equal("task7", result[7].DisplayName);
// Assert.Equal("task7", result[8].DisplayName);
// Assert.Equal("task5", result[9].DisplayName);
// Assert.Equal("task3", result[10].DisplayName);
// Assert.Equal("task2", result[11].DisplayName);
// }
// }
// // [Fact] Assert.Equal(7, result.Count);
// // [Trait("Level", "L0")]
// // [Trait("Category", "Worker")]
// // public async Task JobExtensionIntraTaskState()
// // {
// // using (TestHostContext hc = CreateTestContext())
// // {
// // TestJobExtension testExtension = new TestJobExtension();
// // testExtension.Initialize(hc);
// // List<IStep> result = await testExtension.InitializeJob(_jobEc, _message);
// // var trace = hc.GetTrace(); Assert.Equal("prepare1", result[0].DisplayName);
Assert.Equal("prepare2", result[1].DisplayName);
Assert.Equal("action1", result[2].DisplayName);
Assert.Equal("action2", result[3].DisplayName);
Assert.Equal("action3", result[4].DisplayName);
Assert.Equal("action4", result[5].DisplayName);
Assert.Equal("action5", result[6].DisplayName);
// // trace.Info(string.Join(", ", result.Select(x => x.DisplayName))); Assert.NotNull(result[0].ExecutionContext);
Assert.NotNull(result[1].ExecutionContext);
Assert.NotNull(result[2].ExecutionContext);
Assert.NotNull(result[3].ExecutionContext);
Assert.NotNull(result[4].ExecutionContext);
Assert.NotNull(result[5].ExecutionContext);
Assert.NotNull(result[6].ExecutionContext);
}
}
// // Assert.Equal(12, result.Count); [Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public void UploadDiganosticLogIfEnvironmentVariableSet()
{
using (TestHostContext hc = CreateTestContext())
{
var jobExtension = new JobExtension();
jobExtension.Initialize(hc);
// // result[0].ExecutionContext.TaskVariables.Set("state1", "value1", false); _message.Variables[Constants.Variables.Actions.RunnerDebug] = "true";
// // Assert.Equal("value1", result[5].ExecutionContext.TaskVariables.Get("state1"));
// // Assert.Equal("value1", result[11].ExecutionContext.TaskVariables.Get("state1"));
// // Assert.Null(result[4].ExecutionContext.TaskVariables.Get("state1")); _jobEc = new Runner.Worker.ExecutionContext();
// // Assert.Null(result[1].ExecutionContext.TaskVariables.Get("state1")); _jobEc.Initialize(hc);
// // Assert.Null(result[2].ExecutionContext.TaskVariables.Get("state1")); _jobEc.InitializeJob(_message, _tokenSource.Token);
// // Assert.Null(result[10].ExecutionContext.TaskVariables.Get("state1"));
// // Assert.Null(result[6].ExecutionContext.TaskVariables.Get("state1"));
// // Assert.Null(result[7].ExecutionContext.TaskVariables.Get("state1"));
// // }
// // }
// #if OS_WINDOWS jobExtension.FinalizeJob(_jobEc, _message, DateTime.UtcNow);
// [Fact]
// [Trait("Level", "L0")]
// [Trait("Category", "Worker")]
// public async Task JobExtensionManagementScriptStep()
// {
// using (TestHostContext hc = CreateTestContext())
// {
// hc.EnqueueInstance<IPagingLogger>(_logger.Object);
// hc.EnqueueInstance<IPagingLogger>(_logger.Object);
// Environment.SetEnvironmentVariable("VSTS_AGENT_INIT_INTERNAL_TEMP_HACK", "C:\\init.ps1"); _diagnosticLogManager.Verify(x =>
// Environment.SetEnvironmentVariable("VSTS_AGENT_CLEANUP_INTERNAL_TEMP_HACK", "C:\\clenup.ps1"); x.UploadDiagnosticLogs(
It.IsAny<IExecutionContext>(),
It.IsAny<IExecutionContext>(),
It.IsAny<Pipelines.AgentJobRequestMessage>(),
It.IsAny<DateTime>()),
Times.Once);
}
}
// try [Fact]
// { [Trait("Level", "L0")]
// TestJobExtension testExtension = new TestJobExtension(); [Trait("Category", "Worker")]
// testExtension.Initialize(hc); public void DontUploadDiagnosticLogIfEnvironmentVariableFalse()
// List<IStep> result = await testExtension.InitializeJob(_jobEc, _message); {
using (TestHostContext hc = CreateTestContext())
{
var jobExtension = new JobExtension();
jobExtension.Initialize(hc);
// var trace = hc.GetTrace(); _message.Variables[Constants.Variables.Actions.RunnerDebug] = "false";
// trace.Info(string.Join(", ", result.Select(x => x.DisplayName))); _jobEc = new Runner.Worker.ExecutionContext();
_jobEc.Initialize(hc);
_jobEc.InitializeJob(_message, _tokenSource.Token);
// Assert.Equal(14, result.Count); jobExtension.FinalizeJob(_jobEc, _message, DateTime.UtcNow);
// Assert.True(result[0] is ManagementScriptStep); _diagnosticLogManager.Verify(x =>
// Assert.True(result[13] is ManagementScriptStep); x.UploadDiagnosticLogs(
It.IsAny<IExecutionContext>(),
It.IsAny<IExecutionContext>(),
It.IsAny<Pipelines.AgentJobRequestMessage>(),
It.IsAny<DateTime>()),
Times.Never);
}
}
// Assert.Equal(result[0].DisplayName, "Agent Initialization"); [Fact]
// Assert.Equal(result[13].DisplayName, "Agent Cleanup"); [Trait("Level", "L0")]
// } [Trait("Category", "Worker")]
// finally public void DontUploadDiagnosticLogIfEnvironmentVariableMissing()
// { {
// Environment.SetEnvironmentVariable("VSTS_AGENT_INIT_INTERNAL_TEMP_HACK", ""); using (TestHostContext hc = CreateTestContext())
// Environment.SetEnvironmentVariable("VSTS_AGENT_CLEANUP_INTERNAL_TEMP_HACK", ""); {
// } var jobExtension = new JobExtension();
// } jobExtension.Initialize(hc);
// }
// #endif jobExtension.FinalizeJob(_jobEc, _message, DateTime.UtcNow);
// }
// } _diagnosticLogManager.Verify(x =>
x.UploadDiagnosticLogs(
It.IsAny<IExecutionContext>(),
It.IsAny<IExecutionContext>(),
It.IsAny<Pipelines.AgentJobRequestMessage>(),
It.IsAny<DateTime>()),
Times.Never);
}
}
}
}

View File

@@ -154,64 +154,5 @@ namespace GitHub.Runner.Common.Tests.Worker
_stepRunner.Verify(x => x.RunAsync(It.IsAny<IExecutionContext>()), Times.Never); _stepRunner.Verify(x => x.RunAsync(It.IsAny<IExecutionContext>()), 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<IExecutionContext>(),
// It.IsAny<Pipelines.AgentJobRequestMessage>(),
// It.IsAny<DateTime>()),
// 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<IExecutionContext>(),
// It.IsAny<Pipelines.AgentJobRequestMessage>(),
// It.IsAny<DateTime>()),
// 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<IExecutionContext>(),
// It.IsAny<Pipelines.AgentJobRequestMessage>(),
// It.IsAny<DateTime>()),
// Times.Never);
// }
// }
} }
} }

View File

@@ -1,448 +1,443 @@
// using GitHub.DistributedTask.WebApi; using GitHub.DistributedTask.WebApi;
// using GitHub.Runner.Worker; using GitHub.Runner.Worker;
// using Moq; using Moq;
// using System; using System;
// using System.Collections.Generic; using System.Collections.Generic;
// using System.Globalization; using System.Globalization;
// using System.Linq; using System.Linq;
// using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
// using System.Threading.Tasks; using System.Threading.Tasks;
// using Xunit; using Xunit;
// using GitHub.DistributedTask.Expressions2; using GitHub.DistributedTask.Expressions2;
// using GitHub.DistributedTask.Pipelines.ContextData; using GitHub.DistributedTask.Pipelines.ContextData;
using GitHub.DistributedTask.ObjectTemplating.Tokens;
// namespace GitHub.Runner.Common.Tests.Worker namespace GitHub.Runner.Common.Tests.Worker
// { {
// public sealed class StepsRunnerL0 public sealed class StepsRunnerL0
// { {
// private Mock<IExecutionContext> _ec; private Mock<IExecutionContext> _ec;
// private StepsRunner _stepsRunner; private StepsRunner _stepsRunner;
// private Variables _variables; private Variables _variables;
// private Dictionary<string, PipelineContextData> _contexts; private DictionaryContextData _contexts;
// private TestHostContext CreateTestContext([CallerMemberName] String testName = "") private JobContext _jobContext;
// { private StepsContext _stepContext;
// var hc = new TestHostContext(this, testName); private TestHostContext CreateTestContext([CallerMemberName] String testName = "")
// var expressionManager = new ExpressionManager(); {
// expressionManager.Initialize(hc); var hc = new TestHostContext(this, testName);
// hc.SetSingleton<IExpressionManager>(expressionManager); var expressionManager = new ExpressionManager();
// Dictionary<string, VariableValue> variablesToCopy = new Dictionary<string, VariableValue>(); expressionManager.Initialize(hc);
// variablesToCopy.Add(Constants.Variables.Agent.RetainDefaultEncoding, new VariableValue("true", false)); hc.SetSingleton<IExpressionManager>(expressionManager);
// _variables = new Variables( Dictionary<string, VariableValue> variablesToCopy = new Dictionary<string, VariableValue>();
// hostContext: hc, _variables = new Variables(
// copy: variablesToCopy); hostContext: hc,
// _ec = new Mock<IExecutionContext>(); copy: variablesToCopy);
// _ec.SetupAllProperties(); _ec = new Mock<IExecutionContext>();
// _ec.Setup(x => x.Variables).Returns(_variables); _ec.SetupAllProperties();
_ec.Setup(x => x.Variables).Returns(_variables);
// _contexts = new Dictionary<string, PipelineContextData>(); _contexts = new DictionaryContextData();
// _contexts["github"] = new DictionaryContextData(); _jobContext = new JobContext();
// _contexts["runner"] = new DictionaryContextData(); _contexts["github"] = new DictionaryContextData();
// _contexts["actions"] = new DictionaryContextData(); _contexts["runner"] = new DictionaryContextData();
// _ec.Setup(x => x.ExpressionValues).Returns(_contexts); _contexts["job"] = _jobContext;
_ec.Setup(x => x.ExpressionValues).Returns(_contexts);
_ec.Setup(x => x.JobContext).Returns(_jobContext);
// var _stepContext = new StepsContext(); _stepContext = new StepsContext();
// _ec.Setup(x => x.StepsContext).Returns(_stepContext); _ec.Setup(x => x.StepsContext).Returns(_stepContext);
// _stepsRunner = new StepsRunner();
// _stepsRunner.Initialize(hc);
// return hc;
// }
// [Fact] _ec.Setup(x => x.PostJobSteps).Returns(new Stack<IStep>());
// [Trait("Level", "L0")]
// [Trait("Category", "Worker")]
// public async Task RunNormalStepsAllStepPass()
// {
// using (TestHostContext hc = CreateTestContext())
// {
// // Arrange.
// var variableSets = new[]
// {
// new[] { CreateStep(TaskResult.Succeeded, ExpressionManager.Succeeded), CreateStep(TaskResult.Succeeded, ExpressionManager.Succeeded) },
// new[] { CreateStep(TaskResult.Succeeded, ExpressionManager.Succeeded), CreateStep(TaskResult.Succeeded, ExpressionManager.SucceededOrFailed) },
// new[] { CreateStep(TaskResult.Succeeded, ExpressionManager.Succeeded), CreateStep(TaskResult.Succeeded, ExpressionManager.Always) }
// };
// foreach (var variableSet in variableSets)
// {
// _ec.Object.Result = null;
// // Act. _stepsRunner = new StepsRunner();
// await _stepsRunner.RunAsync( _stepsRunner.Initialize(hc);
// jobContext: _ec.Object, return hc;
// steps: variableSet.Select(x => x.Object).ToList()); }
// // Assert. [Fact]
// Assert.Equal(TaskResult.Succeeded, _ec.Object.Result ?? TaskResult.Succeeded); [Trait("Level", "L0")]
// Assert.Equal(2, variableSet.Length); [Trait("Category", "Worker")]
// variableSet[0].Verify(x => x.RunAsync()); public async Task RunNormalStepsAllStepPass()
// variableSet[1].Verify(x => x.RunAsync()); {
// } using (TestHostContext hc = CreateTestContext())
// } {
// } // Arrange.
var variableSets = new[]
{
new[] { CreateStep(TaskResult.Succeeded, "success()"), CreateStep(TaskResult.Succeeded, "success()") },
new[] { CreateStep(TaskResult.Succeeded, "success()"), CreateStep(TaskResult.Succeeded, "success() || failure()") },
new[] { CreateStep(TaskResult.Succeeded, "success()"), CreateStep(TaskResult.Succeeded, "always()") }
};
foreach (var variableSet in variableSets)
{
_ec.Object.Result = null;
// [Fact] _ec.Setup(x => x.JobSteps).Returns(new Queue<IStep>(variableSet.Select(x => x.Object).ToList()));
// [Trait("Level", "L0")]
// [Trait("Category", "Worker")]
// public async Task RunNormalStepsContinueOnError()
// {
// using (TestHostContext hc = CreateTestContext())
// {
// // Arrange.
// var variableSets = new[]
// {
// new[] { CreateStep(TaskResult.Failed, ExpressionManager.Succeeded, true), CreateStep(TaskResult.Succeeded, ExpressionManager.Succeeded) },
// new[] { CreateStep(TaskResult.Failed, ExpressionManager.Succeeded, true), CreateStep(TaskResult.Succeeded, ExpressionManager.SucceededOrFailed) },
// new[] { CreateStep(TaskResult.Failed, ExpressionManager.Succeeded, true), CreateStep(TaskResult.Succeeded, ExpressionManager.Always) },
// new[] { CreateStep(TaskResult.Failed, ExpressionManager.Succeeded, true), CreateStep(TaskResult.Failed, ExpressionManager.Succeeded, true) },
// new[] { CreateStep(TaskResult.Failed, ExpressionManager.Succeeded, true), CreateStep(TaskResult.Failed, ExpressionManager.SucceededOrFailed, true) },
// new[] { CreateStep(TaskResult.Failed, ExpressionManager.Succeeded, true), CreateStep(TaskResult.Failed, ExpressionManager.Always, true) }
// };
// foreach (var variableSet in variableSets)
// {
// _ec.Object.Result = null;
// // Act. // Act.
// await _stepsRunner.RunAsync( await _stepsRunner.RunAsync(jobContext: _ec.Object);
// jobContext: _ec.Object,
// steps: variableSet.Select(x => x.Object).ToList());
// // Assert. // Assert.
// Assert.Equal(TaskResult.SucceededWithIssues, _ec.Object.Result); Assert.Equal(TaskResult.Succeeded, _ec.Object.Result ?? TaskResult.Succeeded);
// Assert.Equal(2, variableSet.Length); Assert.Equal(2, variableSet.Length);
// variableSet[0].Verify(x => x.RunAsync()); variableSet[0].Verify(x => x.RunAsync());
// variableSet[1].Verify(x => x.RunAsync()); variableSet[1].Verify(x => x.RunAsync());
// } }
// } }
// } }
// [Fact] [Fact]
// [Trait("Level", "L0")] [Trait("Level", "L0")]
// [Trait("Category", "Worker")] [Trait("Category", "Worker")]
// public async Task RunsAfterFailureBasedOnCondition() public async Task RunNormalStepsContinueOnError()
// { {
// using (TestHostContext hc = CreateTestContext()) using (TestHostContext hc = CreateTestContext())
// { {
// // Arrange. // Arrange.
// var variableSets = new[] var variableSets = new[]
// { {
// new new[] { CreateStep(TaskResult.Failed, "success()", true), CreateStep(TaskResult.Succeeded, "success()") },
// { new[] { CreateStep(TaskResult.Failed, "success()", true), CreateStep(TaskResult.Succeeded, "success() || failure()") },
// Steps = new[] { CreateStep(TaskResult.Failed, ExpressionManager.Succeeded), CreateStep(TaskResult.Succeeded, ExpressionManager.Succeeded) }, new[] { CreateStep(TaskResult.Failed, "success()", true), CreateStep(TaskResult.Succeeded, "always()") },
// Expected = false, new[] { CreateStep(TaskResult.Failed, "success()", true), CreateStep(TaskResult.Failed, "success()", true) },
// }, new[] { CreateStep(TaskResult.Failed, "success()", true), CreateStep(TaskResult.Failed, "success() || failure()", true) },
// new new[] { CreateStep(TaskResult.Failed, "success()", true), CreateStep(TaskResult.Failed, "always()", true) }
// { };
// Steps = new[] { CreateStep(TaskResult.Failed, ExpressionManager.Succeeded), CreateStep(TaskResult.Succeeded, ExpressionManager.SucceededOrFailed) }, foreach (var variableSet in variableSets)
// Expected = true, {
// }, _ec.Object.Result = null;
// };
// foreach (var variableSet in variableSets)
// {
// _ec.Object.Result = null;
// // Act. _ec.Setup(x => x.JobSteps).Returns(new Queue<IStep>(variableSet.Select(x => x.Object).ToList()));
// await _stepsRunner.RunAsync(
// jobContext: _ec.Object,
// steps: variableSet.Steps.Select(x => x.Object).ToList());
// // Assert. // Act.
// Assert.Equal(TaskResult.Failed, _ec.Object.Result ?? TaskResult.Succeeded); await _stepsRunner.RunAsync(jobContext: _ec.Object);
// Assert.Equal(2, variableSet.Steps.Length);
// variableSet.Steps[0].Verify(x => x.RunAsync());
// variableSet.Steps[1].Verify(x => x.RunAsync(), variableSet.Expected ? Times.Once() : Times.Never());
// }
// }
// }
// [Fact] // Assert.
// [Trait("Level", "L0")] Assert.Equal(TaskResult.Succeeded, _ec.Object.Result ?? TaskResult.Succeeded);
// [Trait("Category", "Worker")] Assert.Equal(2, variableSet.Length);
// public async Task RunsAlwaysSteps() variableSet[0].Verify(x => x.RunAsync());
// { variableSet[1].Verify(x => x.RunAsync());
// using (TestHostContext hc = CreateTestContext()) }
// { }
// // Arrange. }
// var variableSets = new[]
// {
// new
// {
// Steps = new[] { CreateStep(TaskResult.Succeeded, ExpressionManager.Succeeded), CreateStep(TaskResult.Succeeded, ExpressionManager.Always) },
// Expected = TaskResult.Succeeded,
// },
// new
// {
// Steps = new[] { CreateStep(TaskResult.Failed, ExpressionManager.Succeeded), CreateStep(TaskResult.Succeeded, ExpressionManager.Always) },
// Expected = TaskResult.Failed,
// },
// new
// {
// Steps = new[] { CreateStep(TaskResult.Failed, ExpressionManager.Succeeded), CreateStep(TaskResult.Succeeded, ExpressionManager.Always) },
// Expected = TaskResult.Failed,
// },
// new
// {
// Steps = new[] { CreateStep(TaskResult.Succeeded, ExpressionManager.Succeeded), CreateStep(TaskResult.Failed, ExpressionManager.Always) },
// Expected = TaskResult.Failed,
// },
// new
// {
// Steps = new[] { CreateStep(TaskResult.Succeeded, ExpressionManager.Succeeded), CreateStep(TaskResult.Failed, ExpressionManager.Always, true) },
// Expected = TaskResult.SucceededWithIssues,
// },
// };
// foreach (var variableSet in variableSets)
// {
// _ec.Object.Result = null;
// // Act. [Fact]
// await _stepsRunner.RunAsync( [Trait("Level", "L0")]
// jobContext: _ec.Object, [Trait("Category", "Worker")]
// steps: variableSet.Steps.Select(x => x.Object).ToList()); public async Task RunsAfterFailureBasedOnCondition()
{
using (TestHostContext hc = CreateTestContext())
{
// Arrange.
var variableSets = new[]
{
new
{
Steps = new[] { CreateStep(TaskResult.Failed, "success()"), CreateStep(TaskResult.Succeeded, "success()") },
Expected = false,
},
new
{
Steps = new[] { CreateStep(TaskResult.Failed, "success()"), CreateStep(TaskResult.Succeeded, "success() || failure()") },
Expected = true,
},
};
foreach (var variableSet in variableSets)
{
_ec.Object.Result = null;
// // Assert. _ec.Setup(x => x.JobSteps).Returns(new Queue<IStep>(variableSet.Steps.Select(x => x.Object).ToList()));
// Assert.Equal(variableSet.Expected, _ec.Object.Result ?? TaskResult.Succeeded);
// Assert.Equal(2, variableSet.Steps.Length);
// variableSet.Steps[0].Verify(x => x.RunAsync());
// variableSet.Steps[1].Verify(x => x.RunAsync());
// }
// }
// }
// [Fact] // Act.
// [Trait("Level", "L0")] await _stepsRunner.RunAsync(jobContext: _ec.Object);
// [Trait("Category", "Worker")]
// public async Task SetsJobResultCorrectly()
// {
// using (TestHostContext hc = CreateTestContext())
// {
// // Arrange.
// var variableSets = new[]
// {
// new
// {
// Steps = new[] { CreateStep(TaskResult.Failed, ExpressionManager.Succeeded), CreateStep(TaskResult.Succeeded, ExpressionManager.Succeeded) },
// Expected = TaskResult.Failed
// },
// new
// {
// Steps = new[] { CreateStep(TaskResult.Failed, ExpressionManager.Succeeded), CreateStep(TaskResult.Succeeded, ExpressionManager.SucceededOrFailed) },
// Expected = TaskResult.Failed
// },
// new
// {
// Steps = new[] { CreateStep(TaskResult.Failed, ExpressionManager.Succeeded), CreateStep(TaskResult.Succeeded, ExpressionManager.Always) },
// Expected = TaskResult.Failed
// },
// new
// {
// Steps = new[] { CreateStep(TaskResult.Failed, ExpressionManager.Succeeded, continueOnError: true), CreateStep(TaskResult.Failed, ExpressionManager.Succeeded) },
// Expected = TaskResult.Failed
// },
// new
// {
// Steps = new[] { CreateStep(TaskResult.Failed, ExpressionManager.Succeeded, continueOnError: true), CreateStep(TaskResult.Succeeded, ExpressionManager.Succeeded) },
// Expected = TaskResult.SucceededWithIssues
// },
// new
// {
// Steps = new[] { CreateStep(TaskResult.Failed, ExpressionManager.Succeeded, continueOnError: true), CreateStep(TaskResult.Failed, ExpressionManager.Succeeded, continueOnError: true) },
// Expected = TaskResult.SucceededWithIssues
// },
// new
// {
// Steps = new[] { CreateStep(TaskResult.Succeeded, ExpressionManager.SucceededOrFailed) },
// Expected = TaskResult.Succeeded
// },
// new
// {
// Steps = new[] { CreateStep(TaskResult.Succeeded, ExpressionManager.Succeeded), CreateStep(TaskResult.Failed, ExpressionManager.Succeeded) },
// Expected = TaskResult.Failed
// },
// new
// {
// Steps = new[] { CreateStep(TaskResult.Succeeded, ExpressionManager.Succeeded), CreateStep(TaskResult.SucceededWithIssues, ExpressionManager.Succeeded) },
// Expected = TaskResult.SucceededWithIssues
// },
// new
// {
// Steps = new[] { CreateStep(TaskResult.SucceededWithIssues, ExpressionManager.Succeeded), CreateStep(TaskResult.Succeeded, ExpressionManager.Succeeded) },
// Expected = TaskResult.SucceededWithIssues
// },
// new
// {
// Steps = new[] { CreateStep(TaskResult.SucceededWithIssues, ExpressionManager.Succeeded), CreateStep(TaskResult.Failed, ExpressionManager.Succeeded) },
// Expected = TaskResult.Failed
// },
// // Abandoned
// // Canceled
// // Failed
// // Skipped
// // Succeeded
// // SucceededWithIssues
// };
// foreach (var variableSet in variableSets)
// {
// _ec.Object.Result = null;
// // Act. // Assert.
// await _stepsRunner.RunAsync( Assert.Equal(TaskResult.Failed, _ec.Object.Result ?? TaskResult.Succeeded);
// jobContext: _ec.Object, Assert.Equal(2, variableSet.Steps.Length);
// steps: variableSet.Steps.Select(x => x.Object).ToList()); variableSet.Steps[0].Verify(x => x.RunAsync());
variableSet.Steps[1].Verify(x => x.RunAsync(), variableSet.Expected ? Times.Once() : Times.Never());
}
}
}
// // Assert. [Fact]
// Assert.True( [Trait("Level", "L0")]
// variableSet.Expected == (_ec.Object.Result ?? TaskResult.Succeeded), [Trait("Category", "Worker")]
// $"Expected '{variableSet.Expected}'. Actual '{_ec.Object.Result}'. Steps: {FormatSteps(variableSet.Steps)}"); public async Task RunsAlwaysSteps()
// } {
// } using (TestHostContext hc = CreateTestContext())
// } {
// Arrange.
var variableSets = new[]
{
new
{
Steps = new[] { CreateStep(TaskResult.Succeeded, "success()"), CreateStep(TaskResult.Succeeded, "always()") },
Expected = TaskResult.Succeeded,
},
new
{
Steps = new[] { CreateStep(TaskResult.Failed, "success()"), CreateStep(TaskResult.Succeeded, "always()") },
Expected = TaskResult.Failed,
},
new
{
Steps = new[] { CreateStep(TaskResult.Failed, "success()"), CreateStep(TaskResult.Succeeded, "always()") },
Expected = TaskResult.Failed,
},
new
{
Steps = new[] { CreateStep(TaskResult.Succeeded, "success()"), CreateStep(TaskResult.Failed, "always()") },
Expected = TaskResult.Failed,
},
new
{
Steps = new[] { CreateStep(TaskResult.Succeeded, "success()"), CreateStep(TaskResult.Failed, "always()", true) },
Expected = TaskResult.Succeeded,
},
};
foreach (var variableSet in variableSets)
{
_ec.Object.Result = null;
// [Fact] _ec.Setup(x => x.JobSteps).Returns(new Queue<IStep>(variableSet.Steps.Select(x => x.Object).ToList()));
// [Trait("Level", "L0")]
// [Trait("Category", "Worker")]
// public async Task SkipsAfterFailureOnlyBaseOnCondition()
// {
// using (TestHostContext hc = CreateTestContext())
// {
// // Arrange.
// var variableSets = new[]
// {
// new
// {
// Step = new[] { CreateStep(TaskResult.Failed, ExpressionManager.Succeeded), CreateStep(TaskResult.Succeeded, ExpressionManager.Succeeded) },
// Expected = false
// },
// new
// {
// Step = new[] { CreateStep(TaskResult.Failed, ExpressionManager.Succeeded), CreateStep(TaskResult.Succeeded, ExpressionManager.SucceededOrFailed) },
// Expected = true
// },
// new
// {
// Step = new[] { CreateStep(TaskResult.Failed, ExpressionManager.Succeeded), CreateStep(TaskResult.Succeeded, ExpressionManager.Always) },
// Expected = true
// }
// };
// foreach (var variableSet in variableSets)
// {
// _ec.Object.Result = null;
// // Act. // Act.
// await _stepsRunner.RunAsync( await _stepsRunner.RunAsync(jobContext: _ec.Object);
// jobContext: _ec.Object,
// steps: variableSet.Step.Select(x => x.Object).ToList());
// // Assert. // Assert.
// Assert.Equal(2, variableSet.Step.Length); Assert.Equal(variableSet.Expected, _ec.Object.Result ?? TaskResult.Succeeded);
// variableSet.Step[0].Verify(x => x.RunAsync()); Assert.Equal(2, variableSet.Steps.Length);
// variableSet.Step[1].Verify(x => x.RunAsync(), variableSet.Expected ? Times.Once() : Times.Never()); variableSet.Steps[0].Verify(x => x.RunAsync());
// } variableSet.Steps[1].Verify(x => x.RunAsync());
// } }
// } }
}
// [Fact] [Fact]
// [Trait("Level", "L0")] [Trait("Level", "L0")]
// [Trait("Category", "Worker")] [Trait("Category", "Worker")]
// public async Task AlwaysMeansAlways() public async Task SetsJobResultCorrectly()
// { {
// using (TestHostContext hc = CreateTestContext()) using (TestHostContext hc = CreateTestContext())
// { {
// // Arrange. // Arrange.
// var variableSets = new[] var variableSets = new[]
// { {
// new[] { CreateStep(TaskResult.Succeeded, ExpressionManager.Succeeded), CreateStep(TaskResult.Succeeded, ExpressionManager.Always) }, new
// new[] { CreateStep(TaskResult.SucceededWithIssues, ExpressionManager.Succeeded), CreateStep(TaskResult.Succeeded, ExpressionManager.Always) }, {
// new[] { CreateStep(TaskResult.Failed, ExpressionManager.Succeeded), CreateStep(TaskResult.Succeeded, ExpressionManager.Always) }, Steps = new[] { CreateStep(TaskResult.Failed, "success()"), CreateStep(TaskResult.Succeeded, "success()") },
// new[] { CreateStep(TaskResult.Canceled, ExpressionManager.Succeeded), CreateStep(TaskResult.Succeeded, ExpressionManager.Always) } Expected = TaskResult.Failed
// }; },
// foreach (var variableSet in variableSets) new
// { {
// _ec.Object.Result = null; Steps = new[] { CreateStep(TaskResult.Failed, "success()"), CreateStep(TaskResult.Succeeded, "success() || failure()") },
Expected = TaskResult.Failed
},
new
{
Steps = new[] { CreateStep(TaskResult.Failed, "success()"), CreateStep(TaskResult.Succeeded, "always()") },
Expected = TaskResult.Failed
},
new
{
Steps = new[] { CreateStep(TaskResult.Failed, "success()", continueOnError: true), CreateStep(TaskResult.Failed, "success()") },
Expected = TaskResult.Failed
},
new
{
Steps = new[] { CreateStep(TaskResult.Failed, "success()", continueOnError: true), CreateStep(TaskResult.Succeeded, "success()") },
Expected = TaskResult.Succeeded
},
new
{
Steps = new[] { CreateStep(TaskResult.Failed, "success()", continueOnError: true), CreateStep(TaskResult.Failed, "success()", continueOnError: true) },
Expected = TaskResult.Succeeded
},
new
{
Steps = new[] { CreateStep(TaskResult.Succeeded, "success() || failure()") },
Expected = TaskResult.Succeeded
},
new
{
Steps = new[] { CreateStep(TaskResult.Succeeded, "success()"), CreateStep(TaskResult.Failed, "success()") },
Expected = TaskResult.Failed
},
new
{
Steps = new[] { CreateStep(TaskResult.Succeeded, "success()"), CreateStep(TaskResult.Succeeded, "success()") },
Expected = TaskResult.Succeeded
},
// Abandoned
// Canceled
// Failed
// Skipped
// Succeeded
};
foreach (var variableSet in variableSets)
{
_ec.Object.Result = null;
// // Act. _ec.Setup(x => x.JobSteps).Returns(new Queue<IStep>(variableSet.Steps.Select(x => x.Object).ToList()));
// await _stepsRunner.RunAsync(
// jobContext: _ec.Object,
// steps: variableSet.Select(x => x.Object).ToList());
// // Assert. // Act.
// Assert.Equal(2, variableSet.Length); await _stepsRunner.RunAsync(jobContext: _ec.Object);
// variableSet[0].Verify(x => x.RunAsync());
// variableSet[1].Verify(x => x.RunAsync(), Times.Once());
// }
// }
// }
// [Fact] // Assert.
// [Trait("Level", "L0")] Assert.True(
// [Trait("Category", "Worker")] variableSet.Expected == (_ec.Object.Result ?? TaskResult.Succeeded),
// public async Task TreatsConditionErrorAsFailure() $"Expected '{variableSet.Expected}'. Actual '{_ec.Object.Result}'. Steps: {FormatSteps(variableSet.Steps)}");
// { }
// using (TestHostContext hc = CreateTestContext()) }
// { }
// var expressionManager = new Mock<IExpressionManager>();
// expressionManager.Object.Initialize(hc);
// hc.SetSingleton<IExpressionManager>(expressionManager.Object);
// expressionManager.Setup(x => x.Evaluate(It.IsAny<IExecutionContext>(), It.IsAny<IExpressionNode>(), It.IsAny<bool>())).Throws(new Exception());
// // Arrange. [Fact]
// var variableSets = new[] [Trait("Level", "L0")]
// { [Trait("Category", "Worker")]
// new[] { CreateStep(TaskResult.Succeeded, ExpressionManager.Succeeded) }, public async Task SkipsAfterFailureOnlyBaseOnCondition()
// new[] { CreateStep(TaskResult.Succeeded, ExpressionManager.Succeeded) }, {
// }; using (TestHostContext hc = CreateTestContext())
// foreach (var variableSet in variableSets) {
// { // Arrange.
// _ec.Object.Result = null; var variableSets = new[]
{
new
{
Step = new[] { CreateStep(TaskResult.Failed, "success()"), CreateStep(TaskResult.Succeeded, "success()") },
Expected = false
},
new
{
Step = new[] { CreateStep(TaskResult.Failed, "success()"), CreateStep(TaskResult.Succeeded, "success() || failure()") },
Expected = true
},
new
{
Step = new[] { CreateStep(TaskResult.Failed, "success()"), CreateStep(TaskResult.Succeeded, "always()") },
Expected = true
}
};
foreach (var variableSet in variableSets)
{
_ec.Object.Result = null;
// // Act. _ec.Setup(x => x.JobSteps).Returns(new Queue<IStep>(variableSet.Step.Select(x => x.Object).ToList()));
// await _stepsRunner.RunAsync(
// jobContext: _ec.Object,
// steps: variableSet.Select(x => x.Object).ToList());
// // Assert. // Act.
// Assert.Equal(TaskResult.Failed, _ec.Object.Result ?? TaskResult.Succeeded); await _stepsRunner.RunAsync(jobContext: _ec.Object);
// }
// }
// }
// private Mock<IStep> CreateStep(TaskResult result, IExpressionNode condition, Boolean continueOnError = false) // Assert.
// { Assert.Equal(2, variableSet.Step.Length);
// // Setup the step. variableSet.Step[0].Verify(x => x.RunAsync());
// var step = new Mock<IStep>(); variableSet.Step[1].Verify(x => x.RunAsync(), variableSet.Expected ? Times.Once() : Times.Never());
// step.Setup(x => x.Condition).Returns(condition); }
// step.Setup(x => x.ContinueOnError).Returns(continueOnError); }
// step.Setup(x => x.Enabled).Returns(true); }
// step.Setup(x => x.RunAsync()).Returns(Task.CompletedTask);
// // Setup the step execution context. [Fact]
// var stepContext = new Mock<IExecutionContext>(); [Trait("Level", "L0")]
// stepContext.SetupAllProperties(); [Trait("Category", "Worker")]
// stepContext.Setup(x => x.Variables).Returns(_variables); public async Task AlwaysMeansAlways()
// stepContext.Setup(x => x.ExpressionValues).Returns(_contexts); {
// stepContext.Setup(x => x.Complete(It.IsAny<TaskResult?>(), It.IsAny<string>(), It.IsAny<string>())) using (TestHostContext hc = CreateTestContext())
// .Callback((TaskResult? r, string currentOperation, string resultCode) => {
// { // Arrange.
// if (r != null) var variableSets = new[]
// { {
// stepContext.Object.Result = r; new[] { CreateStep(TaskResult.Succeeded, "success()"), CreateStep(TaskResult.Succeeded, "always()") },
// } new[] { CreateStep(TaskResult.Failed, "success()"), CreateStep(TaskResult.Succeeded, "always()") },
// }); new[] { CreateStep(TaskResult.Canceled, "success()"), CreateStep(TaskResult.Succeeded, "always()") }
// stepContext.Object.Result = result; };
// step.Setup(x => x.ExecutionContext).Returns(stepContext.Object); foreach (var variableSet in variableSets)
{
_ec.Object.Result = null;
// return step; _ec.Setup(x => x.JobSteps).Returns(new Queue<IStep>(variableSet.Select(x => x.Object).ToList()));
// }
// private string FormatSteps(IEnumerable<Mock<IStep>> steps) // Act.
// { await _stepsRunner.RunAsync(jobContext: _ec.Object);
// return String.Join(
// " ; ", // Assert.
// steps.Select(x => String.Format( Assert.Equal(2, variableSet.Length);
// CultureInfo.InvariantCulture, variableSet[0].Verify(x => x.RunAsync());
// "Returns={0},Condition=[{1}],ContinueOnError={2},Enabled={3}", variableSet[1].Verify(x => x.RunAsync(), Times.Once());
// x.Object.ExecutionContext.Result, }
// x.Object.Condition, }
// x.Object.ContinueOnError, }
// x.Object.Enabled)));
// } [Fact]
// } [Trait("Level", "L0")]
// } [Trait("Category", "Worker")]
public async Task TreatsConditionErrorAsFailure()
{
using (TestHostContext hc = CreateTestContext())
{
var expressionManager = new Mock<IExpressionManager>();
expressionManager.Object.Initialize(hc);
hc.SetSingleton<IExpressionManager>(expressionManager.Object);
expressionManager.Setup(x => x.Evaluate(It.IsAny<IExecutionContext>(), It.IsAny<string>(), It.IsAny<bool>())).Throws(new Exception());
// Arrange.
var variableSets = new[]
{
new[] { CreateStep(TaskResult.Succeeded, "success()") },
new[] { CreateStep(TaskResult.Succeeded, "success()") },
};
foreach (var variableSet in variableSets)
{
_ec.Object.Result = null;
_ec.Setup(x => x.JobSteps).Returns(new Queue<IStep>(variableSet.Select(x => x.Object).ToList()));
// Act.
await _stepsRunner.RunAsync(jobContext: _ec.Object);
// Assert.
Assert.Equal(TaskResult.Failed, _ec.Object.Result ?? TaskResult.Succeeded);
}
}
}
private Mock<IStep> CreateStep(TaskResult result, string condition, Boolean continueOnError = false)
{
// Setup the step.
var step = new Mock<IStep>();
step.Setup(x => x.Condition).Returns(condition);
step.Setup(x => x.ContinueOnError).Returns(new BooleanToken(null, null, null, continueOnError));
step.Setup(x => x.RunAsync()).Returns(Task.CompletedTask);
// Setup the step execution context.
var stepContext = new Mock<IExecutionContext>();
stepContext.SetupAllProperties();
stepContext.Setup(x => x.Variables).Returns(_variables);
stepContext.Setup(x => x.ExpressionValues).Returns(_contexts);
stepContext.Setup(x => x.JobContext).Returns(_jobContext);
stepContext.Setup(x => x.StepsContext).Returns(_stepContext);
stepContext.Setup(x => x.Complete(It.IsAny<TaskResult?>(), It.IsAny<string>(), It.IsAny<string>()))
.Callback((TaskResult? r, string currentOperation, string resultCode) =>
{
if (r != null)
{
stepContext.Object.Result = r;
}
});
stepContext.Object.Result = result;
step.Setup(x => x.ExecutionContext).Returns(stepContext.Object);
return step;
}
private string FormatSteps(IEnumerable<Mock<IStep>> steps)
{
return String.Join(
" ; ",
steps.Select(x => String.Format(
CultureInfo.InvariantCulture,
"Returns={0},Condition=[{1}],ContinueOnError={2}",
x.Object.ExecutionContext.Result,
x.Object.Condition,
x.Object.ContinueOnError)));
}
}
}

View File

@@ -1,181 +0,0 @@
// using System;
// using System.Collections.Generic;
// using System.Runtime.CompilerServices;
// using GitHub.DistributedTask.WebApi;
// using GitHub.Runner.Worker;
// using Moq;
// using Xunit;
// namespace GitHub.Runner.Common.Tests.Worker
// {
// public sealed class TaskCommandExtensionL0
// {
// private TestHostContext _hc;
// private Mock<IExecutionContext> _ec;
// private ServiceEndpoint _endpoint;
// [Fact]
// [Trait("Level", "L0")]
// [Trait("Category", "Worker")]
// public void SetEndpointAuthParameter()
// {
// SetupMocks();
// TaskCommandExtension commandExtension = new TaskCommandExtension();
// commandExtension.Initialize(_hc);
// var cmd = new Command("task", "setEndpoint");
// cmd.Data = "blah";
// cmd.Properties.Add("field", "authParameter");
// cmd.Properties.Add("id", Guid.Empty.ToString());
// cmd.Properties.Add("key", "test");
// commandExtension.ProcessCommand(_ec.Object, cmd);
// Assert.Equal(_endpoint.Authorization.Parameters["test"], "blah");
// }
// [Fact]
// [Trait("Level", "L0")]
// [Trait("Category", "Worker")]
// public void SetEndpointDataParameter()
// {
// SetupMocks();
// TaskCommandExtension commandExtension = new TaskCommandExtension();
// var cmd = new Command("task", "setEndpoint");
// cmd.Data = "blah";
// cmd.Properties.Add("field", "dataParameter");
// cmd.Properties.Add("id", Guid.Empty.ToString());
// cmd.Properties.Add("key", "test");
// commandExtension.ProcessCommand(_ec.Object, cmd);
// Assert.Equal(_endpoint.Data["test"], "blah");
// }
// [Fact]
// [Trait("Level", "L0")]
// [Trait("Category", "Worker")]
// public void SetEndpointUrlParameter()
// {
// SetupMocks();
// TaskCommandExtension commandExtension = new TaskCommandExtension();
// var cmd = new Command("task", "setEndpoint");
// cmd.Data = "http://blah/";
// cmd.Properties.Add("field", "url");
// cmd.Properties.Add("id", Guid.Empty.ToString());
// commandExtension.ProcessCommand(_ec.Object, cmd);
// Assert.Equal(_endpoint.Url.ToString(), cmd.Data);
// }
// [Fact]
// [Trait("Level", "L0")]
// [Trait("Category", "Worker")]
// public void SetEndpointWithoutValue()
// {
// SetupMocks();
// TaskCommandExtension commandExtension = new TaskCommandExtension();
// var cmd = new Command("task", "setEndpoint");
// Assert.Throws<Exception>(() => commandExtension.ProcessCommand(_ec.Object, cmd));
// }
// [Fact]
// [Trait("Level", "L0")]
// [Trait("Category", "Worker")]
// public void SetEndpointWithoutEndpointField()
// {
// SetupMocks();
// TaskCommandExtension commandExtension = new TaskCommandExtension();
// var cmd = new Command("task", "setEndpoint");
// Assert.Throws<Exception>(() => commandExtension.ProcessCommand(_ec.Object, cmd));
// }
// [Fact]
// [Trait("Level", "L0")]
// [Trait("Category", "Worker")]
// public void SetEndpointInvalidEndpointField()
// {
// SetupMocks();
// TaskCommandExtension commandExtension = new TaskCommandExtension();
// var cmd = new Command("task", "setEndpoint");
// cmd.Properties.Add("field", "blah");
// Assert.Throws<Exception>(() => commandExtension.ProcessCommand(_ec.Object, cmd));
// }
// [Fact]
// [Trait("Level", "L0")]
// [Trait("Category", "Worker")]
// public void SetEndpointWithoutEndpointId()
// {
// SetupMocks();
// TaskCommandExtension commandExtension = new TaskCommandExtension();
// var cmd = new Command("task", "setEndpoint");
// cmd.Properties.Add("field", "url");
// Assert.Throws<Exception>(() => commandExtension.ProcessCommand(_ec.Object, cmd));
// }
// [Fact]
// [Trait("Level", "L0")]
// [Trait("Category", "Worker")]
// public void SetEndpointInvalidEndpointId()
// {
// SetupMocks();
// TaskCommandExtension commandExtension = new TaskCommandExtension();
// var cmd = new Command("task", "setEndpoint");
// cmd.Properties.Add("field", "url");
// cmd.Properties.Add("id", "blah");
// Assert.Throws<Exception>(() => commandExtension.ProcessCommand(_ec.Object, cmd));
// }
// [Fact]
// [Trait("Level", "L0")]
// [Trait("Category", "Worker")]
// public void SetEndpointIdWithoutEndpointKey()
// {
// SetupMocks();
// TaskCommandExtension commandExtension = new TaskCommandExtension();
// var cmd = new Command("task", "setEndpoint");
// cmd.Properties.Add("field", "authParameter");
// cmd.Properties.Add("id", Guid.Empty.ToString());
// Assert.Throws<Exception>(() => commandExtension.ProcessCommand(_ec.Object, cmd));
// }
// [Fact]
// [Trait("Level", "L0")]
// [Trait("Category", "Worker")]
// public void SetEndpointUrlWithInvalidValue()
// {
// SetupMocks();
// TaskCommandExtension commandExtension = new TaskCommandExtension();
// var cmd = new Command("task", "setEndpoint");
// cmd.Data = "blah";
// cmd.Properties.Add("field", "url");
// cmd.Properties.Add("id", Guid.Empty.ToString());
// Assert.Throws<Exception>(() => commandExtension.ProcessCommand(_ec.Object, cmd));
// }
// private void SetupMocks([CallerMemberName] string name = "")
// {
// _hc = new TestHostContext(this, name);
// _ec = new Mock<IExecutionContext>();
// _endpoint = new ServiceEndpoint()
// {
// Id = Guid.Empty,
// Url = new Uri("https://test.com"),
// Authorization = new EndpointAuthorization()
// {
// Scheme = "Test",
// }
// };
// _ec.Setup(x => x.Endpoints).Returns(new List<ServiceEndpoint> { _endpoint });
// }
// }
// }