Skip to content

Commit

Permalink
Convert adapters to Python (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
evacchi authored Feb 13, 2023
1 parent e5bcece commit dc7f8d2
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 98 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/daily-runtime-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
continue-on-error: true
run: |
python3 test-runner/wasi_test_runner.py \
-r ./adapters/${{ matrix.runtime }}.sh \
-r ./adapters/${{ matrix.runtime }}.py \
--json-output-location results.json \
-t tests/assemblyscript/testsuite \
-t tests/c/testsuite
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ python3 -m pip install -r test-runner/requirements.txt
python3 test-runner/wasi_test_runner.py \
-t ./tests/assemblyscript/testsuite/ `# path to folders containing .wasm test files` \
./tests/c/testsuite/ \
-r adapters/wasmtime.sh # path to a runtime adapter
-r adapters/wasmtime.py # path to a runtime adapter
```

Optionally you can specify test cases to skip with the `--exclude-filter` option.
Expand All @@ -45,7 +45,7 @@ python3 test-runner/wasi_test_runner.py
-t ./tests/assemblyscript/testsuite/ `# path to folders containing .wasm test files` \
./tests/c/testsuite/ \
--exclude-filter examples/skip.json \
-r adapters/wasmtime.sh # path to a runtime adapter
-r adapters/wasmtime.py # path to a runtime adapter
```

The default executable in the adapter used for test execution can be
Expand All @@ -55,7 +55,7 @@ overridden using `TEST_RUNTIME_EXE` variable. This only works with adapters defi
```bash
TEST_RUNTIME_EXE="wasmtime --wasm-features all" python3 test-runner/wasi_test_runner.py \
-t ./tests/assemblyscript/testsuite/ \
-r adapters/wasmtime.sh
-r adapters/wasmtime.py
```

## Contributing
Expand Down
29 changes: 29 additions & 0 deletions adapters/wasm-micro-runtime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import argparse
import subprocess
import sys
import os
import shlex

# shlex.split() splits according to shell quoting rules
IWASM = shlex.split(os.getenv("TEST_RUNTIME_EXE", "iwasm"))

parser = argparse.ArgumentParser()
parser.add_argument("--version", action="store_true")
parser.add_argument("--test-file", action="store")
parser.add_argument("--arg", action="append", default=[])
parser.add_argument("--env", action="append", default=[])
parser.add_argument("--dir", action="append", default=[])

args = parser.parse_args()

if args.version:
subprocess.run(IWASM + ["--version"])
sys.exit(0)

TEST_FILE = args.test_file
PROG_ARGS = args.arg
ENV_ARGS = [f"--env={i}" for i in args.env]
DIR_ARGS = [f"--dir={i}" for i in args.dir]

r = subprocess.run(IWASM + ENV_ARGS + DIR_ARGS + [TEST_FILE] + PROG_ARGS)
sys.exit(r.returncode)
47 changes: 0 additions & 47 deletions adapters/wasm-micro-runtime.sh

This file was deleted.

30 changes: 30 additions & 0 deletions adapters/wasmtime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import argparse
import subprocess
import sys
import os
import shlex

# shlex.split() splits according to shell quoting rules
WASMTIME = shlex.split(os.getenv("TEST_RUNTIME_EXE", "wasmtime"))

parser = argparse.ArgumentParser()
parser.add_argument("--version", action="store_true")
parser.add_argument("--test-file", action="store")
parser.add_argument("--arg", action="append", default=[])
parser.add_argument("--env", action="append", default=[])
parser.add_argument("--dir", action="append", default=[])

args = parser.parse_args()

if args.version:
# ensure no args when version is queried
subprocess.run(WASMTIME[0:1] + ["--version"])
sys.exit(0)

TEST_FILE = args.test_file
PROG_ARGS = args.arg
ENV_ARGS = [j for i in args.env for j in ["--env", i]]
DIR_ARGS = [j for i in args.dir for j in ["--dir", i]]

r = subprocess.run(WASMTIME + ENV_ARGS + DIR_ARGS + [TEST_FILE] + PROG_ARGS)
sys.exit(r.returncode)
42 changes: 0 additions & 42 deletions adapters/wasmtime.sh

This file was deleted.

6 changes: 3 additions & 3 deletions doc/adapters.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The test runner is designed to support different types of WebAssembly runtimes.

WASI test runner is designed to support as many WASM runtimes as possible, therefore runtime peculiarities aren't hardcoded in the runner.

In order to integrate WASM runtime with a test runner, the user has to provide a `runtime adapter`. It's an executable file that takes command-line arguments and translates them to a runtime call.
In order to integrate WASM runtime with a test runner, the user has to provide a `runtime adapter`. It's a *Python script* that takes command-line arguments and translates them to a runtime call. The reason for using a Python script over a generic "she-bang" executable, is to ensure cross-platform compatibility, such an executable is expected to be a python a script.

## Interface
The adapter executable must accept the following command line parameters and execute actions associated with those parameters:
Expand Down Expand Up @@ -37,14 +37,14 @@ and check if the exit code is equal to `13`. There are also two test cases in As
Print runtime version:

```bash
$ ./adapter.sh --version
$ ./adapter.py --version
wasmtime-cli 1.0.1
```

Run WASM module:

```bash
$ ./adapter.sh --arg a1 --arg a2 --env E1=env1 --env E2=env2 --test-file test.wasm
$ ./adapter.py --arg a1 --arg a2 --env E1=env1 --env E2=env2 --test-file test.wasm
# Expected to start test.wasm module with E1=env1 and E2=env2
# environment variables defined and arguments a1, a2 passed to
# the module.
Expand Down
2 changes: 1 addition & 1 deletion test-runner/requirements/common.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
colorama==0.4.3
colorama>=0.4.3
4 changes: 3 additions & 1 deletion test-runner/wasi_test_runner/runtime_adapter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import subprocess
import sys
from pathlib import Path
from typing import Dict, NamedTuple, List

Expand All @@ -16,7 +17,7 @@ def __init__(self, adapter_path: str) -> None:

def get_version(self) -> RuntimeVersion:
output = (
subprocess.check_output([self._adapter_path, "--version"], encoding="UTF-8")
subprocess.check_output([sys.executable, self._adapter_path, "--version"], encoding="UTF-8")
.strip()
.split(" ")
)
Expand All @@ -31,6 +32,7 @@ def run_test(
) -> Output:
args = (
[
sys.executable,
self._adapter_path,
"--test-file",
self._abs(test_path),
Expand Down

0 comments on commit dc7f8d2

Please sign in to comment.