Skip to content

Commit 8ee38d0

Browse files
allisonkarlitskayamartinpitt
authored andcommitted
Rewrite integration test in pytest
Move it from tasks/ into test/ as it involves more than the tasks container, and could e.g. also grow integration tests for metrics. Also set up ruff and mypy static code checks, and add a pyproject.toml configuration for these.
1 parent 1316cd4 commit 8ee38d0

File tree

10 files changed

+849
-630
lines changed

10 files changed

+849
-630
lines changed

.github/workflows/tests.yml

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ jobs:
1010
- name: Install dependencies
1111
run: |
1212
sudo apt-get update
13-
sudo apt-get install -y make python3-pyflakes python3-pycodestyle
13+
sudo apt-get install -y make python3-pyflakes python3-pycodestyle python3-pip python3-pytest
14+
# `pip install .[test]` does not work properly on Ubuntu 22.04
15+
sudo pip install ruff mypy types-PyYAML
1416
15-
- name: Run unit tests
17+
- name: Run lint tests
1618
run: make check
1719

1820
tasks:
@@ -35,6 +37,13 @@ jobs:
3537
git config user.email [email protected]
3638
git rebase origin/main
3739
40+
- name: Install test dependencies
41+
run: |
42+
sudo apt-get update
43+
sudo apt-get install -y make python3-pip python3-pytest
44+
# `pip install .[test]` does not work properly on Ubuntu 22.04
45+
sudo pip install pytest_container
46+
3847
# HACK: Ubuntu 22.04 has podman 3.4, which isn't compatible with podman-remote 4 in our tasks container
3948
# This PPA is a backport of podman 4.3 from Debian 12; drop this when moving `runs-on:` to ubuntu-24.04
4049
- name: Update to newer podman
@@ -53,10 +62,16 @@ jobs:
5362
5463
- name: Build tasks container if it changed
5564
if: steps.containers_changed.outputs.tasks
56-
run: make tasks-container
65+
run: |
66+
make tasks-container
67+
# ensure the test uses the locally built one
68+
# pytest_container doesn't have a "pull on demand" mode, so pull all
69+
# of them and disable pulling in the test
70+
podman pull docker.io/rabbitmq quay.io/minio/minio quay.io/minio/mc
71+
echo "PULL_ALWAYS=0" >> "$GITHUB_ENV"
5772
5873
- name: Test local deployment
5974
run: |
60-
echo '${{ secrets.GITHUB_TOKEN }}' > ~/.config/github-token
75+
echo '${{ secrets.GITHUB_TOKEN }}' > github-token
6176
PRN=$(echo "$GITHUB_REF" | cut -f3 -d '/')
62-
tasks/run-local.sh -p $PRN -t ~/.config/github-token
77+
python3 -m pytest -vv --pr $PRN --pr-repository '${{ github.repository }}' --github-token=github-token

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ local-data/
66
tasks/credentials/**/*.key
77
tasks/credentials/**/*.pem
88
__pycache__
9+
port_check.lock

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ all:
66
check:
77
python3 -m pyflakes tasks tasks/container/webhook
88
python3 -m pycodestyle --max-line-length=120 --ignore=E722 tasks tasks/container/webhook
9+
if command -v mypy >/dev/null && pip show types-PyYAML >/dev/null; then mypy test; else echo "SKIP: mypy or types-PyYAML not installed"; fi
10+
if command -v ruff >/dev/null; then ruff check test; else echo "SKIP: ruff not installed"; fi
911

1012
TAG := $(shell date --iso-8601)
1113
TASK_SECRETS := /var/lib/cockpit-secrets/tasks

ansible/roles/webhook/tasks/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
dest: /run/cockpit-tasks-webhook.yaml
88
mode: preserve
99

10-
# keep this in sync with tasks/run-local.sh
10+
# keep this in sync with test/test_deployment.py
1111
- name: Generate flat files from RabbitMQ config map
1212
shell: |
1313
rm -rf /etc/rabbitmq

