2022-09-15 15:30:01 +03:00
|
|
|
import os
|
|
|
|
import pytest
|
|
|
|
import subprocess
|
2023-04-17 16:07:30 +03:00
|
|
|
import threading
|
2022-09-15 15:30:01 +03:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
import ports
|
|
|
|
|
|
|
|
# These are the names of the environment variables that should be passed through to the HGE binary.
|
|
|
|
# Other variables are ignored.
|
2022-09-28 12:19:47 +03:00
|
|
|
_PASS_THROUGH_ENV_VARS = set([
|
2022-09-15 15:30:01 +03:00
|
|
|
'PATH', # required for basically anything to work
|
2022-12-21 18:55:24 +03:00
|
|
|
|
2022-09-15 15:30:01 +03:00
|
|
|
'HASURA_GRAPHQL_PG_SOURCE_URL_1',
|
|
|
|
'HASURA_GRAPHQL_PG_SOURCE_URL_2',
|
2022-12-21 18:55:24 +03:00
|
|
|
'HASURA_GRAPHQL_CITUS_SOURCE_URL',
|
|
|
|
'HASURA_GRAPHQL_MSSQL_SOURCE_URL',
|
|
|
|
|
|
|
|
# required for Nix-based ODBC driver configuration
|
|
|
|
'ODBCSYSINI',
|
|
|
|
'ODBCINSTINI',
|
2022-09-15 15:30:01 +03:00
|
|
|
])
|
|
|
|
|
2022-09-28 12:19:47 +03:00
|
|
|
|
2023-04-25 15:49:31 +03:00
|
|
|
def hge_port(worker_id: str) -> int:
|
|
|
|
return ports.find_free_port(worker_id)
|
2022-09-15 15:30:01 +03:00
|
|
|
|
2022-09-28 12:19:47 +03:00
|
|
|
|
2022-09-15 15:30:01 +03:00
|
|
|
def hge_server(
|
|
|
|
request: pytest.FixtureRequest,
|
2022-09-28 12:19:47 +03:00
|
|
|
hge_bin: str,
|
2022-09-15 15:30:01 +03:00
|
|
|
hge_port: int,
|
2022-09-28 12:19:47 +03:00
|
|
|
hge_url: str,
|
2022-09-29 20:18:49 +03:00
|
|
|
hge_key: Optional[str],
|
2022-09-28 12:19:47 +03:00
|
|
|
hge_fixture_env: dict[str, str],
|
2022-11-15 22:07:34 +03:00
|
|
|
metadata_schema_url: str,
|
2023-04-17 16:07:30 +03:00
|
|
|
) -> subprocess.Popen[bytes]:
|
2022-09-28 12:19:47 +03:00
|
|
|
hge_env: dict[str, str] = {name: value for name, value in os.environ.items() if name in _PASS_THROUGH_ENV_VARS}
|
|
|
|
hge_marker_env: dict[str, str] = {marker.args[0]: marker.args[1] for marker in request.node.iter_markers('hge_env') if marker.args[1] is not None}
|
|
|
|
env = {
|
|
|
|
**hge_env,
|
|
|
|
**hge_fixture_env,
|
|
|
|
**hge_marker_env,
|
|
|
|
}
|
2022-09-15 15:30:01 +03:00
|
|
|
|
2022-09-29 20:18:49 +03:00
|
|
|
hge_key_args = ['--admin-secret', hge_key] if hge_key else []
|
|
|
|
|
2023-04-17 16:07:30 +03:00
|
|
|
if request.node.get_closest_marker('capture_hge_logs'):
|
|
|
|
# capture the logs
|
|
|
|
stdout = subprocess.PIPE
|
|
|
|
stderr = subprocess.STDOUT # combine with stdout
|
|
|
|
else:
|
|
|
|
# just stream outwards, so that the user can see the logs on a failing test
|
|
|
|
stdout = None
|
|
|
|
stderr = None
|
|
|
|
|
2022-09-15 15:30:01 +03:00
|
|
|
print(f'Starting GraphQL Engine on {hge_url}...')
|
|
|
|
hge_process = subprocess.Popen(
|
|
|
|
args = [
|
|
|
|
hge_bin,
|
2022-11-15 22:07:34 +03:00
|
|
|
'--metadata-database-url', metadata_schema_url,
|
2022-09-15 15:30:01 +03:00
|
|
|
'serve',
|
|
|
|
'--server-port', str(hge_port),
|
|
|
|
'--stringify-numeric-types',
|
2022-09-29 20:18:49 +03:00
|
|
|
*hge_key_args,
|
2022-09-15 15:30:01 +03:00
|
|
|
],
|
2022-09-28 12:19:47 +03:00
|
|
|
env = env,
|
2023-04-17 16:07:30 +03:00
|
|
|
stdout = stdout,
|
|
|
|
stderr = stderr,
|
2022-09-15 15:30:01 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
def stop():
|
|
|
|
if hge_process.poll() is None:
|
|
|
|
print(f'Stopping GraphQL Engine on {hge_url}...')
|
|
|
|
hge_process.terminate()
|
|
|
|
try:
|
|
|
|
hge_process.wait(timeout = 5)
|
|
|
|
print(f'GraphQL Engine on {hge_url} has stopped.')
|
|
|
|
except subprocess.TimeoutExpired:
|
|
|
|
print(f'Given up waiting; killing GraphQL Engine...')
|
|
|
|
hge_process.kill()
|
|
|
|
hge_process.wait()
|
|
|
|
print(f'GraphQL Engine has been successfully killed.')
|
|
|
|
else:
|
|
|
|
print(f'GraphQL Engine on {hge_url} has already stopped.')
|
|
|
|
|
2023-04-17 16:07:30 +03:00
|
|
|
# Stop in the background so we don't hold up other tests.
|
|
|
|
request.addfinalizer(lambda: threading.Thread(target = stop).start())
|
|
|
|
|
|
|
|
try:
|
|
|
|
ports.wait_for_port(hge_port, timeout = 30)
|
|
|
|
except TimeoutError:
|
|
|
|
# print the logs so we can diagnose the issue
|
|
|
|
if hge_process.stdout:
|
|
|
|
print(hge_process.stdout.read())
|
|
|
|
raise
|
2022-09-15 15:30:01 +03:00
|
|
|
|
2023-04-17 16:07:30 +03:00
|
|
|
return hge_process
|