@@ -18,9 +18,9 @@ namespace VirtualClient.Actions
1818
1919 [ TestFixture ]
2020 [ Category ( "Unit" ) ]
21- public class PowershellExecutorTests
21+ public class PowerShellExecutorTests
2222 {
23- private static readonly string ExamplesDirectory = MockFixture . GetDirectory ( typeof ( PowershellExecutorTests ) , "Examples" , "ScriptExecutor" ) ;
23+ private static readonly string ExamplesDirectory = MockFixture . GetDirectory ( typeof ( PowerShellExecutorTests ) , "Examples" , "ScriptExecutor" ) ;
2424
2525 private MockFixture fixture ;
2626 private DependencyPath mockPackage ;
@@ -35,7 +35,7 @@ public void SetupTest(PlatformID platform = PlatformID.Win32NT)
3535
3636 this . fixture . Dependencies . RemoveAll < IEnumerable < IBlobManager > > ( ) ;
3737
38- this . exampleResults = File . ReadAllText ( Path . Combine ( PowershellExecutorTests . ExamplesDirectory , "validJsonExample.json" ) ) ;
38+ this . exampleResults = File . ReadAllText ( Path . Combine ( PowerShellExecutorTests . ExamplesDirectory , "validJsonExample.json" ) ) ;
3939
4040 this . fixture . FileSystem . Setup ( fe => fe . File . Exists ( It . IsAny < string > ( ) ) )
4141 . Returns ( true ) ;
@@ -127,46 +127,92 @@ public void SetupTest(PlatformID platform = PlatformID.Win32NT)
127127
128128 this . fixture . Parameters = new Dictionary < string , IConvertible > ( )
129129 {
130- { nameof ( PowershellExecutor . PackageName ) , "workloadPackage" } ,
131- { nameof ( PowershellExecutor . Scenario ) , "GenericScriptWorkload" } ,
132- { nameof ( PowershellExecutor . CommandLine ) , "parameter1 parameter2" } ,
133- { nameof ( PowershellExecutor . ScriptPath ) , "genericScript.ps1" } ,
134- { nameof ( PowershellExecutor . LogPaths ) , "*.log;*.txt;*.json" } ,
135- { nameof ( PowershellExecutor . ToolName ) , "GenericTool" } ,
136- { nameof ( PowershellExecutor . UsePwsh ) , false }
130+ { nameof ( PowerShellExecutor . PackageName ) , "workloadPackage" } ,
131+ { nameof ( PowerShellExecutor . Scenario ) , "GenericScriptWorkload" } ,
132+ { nameof ( PowerShellExecutor . CommandLine ) , "parameter1 parameter2" } ,
133+ { nameof ( PowerShellExecutor . ScriptPath ) , "genericScript.ps1" } ,
134+ { nameof ( PowerShellExecutor . LogPaths ) , "*.log;*.txt;*.json" } ,
135+ { nameof ( PowerShellExecutor . ToolName ) , "GenericTool" }
137136 } ;
138137
139138 this . fixture . ProcessManager . OnCreateProcess = ( command , arguments , directory ) => this . fixture . Process ;
140139 }
141140
142141 [ Test ]
143- [ TestCase ( PlatformID . Win32NT , @"\win-x64" , @"genericScript.ps1" , true , false , "powershell" ) ]
144- [ TestCase ( PlatformID . Win32NT , @"\win-x64" , @"genericScript.ps1" , false , false , "powershell" ) ]
145- [ TestCase ( PlatformID . Win32NT , @"\win-x64" , @"genericScript.ps1" , true , true , "pwsh" ) ]
146- [ TestCase ( PlatformID . Win32NT , @"\win-x64" , @"genericScript.ps1" , false , true , "pwsh" ) ]
142+ [ TestCase ( @"genericScript.ps1" , "powershell" ) ]
143+ [ TestCase ( @"genericScript.ps1" , "powershell" ) ]
144+ [ TestCase ( @"genericScript.ps1" , "powershell" ) ]
145+ [ TestCase ( @"genericScript.ps1" , "powershell.exe" ) ]
146+ [ TestCase ( @"genericScript.ps1" , "PowerShell.exe" ) ]
147+ [ TestCase ( @"genericScript.ps1" , @"C:\Any\Custom\Location\powershell.exe" ) ]
148+ [ TestCase ( @"genericScript.ps1" , "pwsh" ) ]
149+ [ TestCase ( @"genericScript.ps1" , "pwsh.exe" ) ]
150+ [ TestCase ( @"genericScript.ps1" , @"C:\Any\Custom\Location\pwsh.exe" ) ]
147151 [ Platform ( Exclude = "Unix,Linux,MacOsX" ) ]
148- public async Task PowershellExecutorExecutesTheCorrectWorkloadCommands ( PlatformID platform , string platformSpecificPath , string command , bool runElevated , bool usePwsh , string executorType )
152+ public async Task PowershellExecutorExecutesTheCorrectWorkloadCommands_Windows_Scenarios ( string command , string executable )
149153 {
150- this . SetupTest ( platform ) ;
151- this . fixture . Parameters [ nameof ( PowershellExecutor . RunElevated ) ] = runElevated ;
152- this . fixture . Parameters [ nameof ( PowershellExecutor . ScriptPath ) ] = command ;
153- this . fixture . Parameters [ nameof ( PowershellExecutor . UsePwsh ) ] = usePwsh ;
154+ this . SetupTest ( PlatformID . Win32NT ) ;
155+ this . fixture . Parameters [ nameof ( PowerShellExecutor . ScriptPath ) ] = command ;
156+ this . fixture . Parameters [ nameof ( PowerShellExecutor . Executable ) ] = executable ;
154157
155- string fullCommand = $ "{ this . mockPackage . Path } { platformSpecificPath } \\ { command } parameter1 parameter2";
158+ string fullCommand = $ "{ this . mockPackage . Path } \\ win-x64 \\ { command } parameter1 parameter2";
156159
157- using ( TestPowershellExecutor executor = new TestPowershellExecutor ( this . fixture ) )
160+ using ( TestPowerShellExecutor executor = new TestPowerShellExecutor ( this . fixture ) )
158161 {
159162 bool commandExecuted = false ;
163+ await executor . InitializeAsync ( EventContext . None , CancellationToken . None ) ;
164+ string workingDirectory = executor . ExecutableDirectory ;
165+
166+ string expectedCommand = $ "{ executable } -ExecutionPolicy Bypass -NoProfile -NonInteractive -WindowStyle Hidden -Command \" cd '{ workingDirectory } ';{ fullCommand } \" ";
167+ this . fixture . ProcessManager . OnCreateProcess = ( exe , arguments , workingDirectory ) =>
168+ {
169+ if ( expectedCommand == $ "{ exe } { arguments } ")
170+ {
171+ commandExecuted = true ;
172+ }
173+
174+ return new InMemoryProcess
175+ {
176+ StartInfo = new ProcessStartInfo
177+ {
178+ FileName = exe ,
179+ Arguments = arguments
180+ } ,
181+ ExitCode = 0 ,
182+ OnStart = ( ) => true ,
183+ OnHasExited = ( ) => true
184+ } ;
185+ } ;
160186
161- await executor . InitializeAsync ( EventContext . None , CancellationToken . None )
162- . ConfigureAwait ( false ) ;
187+ await executor . ExecuteAsync ( CancellationToken . None ) ;
163188
189+ Assert . DoesNotThrowAsync ( ( ) => executor . ExecuteAsync ( CancellationToken . None ) ) ;
190+ Assert . IsTrue ( commandExecuted ) ;
191+ }
192+ }
193+
194+ [ Test ]
195+ [ TestCase ( "genericScript.ps1" , "pwsh" ) ]
196+ [ TestCase ( "genericScript.ps1" , @"/home/any/custom/location/pwsh" ) ]
197+ [ TestCase ( "genericScript.ps1" , "sudo pwsh" ) ]
198+ public async Task PowershellExecutorExecutesTheCorrectWorkloadCommands_Unix_Scenarios ( string command , string executable )
199+ {
200+ this . SetupTest ( PlatformID . Unix ) ;
201+ this . fixture . Parameters [ nameof ( PowerShellExecutor . ScriptPath ) ] = command ;
202+ this . fixture . Parameters [ nameof ( PowerShellExecutor . Executable ) ] = executable ;
203+
204+ string fullCommand = $ "{ this . mockPackage . Path } /linux-x64/{ command } parameter1 parameter2";
205+
206+ using ( TestPowerShellExecutor executor = new TestPowerShellExecutor ( this . fixture ) )
207+ {
208+ bool commandExecuted = false ;
209+ await executor . InitializeAsync ( EventContext . None , CancellationToken . None ) ;
164210 string workingDirectory = executor . ExecutableDirectory ;
165211
166- string expectedCommand = $ "{ executorType } -ExecutionPolicy Bypass -NoProfile -NonInteractive -WindowStyle Hidden -Command \" cd '{ workingDirectory } ';{ fullCommand } \" ";
212+ string expectedCommand = $ "{ executable } -ExecutionPolicy Bypass -NoProfile -NonInteractive -WindowStyle Hidden -Command \" cd '{ workingDirectory } ';{ fullCommand } \" ";
167213 this . fixture . ProcessManager . OnCreateProcess = ( exe , arguments , workingDirectory ) =>
168214 {
169- if ( expectedCommand == $ "{ exe } { arguments } ")
215+ if ( expectedCommand == $ "{ exe } { arguments } ")
170216 {
171217 commandExecuted = true ;
172218 }
@@ -184,37 +230,51 @@ await executor.InitializeAsync(EventContext.None, CancellationToken.None)
184230 } ;
185231 } ;
186232
187- await executor . ExecuteAsync ( CancellationToken . None )
188- . ConfigureAwait ( false ) ;
233+ await executor . ExecuteAsync ( CancellationToken . None ) ;
189234
190235 Assert . DoesNotThrowAsync ( ( ) => executor . ExecuteAsync ( CancellationToken . None ) ) ;
191236 Assert . IsTrue ( commandExecuted ) ;
192237 }
193238 }
194239
195240 [ Test ]
196- [ TestCase ( PlatformID . Win32NT , @"\win-x64\" ) ]
197- public void PowershellExecutorDoesNotThrowWhenTheWorkloadDoesNotProduceValidMetricsFile ( PlatformID platform , string platformSpecificPath )
241+ public void PowershellExecutorDoesNotThrowWhenTheWorkloadDoesNotProduceValidMetricsFile ( )
198242 {
199- this . SetupTest ( platform ) ;
200- this . fixture . File . Setup ( fe => fe . Exists ( $ "{ this . mockPackage . Path } { platformSpecificPath } test-metrics.json") )
243+ this . SetupTest ( PlatformID . Win32NT ) ;
244+ this . fixture . File . Setup ( fe => fe . Exists ( $@ "{ this . mockPackage . Path } \win-x64\ test-metrics.json") )
201245 . Returns ( false ) ;
202246
203- using ( TestPowershellExecutor executor = new TestPowershellExecutor ( this . fixture ) )
247+ using ( TestPowerShellExecutor executor = new TestPowerShellExecutor ( this . fixture ) )
204248 {
205249 this . fixture . ProcessManager . OnCreateProcess = ( command , arguments , directory ) => this . fixture . Process ;
206250
207251 Assert . DoesNotThrowAsync ( ( ) => executor . ExecuteAsync ( CancellationToken . None ) ) ;
208252 }
209253 }
210254
211- private class TestPowershellExecutor : PowershellExecutor
255+ private class TestPowerShellExecutor : PowerShellExecutor
212256 {
213- public TestPowershellExecutor ( MockFixture fixture )
257+ public TestPowerShellExecutor ( MockFixture fixture )
214258 : base ( fixture . Dependencies , fixture . Parameters )
215259 {
216260 }
217261
262+ public new string ExecutablePath
263+ {
264+ get
265+ {
266+ return base . ExecutablePath ;
267+ }
268+ }
269+
270+ public new string ExecutableDirectory
271+ {
272+ get
273+ {
274+ return base . ExecutableDirectory ;
275+ }
276+ }
277+
218278 public new Task InitializeAsync ( EventContext telemetryContext , CancellationToken cancellationToken )
219279 {
220280 return base . InitializeAsync ( telemetryContext , cancellationToken ) ;
0 commit comments