Skip to content

Commit

Permalink
Tests: Fix server/client not loaded yet race (getting connection errors)
Browse files Browse the repository at this point in the history
  • Loading branch information
roekatz committed Feb 15, 2024
1 parent d413453 commit de781bc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
10 changes: 3 additions & 7 deletions packages/opal-client/opal_client/tests/data_updater_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
UpdateCallback,
)
from opal_common.schemas.store import JSONPatchAction
from opal_common.tests.test_utils import wait_for_server
from opal_common.utils import get_authorization_header
from opal_server.config import opal_server_config
from opal_server.server import OpalServer
Expand Down Expand Up @@ -96,7 +97,6 @@ def check() -> int:

@server_app.on_event("startup")
async def startup_event():
await asyncio.sleep(0.4)
# signal the server is ready
event.set()

Expand All @@ -109,6 +109,8 @@ def server():
# Run the server as a separate process
proc = Process(target=setup_server, args=(event,), daemon=True)
proc.start()
assert event.wait(5)
wait_for_server(PORT)
yield event
proc.kill() # Cleanup after test

Expand Down Expand Up @@ -164,8 +166,6 @@ async def run():
async def test_data_updater(server):
"""Disable auto-update on connect (fetch_on_connect=False) Connect to OPAL-
server trigger a Data-update and check our policy store gets the update."""
# Wait for the server to start
server.wait(5)
# config to use mock OPA
policy_store = PolicyStoreClientFactory.create(store_type=PolicyStoreTypes.MOCK)
updater = DataUpdater(
Expand Down Expand Up @@ -232,8 +232,6 @@ async def test_data_updater(server):
async def test_data_updater_with_report_callback(server):
"""Disable auto-update on connect (fetch_on_connect=False) Connect to OPAL-
server trigger a Data-update and check our policy store gets the update."""
# Wait for the server to start
server.wait(5)
# config to use mock OPA
policy_store = PolicyStoreClientFactory.create(store_type=PolicyStoreTypes.MOCK)
updater = DataUpdater(
Expand Down Expand Up @@ -292,8 +290,6 @@ async def test_data_updater_with_report_callback(server):
@pytest.mark.asyncio
async def test_client_get_initial_data(server):
"""Connect to OPAL-server and make sure data is fetched on-connect."""
# Wait for the server to start
server.wait(5)
# config to use mock OPA
policy_store = PolicyStoreClientFactory.create(store_type=PolicyStoreTypes.MOCK)
updater = DataUpdater(
Expand Down
17 changes: 17 additions & 0 deletions packages/opal-common/opal_common/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import requests
import time


def wait_for_server(port: int, timeout: int = 2):
"""
Waits for the http server (of either the server or the client) to be available
"""
start = time.time()
while time.time() - start < timeout:
try:
# Assumes both server and client have "/" route
requests.get(f"http://localhost:{port}/")
return
except requests.exceptions.ConnectionError:
time.sleep(0.1)
raise TimeoutError(f"Server did not start within {timeout} seconds")
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from fastapi_websocket_pubsub import PubSubClient
from flaky import flaky
from opal_common.schemas.webhook import GitWebhookRequestParams
from opal_common.tests.test_utils import wait_for_server
from opal_server.policy.webhook.api import get_webhook_router, is_matching_webhook_url
from opal_server.policy.webhook.deps import (
extracted_git_changes,
Expand Down Expand Up @@ -245,7 +246,6 @@ async def publish(event):

@server_app.on_event("startup")
async def startup_event():
await asyncio.sleep(0.4)
# signal the server is ready
event.set()

Expand All @@ -266,6 +266,8 @@ def github_mode_server():
daemon=True,
)
proc.start()
assert event.wait(5)
wait_for_server(PORT)
yield event
proc.kill() # Cleanup after test

Expand All @@ -286,6 +288,8 @@ def gitlab_mode_server():
# Run the server as a separate process
proc = Process(target=setup_server, args=(event, webhook_config), daemon=True)
proc.start()
assert event.wait(5)
wait_for_server(PORT)
yield event
proc.kill() # Cleanup after test

Expand All @@ -307,6 +311,8 @@ def azure_git_mode_server():
# Run the server as a separate process
proc = Process(target=setup_server, args=(event, webhook_config), daemon=True)
proc.start()
assert event.wait(5)
wait_for_server(PORT)
yield event
proc.kill() # Cleanup after test

Expand All @@ -328,15 +334,15 @@ def bitbucket_mode_server():
# Run the server as a separate process
proc = Process(target=setup_server, args=(event, webhook_config), daemon=True)
proc.start()
assert event.wait(5)
wait_for_server(PORT)
yield event
proc.kill() # Cleanup after test


@pytest.mark.asyncio
async def test_webhook_mock_github(github_mode_server):
"""Test the webhook route simulating a webhook from Github."""
# Wait for server to be ready
github_mode_server.wait(5)
# simulate a webhook
async with ClientSession() as session:
async with session.post(
Expand All @@ -355,8 +361,6 @@ async def test_webhook_mock_github(github_mode_server):
@pytest.mark.asyncio
async def test_webhook_mock_gitlab(gitlab_mode_server):
"""Test the webhook route simulating a webhook from Gitlab."""
# Wait for server to be ready
gitlab_mode_server.wait(5)
# simulate a webhook
async with ClientSession() as session:
async with session.post(
Expand All @@ -375,8 +379,6 @@ async def test_webhook_mock_gitlab(gitlab_mode_server):
@pytest.mark.asyncio
async def test_webhook_mock_azure_git(azure_git_mode_server):
"""Test the webhook route simulating a webhook from Azure-Git."""
# Wait for server to be ready
azure_git_mode_server.wait(5)
# simulate a webhook
async with ClientSession() as session:
async with session.post(
Expand All @@ -395,8 +397,6 @@ async def test_webhook_mock_azure_git(azure_git_mode_server):
@pytest.mark.asyncio
async def test_webhook_mock_bitbucket(bitbucket_mode_server):
"""Test the webhook route simulating a webhook from Azure-Git."""
# Wait for server to be ready
bitbucket_mode_server.wait(5)
# simulate a webhook
async with ClientSession() as session:
async with session.post(
Expand Down

0 comments on commit de781bc

Please sign in to comment.