Skip to content

Commit 69d97f6

Browse files
committed
OPT: run integration tests in a containerized stack
1 parent c7f6345 commit 69d97f6

File tree

10 files changed

+91
-8
lines changed

10 files changed

+91
-8
lines changed

config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
dbname=env('DB_DATABASE'),
6060
)
6161
elif env('DB_CONNECTION') == 'postgresql':
62+
63+
host = 'db' if env('INTEGRATION_TEST') else env('DB_HOST', 'localhost')
64+
6265
DATABASE_URL = "postgresql://{user}:{password}@{host}:{port}/{dbname}".format(
6366
user=env('DB_USERNAME', 'root'),
6467
password=env('DB_PASSWORD', ''),

docker-compose.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
services:
22
db:
3+
hostname: db
34
image: postgres
45
restart: always
56
environment:
@@ -10,6 +11,12 @@ services:
1011
- pgdata:/var/lib/postgresql/data
1112
ports:
1213
- '5432:5432'
14+
healthcheck:
15+
test: [ "CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}" ]
16+
interval: 10s
17+
retries: 5
18+
start_period: 30s
19+
timeout: 10s
1320

1421
volumes:
1522
pgdata:

elekto/constants.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,23 @@
1313
# limitations under the License.
1414
#
1515
# Author(s): Manish Sahani <rec.manish.sahani@gmail.com>
16+
import os
1617

1718
# Application's CSRF Security
1819
CSRF_STATE = 'state'
1920
AUTH_STATE = 'authentication'
2021

21-
# Github Endpoints
22-
GITHUB_AUTHORIZE = 'https://github.com/login/oauth/authorize'
23-
GITHUB_ACCESS = 'https://github.com/login/oauth/access_token'
24-
GITHUB_PROFILE = 'https://api.github.com/user'
22+
# GitHub Endpoints
23+
# TODO: Make the GitHub externally configurable/overridable. If not specified in env, use github.com
24+
github_host = 'https://github.com'
25+
# github_host = 'http://localhost:9000'
2526

27+
if os.environ.get('INTEGRATION_TEST') == 'true':
28+
github_host = 'http://github:9000'
29+
30+
GITHUB_AUTHORIZE = f'{github_host}/login/oauth/authorize'
31+
GITHUB_ACCESS = f'{github_host}/login/oauth/access_token'
32+
GITHUB_PROFILE = f'{github_host}/user'
2633

2734
# Election attributes related constants
2835
ELEC_STAT_COMPLETED = 'completed'

entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ if [ "$APP_DEBUG" == "True" ]; then
1212
echo "in Debug mode"
1313
./console --run
1414
else
15-
if [ $APP_CONNECT == "socket" ]; then
15+
if [ "$APP_CONNECT" == "socket" ]; then
1616
# socket mode for fronting by nginx
1717
echo "with a socket connection on $APP_PORT"
1818
uwsgi --module elekto:APP --processes 8 --socket :$APP_PORT

integration_tests/Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM python:3.11
2+
3+
WORKDIR /app
4+
5+
COPY requirements.txt .
6+
RUN pip install -r requirements.txt
7+
RUN playwright install
8+
RUN playwright install-deps
9+
10+
COPY tests tests
11+
12+
CMD ["pytest", "tests"]

integration_tests/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ venv: clean
1313
.PHONY: test
1414
test:
1515
$(PYTEST)
16+
17+
.PHONY: dctest
18+
dctest:
19+
docker compose --profile test up --abort-on-container-exit --exit-code-from integration_tests

integration_tests/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ Tests can be run from the `elekto/integration_tests` directory. Tests runner is
2626
using Playwright. A virtual environment is required to run the tests. Tests assume all infra runs at the default ports.
2727
- Create the virtual env with `make venv`. This will also install dependencies.
2828
- Run tests with `make test`.
29+
30+
To run tests entirely in a Docker Compose setup, use `make dctest`.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
include:
2+
- ../docker-compose.yml # Defines the `db` service
3+
- ../../github-static-mock/docker-compose.yaml
4+
5+
services:
6+
integration_tests:
7+
build: .
8+
environment:
9+
ELEKTO_HOST: "elekto:8000"
10+
GITHUB_HOST: "github:9000"
11+
depends_on:
12+
elekto:
13+
condition: service_healthy
14+
db:
15+
condition: service_healthy
16+
profiles:
17+
- test
18+
volumes:
19+
- "./tests:/app/tests"
20+
elekto:
21+
build: ../
22+
user: "root" # To access /app/meta/.git/FETCH_HEAD
23+
ports:
24+
# Links will point to `http://elekto:8000/`, so you must manually patch the URLs to localhost in your browser
25+
# after each redirect. Exposing the Elekto service allows for manual checking during tests if desired.
26+
- "8000:8000"
27+
environment:
28+
DB_CONNECTION: postgresql
29+
DB_PORT: 5432
30+
INTEGRATION_TEST: true
31+
APP_PORT: 8000
32+
healthcheck:
33+
test: [ "CMD", "curl", "-f", "http://0.0.0.0:8000/app/" ]
34+
interval: 10s
35+
retries: 5
36+
start_period: 5s
37+
timeout: 10s
38+
depends_on:
39+
db:
40+
condition: service_healthy
41+
profiles:
42+
- test

integration_tests/tests/conftest.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import os
12
import pytest
23

34
from utils.github_mock.client import GithubMockUtilityClient
45

56

67
@pytest.fixture
78
def host() -> str:
8-
return 'http://localhost:8000'
9+
return 'http://' + os.environ.get('ELEKTO_HOST', 'localhost:8000')
910

1011

1112
@pytest.fixture
@@ -25,7 +26,7 @@ def app_url(host: str) -> str:
2526

2627
@pytest.fixture
2728
def github_mock_host() -> str:
28-
return 'http://localhost:9000'
29+
return 'http://' + os.environ.get('GITHUB_HOST', 'localhost:9000')
2930

3031

3132
@pytest.fixture

integration_tests/tests/test_login.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
import os
2+
13
import pytest
24
from playwright.sync_api import Page, expect
35

46
from utils.github_mock import GithubMockUtilityClient, User
57

68

9+
ELEKTO_HOST = os.environ.get('ELEKTO_HOST', 'localhost:8000')
10+
11+
712
def logout(page: Page) -> None:
8-
page.goto('http://localhost:8000/app')
13+
page.goto(f'http://{ELEKTO_HOST}/app')
914
logout_link = page.get_by_role('link', name='Logout')
1015
if logout_link.is_visible():
1116
logout_link.click()

0 commit comments

Comments
 (0)