-
Notifications
You must be signed in to change notification settings - Fork 11
477 lines (466 loc) · 18.2 KB
/
test.yaml
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
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.
name: Tests
on:
workflow_call:
inputs:
charm-directory:
type: string
description: The working directory for the docs
default: "."
charmcraft-channel:
description: Charmcraft channel to use
type: string
default: latest/stable
working-directory:
type: string
description: The working directory for jobs
default: "./"
shellcheck-working-directory:
type: string
description: The working directory for the shellcheck-lint job
self-hosted-runner:
type: boolean
description: Whether to use self-hosted runners to run the jobs.
default: true
self-hosted-runner-arch:
type: string
description: Architecture to use on self-hosted runners to run the jobs.
default: "x64"
self-hosted-runner-label:
type: string
description: Label for selecting the self-hosted runners.
default: "large"
self-hosted-runner-image:
type: string
description: Image of the requested runner. Supports only 'jammy' or 'noble'.
default: "jammy"
pre-run-script:
description: Path to the bash script to be run before the integration tests
type: string
concurrency:
group: operator-workflows-${{ github.workflow }}-${{ inputs.working-directory }}-tests-${{ github.ref }}-self-hosted-${{ inputs.self-hosted-runner }}
cancel-in-progress: true
jobs:
inclusive-naming-check:
name: Inclusive naming
runs-on: >-
${{
inputs.self-hosted-runner &&
fromJson(format('[''self-hosted'', ''{0}'', ''{1}'', ''{2}'']',
inputs.self-hosted-runner-arch, inputs.self-hosted-runner-label, inputs.self-hosted-runner-image
)) || 'ubuntu-22.04'
}}
defaults:
run:
working-directory: ${{ inputs.working-directory }}
steps:
- uses: actions/[email protected]
with:
repository: canonical/Inclusive-naming
path: ${{ inputs.working-directory }}
- run: mv * /tmp
- uses: actions/[email protected]
- name: Merge configuration files
run: |
# Combine all entries and replace matching elements by
# .name in .rules for the ones in .woke.yaml
woke_file=""
if [ -f .woke.yaml ]; then
woke_file=".woke.yaml"
elif [ -f .woke.yml ]; then
woke_file=".woke.yml"
fi
if [ ! -z "$woke_file" ]; then
yq eval-all '
(
. as $item ireduce ({}; . *+ $item) | .rules | unique_by(.name)
) as $mergedArray | . as $item ireduce ({}; . *+ $item) | .rules = $mergedArray
' $woke_file /tmp/config.yml | tee /tmp/merged.yml
mv /tmp/merged.yml /tmp/config.yml
fi
- name: Run inclusive naming check
uses: canonical/inclusive-naming@main
with:
fail-on-error: "true"
github-token: ${{ secrets.GITHUB_TOKEN }}
reporter: github-pr-review
woke-args: ". -c /tmp/config.yml"
filter-mode: nofilter
workdir: ${{ inputs.working-directory }}
woke-version: latest
vale:
name: Style checker
runs-on: >-
${{
inputs.self-hosted-runner &&
fromJson(format('[''self-hosted'', ''{0}'', ''{1}'', ''{2}'']',
inputs.self-hosted-runner-arch, inputs.self-hosted-runner-label, inputs.self-hosted-runner-image
)) || 'ubuntu-22.04'
}}
defaults:
run:
shell: bash
working-directory: ${{ inputs.working-directory }}
steps:
- name: Checkout repo to runner
uses: actions/[email protected]
- name: Install styles
uses: canonical/praecepta@main
run: |
apt-get update -y && apt-get install -y tree
- name: Run Vale tests
uses: errata-ai/vale-action@reviewdog
with:
files: ./docs
fail_on_error: false
shellcheck-lint:
name: Shell scripts lint
runs-on: >-
${{
inputs.self-hosted-runner &&
fromJson(format('[''self-hosted'', ''{0}'', ''{1}'', ''{2}'']',
inputs.self-hosted-runner-arch, inputs.self-hosted-runner-label, inputs.self-hosted-runner-image
)) || 'ubuntu-22.04'
}}
defaults:
run:
working-directory: ${{ inputs.shellcheck-working-directory || inputs.working-directory }}
steps:
- uses: actions/[email protected]
- name: Gather files to scan
shell: bash
id: gather
run: |
declare -a filepaths
shebangregex="^#! */[^ ]*/(env *)?[abk]*sh"
set -f # temporarily disable globbing so that globs in inputs aren't expanded
while IFS= read -r -d '' file; do
filepaths+=("$file")
done < <(find . \
-type f \
'(' \
-name '*.bash' \
-o -name '.bashrc' \
-o -name 'bashrc' \
-o -name '.bash_aliases' \
-o -name '.bash_completion' \
-o -name '.bash_login' \
-o -name '.bash_logout' \
-o -name '.bash_profile' \
-o -name 'bash_profile' \
-o -name '*.ksh' \
-o -name 'suid_profile' \
-o -name '*.zsh' \
-o -name '.zlogin' \
-o -name 'zlogin' \
-o -name '.zlogout' \
-o -name 'zlogout' \
-o -name '.zprofile' \
-o -name 'zprofile' \
-o -name '.zsenv' \
-o -name 'zsenv' \
-o -name '.zshrc' \
-o -name 'zshrc' \
-o -name '*.sh' \
-o -path '*/.profile' \
-o -path '*/profile' \
-o -name '*.shlib' \
')' \
-print0)
while IFS= read -r -d '' file; do
head -n1 "$file" | grep -Eqs "$shebangregex" || continue
filepaths+=("$file")
done < <(find . \
-type f ! -name '*.*' -perm /111 \
-print0)
echo "filepaths=${filepaths[@]}" >> $GITHUB_OUTPUT
set +f # re-enable globbing
- if: ${{ steps.gather.outputs.filepaths != '' }}
name: Shellcheck Problem Matchers
uses: lumaxis/[email protected]
- if: ${{ steps.gather.outputs.filepaths != '' }}
run: shellcheck -f gcc ${{steps.gather.outputs.filepaths}}
docker-lint:
name: Dockerfile lint
runs-on: >-
${{
inputs.self-hosted-runner &&
fromJson(format('[''self-hosted'', ''{0}'', ''{1}'', ''{2}'']',
inputs.self-hosted-runner-arch, inputs.self-hosted-runner-label, inputs.self-hosted-runner-image
)) || 'ubuntu-22.04'
}}
defaults:
run:
working-directory: ${{ inputs.working-directory }}
steps:
- uses: actions/[email protected]
- name: Check for Dockerfiles
id: dockerfiles
run: echo -n "found=$(find . -name "*Dockerfile" -printf "${{ inputs.working-directory }}%p ")" >> $GITHUB_OUTPUT
- if: ${{ steps.dockerfiles.outputs.found != '' }}
name: Run HadoLint
id: hado-lint
uses: jbergstroem/hadolint-gh-action@v1
with:
dockerfile: "${{ steps.dockerfiles.outputs.found }}"
metadata-lint:
name: Lint metadata.yaml
runs-on: >-
${{
inputs.self-hosted-runner &&
fromJson(format('[''self-hosted'', ''{0}'', ''{1}'', ''{2}'']',
inputs.self-hosted-runner-arch, inputs.self-hosted-runner-label, inputs.self-hosted-runner-image
)) || 'ubuntu-22.04'
}}
defaults:
run:
working-directory: ${{ inputs.working-directory }}
steps:
- uses: actions/[email protected]
- name: Check file existence
id: metadata-yaml
run: echo "exists=$([ -f metadata.yaml ] && echo "true" || echo "false")" >> $GITHUB_OUTPUT
- name: Run lint
if: steps.metadata-yaml.outputs.exists == 'true'
id: run-lint
run: |
options=""
if [[ "${{ inputs.self-hosted-runner-image }}" == "noble" ]]; then
options+=" --break-system-packages"
fi
python3 -m pip install check-jsonschema $options
curl -fLsSo $PWD/metadata.schema https://raw.githubusercontent.com/canonical/operator-workflows/main/.github/files/metadata.schema
check-jsonschema metadata.yaml --schemafile metadata.schema
lint-and-unit-test:
name: Lint and unit tests
runs-on: >-
${{
inputs.self-hosted-runner &&
fromJson(format('[''self-hosted'', ''{0}'', ''{1}'', ''{2}'']',
inputs.self-hosted-runner-arch, inputs.self-hosted-runner-label, inputs.self-hosted-runner-image
)) || 'ubuntu-22.04'
}}
defaults:
run:
working-directory: ${{ inputs.working-directory }}
steps:
- uses: actions/[email protected]
- name: Pre-run script
if: ${{ inputs.pre-run-script != '' }}
run: bash -xe ${{ inputs.pre-run-script }}
- name: Install tox
run: |
options=""
if [[ "${{ inputs.self-hosted-runner-image }}" == "noble" ]]; then
options+=" --break-system-packages"
fi
python3 -m pip install tox $options
- name: Run tests
id: run-tests
run: |
# Ensure that stdout appears as normal and redirect to file and exit depends on exit code of first command
STDOUT_LOG=$(mktemp --suffix=stdout.log)
echo STDOUT_LOG=$STDOUT_LOG >> $GITHUB_ENV
tox --result-json=test-result.json | tee $STDOUT_LOG ; test ${PIPESTATUS[0]} -eq 0
- name: Check lint and test stdout
run: |
# Check dependencies
EXPECTED_LINT_DEPS="\
mypy \
isort \
black \
flake8-docstrings \
flake8-docstrings-complete \
flake8-builtins \
flake8-test-docs \
pep8-naming \
codespell \
pylint \
pydocstyle \
"
for EXPECTED_LINT_DEP in $EXPECTED_LINT_DEPS; do
# Check that there is a `lint...<dependency>` line for each of the expected dependencies
DEP_REGEX="lint.*$EXPECTED_LINT_DEP"
if ! grep -q "$DEP_REGEX" $STDOUT_LOG ; then
# Write to stderr
>&2 echo "$EXPECTED_LINT_DEP should be in deps of [testenv:lint] environment in tox.ini"
exit 1
fi
done
# Check commands
EXPECTED_LINT_CMDS="\
pydocstyle \
codespell \
flake8 \
isort \
black \
mypy \
pylint \
"
for EXPECTED_LINT_CMD in $EXPECTED_LINT_CMDS; do
# Check that there is a `lint...commands...<command>` line for each of the expected commands
CMD_REGEX="lint.*commands.*$EXPECTED_LINT_CMD"
if ! grep -q "$CMD_REGEX" $STDOUT_LOG ; then
# Write to stderr
>&2 echo "$EXPECTED_LINT_CMD should be in commands of [testenv:lint] environment in tox.ini"
exit 1
fi
done
- name: Export test report
if: always() && !cancelled()
uses: actions/[email protected]
with:
script: |
const no_color = (text) => {
return text.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
}
const sha = '${{ github.event.pull_request.head.sha }}';
const fs = require('fs');
const result = JSON.parse(fs.readFileSync('${{ inputs.working-directory }}test-result.json')).testenvs;
let lint_result = result.lint.test;
let lint_success = true;
let lint_output = '';
for (let lint_test_result of lint_result) {
if (lint_test_result.retcode != 0) {
lint_success = false;
}
if (lint_test_result.output) {
lint_output += lint_test_result.output;
}
}
let unit_result = result.unit.test;
let unit_success = unit_result[0].retcode == 0;
let unit_output = unit_result[0].output;
let static_result = result.static.test;
let static_output = static_result[0].output;
let coverage_result = result["coverage-report"].test;
let coverage_output = coverage_result[0].output;
let reports = [];
if (!lint_success) {
reports.push(
`Lint checks failed for ${sha}\n
\`\`\`\n${no_color(lint_output).trim()}\n\`\`\``
);
}
if (!unit_success) {
reports.push(
`Unit tests failed for ${sha}\n
\`\`\`\n${no_color(unit_output).trim()}\n\`\`\``
);
}
reports.push(
`Test coverage for ${sha}\n
\`\`\`\n${no_color(coverage_output).trim()}\n\`\`\`
Static code analysis report\n
\`\`\`\n${no_color(static_output).trim()}\n\`\`\``
);
let json = JSON.stringify(reports);
fs.writeFileSync('report.json', json);
- name: Upload coverage report
uses: actions/upload-artifact@v4
if: always() && github.event_name == 'pull_request' && !cancelled()
with:
name: report
path: report.json
overwrite: true
draft-publish-docs:
name: Draft publish docs
runs-on: >-
${{
inputs.self-hosted-runner &&
fromJson(format('[''self-hosted'', ''{0}'', ''{1}'', ''{2}'']',
inputs.self-hosted-runner-arch, inputs.self-hosted-runner-label, inputs.self-hosted-runner-image
)) || 'ubuntu-22.04'
}}
defaults:
run:
working-directory: ${{ inputs.working-directory }}/${{ inputs.charm-directory }}
steps:
- uses: actions/[email protected]
- name: Search for docs folder
id: docs-exist
run: echo "docs_exist=$([[ -d docs ]] && echo 'True' || echo 'False')" >> $GITHUB_OUTPUT
- name: Publish documentation
if: ${{ steps.docs-exist.outputs.docs_exist == 'True' && !github.event.pull_request.head.repo.fork }}
uses: canonical/discourse-gatekeeper@stable
with:
discourse_host: discourse.charmhub.io
discourse_api_username: ${{ secrets.DISCOURSE_API_USERNAME }}
discourse_api_key: ${{ secrets.DISCOURSE_API_KEY }}
dry_run: true
github_token: ${{ secrets.GITHUB_TOKEN }}
charm_dir: ${{ inputs.working-directory }}/${{ inputs.charm-directory }}
license-headers-check:
name: Check license headers
runs-on: >-
${{
inputs.self-hosted-runner &&
fromJson(format('[''self-hosted'', ''{0}'', ''{1}'', ''{2}'']',
inputs.self-hosted-runner-arch, inputs.self-hosted-runner-label, inputs.self-hosted-runner-image
)) || 'ubuntu-22.04'
}}
defaults:
run:
working-directory: ${{ inputs.working-directory }}
steps:
- uses: actions/[email protected]
- name: Check .licenserc.yaml exists
id: licenserc-yaml
run: echo "exists=$([ -f .licenserc.yaml ] && echo "true" || echo "false")" >> $GITHUB_OUTPUT
- name: Get default license configuration
if: steps.licenserc-yaml.outputs.exists == 'false'
run: |
curl -fLsSo .licenserc.yaml https://raw.githubusercontent.com/canonical/operator-workflows/main/.github/files/.licenserc.yaml
- name: Check license headers
uses: apache/skywalking-eyes/header@main
with:
config: .licenserc.yaml
lib-check:
name: Check libraries
runs-on: >-
${{
inputs.self-hosted-runner &&
fromJson(format('[''self-hosted'', ''{0}'', ''{1}'', ''{2}'']',
inputs.self-hosted-runner-arch, inputs.self-hosted-runner-label, inputs.self-hosted-runner-image
)) || 'ubuntu-22.04'
}}
steps:
- uses: actions/[email protected]
- name: Check libs
if: ${{ !github.event.pull_request.head.repo.fork }}
uses: canonical/charming-actions/[email protected]
with:
credentials: ${{ secrets.CHARMHUB_TOKEN }}
github-token: ${{ secrets.GITHUB_TOKEN }}
charmcraft-channel: ${{ inputs.charmcraft-channel }}
required_status_checks:
name: Required Test Status Checks
needs:
- draft-publish-docs
- docker-lint
- inclusive-naming-check
- lib-check
- lint-and-unit-test
- metadata-lint
- shellcheck-lint
- license-headers-check
runs-on: >-
${{
inputs.self-hosted-runner &&
fromJson(format('[''self-hosted'', ''{0}'', ''{1}'', ''{2}'']',
inputs.self-hosted-runner-arch, inputs.self-hosted-runner-label, inputs.self-hosted-runner-image
)) || 'ubuntu-22.04'
}}
if: always() && !cancelled()
timeout-minutes: 5
steps:
- run: |
[ '${{ needs.draft-publish-docs.result }}' = 'success' ] || (echo draft-publish-docs failed && false)
[ '${{ needs.docker-lint.result }}' = 'success' ] || (echo docker-lint failed && false)
[ '${{ needs.inclusive-naming-check.result }}' = 'success' ] || (echo inclusive-naming-check failed && false)
[ '${{ needs.lib-check.result }}' = 'success' ] || (echo lib-check failed && false)
[ '${{ needs.lint-and-unit-test.result }}' = 'success' ] || (echo lint-and-unit-test failed && false)
[ '${{ needs.metadata-lint.result }}' = 'success' ] || (echo metadata-lint failed && false)
[ '${{ needs.shellcheck-lint.result }}' = 'success' ] || (echo shellcheck-lint failed && false)
[ '${{ needs.license-headers-check.result }}' = 'success' ] || (echo license-headers-check failed && false)