Skip to content

Commit 4dfdd03

Browse files
author
Kyle Zhang
committed
Merged PR 5297230: Add shell adapter to ptf
Related work items: #28834937
1 parent 734ba95 commit 4dfdd03

17 files changed

+878
-14
lines changed

docs/PTFUserGuide.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ The configuration allows an adapter to be created in various ways:
143143

144144
PowerShell adapter maps interface methods to PowerShell scripts. Users are able to pass objects between their managed test code and PowerShell scripts directly. This avoids the parameter and return value parsing issue.
145145

146+
* Shell adapter
147+
148+
The Shell adapter, which leverages Windows Subsystem for Linux (WSL), allows users to map interface methods to shell scripts and use the scripts to perform operations during the test. Users must specify the location of the scripts and have a .sh script with a name corresponding to each API call in the test suit. When the API is called, the script will be invoked and its stdout and stderr are recorded in the test log as LogEntryType.Comment.
149+
146150
* Managed adapter
147151

148152
The managed adapter allows users to use managed code to implement the interface methods.
@@ -212,6 +216,81 @@ public void TestPowerShellAdapter()
212216
}
213217
```
214218

219+
### Shell Adapter
220+
221+
Shell Adapter leverages bash in Windows Subsystem for Linux (WSL) to run shell scripts. Users can bind their adapter interfaces to a shell adapter by defining them as "shell" in the PTF configuration files. The implementation will run the corresponding shell script file when one of the adapter's methods is called.
222+
223+
__Benefits__
224+
225+
Shell adapters are easy to use. For example, they are suitable for service setup/shutdown jobs. Users can write a Stop.sh to login a Linux machine via SSH and stop a service.
226+
227+
__Limitation__
228+
229+
1. WSL must be running on Windows 10 version 1607 (the Anniversary update) or later.
230+
2. Do not encode the shell script using UTF-8 with Byte Order Mark.
231+
232+
__Usage__
233+
234+
Configure `<Adapters>` section of the ptfconfig file:
235+
236+
```
237+
<Adapter xsi:type="shell" name="IMyScriptAdapter" scriptdir=".\" />
238+
```
239+
240+
Subsequently, users can invoke `IMyScriptAdapter.AMethod(parameters)`. PTF will look up a script named `AMethod.sh` in the scriptdir directory and execute it with the parameters.
241+
242+
__Parameters__
243+
244+
PTF invokes a shell script using the following parameters:
245+
246+
__Parameter name__ | __Value__
247+
-------------------|----------
248+
$1 |First input parameter.
249+
$2 |Second input parameter.
250+
... |
251+
252+
__Return values__
253+
254+
To pass a return value back to PTF, print the return value at the last line of stdout.
255+
256+
To pass an exception message back to PTF, print the message to stderr and exit the script with a non-zero exit code.
257+
258+
```
259+
echo "<failure message>" >&2
260+
exit 1
261+
```
262+
263+
Provide the `ToString()` method and the `Parse()` method in a custom type to pass custom type values as parameters to script adapter, e.g.:
264+
265+
```
266+
static public String ToString()
267+
static public CustomType.Parse(String str)
268+
```
269+
270+
__Exception__
271+
272+
If the shell script execution fails, PTF will raise an `InvalidOperationException`.
273+
274+
If the shell script file is not found, PTF will raise an `AssertInconclusiveException`.
275+
276+
__PTF Properties__
277+
278+
PTF properties are provided to shell adapters by environment variables with PTFProp_ as prefix, and dot in property name replaced by underline(_).
279+
For example, if a user has the following configuration in the PTFconfig
280+
281+
```
282+
<Properties>
283+
<Property name="ServerName" value="MS-Server" />
284+
<Property name="SSH.Port" value="4242" />
285+
</Properties>
286+
```
287+
288+
To initiate an SSH connection to the server. You can use the following command in shell script.
289+
290+
```
291+
ssh -p $PTFProp_SSH_Port $PTFProp_ServerName
292+
```
293+
215294
## <a name="4.4"> Extensive Logging Support
216295

217296
PTF provides problem-oriented logging capabilities (begin/end test group, verification pass/failure, requirement capture, debugging). Most of the logging is done automatically so that the test suite developer does not have to write them manually.

src/TestFramework/Core/Adapters/ManagedAdapterBase.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@ public static T Wrap<T>(Type adapterType, Type typeToProxy) where T : IAdapter
209209
/// This method can be overridden by extenders to do special initialization code.
210210
/// It calls base class's Initialize method to ensure the test site is initialized.
211211
/// </summary>
212-
/// <param name="targetMethod"></param>
213-
/// <param name="args"></param>
214-
/// <returns></returns>
212+
/// <param name="targetMethod">The method the caller invoked.</param>
213+
/// <param name="args">The arguments the caller passed to the method.</param>
214+
/// <returns>The return value of the Initialize implementation.</returns>
215215
protected override object Initialize(MethodInfo targetMethod, object[] args)
216216
{
217217
base.Initialize(targetMethod, args);
@@ -244,8 +244,8 @@ protected override object Initialize(MethodInfo targetMethod, object[] args)
244244
/// <summary>
245245
/// Can be overridden by extenders to do special processing of TestSite getter.
246246
/// </summary>
247-
/// <param name="targetMethod"></param>
248-
/// <returns></returns>
247+
/// <param name="targetMethod">The method the caller invoked.</param>
248+
/// <returns>The return value of the GetSite implementation.</returns>
249249
protected override object GetSite(MethodInfo targetMethod)
250250
{
251251
return base.GetSite(targetMethod);
@@ -254,8 +254,8 @@ protected override object GetSite(MethodInfo targetMethod)
254254
/// <summary>
255255
/// Can be overridden by extenders to do special processing of Reset.
256256
/// </summary>
257-
/// <param name="targetMethod"></param>
258-
/// <returns></returns>
257+
/// <param name="targetMethod">The method the caller invoked.</param>
258+
/// <returns>The return value of the Reset implementation.</returns>
259259
protected override object Reset(MethodInfo targetMethod)
260260
{
261261
TestSite.Log.Add(LogEntryKind.EnterAdapter,
@@ -284,8 +284,8 @@ protected override object Reset(MethodInfo targetMethod)
284284
/// <summary>
285285
/// Can be overridden by extenders to do special processing of Dispose.
286286
/// </summary>
287-
/// <param name="targetMethod"></param>
288-
/// <returns></returns>
287+
/// <param name="targetMethod">The method the caller invoked.</param>
288+
/// <returns>The return value of the Dispose implementation.</returns>
289289
protected override object Dispose(MethodInfo targetMethod)
290290
{
291291
try

src/TestFramework/Core/Adapters/PowerShellAdapterProxy.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public static T Wrap<T>(string scriptDirectory, Type typeToProxy) where T : IAda
4242
/// Can be overridden by extenders to do special initialization code.
4343
/// Call base to ensure the test site is initialized.
4444
/// </summary>
45-
/// <param name="targetMethod"></param>
46-
/// <param name="args"></param>
47-
/// <returns></returns>
45+
/// <param name="targetMethod">The method the caller invoked.</param>
46+
/// <param name="args">The arguments the caller passed to the method.</param>
47+
/// <returns>The return value of the Initialize implementation.</returns>
4848
protected override object Initialize(MethodInfo targetMethod, object[] args)
4949
{
5050
base.Initialize(targetMethod, args);
@@ -54,8 +54,8 @@ protected override object Initialize(MethodInfo targetMethod, object[] args)
5454
/// <summary>
5555
/// Can be overridden by extenders to do special processing of Reset.
5656
/// </summary>
57-
/// <param name="targetMethod"></param>
58-
/// <returns></returns>
57+
/// <param name="targetMethod">The method the caller invoked.</param>
58+
/// <returns>The return value of the Reset implementation.</returns>
5959
protected override object Reset(MethodInfo targetMethod)
6060
{
6161
return this.ExecuteMethod(targetMethod, null);

0 commit comments

Comments
 (0)