-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathconftest.py
More file actions
104 lines (79 loc) · 2.98 KB
/
conftest.py
File metadata and controls
104 lines (79 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# (C) 2024 GoodData Corporation
import contextlib
import os
import socket
from collections.abc import Iterable
from contextlib import closing
from pathlib import Path
from typing import Union
import pytest
from gooddata_flexconnect.function.flight_methods import (
create_flexconnect_flight_methods,
)
from gooddata_flight_server import FlightServerMethods, FlightServerMethodsFactory, GoodDataFlightServer, create_server
_CURRENT_DIR = Path(__file__).parent
_TLS_DIR = _CURRENT_DIR / "tls"
def _find_free_port():
"""
see: https://stackoverflow.com/a/45690594
"""
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
s.bind(("127.0.0.1", 0))
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
return s.getsockname()[1]
def _clean_env_vars():
to_drop = [key for key in os.environ if key.startswith("GOODDATA_FLIGHT")]
for key in to_drop:
os.environ.pop(key, None)
@contextlib.contextmanager
def server(
methods: Union[FlightServerMethods, FlightServerMethodsFactory],
tls: bool = False,
mtls: bool = False,
) -> GoodDataFlightServer:
port = _find_free_port()
os.environ["GOODDATA_FLIGHT_SERVER__LISTEN_PORT"] = str(port)
os.environ["GOODDATA_FLIGHT_SERVER__ADVERTISE_HOST"] = "localhost"
if tls:
cert = os.path.join(_TLS_DIR, "server-cert.pem")
key = os.path.join(_TLS_DIR, "server-key.pem")
os.environ["GOODDATA_FLIGHT_SERVER__USE_TLS"] = "TRUE"
os.environ["GOODDATA_FLIGHT_SERVER__TLS_CERTIFICATE"] = f"@{cert}"
os.environ["GOODDATA_FLIGHT_SERVER__TLS_PRIVATE_KEY"] = f"@{key}"
if mtls:
ca_cert = os.path.join(_TLS_DIR, "ca-cert.pem")
os.environ["GOODDATA_FLIGHT_SERVER__TLS_ROOT_CERTIFICATE"] = f"@{ca_cert}"
_server = create_server(methods)
_server.start()
# started = _server.wait_for_start(timeout=5.0)
started = _server.wait_for_start()
if not started:
raise AssertionError(f"Test fixture unable to start server in time on port {port}.")
yield _server
_clean_env_vars()
_server.stop()
_server.wait_for_stop()
@contextlib.contextmanager
def flexconnect_server(
modules: Iterable[str],
tls: bool = False,
mtls: bool = False,
) -> GoodDataFlightServer:
funs = ", ".join([f'"{module}"' for module in modules])
funs = f"[{funs}]"
os.environ["GOODDATA_FLIGHT_FLEXCONNECT__FUNCTIONS"] = funs
os.environ["GOODDATA_FLIGHT_FLEXCONNECT__CALL_DEADLINE_MS"] = "500"
with server(create_flexconnect_flight_methods, tls, mtls) as s:
yield s
@pytest.fixture(scope="session")
def tls_ca_cert():
with open(os.path.join(_TLS_DIR, "ca-cert.pem"), "rb") as f:
return f.read()
@pytest.fixture(scope="session")
def tls_client_cert():
with open(os.path.join(_TLS_DIR, "client-cert.pem"), "rb") as f:
return f.read()
@pytest.fixture(scope="session")
def tls_client_key():
with open(os.path.join(_TLS_DIR, "client-key.pem"), "rb") as f:
return f.read()