From d7c5e29cf4d6b28e8d8ccd76fdb35058a129c8fa Mon Sep 17 00:00:00 2001 From: kkeirstead Date: Tue, 17 Aug 2021 20:16:11 -0700 Subject: [PATCH 1/7] Recreating the changes I accidentally deleted earlier today...whoops. Three basic test cases that in theory should work, but don't for reasons that might be local to my machine. --- dotnet-monitor.sln | 7 ++ ...nostics.Monitoring.ExecuteActionApp.csproj | 12 +++ .../Program.cs | 33 ++++++++ .../ExecuteActionTests.cs | 83 +++++++++++++++++++ .../CollectionRules/Actions/ExecuteAction.cs | 12 +++ 5 files changed, 147 insertions(+) create mode 100644 src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Microsoft.Diagnostics.Monitoring.ExecuteActionApp.csproj create mode 100644 src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs create mode 100644 src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs diff --git a/dotnet-monitor.sln b/dotnet-monitor.sln index 0ad570d658c..2d67f85e35a 100644 --- a/dotnet-monitor.sln +++ b/dotnet-monitor.sln @@ -38,6 +38,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Diagnostics.Monit EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Diagnostics.Monitoring.Tool.UnitTests", "src\Tests\Microsoft.Diagnostics.Monitoring.Tool.UnitTests\Microsoft.Diagnostics.Monitoring.Tool.UnitTests.csproj", "{0DBE362D-82F1-4740-AE6A-40C1A82EDCDB}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Diagnostics.Monitoring.ExecuteActionApp", "src\Tests\Microsoft.Diagnostics.Monitoring.ExecuteActionApp\Microsoft.Diagnostics.Monitoring.ExecuteActionApp.csproj", "{A5A0CAAB-C200-44D2-BC93-8445C6E748AD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -92,6 +94,10 @@ Global {0DBE362D-82F1-4740-AE6A-40C1A82EDCDB}.Debug|Any CPU.Build.0 = Debug|Any CPU {0DBE362D-82F1-4740-AE6A-40C1A82EDCDB}.Release|Any CPU.ActiveCfg = Release|Any CPU {0DBE362D-82F1-4740-AE6A-40C1A82EDCDB}.Release|Any CPU.Build.0 = Release|Any CPU + {A5A0CAAB-C200-44D2-BC93-8445C6E748AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A5A0CAAB-C200-44D2-BC93-8445C6E748AD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A5A0CAAB-C200-44D2-BC93-8445C6E748AD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A5A0CAAB-C200-44D2-BC93-8445C6E748AD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -112,6 +118,7 @@ Global {3AD0A40B-C569-4712-9764-7A788B9CD811} = {C7568468-1C79-4944-8136-18812A7F9EA7} {173F959B-231B-45D1-8328-9460D4C5BC71} = {19FAB78C-3351-4911-8F0C-8C6056401740} {0DBE362D-82F1-4740-AE6A-40C1A82EDCDB} = {C7568468-1C79-4944-8136-18812A7F9EA7} + {A5A0CAAB-C200-44D2-BC93-8445C6E748AD} = {C7568468-1C79-4944-8136-18812A7F9EA7} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {46465737-C938-44FC-BE1A-4CE139EBB5E0} diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Microsoft.Diagnostics.Monitoring.ExecuteActionApp.csproj b/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Microsoft.Diagnostics.Monitoring.ExecuteActionApp.csproj new file mode 100644 index 00000000000..a23ef79e695 --- /dev/null +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Microsoft.Diagnostics.Monitoring.ExecuteActionApp.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp3.1 + + + + + + + diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs b/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs new file mode 100644 index 00000000000..ff96b49210c --- /dev/null +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs @@ -0,0 +1,33 @@ +// 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.Threading.Tasks; + +namespace Microsoft.Diagnostics.Monitoring.ExecuteActionApp +{ + internal class Program + { + public static int Main(string[] args) + { + string testType = args[1]; + const int DelayMs = 3000; // Should be greater than the token delay in the test. + + switch (testType) + { + case "ZeroExitCode": + return 0; ; + + case "NonzeroExitCode": + return -1; + + case "TokenCancellation": + Task.Delay(DelayMs); + return 0; + + default: + return -100; + } + } + } +} diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs new file mode 100644 index 00000000000..b89f1f6dd5b --- /dev/null +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs @@ -0,0 +1,83 @@ +// 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; + +namespace Microsoft.Diagnostics.Monitoring.Tool.UnitTests +{ + public sealed class ExecuteActionTests + { + private const int TokenTimeoutMs = 2000; // Arbitrarily set + + // This should be identical to the error message found in Strings.resx (except without the process's exit code) + private const string NonzeroExitCodeMessage = "The process exited with exit code"; + + [Fact] + public async Task ExecuteAction_ZeroExitCode() + { + ExecuteAction action = new(); + + ExecuteOptions options = new(); + + options.Path = DotNetHost.HostExePath; + options.Arguments = Assembly.GetExecutingAssembly().Location.Replace( + Assembly.GetExecutingAssembly().GetName().Name, + "Microsoft.Diagnostics.Monitoring.ExecuteActionApp") + " ZeroExitCode"; + + using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TokenTimeoutMs); + CancellationToken cancellationToken = cancellationTokenSource.Token; + + CollectionRuleActionResult result = await action.ExecuteAsync(options, null, cancellationToken); + + Assert.Equal("0", result.OutputValues["ExitCode"]); + } + + [Fact] + public async Task ExecuteAction_NonzeroExitCode() + { + ExecuteAction action = new(); + + ExecuteOptions options = new(); + + options.Path = DotNetHost.HostExePath; + options.Arguments = Assembly.GetExecutingAssembly().Location.Replace( + Assembly.GetExecutingAssembly().GetName().Name, + "Microsoft.Diagnostics.Monitoring.ExecuteActionApp") + " NonzeroExitCode"; + + using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TokenTimeoutMs); + CancellationToken cancellationToken = cancellationTokenSource.Token; + + InvalidOperationException invalidOperationException = await Assert.ThrowsAsync( + () => action.ExecuteAsync(options, null, cancellationToken)); + Assert.Contains(NonzeroExitCodeMessage, invalidOperationException.Message); + } + + [Fact] + public async Task ExecuteAction_TokenCancellation() + { + ExecuteAction action = new(); + + ExecuteOptions options = new(); + + options.Path = DotNetHost.HostExePath; + options.Arguments = Assembly.GetExecutingAssembly().Location.Replace( + Assembly.GetExecutingAssembly().GetName().Name, + "Microsoft.Diagnostics.Monitoring.ExecuteActionApp") + " TokenCancellation"; + + using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TokenTimeoutMs); + CancellationToken cancellationToken = cancellationTokenSource.Token; + + OperationCanceledException invalidOperationException = await Assert.ThrowsAsync( + () => action.ExecuteAsync(options, null, cancellationToken)); + Assert.Contains(NonzeroExitCodeMessage, invalidOperationException.Message); + } + } +} diff --git a/src/Tools/dotnet-monitor/CollectionRules/Actions/ExecuteAction.cs b/src/Tools/dotnet-monitor/CollectionRules/Actions/ExecuteAction.cs index 4464b6c5b9d..06a21355e82 100644 --- a/src/Tools/dotnet-monitor/CollectionRules/Actions/ExecuteAction.cs +++ b/src/Tools/dotnet-monitor/CollectionRules/Actions/ExecuteAction.cs @@ -29,6 +29,18 @@ public async Task ExecuteAsync(ExecuteOptions option using Process process = new Process(); process.StartInfo = new ProcessStartInfo(path, arguments); + + /* + process.StartInfo = new ProcessStartInfo(path); + + string[] args = arguments.Split(); + + foreach (string arg in args) + { + process.StartInfo.ArgumentList.Add(arg); + } + */ + process.EnableRaisingEvents = true; // Completion source that is signaled when the process exits From 1014578cb1cb80b9d65c443969213bbc710d3e6c Mon Sep 17 00:00:00 2001 From: kkeirstead Date: Wed, 18 Aug 2021 09:46:35 -0700 Subject: [PATCH 2/7] Three basic tests are now working. Had to change Directory.Builds.Props due to the error saying -> the feature global using directive is currently in preview and unsupported. Planning to further flesh out testing and clean up code. --- Directory.Build.props | 1 + dotnet-monitor.sln | 2 +- ...t.Diagnostics.Monitoring.ExecuteActionApp.csproj | 12 ------------ ...crosoft.Diagnostics.Monitoring.ExecuteApp.csproj | 10 ++++++++++ .../Program.cs | 11 +++++++---- .../ExecuteActionTests.cs | 13 ++++++++----- .../CollectionRules/Actions/ExecuteAction.cs | 3 +-- 7 files changed, 28 insertions(+), 24 deletions(-) delete mode 100644 src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Microsoft.Diagnostics.Monitoring.ExecuteActionApp.csproj create mode 100644 src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Microsoft.Diagnostics.Monitoring.ExecuteApp.csproj diff --git a/Directory.Build.props b/Directory.Build.props index d354b43f1dd..2bf4a075495 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -9,6 +9,7 @@ 4 true true + true diff --git a/dotnet-monitor.sln b/dotnet-monitor.sln index 2d67f85e35a..1c165d5e118 100644 --- a/dotnet-monitor.sln +++ b/dotnet-monitor.sln @@ -38,7 +38,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Diagnostics.Monit EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Diagnostics.Monitoring.Tool.UnitTests", "src\Tests\Microsoft.Diagnostics.Monitoring.Tool.UnitTests\Microsoft.Diagnostics.Monitoring.Tool.UnitTests.csproj", "{0DBE362D-82F1-4740-AE6A-40C1A82EDCDB}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Diagnostics.Monitoring.ExecuteActionApp", "src\Tests\Microsoft.Diagnostics.Monitoring.ExecuteActionApp\Microsoft.Diagnostics.Monitoring.ExecuteActionApp.csproj", "{A5A0CAAB-C200-44D2-BC93-8445C6E748AD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Diagnostics.Monitoring.ExecuteApp", "src\Tests\Microsoft.Diagnostics.Monitoring.ExecuteActionApp\Microsoft.Diagnostics.Monitoring.ExecuteApp.csproj", "{A5A0CAAB-C200-44D2-BC93-8445C6E748AD}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Microsoft.Diagnostics.Monitoring.ExecuteActionApp.csproj b/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Microsoft.Diagnostics.Monitoring.ExecuteActionApp.csproj deleted file mode 100644 index a23ef79e695..00000000000 --- a/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Microsoft.Diagnostics.Monitoring.ExecuteActionApp.csproj +++ /dev/null @@ -1,12 +0,0 @@ - - - - Exe - netcoreapp3.1 - - - - - - - diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Microsoft.Diagnostics.Monitoring.ExecuteApp.csproj b/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Microsoft.Diagnostics.Monitoring.ExecuteApp.csproj new file mode 100644 index 00000000000..9ff1e502693 --- /dev/null +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Microsoft.Diagnostics.Monitoring.ExecuteApp.csproj @@ -0,0 +1,10 @@ + + + Exe + netcoreapp3.1 + + + + + + diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs b/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs index ff96b49210c..ba16c64c44c 100644 --- a/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs @@ -2,15 +2,18 @@ // 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.Threading.Tasks; +using System.IO; +using System.Threading; -namespace Microsoft.Diagnostics.Monitoring.ExecuteActionApp +namespace Microsoft.Diagnostics.Monitoring.ExecuteApp { internal class Program { public static int Main(string[] args) { - string testType = args[1]; + File.WriteAllText("C:\\Users\\kkeirstead\\FileOutputs\\Test.txt", args[0]); // Testing purposes only + + string testType = args[0]; const int DelayMs = 3000; // Should be greater than the token delay in the test. switch (testType) @@ -22,7 +25,7 @@ public static int Main(string[] args) return -1; case "TokenCancellation": - Task.Delay(DelayMs); + Thread.Sleep(DelayMs); return 0; default: diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs index b89f1f6dd5b..c819d281588 100644 --- a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs @@ -20,6 +20,9 @@ public sealed class ExecuteActionTests // This should be identical to the error message found in Strings.resx (except without the process's exit code) private const string NonzeroExitCodeMessage = "The process exited with exit code"; + // This should be identical to the error message found in Strings.resx (except without the process's exit code) + private const string TaskCanceledMessage = "A task was canceled"; + [Fact] public async Task ExecuteAction_ZeroExitCode() { @@ -30,7 +33,7 @@ public async Task ExecuteAction_ZeroExitCode() options.Path = DotNetHost.HostExePath; options.Arguments = Assembly.GetExecutingAssembly().Location.Replace( Assembly.GetExecutingAssembly().GetName().Name, - "Microsoft.Diagnostics.Monitoring.ExecuteActionApp") + " ZeroExitCode"; + "Microsoft.Diagnostics.Monitoring.ExecuteApp") + " ZeroExitCode"; using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TokenTimeoutMs); CancellationToken cancellationToken = cancellationTokenSource.Token; @@ -50,7 +53,7 @@ public async Task ExecuteAction_NonzeroExitCode() options.Path = DotNetHost.HostExePath; options.Arguments = Assembly.GetExecutingAssembly().Location.Replace( Assembly.GetExecutingAssembly().GetName().Name, - "Microsoft.Diagnostics.Monitoring.ExecuteActionApp") + " NonzeroExitCode"; + "Microsoft.Diagnostics.Monitoring.ExecuteApp") + " NonzeroExitCode"; using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TokenTimeoutMs); CancellationToken cancellationToken = cancellationTokenSource.Token; @@ -70,14 +73,14 @@ public async Task ExecuteAction_TokenCancellation() options.Path = DotNetHost.HostExePath; options.Arguments = Assembly.GetExecutingAssembly().Location.Replace( Assembly.GetExecutingAssembly().GetName().Name, - "Microsoft.Diagnostics.Monitoring.ExecuteActionApp") + " TokenCancellation"; + "Microsoft.Diagnostics.Monitoring.ExecuteApp") + " TokenCancellation"; using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TokenTimeoutMs); CancellationToken cancellationToken = cancellationTokenSource.Token; - OperationCanceledException invalidOperationException = await Assert.ThrowsAsync( + TaskCanceledException invalidOperationException = await Assert.ThrowsAsync( () => action.ExecuteAsync(options, null, cancellationToken)); - Assert.Contains(NonzeroExitCodeMessage, invalidOperationException.Message); + Assert.Contains(TaskCanceledMessage, invalidOperationException.Message); } } } diff --git a/src/Tools/dotnet-monitor/CollectionRules/Actions/ExecuteAction.cs b/src/Tools/dotnet-monitor/CollectionRules/Actions/ExecuteAction.cs index 06a21355e82..7b94f0f9c15 100644 --- a/src/Tools/dotnet-monitor/CollectionRules/Actions/ExecuteAction.cs +++ b/src/Tools/dotnet-monitor/CollectionRules/Actions/ExecuteAction.cs @@ -38,8 +38,7 @@ public async Task ExecuteAsync(ExecuteOptions option foreach (string arg in args) { process.StartInfo.ArgumentList.Add(arg); - } - */ + }*/ process.EnableRaisingEvents = true; From 1dc9038fa9ff2556a7968533d6043dbb02141fe2 Mon Sep 17 00:00:00 2001 From: kkeirstead Date: Wed, 18 Aug 2021 12:02:48 -0700 Subject: [PATCH 3/7] Added in test that writes to a file and then ensures that the file was actually written to. Also ensures multiple arguments works properly. Still need to do cleanup and potentially add more tests. --- .../Program.cs | 14 ++++++- .../ExecuteActionTests.cs | 42 +++++++++++++++++-- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs b/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs index ba16c64c44c..3fd20be6555 100644 --- a/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs @@ -11,7 +11,7 @@ internal class Program { public static int Main(string[] args) { - File.WriteAllText("C:\\Users\\kkeirstead\\FileOutputs\\Test.txt", args[0]); // Testing purposes only + //File.WriteAllText("C:\\Users\\kkeirstead\\FileOutputs\\Test.txt", args[0]); // Testing purposes only string testType = args[0]; const int DelayMs = 3000; // Should be greater than the token delay in the test. @@ -28,8 +28,18 @@ public static int Main(string[] args) Thread.Sleep(DelayMs); return 0; + case "TextFileOutput": + //File.WriteAllText("C:\\Users\\kkeirstead\\FileOutputs\\Test2.txt", args[1]); // Testing purposes only + //File.WriteAllText("C:\\Users\\kkeirstead\\FileOutputs\\Test3.txt", args[2]); // Testing purposes only + + string textFilePath = args[1]; + string textFileMessage = args[2]; + + File.WriteAllText(textFilePath, textFileMessage); + return 0; + default: - return -100; + return -100; // Arbitrary nonzero exit code } } } diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs index c819d281588..dea8aae1bc3 100644 --- a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs @@ -10,6 +10,7 @@ using Microsoft.Diagnostics.Monitoring.TestCommon; using System.Threading; using System; +using System.IO; namespace Microsoft.Diagnostics.Monitoring.Tool.UnitTests { @@ -20,7 +21,6 @@ public sealed class ExecuteActionTests // This should be identical to the error message found in Strings.resx (except without the process's exit code) private const string NonzeroExitCodeMessage = "The process exited with exit code"; - // This should be identical to the error message found in Strings.resx (except without the process's exit code) private const string TaskCanceledMessage = "A task was canceled"; [Fact] @@ -59,7 +59,7 @@ public async Task ExecuteAction_NonzeroExitCode() CancellationToken cancellationToken = cancellationTokenSource.Token; InvalidOperationException invalidOperationException = await Assert.ThrowsAsync( - () => action.ExecuteAsync(options, null, cancellationToken)); + () => action.ExecuteAsync(options, null, cancellationToken)); Assert.Contains(NonzeroExitCodeMessage, invalidOperationException.Message); } @@ -79,8 +79,44 @@ public async Task ExecuteAction_TokenCancellation() CancellationToken cancellationToken = cancellationTokenSource.Token; TaskCanceledException invalidOperationException = await Assert.ThrowsAsync( - () => action.ExecuteAsync(options, null, cancellationToken)); + () => action.ExecuteAsync(options, null, cancellationToken)); Assert.Contains(TaskCanceledMessage, invalidOperationException.Message); } + + [Fact] + public async Task ExecuteAction_TextFileOutput() + { + ExecuteAction action = new(); + + ExecuteOptions options = new(); + + DirectoryInfo outputDirectory = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "ExecuteAction", Guid.NewGuid().ToString())); + string textFileOutputPath = outputDirectory.FullName; + + textFileOutputPath += "\\file.txt"; + + const string testMessage = "TestMessage"; + + options.Path = DotNetHost.HostExePath; + options.Arguments = Assembly.GetExecutingAssembly().Location.Replace( + Assembly.GetExecutingAssembly().GetName().Name, + "Microsoft.Diagnostics.Monitoring.ExecuteApp") + " TextFileOutput " + textFileOutputPath + " " + testMessage; + + using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TokenTimeoutMs); + CancellationToken cancellationToken = cancellationTokenSource.Token; + + CollectionRuleActionResult result = await action.ExecuteAsync(options, null, cancellationToken); + + Assert.Equal("0", result.OutputValues["ExitCode"]); + Assert.Equal(testMessage, File.ReadAllText(textFileOutputPath)); + + try + { + outputDirectory?.Delete(recursive: true); + } + catch + { + } + } } } From 3512c067c77cbd1054ce20d41b1299c158f04d2d Mon Sep 17 00:00:00 2001 From: kkeirstead Date: Wed, 18 Aug 2021 14:54:57 -0700 Subject: [PATCH 4/7] Cleaning up the tests. --- .../Program.cs | 14 +--- .../ExecuteActionTests.cs | 84 +++++++++++-------- .../CollectionRules/Actions/ExecuteAction.cs | 10 --- ...5b6cf9316c46308d2e3abe6d4df13b.failure.txt | 9 ++ src/Tools/dotnet-monitor/Strings.resx | 4 + 5 files changed, 64 insertions(+), 57 deletions(-) create mode 100644 src/Tools/dotnet-monitor/MSBuild_Logs/MSBuild_pid-20748_0f5b6cf9316c46308d2e3abe6d4df13b.failure.txt diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs b/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs index 3fd20be6555..03b6525c0cf 100644 --- a/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs @@ -11,15 +11,13 @@ internal class Program { public static int Main(string[] args) { - //File.WriteAllText("C:\\Users\\kkeirstead\\FileOutputs\\Test.txt", args[0]); // Testing purposes only - string testType = args[0]; - const int DelayMs = 3000; // Should be greater than the token delay in the test. + const int DelayMs = 3000; // Must be greater than TokenTimeoutMs in ExecuteActionTests. switch (testType) { case "ZeroExitCode": - return 0; ; + return 0; case "NonzeroExitCode": return -1; @@ -29,13 +27,7 @@ public static int Main(string[] args) return 0; case "TextFileOutput": - //File.WriteAllText("C:\\Users\\kkeirstead\\FileOutputs\\Test2.txt", args[1]); // Testing purposes only - //File.WriteAllText("C:\\Users\\kkeirstead\\FileOutputs\\Test3.txt", args[2]); // Testing purposes only - - string textFilePath = args[1]; - string textFileMessage = args[2]; - - File.WriteAllText(textFilePath, textFileMessage); + File.WriteAllText(args[1], args[2]); return 0; default: diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs index dea8aae1bc3..7b50b9433d5 100644 --- a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs @@ -11,15 +11,14 @@ 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 { - private const int TokenTimeoutMs = 2000; // Arbitrarily set - - // This should be identical to the error message found in Strings.resx (except without the process's exit code) - private const string NonzeroExitCodeMessage = "The process exited with exit code"; + private const int TokenTimeoutMs = 2000; // Must be less than DelayMs in the ExecuteApp private const string TaskCanceledMessage = "A task was canceled"; @@ -31,14 +30,9 @@ public async Task ExecuteAction_ZeroExitCode() ExecuteOptions options = new(); options.Path = DotNetHost.HostExePath; - options.Arguments = Assembly.GetExecutingAssembly().Location.Replace( - Assembly.GetExecutingAssembly().GetName().Name, - "Microsoft.Diagnostics.Monitoring.ExecuteApp") + " ZeroExitCode"; + options.Arguments = GenerateArgumentsString(new string[] { "ZeroExitCode" }); - using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TokenTimeoutMs); - CancellationToken cancellationToken = cancellationTokenSource.Token; - - CollectionRuleActionResult result = await action.ExecuteAsync(options, null, cancellationToken); + CollectionRuleActionResult result = await action.ExecuteAsync(options, null, CreateCancellationToken()); Assert.Equal("0", result.OutputValues["ExitCode"]); } @@ -51,16 +45,12 @@ public async Task ExecuteAction_NonzeroExitCode() ExecuteOptions options = new(); options.Path = DotNetHost.HostExePath; - options.Arguments = Assembly.GetExecutingAssembly().Location.Replace( - Assembly.GetExecutingAssembly().GetName().Name, - "Microsoft.Diagnostics.Monitoring.ExecuteApp") + " NonzeroExitCode"; - - using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TokenTimeoutMs); - CancellationToken cancellationToken = cancellationTokenSource.Token; + options.Arguments = GenerateArgumentsString(new string[] { "NonzeroExitCode" }); InvalidOperationException invalidOperationException = await Assert.ThrowsAsync( - () => action.ExecuteAsync(options, null, cancellationToken)); - Assert.Contains(NonzeroExitCodeMessage, invalidOperationException.Message); + () => action.ExecuteAsync(options, null, CreateCancellationToken())); + + Assert.Contains(string.Format(Strings.ErrorMessage_NonzeroExitCode, "-1"), invalidOperationException.Message); } [Fact] @@ -71,15 +61,11 @@ public async Task ExecuteAction_TokenCancellation() ExecuteOptions options = new(); options.Path = DotNetHost.HostExePath; - options.Arguments = Assembly.GetExecutingAssembly().Location.Replace( - Assembly.GetExecutingAssembly().GetName().Name, - "Microsoft.Diagnostics.Monitoring.ExecuteApp") + " TokenCancellation"; - - using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TokenTimeoutMs); - CancellationToken cancellationToken = cancellationTokenSource.Token; + options.Arguments = GenerateArgumentsString(new string[] { "TokenCancellation" }); ; TaskCanceledException invalidOperationException = await Assert.ThrowsAsync( - () => action.ExecuteAsync(options, null, cancellationToken)); + () => action.ExecuteAsync(options, null, CreateCancellationToken())); + Assert.Contains(TaskCanceledMessage, invalidOperationException.Message); } @@ -91,21 +77,14 @@ public async Task ExecuteAction_TextFileOutput() ExecuteOptions options = new(); DirectoryInfo outputDirectory = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "ExecuteAction", Guid.NewGuid().ToString())); - string textFileOutputPath = outputDirectory.FullName; - - textFileOutputPath += "\\file.txt"; + string textFileOutputPath = outputDirectory.FullName + "\\file.txt"; const string testMessage = "TestMessage"; options.Path = DotNetHost.HostExePath; - options.Arguments = Assembly.GetExecutingAssembly().Location.Replace( - Assembly.GetExecutingAssembly().GetName().Name, - "Microsoft.Diagnostics.Monitoring.ExecuteApp") + " TextFileOutput " + textFileOutputPath + " " + testMessage; - - using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TokenTimeoutMs); - CancellationToken cancellationToken = cancellationTokenSource.Token; + options.Arguments = GenerateArgumentsString(new string[] { "TextFileOutput", textFileOutputPath, testMessage }); - CollectionRuleActionResult result = await action.ExecuteAsync(options, null, cancellationToken); + CollectionRuleActionResult result = await action.ExecuteAsync(options, null, CreateCancellationToken()); Assert.Equal("0", result.OutputValues["ExitCode"]); Assert.Equal(testMessage, File.ReadAllText(textFileOutputPath)); @@ -118,5 +97,38 @@ public async Task ExecuteAction_TextFileOutput() { } } + + [Fact] + public async Task ExecuteAction_InvalidPath() + { + ExecuteAction action = new(); + + ExecuteOptions options = new(); + + string uniquePathName = Guid.NewGuid().ToString(); + + options.Path = uniquePathName; + options.Arguments = Assembly.GetExecutingAssembly().Location.Replace( + Assembly.GetExecutingAssembly().GetName().Name, + "Microsoft.Diagnostics.Monitoring.ExecuteApp"); + + FileNotFoundException fileNotFoundException = await Assert.ThrowsAsync( + () => action.ExecuteAsync(options, null, CreateCancellationToken())); + + Assert.Equal(string.Format(Strings.ErrorMessage_FileNotFound, uniquePathName), fileNotFoundException.Message); + } + + private static string GenerateArgumentsString(string[] additionalArgs) + { + return Assembly.GetExecutingAssembly().Location.Replace( + Assembly.GetExecutingAssembly().GetName().Name, + "Microsoft.Diagnostics.Monitoring.ExecuteApp") + ' ' + string.Join(' ', additionalArgs); + } + + private static CancellationToken CreateCancellationToken() + { + CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TokenTimeoutMs); + return cancellationTokenSource.Token; + } } } diff --git a/src/Tools/dotnet-monitor/CollectionRules/Actions/ExecuteAction.cs b/src/Tools/dotnet-monitor/CollectionRules/Actions/ExecuteAction.cs index 7b94f0f9c15..c0cdd4fb19d 100644 --- a/src/Tools/dotnet-monitor/CollectionRules/Actions/ExecuteAction.cs +++ b/src/Tools/dotnet-monitor/CollectionRules/Actions/ExecuteAction.cs @@ -30,16 +30,6 @@ public async Task ExecuteAsync(ExecuteOptions option process.StartInfo = new ProcessStartInfo(path, arguments); - /* - process.StartInfo = new ProcessStartInfo(path); - - string[] args = arguments.Split(); - - foreach (string arg in args) - { - process.StartInfo.ArgumentList.Add(arg); - }*/ - process.EnableRaisingEvents = true; // Completion source that is signaled when the process exits diff --git a/src/Tools/dotnet-monitor/MSBuild_Logs/MSBuild_pid-20748_0f5b6cf9316c46308d2e3abe6d4df13b.failure.txt b/src/Tools/dotnet-monitor/MSBuild_Logs/MSBuild_pid-20748_0f5b6cf9316c46308d2e3abe6d4df13b.failure.txt new file mode 100644 index 00000000000..107f91c9aa9 --- /dev/null +++ b/src/Tools/dotnet-monitor/MSBuild_Logs/MSBuild_pid-20748_0f5b6cf9316c46308d2e3abe6d4df13b.failure.txt @@ -0,0 +1,9 @@ +UNHANDLED EXCEPTIONS FROM PROCESS 20748: +===================== +8/18/2021 2:19:23 PM +System.IO.IOException: Pipe is broken. + at System.IO.Pipes.PipeStream.WriteAsyncCore(ReadOnlyMemory`1 buffer, CancellationToken cancellationToken) in System.IO.Pipes.dll:token 0x60000ec+0x33 + at System.IO.Pipes.PipeStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) in System.IO.Pipes.dll:token 0x60000e1+0x4a + at System.IO.Pipes.PipeStream.Write(Byte[] buffer, Int32 offset, Int32 count) in System.IO.Pipes.dll:token 0x60000df+0x8 + at Microsoft.Build.BackEnd.NodeEndpointOutOfProcBase.RunReadLoop(Stream localReadPipe, Stream localWritePipe, ConcurrentQueue`1 localPacketQueue, AutoResetEvent localPacketAvailable, AutoResetEvent localTerminatePacketPump) in Microsoft.Build.dll:token 0x600107a+0x1b8 +=================== diff --git a/src/Tools/dotnet-monitor/Strings.resx b/src/Tools/dotnet-monitor/Strings.resx index f5a7e952b99..5496192cb22 100644 --- a/src/Tools/dotnet-monitor/Strings.resx +++ b/src/Tools/dotnet-monitor/Strings.resx @@ -243,6 +243,10 @@ Unable to bind any urls. Gets a string similar to "Unable to bind any urls.". + + Unable to start: {0} {1} + {0} = FileName, {1} = Process's Arguments + Unhandled connection mode: {0} Gets the format string for egress provider failure due to missing provider. From 820a580c2289fcfb6e77b21fb38734d44149e6fc Mon Sep 17 00:00:00 2001 From: kkeirstead Date: Wed, 18 Aug 2021 16:45:54 -0700 Subject: [PATCH 5/7] Minor tweaks. Noticed the presence of MSBuild_Logs, which I haven't seen before -> not sure what that means and if it's an issue, but made reference to broken pipes. --- dotnet-monitor.sln | 2 +- ...oft.Diagnostics.Monitoring.ExecuteActionApp.csproj} | 0 .../Program.cs | 2 +- .../ExecuteActionTests.cs | 10 ++++------ .../CollectionRules/Actions/ExecuteAction.cs | 1 - ...-20748_0f5b6cf9316c46308d2e3abe6d4df13b.failure.txt | 9 --------- 6 files changed, 6 insertions(+), 18 deletions(-) rename src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/{Microsoft.Diagnostics.Monitoring.ExecuteApp.csproj => Microsoft.Diagnostics.Monitoring.ExecuteActionApp.csproj} (100%) delete mode 100644 src/Tools/dotnet-monitor/MSBuild_Logs/MSBuild_pid-20748_0f5b6cf9316c46308d2e3abe6d4df13b.failure.txt diff --git a/dotnet-monitor.sln b/dotnet-monitor.sln index 1c165d5e118..2d67f85e35a 100644 --- a/dotnet-monitor.sln +++ b/dotnet-monitor.sln @@ -38,7 +38,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Diagnostics.Monit EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Diagnostics.Monitoring.Tool.UnitTests", "src\Tests\Microsoft.Diagnostics.Monitoring.Tool.UnitTests\Microsoft.Diagnostics.Monitoring.Tool.UnitTests.csproj", "{0DBE362D-82F1-4740-AE6A-40C1A82EDCDB}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Diagnostics.Monitoring.ExecuteApp", "src\Tests\Microsoft.Diagnostics.Monitoring.ExecuteActionApp\Microsoft.Diagnostics.Monitoring.ExecuteApp.csproj", "{A5A0CAAB-C200-44D2-BC93-8445C6E748AD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Diagnostics.Monitoring.ExecuteActionApp", "src\Tests\Microsoft.Diagnostics.Monitoring.ExecuteActionApp\Microsoft.Diagnostics.Monitoring.ExecuteActionApp.csproj", "{A5A0CAAB-C200-44D2-BC93-8445C6E748AD}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Microsoft.Diagnostics.Monitoring.ExecuteApp.csproj b/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Microsoft.Diagnostics.Monitoring.ExecuteActionApp.csproj similarity index 100% rename from src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Microsoft.Diagnostics.Monitoring.ExecuteApp.csproj rename to src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Microsoft.Diagnostics.Monitoring.ExecuteActionApp.csproj diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs b/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs index 03b6525c0cf..e65bd779436 100644 --- a/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs @@ -5,7 +5,7 @@ using System.IO; using System.Threading; -namespace Microsoft.Diagnostics.Monitoring.ExecuteApp +namespace Microsoft.Diagnostics.Monitoring.ExecuteActionApp { internal class Program { diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs index 7b50b9433d5..f05fc3bb9f1 100644 --- a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs @@ -63,10 +63,10 @@ public async Task ExecuteAction_TokenCancellation() options.Path = DotNetHost.HostExePath; options.Arguments = GenerateArgumentsString(new string[] { "TokenCancellation" }); ; - TaskCanceledException invalidOperationException = await Assert.ThrowsAsync( + TaskCanceledException taskCanceledException = await Assert.ThrowsAsync( () => action.ExecuteAsync(options, null, CreateCancellationToken())); - Assert.Contains(TaskCanceledMessage, invalidOperationException.Message); + Assert.Contains(TaskCanceledMessage, taskCanceledException.Message); } [Fact] @@ -108,9 +108,7 @@ public async Task ExecuteAction_InvalidPath() string uniquePathName = Guid.NewGuid().ToString(); options.Path = uniquePathName; - options.Arguments = Assembly.GetExecutingAssembly().Location.Replace( - Assembly.GetExecutingAssembly().GetName().Name, - "Microsoft.Diagnostics.Monitoring.ExecuteApp"); + options.Arguments = GenerateArgumentsString(Array.Empty()); FileNotFoundException fileNotFoundException = await Assert.ThrowsAsync( () => action.ExecuteAsync(options, null, CreateCancellationToken())); @@ -122,7 +120,7 @@ private static string GenerateArgumentsString(string[] additionalArgs) { return Assembly.GetExecutingAssembly().Location.Replace( Assembly.GetExecutingAssembly().GetName().Name, - "Microsoft.Diagnostics.Monitoring.ExecuteApp") + ' ' + string.Join(' ', additionalArgs); + "Microsoft.Diagnostics.Monitoring.ExecuteActionApp") + ' ' + string.Join(' ', additionalArgs); } private static CancellationToken CreateCancellationToken() diff --git a/src/Tools/dotnet-monitor/CollectionRules/Actions/ExecuteAction.cs b/src/Tools/dotnet-monitor/CollectionRules/Actions/ExecuteAction.cs index c0cdd4fb19d..4464b6c5b9d 100644 --- a/src/Tools/dotnet-monitor/CollectionRules/Actions/ExecuteAction.cs +++ b/src/Tools/dotnet-monitor/CollectionRules/Actions/ExecuteAction.cs @@ -29,7 +29,6 @@ public async Task ExecuteAsync(ExecuteOptions option using Process process = new Process(); process.StartInfo = new ProcessStartInfo(path, arguments); - process.EnableRaisingEvents = true; // Completion source that is signaled when the process exits diff --git a/src/Tools/dotnet-monitor/MSBuild_Logs/MSBuild_pid-20748_0f5b6cf9316c46308d2e3abe6d4df13b.failure.txt b/src/Tools/dotnet-monitor/MSBuild_Logs/MSBuild_pid-20748_0f5b6cf9316c46308d2e3abe6d4df13b.failure.txt deleted file mode 100644 index 107f91c9aa9..00000000000 --- a/src/Tools/dotnet-monitor/MSBuild_Logs/MSBuild_pid-20748_0f5b6cf9316c46308d2e3abe6d4df13b.failure.txt +++ /dev/null @@ -1,9 +0,0 @@ -UNHANDLED EXCEPTIONS FROM PROCESS 20748: -===================== -8/18/2021 2:19:23 PM -System.IO.IOException: Pipe is broken. - at System.IO.Pipes.PipeStream.WriteAsyncCore(ReadOnlyMemory`1 buffer, CancellationToken cancellationToken) in System.IO.Pipes.dll:token 0x60000ec+0x33 - at System.IO.Pipes.PipeStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) in System.IO.Pipes.dll:token 0x60000e1+0x4a - at System.IO.Pipes.PipeStream.Write(Byte[] buffer, Int32 offset, Int32 count) in System.IO.Pipes.dll:token 0x60000df+0x8 - at Microsoft.Build.BackEnd.NodeEndpointOutOfProcBase.RunReadLoop(Stream localReadPipe, Stream localWritePipe, ConcurrentQueue`1 localPacketQueue, AutoResetEvent localPacketAvailable, AutoResetEvent localTerminatePacketPump) in Microsoft.Build.dll:token 0x600107a+0x1b8 -=================== From bd42c82d8398b39c1c0a9b692896e1e7e1de14fb Mon Sep 17 00:00:00 2001 From: kkeirstead Date: Thu, 19 Aug 2021 21:14:44 -0700 Subject: [PATCH 6/7] Small tweaks for Justin. --- .../Program.cs | 22 +++++++++---------- .../ExecuteActionTests.cs | 12 +++++----- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs b/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs index c87e04847c4..9902f59ce7f 100644 --- a/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.ExecuteActionApp/Program.cs @@ -16,34 +16,34 @@ public static int Main(string[] args) { string testType = args[0]; - string[] additionalArgs = args.Skip(1).ToArray(); + string[] testArgs = args.Skip(1).ToArray(); switch (testType) { case "ZeroExitCode": - Assert.Equal(1, args.Length); + Assert.Equal(0, testArgs.Length); return 0; case "NonzeroExitCode": - Assert.Equal(1, args.Length); - return -1; + Assert.Equal(0, testArgs.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 + Assert.Equal(1, testArgs.Length); + string delayArg = testArgs[0]; + int delay = int.Parse(delayArg); Thread.Sleep(delay); return 0; case "TextFileOutput": - Assert.Equal(3, args.Length); - string pathArg = additionalArgs[0]; - string contentsArg = additionalArgs[1]; + Assert.Equal(2, testArgs.Length); + string pathArg = testArgs[0]; + string contentsArg = testArgs[1]; File.WriteAllText(pathArg, contentsArg); return 0; default: - throw new ArgumentException("Unknown provided test type."); + throw new ArgumentException($"Unknown test type {testType}."); } } } diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs index 39fedd4e461..7f02b72fd60 100644 --- a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs @@ -19,6 +19,7 @@ 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() @@ -52,7 +53,7 @@ public async Task ExecuteAction_NonzeroExitCode() InvalidOperationException invalidOperationException = await Assert.ThrowsAsync( () => action.ExecuteAsync(options, null, cancellationTokenSource.Token)); - Assert.Contains(string.Format(Strings.ErrorMessage_NonzeroExitCode, "-1"), invalidOperationException.Message); + Assert.Contains(string.Format(Strings.ErrorMessage_NonzeroExitCode, "1"), invalidOperationException.Message); } [Fact] @@ -63,7 +64,7 @@ public async Task ExecuteAction_TokenCancellation() ExecuteOptions options = new(); options.Path = DotNetHost.HostExePath; - options.Arguments = GenerateArgumentsString(new string[] { "Sleep", TokenTimeoutMs.ToString() }); ; + options.Arguments = GenerateArgumentsString(new string[] { "Sleep", (TokenTimeoutMs + DelayMs).ToString() }); ; using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TokenTimeoutMs); @@ -145,16 +146,15 @@ public async Task ExecuteAction_IgnoreExitCode() CollectionRuleActionResult result = await action.ExecuteAsync(options, null, cancellationTokenSource.Token); - ValidateActionResult(result, "-1"); + ValidateActionResult(result, "1"); } private static string GenerateArgumentsString(string[] additionalArgs) { Assembly currAssembly = Assembly.GetExecutingAssembly(); - return AssemblyHelper.GetAssemblyArtifactBinPath(currAssembly, currAssembly.GetName().Name, TargetFrameworkMoniker.NetCoreApp31).Replace( - currAssembly.GetName().Name, - "Microsoft.Diagnostics.Monitoring.ExecuteActionApp") + ' ' + string.Join(' ', additionalArgs); + return AssemblyHelper.GetAssemblyArtifactBinPath(currAssembly, "Microsoft.Diagnostics.Monitoring.ExecuteActionApp", TargetFrameworkMoniker.NetCoreApp31) + + ' ' + string.Join(' ', additionalArgs); } private static void ValidateActionResult(CollectionRuleActionResult result, string expectedExitCode) From 21ad5644321142530ff118f46bdd8f13a9ee5960 Mon Sep 17 00:00:00 2001 From: kkeirstead Date: Fri, 20 Aug 2021 09:12:22 -0700 Subject: [PATCH 7/7] Small tweak for Justin. --- .../ExecuteActionTests.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs index 7f02b72fd60..6a0a6ad4b5a 100644 --- a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExecuteActionTests.cs @@ -13,6 +13,7 @@ using System.IO; using System.Diagnostics; using Microsoft.Diagnostics.Tools.Monitor; +using System.Collections.Generic; namespace Microsoft.Diagnostics.Monitoring.Tool.UnitTests { @@ -153,8 +154,15 @@ private static string GenerateArgumentsString(string[] additionalArgs) { Assembly currAssembly = Assembly.GetExecutingAssembly(); - return AssemblyHelper.GetAssemblyArtifactBinPath(currAssembly, "Microsoft.Diagnostics.Monitoring.ExecuteActionApp", TargetFrameworkMoniker.NetCoreApp31) - + ' ' + string.Join(' ', additionalArgs); + List 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)