|
1 | | -import logging |
2 | | -import socket |
3 | 1 | import ssl |
4 | | -import threading |
5 | | -import time |
6 | 2 | import pytest |
7 | 3 | from pytest_httpserver import HTTPServer |
8 | | -import requests |
9 | 4 | from tests.functional.backend_api.app_config import AppConfig |
10 | | -from threading import Thread |
11 | 5 | import trustme |
12 | | -from create_app import create_app |
13 | 6 |
|
14 | 7 |
|
15 | 8 | @pytest.fixture(scope="session") |
@@ -43,42 +36,6 @@ def httpclient_ssl_context(ca): |
43 | 36 | return ssl.create_default_context(cafile=ca_temp_path) |
44 | 37 |
|
45 | 38 |
|
46 | | -@pytest.fixture(scope="session") |
47 | | -def app_port() -> int: |
48 | | - logging.info("Getting free port") |
49 | | - return get_free_port() |
50 | | - |
51 | | - |
52 | | -@pytest.fixture(scope="session") |
53 | | -def app_url(app_port: int) -> str: |
54 | | - return f"http://localhost:{app_port}" |
55 | | - |
56 | | - |
57 | | -@pytest.fixture(scope="session") |
58 | | -def app_config(make_httpserver, ca): |
59 | | - logging.info("Creating APP CONFIG") |
60 | | - with ca.cert_pem.tempfile() as ca_temp_path: |
61 | | - app_config = AppConfig( |
62 | | - { |
63 | | - "AZURE_OPENAI_ENDPOINT": f"https://localhost:{make_httpserver.port}", |
64 | | - "AZURE_SEARCH_SERVICE": f"https://localhost:{make_httpserver.port}", |
65 | | - "AZURE_CONTENT_SAFETY_ENDPOINT": f"https://localhost:{make_httpserver.port}", |
66 | | - "SSL_CERT_FILE": ca_temp_path, |
67 | | - "CURL_CA_BUNDLE": ca_temp_path, |
68 | | - } |
69 | | - ) |
70 | | - logging.info(f"Created app config: {app_config.get_all()}") |
71 | | - yield app_config |
72 | | - |
73 | | - |
74 | | -@pytest.fixture(scope="session", autouse=True) |
75 | | -def manage_app(app_port: int, app_config: AppConfig): |
76 | | - app_config.apply_to_environment() |
77 | | - start_app(app_port) |
78 | | - yield |
79 | | - app_config.remove_from_environment() |
80 | | - |
81 | | - |
82 | 39 | @pytest.fixture(scope="function", autouse=True) |
83 | 40 | def setup_default_mocking(httpserver: HTTPServer, app_config: AppConfig): |
84 | 41 | httpserver.expect_request( |
@@ -154,39 +111,3 @@ def setup_default_mocking(httpserver: HTTPServer, app_config: AppConfig): |
154 | 111 | yield |
155 | 112 |
|
156 | 113 | httpserver.check() |
157 | | - |
158 | | - |
159 | | -def start_app(app_port: int) -> Thread: |
160 | | - logging.info(f"Starting application on port {app_port}") |
161 | | - app = create_app() |
162 | | - app_process = threading.Thread(target=lambda: app.run(port=app_port)) |
163 | | - app_process.daemon = True |
164 | | - app_process.start() |
165 | | - wait_for_app(app_port) |
166 | | - logging.info("Application started") |
167 | | - return app_process |
168 | | - |
169 | | - |
170 | | -def wait_for_app(port: int, initial_check_delay: int = 10): |
171 | | - attempts = 0 |
172 | | - time.sleep(initial_check_delay) |
173 | | - while attempts < 10: |
174 | | - try: |
175 | | - response = requests.get(f"http://localhost:{port}/api/config") |
176 | | - if response.status_code == 200: |
177 | | - return |
178 | | - except Exception: |
179 | | - pass |
180 | | - |
181 | | - attempts += 1 |
182 | | - time.sleep(1) |
183 | | - |
184 | | - raise Exception("App failed to start") |
185 | | - |
186 | | - |
187 | | -def get_free_port() -> int: |
188 | | - s = socket.socket(socket.AF_INET, type=socket.SOCK_STREAM) |
189 | | - s.bind(("localhost", 0)) |
190 | | - _, port = s.getsockname() |
191 | | - s.close() |
192 | | - return port |
0 commit comments