Skip to content

Commit

Permalink
Trying more fixes...
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-andersson committed Feb 11, 2025
1 parent 3ddce70 commit 8e6450f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/unittesting-pytest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
services:
postgres:
image: postgres:13
container_name: postgres-test-db
ports:
- 5432:5432
env:
Expand All @@ -39,6 +40,7 @@ jobs:

mongo:
image: mongo:4.4
container_name: mongo-test-db
ports:
- 27017:27017
env:
Expand All @@ -62,7 +64,7 @@ jobs:
env:
UNITTEST_DBUSER: unittest_username
UNITTEST_DBPASS: unittest_password
UNITTEST_POSTGRESHOST: postgres
UNITTEST_GITHUB: True
run: |
pytest fedn/tests --doctest-modules --junitxml=junit/test-results.xml --cov=com --cov-report=xml --cov-report=html
18 changes: 12 additions & 6 deletions fedn/tests/stores/helpers/database_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ def network_id():

@pytest.fixture(scope="package")
def mongo_connection():
already_running, _, port = start_mongodb_container()
if not os.environ.get("UNITTEST_GITHUB"):
_, port = start_mongodb_container()
else:
port = 27017

def mongo_config():
return {
Expand All @@ -29,7 +32,8 @@ def mongo_config():
with patch('fedn.network.storage.dbconnection.get_statestore_config', return_value=mongo_config()), \
patch('fedn.network.storage.dbconnection.get_network_config', return_value=network_id()):
yield DatabaseConnection(force_create_new=True)
if not already_running:

if not os.environ.get("UNITTEST_GITHUB"):
stop_mongodb_container()

@pytest.fixture(scope="package")
Expand All @@ -48,8 +52,10 @@ def sql_config():

@pytest.fixture(scope="package")
def postgres_connection():
already_running, _, port = start_postgres_container()

if not os.environ.get("UNITTEST_GITHUB"):
_, port = start_postgres_container()
else:
port = 5432


def postgres_config():
Expand All @@ -59,13 +65,13 @@ def postgres_config():
"username": os.environ.get("UNITTEST_DBUSER", "_"),
"password": os.environ.get("UNITTEST_DBPASS", "_"),
"database": "fedn_db",
"host": os.environ.get("UNITTEST_POSTGRESHOST", "localhost"),
"host": "localhost",
"port": port
}
}

with patch('fedn.network.storage.dbconnection.get_statestore_config', return_value=postgres_config()), \
patch('fedn.network.storage.dbconnection.get_network_config', return_value=network_id()):
yield DatabaseConnection(force_create_new=True)
if not already_running:
if not os.environ.get("UNITTEST_GITHUB"):
stop_postgres_container()
5 changes: 1 addition & 4 deletions fedn/tests/stores/helpers/mongo_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@
"""
def start_mongodb_container():
client = docker.from_env()

already_running = False
try:
container = client.containers.get(CONTAINER_NAME)
already_running = True
except docker.errors.NotFound:
container = client.containers.run(
"mongo:7.0",
Expand All @@ -39,7 +36,7 @@ def start_mongodb_container():
else:
raise Exception("Could not start MongoDB container")

return already_running, container, port
return container, port

def stop_mongodb_container():
client = docker.from_env()
Expand Down
8 changes: 3 additions & 5 deletions fedn/tests/stores/helpers/postgres_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@
"""
def start_postgres_container():
client = docker.from_env()
already_running = False
try:
container = client.containers.get(CONTAINER_NAME)
already_running = True
except docker.errors.NotFound:
container = client.containers.run(
"postgres:15",
detach=True,
ports={"5432/tcp": None}, # Let Docker choose an available port
name=CONTAINER_NAME,
environment={
"POSTGRES_USER": os.environ.get("TEST_USER", "_"),
"POSTGRES_PASSWORD": os.environ.get("TEST_PASS", "_"),
"POSTGRES_USER": os.environ.get("UNITTEST_DBUSER", "_"),
"POSTGRES_PASSWORD": os.environ.get("UNITTEST_DBPASS", "_"),
"POSTGRES_DB": "fedn_db"
}
)
Expand All @@ -37,7 +35,7 @@ def start_postgres_container():
else:
raise Exception("Could not start Postgres container")

return already_running, container, port
return container, port

def stop_postgres_container():
client = docker.from_env()
Expand Down

0 comments on commit 8e6450f

Please sign in to comment.