-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tests.integration): add test for each main version of applicatio…
…n (2.14 and 2.15), add shortcut test
- Loading branch information
Showing
12 changed files
with
210 additions
and
121 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
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 |
---|---|---|
@@ -1,86 +1,23 @@ | ||
import os | ||
import socket | ||
import subprocess | ||
import time | ||
from pathlib import Path | ||
import typing as t | ||
|
||
import pytest | ||
|
||
from pathlib import Path | ||
from tests.integration.server_mock.builder import build | ||
|
||
EXE_NAME = "AntaresWebServer.exe" if os.name == "nt" else "AntaresWebServer" | ||
"""Name of the executable file for the Antares web server.""" | ||
|
||
SPAWN_TIMEOUT = 10 | ||
"""Timeout in seconds to wait for the server process to start.""" | ||
|
||
SERVER_TIMEOUT = 10 | ||
"""Timeout in seconds to wait for the server to be ready.""" | ||
|
||
|
||
@pytest.fixture(name="antares_web_server_path", scope="session", autouse=True) | ||
def antares_web_server_path_fixture(tmp_path_factory: pytest.TempPathFactory) -> Path: | ||
"""Fixture used to build the Antares web server application.""" | ||
target_dir = tmp_path_factory.mktemp("antares_web_server", numbered=False) | ||
return build(target_dir, exe_name=EXE_NAME) | ||
|
||
|
||
def ping(host: str, port: int, timeout: float = 2) -> bool: | ||
""" | ||
Checks if a host is reachable by attempting to connect to a specified port. | ||
Args: | ||
host: The host to connect to. | ||
port: The port to connect on. | ||
timeout: The time in seconds to wait for a connection. | ||
Returns: | ||
True if the host is reachable, False otherwise. | ||
""" | ||
try: | ||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: | ||
sock.settimeout(timeout) | ||
sock.connect((host, port)) | ||
except (socket.timeout, ConnectionError): | ||
return False | ||
else: | ||
return True | ||
|
||
|
||
@pytest.fixture(name="antares_web_server") | ||
def antares_web_server_fixture(antares_web_server_path: Path) -> t.Generator[subprocess.Popen, None, None]: | ||
"""Fixture used to provide a running instance of the Antares web server.""" | ||
# Spawn the server process | ||
assert antares_web_server_path.exists() | ||
server = subprocess.Popen( | ||
[str(antares_web_server_path)], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
text=True, | ||
shell=False, | ||
) | ||
|
||
try: | ||
# Wait for the subprocess to start | ||
timeout_time = time.time() + SPAWN_TIMEOUT | ||
while time.time() < timeout_time: | ||
if server.poll() is None: | ||
break | ||
time.sleep(0.1) | ||
else: | ||
raise RuntimeError("The process did not start in time.") | ||
@pytest.fixture(name="antares_web_server_paths", scope="session", autouse=True) | ||
def antares_web_server_paths_fixture(tmp_path_factory: pytest.TempPathFactory) -> [Path]: | ||
"""Fixture used to build both the Antares web server version (2.14.4 and 2.15.2).""" | ||
target_dir = tmp_path_factory.mktemp("servers", numbered=False) | ||
|
||
# Wait for the server to be ready | ||
timeout_time = time.time() + SERVER_TIMEOUT | ||
while time.time() < timeout_time: | ||
if ping("localhost", 8080, timeout=0.1): | ||
break | ||
time.sleep(0.1) | ||
else: | ||
raise RuntimeError("The server did not start in time.") | ||
yield server | ||
apps = [] | ||
for version in ["2.14.4", "2.15.2"]: | ||
import tests.integration.server_mock.server as server | ||
server.__dict__["version"] = version | ||
version_target_dir = target_dir.joinpath("AntaresWeb-"+version) | ||
apps.append(build(version_target_dir, exe_name=EXE_NAME)) | ||
|
||
finally: | ||
server.terminate() | ||
server.wait() | ||
return apps |
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 |
---|---|---|
@@ -1,11 +1,76 @@ | ||
import httpx # type: ignore | ||
import subprocess | ||
import socket | ||
import time | ||
|
||
SPAWN_TIMEOUT = 10 | ||
"""Timeout in seconds to wait for the server process to start.""" | ||
|
||
def test_server_health(antares_web_server): | ||
SERVER_TIMEOUT = 10 | ||
"""Timeout in seconds to wait for the server to be ready.""" | ||
|
||
|
||
def ping(host: str, port: int, timeout: float = 2) -> bool: | ||
""" | ||
Checks if a host is reachable by attempting to connect to a specified port. | ||
Args: | ||
host: The host to connect to. | ||
port: The port to connect on. | ||
timeout: The time in seconds to wait for a connection. | ||
Returns: | ||
True if the host is reachable, False otherwise. | ||
""" | ||
try: | ||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: | ||
sock.settimeout(timeout) | ||
sock.connect((host, port)) | ||
except (socket.timeout, ConnectionError): | ||
return False | ||
else: | ||
return True | ||
|
||
|
||
def test_server_health(antares_web_server_paths): | ||
""" | ||
Test the health endpoint of the Antares web server. | ||
After retrieving newly built servers, run each, make a | ||
simple get request and kill it. | ||
""" | ||
|
||
res = httpx.get("http://localhost:8080/health", timeout=0.25) | ||
assert res.status_code == 200, res.json() | ||
assert res.json() == {"status": "available"} | ||
# Set up: run each server | ||
for server_path in antares_web_server_paths: | ||
server = subprocess.Popen( | ||
[str(server_path)], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
text=True, | ||
shell=False, | ||
) | ||
|
||
# Wait for the subprocess to start | ||
timeout_time = time.time() + SPAWN_TIMEOUT | ||
while time.time() < timeout_time: | ||
if server.poll() is None: | ||
break | ||
time.sleep(0.1) | ||
else: | ||
raise RuntimeError("The process did not start in time.") | ||
|
||
# Wait for the server to be ready | ||
timeout_time = time.time() + SERVER_TIMEOUT | ||
while time.time() < timeout_time: | ||
if ping("localhost", 8080, timeout=0.1): | ||
break | ||
time.sleep(0.1) | ||
else: | ||
raise RuntimeError("The server did not start in time.") | ||
|
||
res = httpx.get("http://localhost:8080/health", timeout=0.25) | ||
assert res.status_code == 200, res.json() | ||
assert res.json() == {"status": "available"} | ||
|
||
# Tear down: kill server and make sure it is terminated | ||
server.terminate() | ||
server.wait() |
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,27 @@ | ||
db: | ||
url: sqlite:///database.db | ||
debug: false | ||
launcher: | ||
local: | ||
binaries: | ||
880: ./AntaresWeb/antares_solver/antares-8.8-solver | ||
logging: | ||
logfile: ./tmp/antarest.log | ||
root_path: api | ||
security: | ||
disabled: true | ||
jwt: | ||
key: super-secret | ||
server: | ||
services: | ||
- watcher | ||
worker_threadpool_size: 12 | ||
storage: | ||
archive_dir: ./examples/archives | ||
matrixstore: ./matrices | ||
tmp_dir: ./tmp | ||
workspaces: | ||
default: | ||
path: ./examples/internal_studies/ | ||
studies: | ||
path: ./examples/studies/ |
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 |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "2.14.4" | ||
__version__ = "2.15.2" |
Oops, something went wrong.