Skip to content

Commit b865e97

Browse files
authored
ci: parallelize unit tests (#13036)
Signed-off-by: Marc 'risson' Schmitt <[email protected]>
1 parent 24a364b commit b865e97

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

.github/workflows/ci-main.yml

+25-6
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,26 @@ jobs:
4343
uses: ./.github/actions/setup
4444
- name: run migrations
4545
run: poetry run python -m lifecycle.migrate
46+
test-make-seed:
47+
runs-on: ubuntu-latest
48+
steps:
49+
- id: seed
50+
run: |
51+
echo "seed=$(printf "%d\n" "0x$(openssl rand -hex 4)")" >> "$GITHUB_OUTPUT"
52+
outputs:
53+
seed: ${{ steps.seed.outputs.seed }}
4654
test-migrations-from-stable:
47-
name: test-migrations-from-stable - PostgreSQL ${{ matrix.psql }}
55+
name: test-migrations-from-stable - PostgreSQL ${{ matrix.psql }} - Run ${{ matrix.run_id }}/5
4856
runs-on: ubuntu-latest
57+
timeout-minutes: 20
58+
needs: test-make-seed
4959
strategy:
5060
fail-fast: false
5161
matrix:
5262
psql:
5363
- 15-alpine
5464
- 16-alpine
65+
run_id: [1, 2, 3, 4, 5]
5566
steps:
5667
- uses: actions/checkout@v4
5768
with:
@@ -93,28 +104,36 @@ jobs:
93104
env:
94105
# Test in the main database that we just migrated from the previous stable version
95106
AUTHENTIK_POSTGRESQL__TEST__NAME: authentik
107+
CI_TEST_SEED: ${{ needs.test-make-seed.outputs.seed }}
108+
CI_RUN_ID: ${{ matrix.run_id }}
109+
CI_TOTAL_RUNS: "5"
96110
run: |
97-
poetry run make test
111+
poetry run make ci-test
98112
test-unittest:
99-
name: test-unittest - PostgreSQL ${{ matrix.psql }}
113+
name: test-unittest - PostgreSQL ${{ matrix.psql }} - Run ${{ matrix.run_id }}/5
100114
runs-on: ubuntu-latest
101-
timeout-minutes: 30
115+
timeout-minutes: 20
116+
needs: test-make-seed
102117
strategy:
103118
fail-fast: false
104119
matrix:
105120
psql:
106121
- 15-alpine
107122
- 16-alpine
123+
run_id: [1, 2, 3, 4, 5]
108124
steps:
109125
- uses: actions/checkout@v4
110126
- name: Setup authentik env
111127
uses: ./.github/actions/setup
112128
with:
113129
postgresql_version: ${{ matrix.psql }}
114130
- name: run unittest
131+
env:
132+
CI_TEST_SEED: ${{ needs.test-make-seed.outputs.seed }}
133+
CI_RUN_ID: ${{ matrix.run_id }}
134+
CI_TOTAL_RUNS: "5"
115135
run: |
116-
poetry run make test
117-
poetry run coverage xml
136+
poetry run make ci-test
118137
- if: ${{ always() }}
119138
uses: codecov/codecov-action@v5
120139
with:

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,8 @@ ci-bandit: ci--meta-debug
284284

285285
ci-pending-migrations: ci--meta-debug
286286
ak makemigrations --check
287+
288+
ci-test: ci--meta-debug
289+
coverage run manage.py test --keepdb --randomly-seed ${CI_TEST_SEED} authentik
290+
coverage report
291+
coverage xml

authentik/root/test_plugin.py

+18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import math
12
from os import environ
23
from ssl import OPENSSL_VERSION
34

@@ -24,3 +25,20 @@ def pytest_report_header(*_, **__):
2425
f"authentik version: {get_full_version()}",
2526
f"OpenSSL version: {OPENSSL_VERSION}, FIPS: {backend._fips_enabled}",
2627
]
28+
29+
30+
def pytest_collection_modifyitems(config: pytest.Config, items: list[pytest.Item]) -> None:
31+
current_id = int(environ.get("CI_RUN_ID", 0)) - 1
32+
total_ids = int(environ.get("CI_TOTAL_RUNS", 0))
33+
34+
if total_ids:
35+
num_tests = len(items)
36+
matrix_size = math.ceil(num_tests / total_ids)
37+
38+
start = current_id * matrix_size
39+
end = (current_id + 1) * matrix_size
40+
41+
deselected_items = items[:start] + items[end:]
42+
config.hook.pytest_deselected(items=deselected_items)
43+
items[:] = items[start:end]
44+
print(f" Executing {start} - {end} tests")

0 commit comments

Comments
 (0)