-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate autobahn tests with pytest (#5809)
* Integrate autobahn tests with pytest * Fix docker compose file paths * Fix typo in CHANGES file * Fix add python-on-whales dependency to .in file instead of .txt * Use pathlib instead of os * Use buildx instead of compose build * Regenerate dev requirements * Rename changes file * Use request fspath instead of hard coded path * Create a sepearte builder when building aiohttp * Use subprocess instead of python-on-whales * Extract failed tests and make assertions on them * Fix lint issues * Fix fixture scope * Add ports to docker-compose files * Add wait-for-it package * Use xfail instead of fail * Use wstest cmd tool instead of the docker image * Fix lint issues * Use assert statement with custom output Co-authored-by: Sviatoslav Sydorenko <[email protected]> * Code cleanup * Use docker instead of docker-compose * Add xfail decorator * Add tmp_path * Remove gitignore * Skip tests only on macOS * Check if docker is available * Regenerate dev.txt Co-authored-by: Sviatoslav Sydorenko <[email protected]>
- Loading branch information
Showing
14 changed files
with
163 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Automated running autobahn test suite by integrating with pytest. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
-r test.txt | ||
-r doc.txt | ||
cherry_picker==2.0.0; python_version>="3.6" | ||
python-on-whales==0.19.0 | ||
wait-for-it==2.2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FROM crossbario/autobahn-testsuite:0.8.2 | ||
|
||
RUN apt-get update && apt-get install python3 python3-pip -y | ||
RUN pip3 install wait-for-it | ||
|
||
CMD ["wstest", "--mode", "fuzzingserver", "--spec", "/config/fuzzingserver.json"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import json | ||
import subprocess | ||
import sys | ||
from pathlib import Path | ||
from typing import Any, Dict, Generator, List | ||
|
||
import pytest | ||
from pytest import TempPathFactory | ||
from python_on_whales import DockerException, docker | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def report_dir(tmp_path_factory: TempPathFactory) -> Path: | ||
return tmp_path_factory.mktemp("reports") | ||
|
||
|
||
@pytest.fixture(scope="session", autouse=True) | ||
def build_autobahn_testsuite() -> Generator[None, None, None]: | ||
|
||
try: | ||
docker.build( | ||
file="tests/autobahn/Dockerfile.autobahn", | ||
tags=["autobahn-testsuite"], | ||
context_path=".", | ||
) | ||
except DockerException: | ||
pytest.skip(msg="The docker daemon is not running.") | ||
|
||
try: | ||
yield | ||
finally: | ||
docker.image.remove(x="autobahn-testsuite") | ||
|
||
|
||
def get_failed_tests(report_path: str, name: str) -> List[Dict[str, Any]]: | ||
path = Path(report_path) | ||
result_summary = json.loads((path / "index.json").read_text())[name] | ||
failed_messages = [] | ||
PASS = {"OK", "INFORMATIONAL"} | ||
entry_fields = {"case", "description", "expectation", "expected", "received"} | ||
for results in result_summary.values(): | ||
if results["behavior"] in PASS and results["behaviorClose"] in PASS: | ||
continue | ||
report = json.loads((path / results["reportfile"]).read_text()) | ||
failed_messages.append({field: report[field] for field in entry_fields}) | ||
return failed_messages | ||
|
||
|
||
@pytest.mark.skipif(sys.platform == "darwin", reason="Don't run on macOS") | ||
@pytest.mark.xfail | ||
def test_client(report_dir: Path, request: Any) -> None: | ||
try: | ||
print("Starting autobahn-testsuite server") | ||
autobahn_container = docker.run( | ||
detach=True, | ||
image="autobahn-testsuite", | ||
name="autobahn", | ||
publish=[(9001, 9001)], | ||
remove=True, | ||
volumes=[ | ||
(f"{request.fspath.dirname}/client", "/config"), | ||
(f"{report_dir}", "/reports"), | ||
], | ||
) | ||
print("Running aiohttp test client") | ||
client = subprocess.Popen( | ||
["wait-for-it", "-s", "localhost:9001", "--"] | ||
+ [sys.executable] | ||
+ ["tests/autobahn/client/client.py"] | ||
) | ||
client.wait() | ||
finally: | ||
print("Stopping client and server") | ||
client.terminate() | ||
client.wait() | ||
autobahn_container.stop() | ||
|
||
failed_messages = get_failed_tests(f"{report_dir}/clients", "aiohttp") | ||
|
||
assert not failed_messages, "\n".join( | ||
"\n\t".join( | ||
f"{field}: {msg[field]}" | ||
for field in ("case", "description", "expectation", "expected", "received") | ||
) | ||
for msg in failed_messages | ||
) | ||
|
||
|
||
@pytest.mark.skipif(sys.platform == "darwin", reason="Don't run on macOS") | ||
@pytest.mark.xfail | ||
def test_server(report_dir: Path, request: Any) -> None: | ||
try: | ||
print("Starting aiohttp test server") | ||
server = subprocess.Popen( | ||
[sys.executable] + ["tests/autobahn/server/server.py"] | ||
) | ||
print("Starting autobahn-testsuite client") | ||
docker.run( | ||
image="autobahn-testsuite", | ||
name="autobahn", | ||
remove=True, | ||
volumes=[ | ||
(f"{request.fspath.dirname}/server", "/config"), | ||
(f"{report_dir}", "/reports"), | ||
], | ||
networks=["host"], | ||
command=[ | ||
"wait-for-it", | ||
"-s", | ||
"localhost:9001", | ||
"--", | ||
"wstest", | ||
"--mode", | ||
"fuzzingclient", | ||
"--spec", | ||
"/config/fuzzingclient.json", | ||
], | ||
) | ||
finally: | ||
print("Stopping client and server") | ||
server.terminate() | ||
server.wait() | ||
|
||
failed_messages = get_failed_tests(f"{report_dir}/servers", "AutobahnServer") | ||
|
||
assert not failed_messages, "\n".join( | ||
"\n\t".join( | ||
f"{field}: {msg[field]}" | ||
for field in ("case", "description", "expectation", "expected", "received") | ||
) | ||
for msg in failed_messages | ||
) |