Skip to content

Commit

Permalink
Add L1 tests for signing (#2873)
Browse files Browse the repository at this point in the history
* Add L1 tests for signature verification.

* Clean.

* Clean.

* Clean.

* Dispose.

* Dispose.

* Add TearDown.

* Add try/finally.

* Rename l1 host context.

* Fix.

* Add debug printing.

* Install Nuget in CI for Windows tests.

* Fix external.
  • Loading branch information
stephenmichaelf authored Mar 30, 2020
1 parent c3a88f2 commit 8fb1c2a
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 102 deletions.
3 changes: 3 additions & 0 deletions .vsts.template.windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ steps:
workingDirectory: src
displayName: Unit tests

- task: NuGetToolInstaller@1
displayName: Install NuGet for signing tests

- script: dev.cmd testl1
workingDirectory: src
displayName: Functional tests
Expand Down
7 changes: 7 additions & 0 deletions src/Misc/externals.sh
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,11 @@ fi
if [[ "$L1_MODE" != "" || "$PRECACHE" != "" ]]; then
# cmdline task
acquireExternalTool "$CONTAINER_URL/l1Tasks/d9bafed4-0b18-4f58-968d-86655b4d2ce9.zip" "Tasks" false dont_uncompress

# with the current setup of this package there are backslashes so it fails to extract on non-windows at runtime
# we may need to fix this in the Agent
if [[ "$PACKAGERUNTIME" == "win-x64" || "$PACKAGERUNTIME" == "win-x86" ]]; then
# signed service tree task
acquireExternalTool "$CONTAINER_URL/l1Tasks/5515f72c-5faa-4121-8a46-8f42a8f42132.zip" "Tasks" false dont_uncompress
fi
fi
20 changes: 16 additions & 4 deletions src/Test/L1/Mock/FakeConfigurationStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class FakeConfigurationStore : AgentService, IConfigurationStore

public string RootFolder => Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/TestRuns/" + WorkingDirectoryName;

private AgentSettings _agentSettings;

public bool IsConfigured()
{
return true;
Expand Down Expand Up @@ -36,11 +38,21 @@ public CredentialData GetCredentials()

public AgentSettings GetSettings()
{
return new AgentSettings
if (_agentSettings == null)
{
AgentName = "TestAgent",
WorkFolder = RootFolder + "/w"
};
_agentSettings = new AgentSettings
{
AgentName = "TestAgent",
WorkFolder = RootFolder + "/w"
};
}

return _agentSettings;
}

public void UpdateSettings(AgentSettings agentSettings)
{
_agentSettings = agentSettings;
}

public void SaveCredential(CredentialData credential)
Expand Down
51 changes: 36 additions & 15 deletions src/Test/L1/Worker/L1TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class TestResults
public bool TimedOut { get; internal set; }
}

public class L1TestBase
public class L1TestBase : IDisposable
{
protected TimeSpan ChannelTimeout = TimeSpan.FromSeconds(100);
protected TimeSpan JobTimeout = TimeSpan.FromSeconds(100);
Expand Down Expand Up @@ -93,8 +93,7 @@ protected static TaskStep CreateScriptTask(string script)
return step;
}

protected async Task<TestResults> RunWorker(Pipelines.AgentJobRequestMessage message,
[CallerMemberName] string testName = "")
public void SetupL1([CallerMemberName] string testName = "")
{
// Clear working directory
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/TestRuns/" + testName;
Expand All @@ -108,21 +107,29 @@ protected async Task<TestResults> RunWorker(Pipelines.AgentJobRequestMessage mes
var stringFile = Path.Combine(assemblyLocation, "en-US", "strings.json");
StringUtil.LoadExternalLocalization(stringFile);

using (L1HostContext context = new L1HostContext("Agent", GetLogFile(this, testName)))
{
SetupMocks(context);
_l1HostContext = new L1HostContext("Agent", GetLogFile(this, testName));
SetupMocks(_l1HostContext);

// Use different working directories for each test
var config = GetMockedService<FakeConfigurationStore>();
config.WorkingDirectoryName = testName;
// Use different working directories for each test
var config = GetMockedService<FakeConfigurationStore>(); // TODO: Need to update this. can hack it for now.
config.WorkingDirectoryName = testName;
}

await SetupMessage(context, message);
protected L1HostContext _l1HostContext;

using (var cts = new CancellationTokenSource())
{
cts.CancelAfter((int)JobTimeout.TotalMilliseconds);
return await RunWorker(context, message, cts.Token);
}
protected async Task<TestResults> RunWorker(Pipelines.AgentJobRequestMessage message)
{
if (_l1HostContext == null)
{
throw new InvalidOperationException("Must call SetupL1() to initialize L1HostContext before calling RunWorker()");
}

await SetupMessage(_l1HostContext, message);

using (var cts = new CancellationTokenSource())
{
cts.CancelAfter((int)JobTimeout.TotalMilliseconds);
return await RunWorker(_l1HostContext, message, cts.Token);
}
}

Expand Down Expand Up @@ -207,6 +214,20 @@ await processChannel.SendAsync(
}
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
this._l1HostContext?.Dispose();
}
}

protected static readonly String JobMessageTemplate = @"
{
'mask': [
Expand Down
Loading

0 comments on commit 8fb1c2a

Please sign in to comment.