-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Execute Action - Testing #742
Merged
kkeirstead
merged 8 commits into
dotnet:feature/triggers
from
kkeirstead:kkeirstead/triggers/ExecuteActionTesting
Aug 20, 2021
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d7c5e29
Recreating the changes I accidentally deleted earlier today...whoops.…
kkeirstead 1014578
Three basic tests are now working. Had to change Directory.Builds.Pro…
kkeirstead 1dc9038
Added in test that writes to a file and then ensures that the file wa…
kkeirstead 3512c06
Cleaning up the tests.
kkeirstead 820a580
Minor tweaks. Noticed the presence of MSBuild_Logs, which I haven't s…
kkeirstead 7878c01
Merged with feature/Triggers; changes for Justin and Patrick includin…
kkeirstead bd42c82
Small tweaks for Justin.
kkeirstead 21ad564
Small tweak for Justin.
kkeirstead File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...tics.Monitoring.ExecuteActionApp/Microsoft.Diagnostics.Monitoring.ExecuteActionApp.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFrameworks>netcoreapp3.1</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Microsoft.Diagnostics.Monitoring.TestCommon\Microsoft.Diagnostics.Monitoring.TestCommon.csproj" /> | ||
</ItemGroup> | ||
</Project> |
50 changes: 50 additions & 0 deletions
50
src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading; | ||
using Xunit; | ||
|
||
namespace Microsoft.Diagnostics.Monitoring.ExecuteActionApp | ||
{ | ||
internal class Program | ||
{ | ||
public static int Main(string[] args) | ||
{ | ||
string testType = args[0]; | ||
|
||
string[] additionalArgs = args.Skip(1).ToArray(); | ||
jander-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
switch (testType) | ||
{ | ||
case "ZeroExitCode": | ||
Assert.Equal(1, args.Length); | ||
jander-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return 0; | ||
|
||
case "NonzeroExitCode": | ||
Assert.Equal(1, args.Length); | ||
return -1; | ||
|
||
case "Sleep": | ||
Assert.Equal(2, args.Length); | ||
string delayArg = additionalArgs[0]; | ||
int delay = int.Parse(delayArg) + 1000; // Add a second delay to the token cancellation time | ||
jander-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Thread.Sleep(delay); | ||
return 0; | ||
|
||
case "TextFileOutput": | ||
Assert.Equal(3, args.Length); | ||
string pathArg = additionalArgs[0]; | ||
string contentsArg = additionalArgs[1]; | ||
File.WriteAllText(pathArg, contentsArg); | ||
return 0; | ||
|
||
default: | ||
throw new ArgumentException("Unknown provided test type."); | ||
jander-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
} |
169 changes: 169 additions & 0 deletions
169
src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Options.Actions; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Actions; | ||
using System.Reflection; | ||
using Microsoft.Diagnostics.Monitoring.TestCommon; | ||
using System.Threading; | ||
using System; | ||
using System.IO; | ||
using System.Diagnostics; | ||
using Microsoft.Diagnostics.Tools.Monitor; | ||
|
||
namespace Microsoft.Diagnostics.Monitoring.Tool.UnitTests | ||
{ | ||
public sealed class ExecuteActionTests | ||
jander-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
private const int TokenTimeoutMs = 10000; | ||
|
||
[Fact] | ||
public async Task ExecuteAction_ZeroExitCode() | ||
{ | ||
ExecuteAction action = new(); | ||
|
||
ExecuteOptions options = new(); | ||
|
||
options.Path = DotNetHost.HostExePath; | ||
options.Arguments = GenerateArgumentsString(new string[] { "ZeroExitCode" }); | ||
|
||
using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TokenTimeoutMs); | ||
|
||
CollectionRuleActionResult result = await action.ExecuteAsync(options, null, cancellationTokenSource.Token); | ||
|
||
ValidateActionResult(result, "0"); | ||
} | ||
|
||
[Fact] | ||
public async Task ExecuteAction_NonzeroExitCode() | ||
{ | ||
ExecuteAction action = new(); | ||
|
||
ExecuteOptions options = new(); | ||
|
||
options.Path = DotNetHost.HostExePath; | ||
options.Arguments = GenerateArgumentsString(new string[] { "NonzeroExitCode" }); | ||
|
||
using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TokenTimeoutMs); | ||
|
||
InvalidOperationException invalidOperationException = await Assert.ThrowsAsync<InvalidOperationException>( | ||
() => action.ExecuteAsync(options, null, cancellationTokenSource.Token)); | ||
|
||
Assert.Contains(string.Format(Strings.ErrorMessage_NonzeroExitCode, "-1"), invalidOperationException.Message); | ||
} | ||
|
||
[Fact] | ||
public async Task ExecuteAction_TokenCancellation() | ||
{ | ||
ExecuteAction action = new(); | ||
|
||
ExecuteOptions options = new(); | ||
|
||
options.Path = DotNetHost.HostExePath; | ||
options.Arguments = GenerateArgumentsString(new string[] { "Sleep", TokenTimeoutMs.ToString() }); ; | ||
|
||
using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TokenTimeoutMs); | ||
|
||
TaskCanceledException taskCanceledException = await Assert.ThrowsAsync<TaskCanceledException>( | ||
() => action.ExecuteAsync(options, null, cancellationTokenSource.Token)); | ||
} | ||
|
||
[Fact] | ||
public async Task ExecuteAction_TextFileOutput() | ||
{ | ||
ExecuteAction action = new(); | ||
|
||
ExecuteOptions options = new(); | ||
|
||
DirectoryInfo outputDirectory = null; | ||
|
||
try | ||
{ | ||
outputDirectory = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "ExecuteAction", Guid.NewGuid().ToString())); | ||
string textFileOutputPath = Path.Combine(outputDirectory.FullName, "file.txt"); | ||
|
||
const string testMessage = "TestMessage"; | ||
|
||
options.Path = DotNetHost.HostExePath; | ||
options.Arguments = GenerateArgumentsString(new string[] { "TextFileOutput", textFileOutputPath, testMessage }); | ||
|
||
using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TokenTimeoutMs); | ||
|
||
CollectionRuleActionResult result = await action.ExecuteAsync(options, null, cancellationTokenSource.Token); | ||
|
||
ValidateActionResult(result, "0"); | ||
|
||
Assert.Equal(testMessage, File.ReadAllText(textFileOutputPath)); | ||
} | ||
finally | ||
{ | ||
try | ||
{ | ||
outputDirectory?.Delete(recursive: true); | ||
} | ||
catch | ||
{ | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task ExecuteAction_InvalidPath() | ||
{ | ||
ExecuteAction action = new(); | ||
|
||
ExecuteOptions options = new(); | ||
|
||
string uniquePathName = Guid.NewGuid().ToString(); | ||
|
||
options.Path = uniquePathName; | ||
options.Arguments = GenerateArgumentsString(Array.Empty<string>()); | ||
|
||
using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TokenTimeoutMs); | ||
|
||
FileNotFoundException fileNotFoundException = await Assert.ThrowsAsync<FileNotFoundException>( | ||
() => action.ExecuteAsync(options, null, cancellationTokenSource.Token)); | ||
|
||
Assert.Equal(string.Format(Strings.ErrorMessage_FileNotFound, uniquePathName), fileNotFoundException.Message); | ||
} | ||
|
||
[Fact] | ||
public async Task ExecuteAction_IgnoreExitCode() | ||
{ | ||
ExecuteAction action = new(); | ||
|
||
ExecuteOptions options = new(); | ||
|
||
options.Path = DotNetHost.HostExePath; | ||
options.Arguments = GenerateArgumentsString(new string[] { "NonzeroExitCode" }); | ||
options.IgnoreExitCode = true; | ||
|
||
using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TokenTimeoutMs); | ||
|
||
CollectionRuleActionResult result = await action.ExecuteAsync(options, null, cancellationTokenSource.Token); | ||
|
||
ValidateActionResult(result, "-1"); | ||
} | ||
|
||
private static string GenerateArgumentsString(string[] additionalArgs) | ||
{ | ||
Assembly currAssembly = Assembly.GetExecutingAssembly(); | ||
|
||
return AssemblyHelper.GetAssemblyArtifactBinPath(currAssembly, currAssembly.GetName().Name, TargetFrameworkMoniker.NetCoreApp31).Replace( | ||
jander-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
currAssembly.GetName().Name, | ||
"Microsoft.Diagnostics.Monitoring.ExecuteActionApp") + ' ' + string.Join(' ', additionalArgs); | ||
} | ||
|
||
private static void ValidateActionResult(CollectionRuleActionResult result, string expectedExitCode) | ||
{ | ||
string actualExitCode; | ||
|
||
Assert.NotNull(result.OutputValues); | ||
Assert.True(result.OutputValues.TryGetValue("ExitCode", out actualExitCode)); | ||
Assert.Equal(expectedExitCode, actualExitCode); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider some argument validation to make it easier to figure out failures
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What did you have in mind? In theory, since for each test we're manually setting the args, shouldn't we know that they'll all be valid?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example:
args
parameter. This allows us to change the ordering of the arguments passed on the command line without having to rewrite all of the test cases.