|
| 1 | +# pystemd E2E Testing Setup |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This setup provides comprehensive end-to-end testing for pystemd using mkosi and systemd-nspawn. Tests run in a real systemd environment inside a container to ensure accurate testing of systemd integration. |
| 6 | + |
| 7 | +## Quick Start |
| 8 | + |
| 9 | +```bash |
| 10 | +# Install dependencies (one-time setup) |
| 11 | +sudo dnf install mkosi systemd-container # Fedora/RHEL |
| 12 | +# or |
| 13 | +sudo apt install mkosi systemd-container # Debian/Ubuntu |
| 14 | + |
| 15 | +# generate keys if they dont exists |
| 16 | +mkosi genkey |
| 17 | + |
| 18 | +# build container, there steps do not need to be run as root. |
| 19 | +mkosi clean && mkosi build |
| 20 | + |
| 21 | +# Start container |
| 22 | +UNIT_NAME=pystemd-e2e-local.service |
| 23 | +MACHINE_NAME=pystemd-test-local |
| 24 | +sudo systemd-run --unit "$UNIT_NAME" --same-dir \ |
| 25 | + systemd-nspawn \ |
| 26 | + --machine="$MACHINE_NAME" \ |
| 27 | + --boot \ |
| 28 | + --directory=pystemd-test \ |
| 29 | + --bind-ro=`pwd`/e2e:/opt/pystemd/e2e |
| 30 | + |
| 31 | +# Run all E2E tests |
| 32 | +sudo systemd-run \ |
| 33 | + --machine="$MACHINE_NAME" \ |
| 34 | + --wait \ |
| 35 | + --pipe \ |
| 36 | + --setenv=PYSTEMD_E2E_CONTAINER=1\ |
| 37 | + --property=PrivateTmp=true \ |
| 38 | + -- \ |
| 39 | + /opt/pystemd/venv/bin/pytest \ |
| 40 | + -o cache_dir=/tmp/pytest_cache \ |
| 41 | + /opt/pystemd/e2e/ -v |
| 42 | +``` |
| 43 | + |
| 44 | + |
| 45 | +## How It Works |
| 46 | + |
| 47 | +### Manual Container Management |
| 48 | + |
| 49 | +The E2E testing workflow uses a straightforward approach: |
| 50 | + |
| 51 | +1. **Build the container image** using mkosi - this creates a Fedora environment with pystemd installed |
| 52 | +2. **Boot the container** using systemd-nspawn as a background service |
| 53 | +3. **Run tests inside the container** using `systemd-run --machine` |
| 54 | +4. **Stop the container** when done |
| 55 | + |
| 56 | +The container runs with `--boot` which starts a full systemd init inside, providing a realistic systemd environment for testing. |
| 57 | + |
| 58 | +### Container Boot Command |
| 59 | + |
| 60 | +The container is booted as a systemd service using systemd-nspawn: |
| 61 | + |
| 62 | +```bash |
| 63 | +UNIT_NAME=pystemd-e2e-local.service |
| 64 | +MACHINE_NAME=pystemd-test |
| 65 | + |
| 66 | +sudo systemd-run --unit "$UNIT_NAME" --same-dir \ |
| 67 | + systemd-nspawn \ |
| 68 | + --machine="$MACHINE_NAME" \ |
| 69 | + --boot \ |
| 70 | + --directory=pystemd-test \ |
| 71 | + --bind-ro=`pwd`/e2e:/opt/pystemd/e2e |
| 72 | +``` |
| 73 | + |
| 74 | +Tests are then executed inside the container using: |
| 75 | + |
| 76 | +```bash |
| 77 | +sudo systemd-run \ |
| 78 | + --machine="$MACHINE_NAME" \ |
| 79 | + --wait \ |
| 80 | + --pipe \ |
| 81 | + --setenv=PYSTEMD_E2E_CONTAINER=1 \ |
| 82 | + --property=PrivateTmp=true \ |
| 83 | + -- \ |
| 84 | + /opt/pystemd/venv/bin/pytest \ |
| 85 | + -o cache_dir=/tmp/pytest_cache \ |
| 86 | + /opt/pystemd/e2e/ -v |
| 87 | +``` |
| 88 | + |
| 89 | + |
| 90 | +## Test Suite |
| 91 | + |
| 92 | +Tests are located in the `e2e/` directory: |
| 93 | + |
| 94 | +- **`test_pystemd_run.py`** - Tests for `pystemd.run()` |
| 95 | +- **`test_manager.py`** - Tests for systemd Manager API |
| 96 | +- **`test_unit.py`** - Tests for Unit operations |
| 97 | +- **`test_transient_units.py`** - Tests for transient unit creation |
| 98 | +- **`test_dbus.py`** - Tests for D-Bus connections |
| 99 | + |
| 100 | +## Adding New Tests |
| 101 | + |
| 102 | +Create a new test file in the `e2e/` directory: |
| 103 | + |
| 104 | +```python |
| 105 | +# e2e/test_my_feature.py |
| 106 | +import pystemd.run |
| 107 | + |
| 108 | +def test_my_feature(): |
| 109 | + """Test my new feature""" |
| 110 | + unit = pystemd.run([b'/bin/echo', b'hello'], wait=True) |
| 111 | + assert unit.Service.ExecMainStatus == 0 |
| 112 | +``` |
| 113 | + |
| 114 | +```bash |
| 115 | +sudo systemd-run \ |
| 116 | + --machine="$MACHINE_NAME" \ |
| 117 | + --wait \ |
| 118 | + --pipe \ |
| 119 | + --setenv=PYSTEMD_E2E_CONTAINER=1 \ |
| 120 | + --property=PrivateTmp=true \ |
| 121 | + -- \ |
| 122 | + /opt/pystemd/venv/bin/pytest \ |
| 123 | + -o cache_dir=/tmp/pytest_cache \ |
| 124 | + /opt/pystemd/e2e/test_my_feature.py -v |
| 125 | +``` |
| 126 | + |
| 127 | + |
| 128 | +And run it using |
| 129 | + |
| 130 | +## CI/CD Integration |
| 131 | + |
| 132 | +The GitHub Actions workflow (`.github/workflows/e2e-tests.yml`) automates E2E testing: |
| 133 | + |
| 134 | +**Triggers:** |
| 135 | +- Pushes to `main` or `develop` branches |
| 136 | +- Pull requests targeting `main` |
| 137 | + |
| 138 | +**Matrix Testing:** |
| 139 | +- Tests across multiple Python versions: 3.11, 3.12, 3.13, 3.14, 3.14t (free-threaded) |
| 140 | +- Each Python version runs in its own container instance |
| 141 | + |
| 142 | + |
| 143 | +## Troubleshooting |
| 144 | + |
| 145 | +### Container fails to start |
| 146 | +```bash |
| 147 | +# Check if another container is running |
| 148 | +sudo machinectl list |
| 149 | + |
| 150 | +# SSH into the container |
| 151 | +sudo machinectl shell pystemd-test |
| 152 | + |
| 153 | +# Terminate stale container |
| 154 | +sudo machinectl terminate pystemd-test |
| 155 | +``` |
| 156 | + |
| 157 | +### Image not found |
| 158 | +```bash |
| 159 | +# Rebuild the mkosi image |
| 160 | +sudo mkosi --force build |
| 161 | +``` |
| 162 | + |
| 163 | +### Tests hang |
| 164 | +```bash |
| 165 | +# Check container status |
| 166 | +sudo machinectl status pystemd-test |
| 167 | + |
| 168 | +# View container logs |
| 169 | +sudo journalctl -M pystemd-test |
| 170 | +``` |
| 171 | + |
| 172 | +### Building with a different Python version |
| 173 | + |
| 174 | +By default, the container is built with Python 3.14. To build with a different Python version, set the `PYTHON_VERSION` environment variable before building: |
| 175 | + |
| 176 | +```bash |
| 177 | +# Build with Python 3.12 |
| 178 | +mkosi clean && mkosi -E PYTHON_VERSION=3.12 build |
| 179 | + |
| 180 | +# Build with Python 3.14 free-threaded |
| 181 | +mkosi clean && mkosi-E PYTHON_VERSION=3.14t build |
| 182 | +``` |
| 183 | + |
| 184 | +The `-E` flag passes the `PYTHON_VERSION` environment variable to mkosi, which is then used by `mkosi.build.chroot` to install the specified Python version using `uv python install`. |
| 185 | + |
0 commit comments