pyproject.toml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
[project]
2+
name = "cockpituous"
3+
description = "Cockpit CI infrastructure"
4+
classifiers = [
5+
"License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)"
6+
]
7+
8+
dependencies = [
9+
"pyyaml",
10+
]
11+
12+
version = "1"
13+
14+
[project.optional-dependencies]
15+
test = [
16+
"ruff",
17+
"mypy",
18+
"pytest",
19+
"pytest_container",
20+
"types-PyYAML",
21+
]
22+
23+
[tool.setuptools]
24+
packages = []
25+
26+
[tool.pytest.ini_options]
27+
addopts = "-m 'not shell'"
28+
markers = [
29+
"shell: interactive shell for development, skipped on normal runs",
30+
]
31+
32+
[tool.ruff]
33+
exclude = [
34+
".git/",
35+
]
36+
line-length = 118
37+
src = []
38+
39+
[tool.ruff.lint]
40+
select = [
41+
"A", # flake8-builtins
42+
"B", # flake8-bugbear
43+
"C4", # flake8-comprehensions
44+
"D300", # pydocstyle: Forbid ''' in docstrings
45+
"DTZ", # flake8-datetimez
46+
"E", # pycodestyle
47+
"EXE", # flake8-executable
48+
"F", # pyflakes
49+
"FBT", # flake8-boolean-trap
50+
"G", # flake8-logging-format
51+
"I", # isort
52+
"ICN", # flake8-import-conventions
53+
"ISC", # flake8-implicit-str-concat
54+
"PIE", # flake8-pie
55+
"PLE", # pylint errors
56+
"PGH", # pygrep-hooks
57+
"PT", # flake8-pytest-style
58+
"RSE", # flake8-raise
59+
"RUF", # ruff rules
60+
"T10", # flake8-debugger
61+
"TCH", # flake8-type-checking
62+
"UP032", # f-string
63+
"W", # warnings (mostly whitespace)
64+
"YTT", # flake8-2020
65+
]
66+
67+
[[tool.mypy.overrides]]
68+
module = ["pytest_container.*", "testinfra.*"]
69+
ignore_missing_imports = true

tasks/README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,33 +60,37 @@ Some helpful commands:
6060

6161
# Deploying locally for development, integration tests
6262

63-
For hacking on the webhook, task container, bots infrastructure,, or validating
63+
For hacking on the webhook, task container, bots infrastructure, or validating
6464
new container images, you can also run a [podman pod](http://docs.podman.io/en/latest/pod.html)
6565
locally with RabbitMQ, webhook, minio S3, and tasks containers.
6666
Without arguments this will run some purely local integration tests:
6767

68-
tasks/run-local.sh
68+
pytest
6969

70-
This will also generate the secrets in a temporary directory, unless they
71-
already exist in `tasks/credentials/`. By default this will use the
70+
This will also generate the secrets in a temporary directory.
71+
By default this will use the
7272
[`ghcr.io/cockpit-project/tasks:latest`](https://ghcr.io/cockpit-project/tasks)
73-
container, but you can run a different tag by setting `$TASKS_TAG`.
73+
container, but you can run a different image by setting `$TASKS_IMAGE`.
7474

7575
You can also test the whole GitHub → webhook → tasks → GitHub status workflow
76-
on some cockpituous PR with specifying the PR number and a GitHub token:
76+
on some cockpituous PR with specifying the PR number, your GitHub token, and
77+
optionally a non-default repository for testing against a fork:
7778

78-
tasks/run-local.sh -p 123 -t ~/.config/cockpit-dev/github-token
79+
pytest -vvsk test_real_pr --pr 123 --pr-repository yourfork/cockpituous --github-token=/home/user/.config/cockpit-dev/github-token
7980

8081
This will run tests-scan/tests-trigger on the given PR and trigger an
8182
[unit-tests](../.cockpit-ci/run) test which simply does `make check`.
8283

8384
You can get an interactive shell with
8485

85-
tasks/run-local.sh -i
86+
pytest -sm shell
8687

8788
to run things manually. For example, use `publish-queue` to inject a job into
8889
AMQP, or run `job-runner` or some bots command.
8990

91+
If you run the test multiple times, you can speed it up with `PULL_ALWAYS=0`
92+
which avoids re-pulling the container images.
93+
9094
# Running with toolbx
9195

9296
This container can also be used for local development with

tasks/mock-github

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22
# Mock GitHub API server for testing an opened PR or an issue for an image-refresh
3-
# You can run this manually in `tasks/run-local.sh -i` with `podman cp` and running
3+
# You can run this manually in `pytest -sm shell` with `podman cp` and running
44
# cd bots
55
# PYTHONPATH=. ./mock-github cockpit-project/bots $(git rev-parse HEAD) &
66
# export GITHUB_API=http://127.0.0.7:8443

0 commit comments

Comments
 (0)