-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c4fe406
commit 9673941
Showing
5 changed files
with
245 additions
and
0 deletions.
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[] testArgs = args.Skip(1).ToArray(); | ||
|
||
switch (testType) | ||
{ | ||
case "ZeroExitCode": | ||
Assert.Equal(0, testArgs.Length); | ||
return 0; | ||
|
||
case "NonzeroExitCode": | ||
Assert.Equal(0, testArgs.Length); | ||
return 1; | ||
|
||
case "Sleep": | ||
Assert.Equal(1, testArgs.Length); | ||
string delayArg = testArgs[0]; | ||
int delay = int.Parse(delayArg); | ||
Thread.Sleep(delay); | ||
return 0; | ||
|
||
case "TextFileOutput": | ||
Assert.Equal(2, testArgs.Length); | ||
string pathArg = testArgs[0]; | ||
string contentsArg = testArgs[1]; | ||
File.WriteAllText(pathArg, contentsArg); | ||
return 0; | ||
|
||
default: | ||
throw new ArgumentException($"Unknown test type {testType}."); | ||
} | ||
} | ||
} | ||
} |
177 changes: 177 additions & 0 deletions
177
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,177 @@ | ||
// 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; | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.Diagnostics.Monitoring.Tool.UnitTests | ||
{ | ||
public sealed class ExecuteActionTests | ||
{ | ||
private const int TokenTimeoutMs = 10000; | ||
private const int DelayMs = 1000; | ||
|
||
[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 + DelayMs).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(); | ||
|
||
List<string> args = new(); | ||
|
||
// Entrypoint assembly | ||
args.Add(AssemblyHelper.GetAssemblyArtifactBinPath(currAssembly, "Microsoft.Diagnostics.Monitoring.ExecuteActionApp", TargetFrameworkMoniker.NetCoreApp31)); | ||
|
||
// Entrypoint arguments | ||
args.AddRange(additionalArgs); | ||
|
||
return string.Join(' ', args); | ||
} | ||
|
||
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