Skip to content

Commit 9a758e3

Browse files
committed
tests: Add pytest and nushell based tests
I've been trying to keep this project in "one" programming language by writing even tests in Rust...but specifically for our integration tests it's pretty painful not just to compile them but have to deal with baking them into the base image. The tmt framework is very GHA like in that it scrapes the git source tree and copies it into the target environment, which works really well with scripts. Now, if you know me you know I am not a fan of dynamic programming languages like bash and Python. I'm one of those folks that actually tries to use Rust for things that feel like "scripts" i.e. they're *mostly* about forking external processes (see the xtask/ crate which uses "xshell"). Some of our testing code is in Rust too. However...there's a giant tension here because: - Iteration speed is very important for tests and scripts - The artifact being an architecture-dependent binary pushes us to inject it into container images; having the binary part of the bootc image under test conceptually forces us to reprovision for each test change, which is super expensive Most other people when faced with the testing challenge would just write shell scripts (or Python); that's definitely what tmt expects people to do. The podman project has a mix of a "bats" suite which is all bash based, and a Go-based framework. The thing is: bash is easy to mess up and has very little ability to do static analysis. Go (and Python) are very verbose for forking external processes. I've been using https://www.nushell.sh/ for my interactive shell for quite a while; I know just enough to get by day to day (but honestly sometimes I still type "bash" and run a few things there that I know how to express in bash but not nu) Anyways though, nushell has a lot of desirable properties for tests (which are basically scripts): - Architecture independent - Running an external process requires zero ceremony; it's the default! - But it *is* easy to e.g. scrape JSON from an external binary into a rich data structure - A decently rich standard library The downside is, it's a new language. And in the end, I'm not going to say it's the only way to write tests...maybe we do end up with some more bash. It wouldn't be the end of the world. But...after playing with this, I definitely like the result. OK, and after some debate we decided to add Python too, so this demos a pytest test. Signed-off-by: Colin Walters <[email protected]>
1 parent 4874db3 commit 9a758e3

File tree

9 files changed

+81
-8
lines changed

9 files changed

+81
-8
lines changed

.github/workflows/python.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: Python static analysis
2+
on: [push, pull_request]
3+
jobs:
4+
ruff:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v4
8+
- uses: chartboost/ruff-action@v1

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ test-tmt:
3838
validate:
3939
cargo fmt
4040
cargo clippy
41+
ruff check
4142
.PHONY: validate
4243

4344
vendor:

hack/provision-derived.sh

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
#!/bin/bash
22
set -xeu
3-
case "$1" in
3+
variant=$1
4+
# I'm a big fan of nushell for interactive use, and I want to support
5+
# using it in our test suite because it's better than bash. First,
6+
# enable EPEL to get it.
7+
. /usr/lib/os-release
8+
if echo $ID_LIKE $ID | grep -q centos; then
9+
dnf config-manager --set-enabled crb
10+
dnf -y install epel-release epel-next-release
11+
fi
12+
# Ensure this is pre-created
13+
mkdir -p -m 0700 /var/roothome
14+
mkdir -p ~/.config/nushell
15+
echo '$env.config = { show_banner: false, }' > ~/.config/nushell/config.nu
16+
touch ~/.config/nushell/env.nu
17+
dnf -y install nu
18+
# And we also add pytest, to support running tests written in Python
19+
dnf -y install python3-pytest
20+
case "$variant" in
421
tmt)
522
# tmt wants rsync
6-
dnf -y install cloud-init rsync && dnf clean all
23+
dnf -y install cloud-init rsync
724
ln -s ../cloud-init.target /usr/lib/systemd/system/default.target.wants
825
# And tmt wants to write to /usr/local/bin
926
rm /usr/local -rf && ln -sr /var/usrlocal /usr/local && mkdir -p /var/usrlocal/bin
@@ -14,3 +31,4 @@ case "$1" in
1431
echo "Unknown variant: $1" exit 1
1532
;;
1633
esac
34+
dnf clean all && rm /var/log/* -rf

plans/integration-run.fmf

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
# This tmt test just demonstrates local tmt usage.
2-
# We'll hopefully expand it to do more interesting things in the
3-
# future and unify with the other test plans.
1+
# Run this via `make test-tmt` which will build a container,
2+
# and a disk image from it.
43
provision:
54
how: virtual
6-
# Generated by `cargo xtask `
5+
# Generated by make test-tmt
76
image: file://./target/testvm/disk.qcow2
87
disk: 20
9-
summary: Basic smoke test
8+
summary: Execute booted tests
109
execute:
1110
how: tmt
12-
script: bootc status
11+
# There's currently two dynamic test frameworks; python and nushell.
12+
# python is well known and understood. nushell is less well known, but
13+
# is quite nice for running subprocesses and the like while making
14+
# it easy to parse JSON etc.
15+
script: |
16+
set -xeu
17+
pytest tests/booted/*.py
18+
ls tests/booted/*-test-*.nu |sort -n | while read t; do nu $t; done

tests/booted/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__

tests/booted/001-test-status.nu

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use std assert
2+
use tap.nu
3+
4+
tap begin "verify bootc status --json looks sane"
5+
6+
let st = bootc status --json | from json
7+
assert equal $st.apiVersion org.containers.bootc/v1alpha1
8+
tap ok

tests/booted/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Booted tests
2+
3+
These are intended to run via tmt; use e.g.
4+
`make test-tmt`.

tests/booted/basic.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Tests which are read-only/nondestructive
2+
3+
import json
4+
import subprocess
5+
6+
def run(*args):
7+
subprocess.check_call(*args)
8+
9+
def test_bootc_status():
10+
o = subprocess.check_output(["bootc", "status", "--json"])
11+
st = json.loads(o)
12+
assert st['apiVersion'] == 'org.containers.bootc/v1alpha1'

tests/booted/tap.nu

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# A simple nushell "library" for the
2+
# "Test anything protocol":
3+
# https://testanything.org/tap-version-14-specification.html
4+
export def begin [description] {
5+
print "TAP version 14"
6+
print $description
7+
}
8+
9+
export def ok [] {
10+
print "ok"
11+
}
12+
13+
export def fail [] {
14+
print "not ok"
15+
}

0 commit comments

Comments
 (0)