Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,25 @@ public void SetupTest(PlatformID platform = PlatformID.Win32NT)
{ nameof(PowershellExecutor.CommandLine), "parameter1 parameter2" },
{ nameof(PowershellExecutor.ScriptPath), "genericScript.ps1" },
{ nameof(PowershellExecutor.LogPaths), "*.log;*.txt;*.json" },
{ nameof(PowershellExecutor.ToolName), "GenericTool" }
{ nameof(PowershellExecutor.ToolName), "GenericTool" },
{ nameof(PowershellExecutor.UsePwsh), false }
};

this.fixture.ProcessManager.OnCreateProcess = (command, arguments, directory) => this.fixture.Process;
}

[Test]
[TestCase(PlatformID.Win32NT, @"\win-x64", @"genericScript.ps1", true)]
[TestCase(PlatformID.Win32NT, @"\win-x64", @"genericScript.ps1", false)]
[TestCase(PlatformID.Win32NT, @"\win-x64", @"genericScript.ps1", true, false, "powershell")]
[TestCase(PlatformID.Win32NT, @"\win-x64", @"genericScript.ps1", false, false, "powershell")]
[TestCase(PlatformID.Win32NT, @"\win-x64", @"genericScript.ps1", true, true, "pwsh")]
[TestCase(PlatformID.Win32NT, @"\win-x64", @"genericScript.ps1", false, true, "pwsh")]
[Platform(Exclude = "Unix,Linux,MacOsX")]
public async Task PowershellExecutorExecutesTheCorrectWorkloadCommands(PlatformID platform, string platformSpecificPath, string command, bool runElevated)
public async Task PowershellExecutorExecutesTheCorrectWorkloadCommands(PlatformID platform, string platformSpecificPath, string command, bool runElevated, bool usePwsh, string executorType)
{
this.SetupTest(platform);
this.fixture.Parameters[nameof(PowershellExecutor.RunElevated)] = runElevated;
this.fixture.Parameters[nameof(PowershellExecutor.ScriptPath)] = command;
this.fixture.Parameters[nameof(PowershellExecutor.UsePwsh)] = usePwsh;

string fullCommand = $"{this.mockPackage.Path}{platformSpecificPath}\\{command} parameter1 parameter2";

Expand All @@ -159,7 +163,7 @@ await executor.InitializeAsync(EventContext.None, CancellationToken.None)

string workingDirectory = executor.ExecutableDirectory;

string expectedCommand = $"powershell -ExecutionPolicy Bypass -NoProfile -NonInteractive -WindowStyle Hidden -Command \"cd '{workingDirectory}';{fullCommand}\"";
string expectedCommand = $"{executorType} -ExecutionPolicy Bypass -NoProfile -NonInteractive -WindowStyle Hidden -Command \"cd '{workingDirectory}';{fullCommand}\"";
this.fixture.ProcessManager.OnCreateProcess = (exe, arguments, workingDirectory) =>
{
if(expectedCommand == $"{exe} {arguments}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace VirtualClient.Actions
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using VirtualClient.Common;
using VirtualClient.Common.Extensions;
using VirtualClient.Common.Telemetry;

/// <summary>
Expand All @@ -26,14 +27,25 @@ public PowershellExecutor(IServiceCollection dependencies, IDictionary<string, I
{
}

/// <summary>
/// The parameter specifies whether to use pwsh, by default it is false
/// </summary>
public bool UsePwsh
{
get
{
return this.Parameters.GetValue<bool>(nameof(this.UsePwsh), false);
}
}

/// <summary>
/// Executes the PowerShell script.
/// </summary>
protected override async Task ExecuteAsync(EventContext telemetryContext, CancellationToken cancellationToken)
{
using (BackgroundOperations profiling = BackgroundOperations.BeginProfiling(this, cancellationToken))
{
string command = "powershell";
string command = this.UsePwsh ? "pwsh" : "powershell";
string commandArguments = SensitiveData.ObscureSecrets(
$"-ExecutionPolicy Bypass -NoProfile -NonInteractive -WindowStyle Hidden -Command \"cd '{this.ExecutableDirectory}';{this.ExecutablePath} {this.CommandLine}\"");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@

namespace VirtualClient.Contracts
{
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Moq;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Moq;
using NUnit.Framework;
using VirtualClient.Common.Contracts;
using VirtualClient.Common.Extensions;
using VirtualClient.Common.Telemetry;
Expand Down Expand Up @@ -252,7 +253,7 @@ public void CombineExtensionProducesTheExpectedPathOnUnixSystems()
}

[Test]
public void CreateCreateFileUploadDescriptorsExtensionCreatesTheExpectedDescriptorsOnUnixSystems_1()
public void CreateFileUploadDescriptorsExtensionCreatesTheExpectedDescriptorsOnUnixSystems_1()
{
this.fixture.Setup(PlatformID.Unix);

Expand Down Expand Up @@ -297,7 +298,7 @@ public void CreateCreateFileUploadDescriptorsExtensionCreatesTheExpectedDescript
}

[Test]
public void CreateCreateFileUploadDescriptorsExtensionCreatesTheExpectedDescriptorsOnUnixSystems_2()
public void CreateFileUploadDescriptorsExtensionCreatesTheExpectedDescriptorsOnUnixSystems_2()
{
this.fixture.Setup(PlatformID.Unix);

Expand Down Expand Up @@ -351,7 +352,8 @@ public void CreateCreateFileUploadDescriptorsExtensionCreatesTheExpectedDescript
}

[Test]
public void CreateCreateFileUploadDescriptorsExtensionCreatesTheExpectedDescriptorsOnWindowsSystems_1()
[Platform(Include = "Win")]
public void CreateFileUploadDescriptorsExtensionCreatesTheExpectedDescriptorsOnWindowsSystems_1()
{
string directory = "C:\\Users\\User\\Logs";
string[] expectedFiles = new string[]
Expand Down Expand Up @@ -394,7 +396,8 @@ public void CreateCreateFileUploadDescriptorsExtensionCreatesTheExpectedDescript
}

[Test]
public void CreateCreateFileUploadDescriptorsExtensionCreatesTheExpectedDescriptorsOnWindowsSystems_2()
[Platform(Include = "Win")]
public void CreateFileUploadDescriptorsExtensionCreatesTheExpectedDescriptorsOnWindowsSystems_2()
{
string directory = "C:\\Users\\User\\Logs";
string[] expectedFiles = new string[]
Expand All @@ -406,7 +409,7 @@ public void CreateCreateFileUploadDescriptorsExtensionCreatesTheExpectedDescript

this.fixture.FileSystem
.Setup(fs => fs.Path.GetDirectoryName(It.IsAny<string>()))
.Returns<string>(file => file.Replace(Path.GetFileName(file), string.Empty));
.Returns<string>(file => Path.GetDirectoryName(file));

this.fixture.FileSystem
.Setup(fs => fs.Directory.GetFiles(directory, "*.*", SearchOption.AllDirectories))
Expand Down
1 change: 1 addition & 0 deletions website/docs/guides/0221-usage-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ This component can be used to execute generic scripts using facilities common to
| PackageName | Name of the workload package built for running the script. If the workload package is being downloaded from blob package store, this needs to match with the package name defined in DependencyPackageInstallation. | String |
| FailFast | Flag indicates that the application should exit immediately on first/any errors regardless of their severity. | Boolean |
| UsePython3 | (Only valid for PythonExecutor) A true value indicates use of "python3" as environment variable to execute python, a false value will use "python" as the environment variable. | Boolean (Default is true) |
| UsePwsh | (Only valid for PowershellExecutor) A true value indicates use of "pwsh", a false value will use "powershell" as the command executor. | Boolean (Default is false) |

``` json
Example:
Expand Down
Loading