graphql-engine/server/tests-py/fixtures/hge.py
Samir Talwar 204ec89c61 server/tests-py: Get all tests passing with separate HGE binaries.
This rewrites the last couple of Python tests that were failing when run with a separate HGE binary per test class. The changes are as follows:

1. The event triggers tests, naming conventions tests, and subscriptions tests all generate a new source DB per test, so can run in parallel.
2. The scheduled triggers tests use the correct URL for the trigger service when the port is generated randomly.
3. Whitespace and trailing commas are added to the scheduled triggers tests.
4. Support for SQL Server is added to _hge.py_ so the naming conventions test that runs on SQL Server passes. (The other SQL Server tests do not pass and we're not going to bother with them for now.)
5. Container names are fixed in _run.sh_.
6. _run.sh_ and _run-new.sh_ don't pull images explicitly as it's annoying when running tests a lot. If you want to pull the latest versions, just run `docker compose pull` from the _server/tests-py_ directory, or the root directory. (If you don't have the images at all, they'll still be pulled automatically.)

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7350
GitOrigin-RevId: db58f310f017b2a0884fcf61ccc56d15583f99bd
2022-12-21 15:56:41 +00:00

83 lines
2.6 KiB
Python

import os
import pytest
import subprocess
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.
_PASS_THROUGH_ENV_VARS = set([
'PATH', # required for basically anything to work
'HASURA_GRAPHQL_PG_SOURCE_URL_1',
'HASURA_GRAPHQL_PG_SOURCE_URL_2',
'HASURA_GRAPHQL_CITUS_SOURCE_URL',
'HASURA_GRAPHQL_MSSQL_SOURCE_URL',
# required for Nix-based ODBC driver configuration
'ODBCSYSINI',
'ODBCINSTINI',
])
def hge_port() -> int:
return ports.find_free_port()
def hge_server(
request: pytest.FixtureRequest,
hge_bin: str,
hge_port: int,
hge_url: str,
hge_key: Optional[str],
hge_fixture_env: dict[str, str],
metadata_schema_url: str,
) -> Optional[str]:
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,
}
hge_key_args = ['--admin-secret', hge_key] if hge_key else []
print(f'Starting GraphQL Engine on {hge_url}...')
hge_process = subprocess.Popen(
args = [
hge_bin,
'--metadata-database-url', metadata_schema_url,
'serve',
'--server-port', str(hge_port),
'--stringify-numeric-types',
*hge_key_args,
],
env = env,
)
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.')
# We can re-enable stopping in the background once tests no longer share a database.
# Until then, HGE can interfere with other tests.
request.addfinalizer(stop)
## Stop in the background so we don't hold up other tests.
# request.addfinalizer(lambda: threading.Thread(target = stop).start())
ports.wait_for_port(hge_port, timeout = 30)
return hge_url