-
Notifications
You must be signed in to change notification settings - Fork 22
466 lines (448 loc) · 17.6 KB
/
test.yaml
File metadata and controls
466 lines (448 loc) · 17.6 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
# Copyright 2025 Canonical Ltd.
# See LICENSE file for licensing details.
name: Tests
on:
workflow_call:
inputs:
charm-directory:
type: string
description: The working directory for the charm
default: "."
charmcraft-channel:
description: Charmcraft channel to use
type: string
default: latest/stable
working-directory:
type: string
description: The working directory for jobs
default: "./"
python-version:
type: string
required: false
description: Python version to use when installing tox
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: "noble"
runs-on-base:
type: string
description: |
The type of machine to run the job on when not using self-hosted runners.
This is directly mapped to the `runs-on` field.
default: "ubuntu-22.04"
pre-run-script:
description: Path to the bash script to be run before the integration tests
type: string
vale-style-check:
description: Whether to enable vale style check.
type: boolean
default: false
vale-files:
description: A JSON-formatted list of glob patterns to run vale check against.
type: string
default: '["docs/**/*.md", "README.md", "CONTRIBUTING.md"]'
with-uv:
description: Whether to run the unit tests with tox-uv
type: boolean
required: false
default: false
require-check-lib:
description: Whether to run the check-libraries action.
type: boolean
required: false
default: true
concurrency:
group: operator-workflows-${{ github.workflow }}-${{ inputs.working-directory }}-tests-${{ github.ref }}-self-hosted-${{ inputs.self-hosted-runner }}-py${{ inputs.python-version }}-uv-${{ inputs.with-uv }}
cancel-in-progress: true
env:
CONCURRENCY_GROUP: operator-workflows-${{ inputs.working-directory }}-tests-${{ github.ref }}-self-hosted-${{ inputs.self-hosted-runner }}-py${{ inputs.python-version }}-uv-${{ inputs.with-uv }}
REPORT_NAME: "report"
jobs:
vale:
name: Style checker
if: ${{ inputs.vale-style-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
)) || inputs.runs-on-base
}}
defaults:
run:
shell: bash
working-directory: ${{ inputs.working-directory }}
steps:
- name: Checkout repo to runner
uses: actions/checkout@v6.0.2
- run: |
sudo apt-get update -y && sudo apt-get install -y tree ruby-rubygems docutils
- name: Install styles
uses: canonical/documentation-style-guide@main
- name: Resolve Vale file paths
id: resolve_vale_files
run: |
set -euo pipefail
shopt -s globstar nullglob
patterns='${{ inputs.vale-files }}'
files=$(
while IFS= read -r pattern; do
for f in $pattern; do
[[ -e "$f" ]] && realpath "$f"
done
done < <(jq -r '.[]' <<<"$patterns") \
| jq -R -s -c 'split("\n") | map(select(length>0))'
)
echo "Files identified for Vale check: $files"
echo "files=$files" >> "$GITHUB_OUTPUT"
- name: Run Vale tests
uses: vale-cli/vale-action@v2.1.1
with:
files: ${{ steps.resolve_vale_files.outputs.files }}
fail_on_error: ${{ inputs.vale-style-check }}
filter_mode: nofilter
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
)) || inputs.runs-on-base
}}
defaults:
run:
working-directory: ${{ inputs.shellcheck-working-directory || inputs.working-directory }}
steps:
- uses: actions/checkout@v6.0.2
- 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/shellcheck-problem-matchers@v2.1.0
- 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
)) || inputs.runs-on-base
}}
defaults:
run:
working-directory: ${{ inputs.working-directory }}
steps:
- uses: actions/checkout@v6.0.2
- 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
)) || inputs.runs-on-base
}}
defaults:
run:
working-directory: ${{ inputs.working-directory }}
steps:
- uses: actions/checkout@v6.0.2
- 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 }}" == "true" && "${{ 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
terraform-lint:
name: Terraform 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
)) || inputs.runs-on-base
}}
defaults:
run:
working-directory: ${{ inputs.working-directory }}
steps:
- uses: actions/checkout@v6.0.2
- name: Setup Tflint
uses: terraform-linters/setup-tflint@v6.2.2
with:
tflint_wrapper_enabled: true
- name: Run Tflint
run: |
tflint --version
tflint --init
tflint --recursive
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
)) || inputs.runs-on-base
}}
defaults:
run:
working-directory: ${{ inputs.working-directory }}
env:
python-version: >-
${{
inputs.python-version ||
inputs.self-hosted-runner &&
(
inputs.self-hosted-runner-image == 'noble' && '3.12' ||
inputs.self-hosted-runner-image == 'jammy' && '3.10' ||
inputs.self-hosted-runner-image == 'focal' && '3.8'
) ||
'3.10'
}}
steps:
- uses: actions/checkout@v6.0.2
- name: Pre-run script
if: ${{ inputs.pre-run-script != '' }}
run: bash -xe ${{ inputs.pre-run-script }}
# Test with tox and regular python
- name: Setup Python
if: ${{ ! inputs.with-uv }}
uses: actions/setup-python@v6.2.0
with:
python-version: ${{ env.python-version }}
- name: Install tox
if: ${{ ! inputs.with-uv }}
run: pip install tox
# Test with tox-uv and python setup by UV
- name: Setup Astral UV
if: ${{ inputs.with-uv }}
uses: astral-sh/setup-uv@v7
with:
python-version: ${{ env.python-version }}
- name: Install tox with UV
if: ${{ inputs.with-uv }}
run: uv tool install tox --with tox-uv
- 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: Export test report
if: always() && !cancelled()
uses: actions/github-script@v8.0.0
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: Name test Report
if: always() && github.event_name == 'pull_request' && !cancelled()
run: |
echo "REPORT_NAME=report-${CONCURRENCY_GROUP//\//_}" >> $GITHUB_ENV
- name: Upload coverage report
uses: actions/upload-artifact@v7
if: always() && github.event_name == 'pull_request' && !cancelled()
with:
name: ${{ env.REPORT_NAME }}
path: report.json
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
)) || inputs.runs-on-base
}}
defaults:
run:
working-directory: ${{ inputs.working-directory }}
steps:
- uses: actions/checkout@v6.0.2
- 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
)) || inputs.runs-on-base
}}
steps:
- uses: actions/checkout@v6.0.2
- name: Check libs
if: ${{ !github.event.pull_request.head.repo.fork && inputs.require-check-lib }}
uses: canonical/charming-actions/check-libraries@2.7.0
with:
credentials: ${{ secrets.CHARMHUB_TOKEN }}
github-token: ${{ secrets.GITHUB_TOKEN }}
charm-path: ${{ inputs.working-directory }}/${{ inputs.charm-directory }}
charmcraft-channel: ${{ inputs.charmcraft-channel }}
required_status_checks:
name: Required Test Status Checks
needs:
- docker-lint
- lib-check
- lint-and-unit-test
- metadata-lint
- shellcheck-lint
- terraform-lint
- license-headers-check
- vale
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
)) || inputs.runs-on-base
}}
if: always() && !cancelled()
timeout-minutes: 5
steps:
- run: |
[ '${{ needs.docker-lint.result }}' = 'success' ] || (echo docker-lint 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)
[ "${{ needs.vale.result }}" = "success" ] || [ "${{ needs.vale.result }}" = "skipped" ] || (echo "vale style check failed" && false)
[ "${{ needs.terraform-lint.result }}" = "success" ] || (echo terraform-lint failed && false)