-
Notifications
You must be signed in to change notification settings - Fork 8
602 lines (576 loc) · 23.4 KB
/
pytest.yml
File metadata and controls
602 lines (576 loc) · 23.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
name: Pytest
on:
workflow_call:
inputs:
splits:
type: number
description: 'Number of test splits default 1 (max 24)'
required: true
default: 1
workflow_dispatch:
inputs:
splits:
type: number
description: 'Number of test splits default 1 (max 24)'
required: true
default: 1
IS_PYTEST_COMBINE_TEST_RESULTS_DISABLED:
type: boolean
description: Override flag for skipping rollup of pytest/coverage html reports
permissions:
contents: read
jobs:
define-matrix:
runs-on: ubuntu-latest
outputs:
SPLIT_LIST: ${{ steps.splits.outputs.SPLIT_LIST }}
SPLIT_COUNT: ${{ steps.splits.outputs.SPLIT_COUNT }}
IS_PYTEST_COMBINE_TEST_RESULTS_DISABLED: ${{ steps.splits.outputs.IS_PYTEST_COMBINE_TEST_RESULTS_DISABLED }}
restored: ${{ steps.restore-test-durations.outputs.cache-hit == '' && 'false' || 'true' }}
steps:
- name: Define Matrix Split
id: splits
run: |
set -ex
export SPLIT_COUNT=${{ inputs.splits || 12 }}
if [ "$SPLIT_COUNT" -gt 24 ]; then
echo "Splits cannot exceed 24"
export SPLIT_COUNT=24
fi
if [ "$ACT" == "true" ] && [ -z "${{ inputs.splits }}" ]; then
echo "Running with act limiting splits to 1 as splits not provided"
export SPLIT_COUNT=1
else
echo "Running on GitHub Actions"
fi
SPLIT_LIST="["
for ((i=1; i<=SPLIT_COUNT; i++)); do
SPLIT_LIST+="\"$i\""
if [ $i -lt $SPLIT_COUNT ]; then
SPLIT_LIST+=", "
fi
done
SPLIT_LIST+="]"
echo "Generated list: $SPLIT_LIST"
export SPLIT_COUNT="$(echo ${SPLIT_LIST} | jq 'length')"
echo "${SPLIT_COUNT} : ${SPLIT_LIST}"
echo "SPLIT_LIST=${SPLIT_LIST}" >> "$GITHUB_OUTPUT"
echo "SPLIT_COUNT=${SPLIT_COUNT}" >> "$GITHUB_OUTPUT"
echo "IS_PYTEST_COMBINE_TEST_RESULTS_DISABLED=${{ inputs.IS_PYTEST_COMBINE_TEST_RESULTS_DISABLED || vars.IS_PYTEST_COMBINE_TEST_RESULTS_DISABLED == 'True' }}" >> $GITHUB_OUTPUT
- name: Restore Test Durations
id: restore-test-durations
uses: actions/cache/restore@v4
with:
path: /tmp/.test_durations
key: tests-durations-${{ github.sha }}
restore-keys: |
tests-durations-${{ github.sha }}-
tests-durations-
fail-on-cache-miss: false
- name: Upload Test Durations
if: steps.restore-test-durations.outputs.cache-hit != ''
uses: actions/upload-artifact@v4
with:
name: test-durations-before
path: /tmp/.test_durations
include-hidden-files: true
pytest:
needs: define-matrix
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
split: ${{ fromJSON(needs.define-matrix.outputs.SPLIT_LIST) }}
container:
image: opendatacanada/ckan-pytest # primary executor
services:
postgres:
image: postgres:13.14
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: pass
POSTGRES_DB: postgres
solr:
image: opendatacanada/solr
redis:
image: redis:3
env:
SPLIT_COUNT: ${{ needs.define-matrix.outputs.SPLIT_COUNT }}
PGHOST: postgres
PGDATABASE: postgres
PGUSER: postgres
PGPASSWORD: pass
CKAN_POSTGRES_DB: ckan_test
CKAN_DATASTORE_POSTGRES_DB: datastore_test
CKAN_POSTGRES_USER: ckan_default
CKAN_DATASTORE_POSTGRES_READ_USER: datastore_read
CKAN_DATASTORE_POSTGRES_WRITE_USER: datastore_write
CKAN_POSTGRES_PWD: pass
CKAN_DATASTORE_POSTGRES_READ_PWD: pass
CKAN_DATASTORE_POSTGRES_WRITE_PWD: pass
CKAN_SQLALCHEMY_URL: postgresql://ckan_default:pass@postgres/ckan_test
CKAN_DATASTORE_WRITE_URL: postgresql://datastore_write:pass@postgres/datastore_test
CKAN_DATASTORE_READ_URL: postgresql://datastore_read:pass@postgres/datastore_test
CKAN_SOLR_URL: http://solr:8983/solr/ckan_registry
CKAN_REDIS_URL: redis://redis:6379/1
PYTEST_COMMON_OPTIONS: -v --ckan-ini=/srv/app/src/ckanext-recombinant/test-core.ini --cov=/srv/app/src/ckanext-recombinant --cov-branch --cov-report=term-missing --cov-config=/srv/app/src/ckanext-recombinant/.coveragerc --disable-warnings --confcutdir=/srv/app/src/ckanext-recombinant/ckanext/recombinant/tests /srv/app/src/ckanext-recombinant/ckanext/recombinant/tests --junitxml=/srv/app/junit/result/junit-${{ matrix.split }}.xml -o junit_family=legacy
steps:
- run: |
echo "${{ matrix.split }} of $SPLIT_COUNT"
- name: Setup JUnit Folders
run: |
mkdir -p /srv/app/junit/result
touch /srv/app/junit/result/test-summary.md
- name: Check Source Dependency Versions
run: |
source /srv/app/venv/bin/activate
for dir in /srv/app/src/*; do
srcName=$(echo "$dir" | awk -F'/' '{print $NF}');
if [[ -d "$dir/.git" ]]; then
printf "${srcName}: Checking for updates..."
cd $dir;
current_branch=$(git rev-parse --abbrev-ref HEAD);
if [[ "$srcName" == "ckan" ]]; then
git fetch canada;
reslog=$(git log HEAD..canada/$current_branch --oneline);
else
git fetch origin;
reslog=$(git log HEAD..origin/$current_branch --oneline);
fi;
if [[ "${reslog}" == "" ]]; then
printf "${srcName}: Up to date. Skipping..."
else
git pull || true;
printf "${srcName}: Pulled updated code. Re-initializing in python environment..."
cd /srv/app;
if [[ -f "$dir/requirements.txt" ]]; then
pip install -r $dir/requirements.txt || true;
fi;
if [[ -f "$dir/dev-requirements.txt" ]]; then
pip install -r $dir/dev-requirements.txt || true;
fi;
if [[ -f "$dir/test-requirements.txt" ]]; then
pip install -r $dir/test-requirements.txt || true;
fi;
if [[ -f "$dir/setup.py" ]]; then
python $dir/setup.py develop || true;
fi;
fi;
else
printf "${srcName}: Not a source repository. Skipping..."
fi;
done;
cd /srv/app;
- name: Checkout Repository (${{ github.head_ref || github.ref_name }})
run: |
cd /srv/app/src/ckanext-recombinant
git fetch origin
if [[ "${{ github.head_ref || github.ref_name }}" != "master" ]]; then
git checkout -b ${{ github.head_ref || github.ref_name }} origin/${{ github.head_ref || github.ref_name }}
fi;
git fetch
git pull
source /srv/app/venv/bin/activate
pip install -e .
pip install -r requirements.txt
pip install -r test-requirements.txt
python3 setup.py develop
cd /srv/app/src/ckan
pip install -e .
python3 setup.py develop
pip install setuptools==70.0.0
cd /srv/app
- name: Setup Databases
run: |
source /srv/app/venv/bin/activate
cd /srv/app/src/ckan
pip install -e .
python3 setup.py develop
pip install setuptools==70.0.0
cd /srv/app
ln -s /srv/app/src/ckan/test-core.ini /srv/app/src/ckanext-recombinant/links/test-core.ini
ln -s /srv/app/src/ckan/who.ini /srv/app/src/ckanext-recombinant/links/who.ini
mkdir -p /srv/app/src/ckanext-recombinant/links/ckanext/datastore/tests
ln -s /srv/app/src/ckan/ckanext/datastore/tests/allowed_functions.txt /srv/app/src/ckanext-recombinant/links/ckanext/datastore/tests/allowed_functions.txt
mkdir -p /srv/app/src/ckanext-recombinant/links/ckan/bin/postgres_init
ln -s /srv/app/src/ckan/bin/postgres_init/1_create_ckan_db.sh /srv/app/src/ckanext-recombinant/links/ckan/bin/postgres_init/1_create_ckan_db.sh
ln -s /srv/app/src/ckan/bin/postgres_init/2_create_ckan_datastore_db.sh /srv/app/src/ckanext-recombinant/links/ckan/bin/postgres_init/2_create_ckan_datastore_db.sh
. /srv/app/src/ckanext-recombinant/links/ckan/bin/postgres_init/1_create_ckan_db.sh
. /srv/app/src/ckanext-recombinant/links/ckan/bin/postgres_init/2_create_ckan_datastore_db.sh
ckan -c /srv/app/src/ckanext-recombinant/test-core.ini db init
ckan -c /srv/app/src/ckanext-recombinant/test-core.ini datastore set-permissions | psql -U postgres --set ON_ERROR_STOP=1
ckan -c /srv/app/src/ckanext-recombinant/test-core.ini recombinant create-triggers -a
ckan -c /srv/app/src/ckanext-recombinant/test-core.ini db upgrade
ckan -c /srv/app/src/ckanext-recombinant/test-core.ini db pending-migrations --apply
- name: Download Test Durations
if: needs.define-matrix.outputs.restored == 'true'
uses: actions/download-artifact@v4
with:
name: test-durations-before
- name: Use Fallback Test Durations
if: needs.define-matrix.outputs.restored == 'false'
run: |
echo "unzip pytest test duration details"
gunzip /srv/app/src/ckanext-recombinant/.test_durations.gz
- name: Run Pytest
run: |
set -ex
echo "${{ matrix.split }} of $SPLIT_COUNT"
source /srv/app/venv/bin/activate
echo "::group::pytest"
pytest $PYTEST_COMMON_OPTIONS --splits $SPLIT_COUNT --group ${{ matrix.split }} --splitting-algorithm least_duration --store-durations --clean-durations
echo "::endgroup::"
- name: Upload Test Durations
if: github.run_attempt == 1
uses: actions/upload-artifact@v4
with:
name: test-durations-after-partial-${{ matrix.split }}
path: .test_durations
if-no-files-found: error
include-hidden-files: true
retention-days: 1
- name: Prep Test Coverage Split File
run: |
mv .coverage coverage-${{ matrix.split }}
- name: Upload Coverage Artifacts
uses: actions/upload-artifact@v4
with:
name: internal_coverage-${{ matrix.split }}
path: coverage-${{ matrix.split }}
if-no-files-found: error
include-hidden-files: true
retention-days: 1
- name: Store Test Results
uses: actions/upload-artifact@v4
if: always()
with:
name: internal_junit-results-${{ matrix.split }}
path: /srv/app/junit/result
retention-days: 1
- name: Test Summary (if Failure)
uses: test-summary/action@v2
with:
paths: "/srv/app/junit/result/*.xml"
if: failure()
- name: Test Summary Local Copy (If Failure)
uses: test-summary/action@v2
with:
paths: "/srv/app/junit/result/*.xml"
output: "/srv/app/junit/result/test-summary.md"
if: failure()
- name: Store Test Summary Results (If Failure)
uses: actions/upload-artifact@v4
with:
name: split-test-summary-${{ matrix.split }}.md
path: "/srv/app/junit/result/test-summary.md"
retention-days: 30
if: failure()
cache-test-durations:
name: Cache Test Durations
needs: pytest
if: github.run_attempt == 1 && (success() || failure())
runs-on: ubuntu-latest
steps:
- name: Download All Partial Test Durations
uses: actions/download-artifact@v4
with:
pattern: test-durations-after-partial-*
- name: Merge All Partial Test Durations
run: |
ls -ltra
jq -s 'add' test-durations-after-partial-*/.test_durations \
| jq 'to_entries | sort_by(.key) | from_entries' \
> /tmp/.test_durations
- name: Upload Final Test Durations
uses: actions/upload-artifact@v4
with:
name: test-durations-after
path: /tmp/.test_durations
if-no-files-found: error
include-hidden-files: true
- name: Cache Final Test Durations
uses: actions/cache/save@v4
with:
path: /tmp/.test_durations
key: tests-durations-${{ github.sha }}
merge-code-coverage-and-upload:
name: Merge & Upload Code Coverage and Results
needs: pytest
if: github.run_attempt == 1 && (success() || failure()) && needs.define-matrix.outputs.IS_PYTEST_COMBINE_TEST_RESULTS_DISABLED == 'false'
runs-on: ubuntu-latest
container:
image: opendatacanada/ckan-pytest # primary executor
steps:
- name: Check Source Dependency Versions
run: |
source /srv/app/venv/bin/activate
for dir in /srv/app/src/*; do
srcName=$(echo "$dir" | awk -F'/' '{print $NF}');
if [[ -d "$dir/.git" ]]; then
printf "${srcName}: Checking for updates..."
cd $dir;
current_branch=$(git rev-parse --abbrev-ref HEAD);
if [[ "$srcName" == "ckan" ]]; then
git fetch canada;
reslog=$(git log HEAD..canada/$current_branch --oneline);
else
git fetch origin;
reslog=$(git log HEAD..origin/$current_branch --oneline);
fi;
if [[ "${reslog}" == "" ]]; then
printf "${srcName}: Up to date. Skipping..."
else
git pull || true;
printf "${srcName}: Pulled updated code. Re-initializing in python environment..."
cd /srv/app;
if [[ -f "$dir/requirements.txt" ]]; then
pip install -r $dir/requirements.txt || true;
fi;
if [[ -f "$dir/dev-requirements.txt" ]]; then
pip install -r $dir/dev-requirements.txt || true;
fi;
if [[ -f "$dir/test-requirements.txt" ]]; then
pip install -r $dir/test-requirements.txt || true;
fi;
if [[ -f "$dir/setup.py" ]]; then
python $dir/setup.py develop || true;
fi;
fi;
else
printf "${srcName}: Not a source repository. Skipping..."
fi;
done;
cd /srv/app;
- name: Checkout Repository (${{ github.head_ref || github.ref_name }})
run: |
cd /srv/app/src/ckanext-recombinant
git fetch origin
if [[ "${{ github.head_ref || github.ref_name }}" != "master" ]]; then
git checkout -b ${{ github.head_ref || github.ref_name }} origin/${{ github.head_ref || github.ref_name }}
fi;
git fetch
git pull
source /srv/app/venv/bin/activate
pip install -e .
pip install -r requirements.txt
pip install -r test-requirements.txt
python3 setup.py develop
pip install codecov-cli==11.2.3
cd /srv/app
- name: Download All Coverage Artifacts
uses: actions/download-artifact@v4
with:
pattern: internal_coverage-*
path: /srv/app/coverage-data
merge-multiple: 'true'
- name: Download All JUnit XML Results
uses: actions/download-artifact@v4
with:
pattern: internal_junit-results-*
merge-multiple: true
path: /srv/app/junit-results
- name: Merge Coverage and JUnit XML files
run: |
set -ex
source /srv/app/venv/bin/activate
mkdir -p /srv/app/results
echo "merge junit test results"
echo "::group::junitparser-merge"
junitparser merge /srv/app/junit-results/*.xml /srv/app/merged.xml
echo "::endgroup::"
echo "::group::junit2html generate"
junit2html /srv/app/merged.xml /srv/app/results/pytest-results.html
cp /srv/app/merged.xml /srv/app/results/junit_results.xml
echo "::endgroup::"
echo "merge pytest coverage results"
echo "::group::coverage combine"
coverage combine --keep -a /srv/app/coverage-data/*
echo "::endgroup::"
echo "::group::pytest-html-report"
coverage html -d /srv/app/results/coverage_html
echo "::endgroup::"
- name: Upload Coverage Reports to CodeCov
if: ${{ !cancelled() }}
continue-on-error: true
run: |
set -ex
source /srv/app/venv/bin/activate
codecov -v upload-coverage -t ${{ secrets.CODECOV_TOKEN }} --report-type coverage --slug open-data/ckanext-recombinant --git-service github --branch ${{ github.head_ref || github.ref_name }} --network-root-folder /srv/app/src/ckanext-recombinant --commit-sha ${{ github.sha }}
- name: Upload Test Results to CodeCov
if: ${{ !cancelled() }}
continue-on-error: true
run: |
set -ex
source /srv/app/venv/bin/activate
codecov -v do-upload -t ${{ secrets.CODECOV_TOKEN }} -f /srv/app/results/junit_results.xml --report-type test_results --slug open-data/ckanext-recombinant --git-service github --branch ${{ github.head_ref || github.ref_name }} --network-root-folder /srv/app/src/ckanext-recombinant --commit-sha ${{ github.sha }}
- name: Pytest Coverage Console Report
run: |
set -ex
source /srv/app/venv/bin/activate
coverage report -m
- name: Upload HTML coverage report
uses: actions/upload-artifact@v4
with:
name: combined-test-coverage-reports
path: /srv/app/results
slack_notify:
name: Send Slack Notifications
needs: [pytest, merge-code-coverage-and-upload]
if: (success() || failure()) && needs.define-matrix.outputs.IS_PYTEST_COMBINE_TEST_RESULTS_DISABLED == 'false'
runs-on: ubuntu-latest
steps:
- name: Slack Notification (Success)
if: ${{ !cancelled() && needs.pytest.result == 'success' }}
uses: slackapi/slack-github-action@v1.23.0
with:
payload: |
{
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "Tests Succeeded :white_check_mark:",
"emoji": true
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Project:*\n${{ github.repository }}"
},
{
"type": "mrkdwn",
"text": "*Branch:*\n${{ github.head_ref || github.ref_name }}"
},
{
"type": "mrkdwn",
"text": "*Commit:*\n${{ github.sha }}"
},
{
"type": "mrkdwn",
"text": "*Author:*\n${{ github.actor }}"
}
]
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "_View it on GitHub:_"
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "View Workflow",
"emoji": true
},
"url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}",
"action_id": "button-action"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "_View it on CodeCov:_"
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "View Test Coverage",
"emoji": true
},
"url": "https://app.codecov.io/gh/${{ github.repository }}/commit/${{ github.sha }}",
"action_id": "button-action"
}
}
]
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
- name: Slack Notification (Failure)
if: ${{ !cancelled() && needs.pytest.result != 'success' }}
uses: slackapi/slack-github-action@v1.23.0
with:
payload: |
{
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "Tests Failed :red_circle:",
"emoji": true
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Project:*\n${{ github.repository }}"
},
{
"type": "mrkdwn",
"text": "*Branch:*\n${{ github.head_ref || github.ref_name }}"
},
{
"type": "mrkdwn",
"text": "*Commit:*\n${{ github.sha }}"
},
{
"type": "mrkdwn",
"text": "*Author:*\n${{ github.actor }}"
}
]
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "_View it on GitHub:_"
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "View Workflow",
"emoji": true
},
"url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}",
"action_id": "button-action"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "_View it on CodeCov:_"
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "View Test Coverage",
"emoji": true
},
"url": "https://app.codecov.io/gh/${{ github.repository }}/commit/${{ github.sha }}",
"action_id": "button-action"
}
}
]
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK