|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" |
| 5 | +cd "$SCRIPT_DIR" |
| 6 | + |
| 7 | +echo "=== Starting SlurmLedger Test Environment ===" |
| 8 | + |
| 9 | +# Start services and rebuild if source has changed |
| 10 | +docker compose up -d --build |
| 11 | + |
| 12 | +echo "Waiting for services to become healthy..." |
| 13 | +# Poll the DB healthcheck rather than sleeping a fixed interval |
| 14 | +for i in $(seq 1 30); do |
| 15 | + if docker compose exec -T db mysqladmin ping -h localhost --silent 2>/dev/null; then |
| 16 | + echo "Database is ready." |
| 17 | + break |
| 18 | + fi |
| 19 | + if [ "$i" -eq 30 ]; then |
| 20 | + echo "ERROR: Database did not become ready in time." >&2 |
| 21 | + docker compose logs db |
| 22 | + exit 1 |
| 23 | + fi |
| 24 | + sleep 2 |
| 25 | +done |
| 26 | + |
| 27 | +# Give cockpit-ws a moment to initialize after the DB is up |
| 28 | +sleep 3 |
| 29 | + |
| 30 | +# --------------------------------------------------------------------------- |
| 31 | +# Unit tests — run against the src/ Python modules directly. |
| 32 | +# src/ is bind-mounted at /usr/share/cockpit/slurmledger inside the container. |
| 33 | +# The unit tests live in test/unit/ on the host; copy them in at runtime so |
| 34 | +# pytest can discover them alongside the source they import. |
| 35 | +# --------------------------------------------------------------------------- |
| 36 | +echo "" |
| 37 | +echo "=== Running Python unit tests ===" |
| 38 | +docker compose exec -T slurmledger bash -c " |
| 39 | + PYTHONPATH=/usr/share/cockpit/slurmledger \ |
| 40 | + python3 -m pytest /opt/slurmledger-tests/unit -v --tb=short |
| 41 | +" || echo "WARN: Some unit tests failed (see output above)" |
| 42 | + |
| 43 | +# --------------------------------------------------------------------------- |
| 44 | +# Integration test — exercise slurmdb.py against the real MariaDB container. |
| 45 | +# Uses --conf to read connection details from the mounted slurmdbd.conf, and |
| 46 | +# --cluster localcluster to match the table prefix in the test fixture. |
| 47 | +# --------------------------------------------------------------------------- |
| 48 | +echo "" |
| 49 | +echo "=== Testing slurmdb.py against real database ===" |
| 50 | +docker compose exec -T slurmledger python3 /usr/share/cockpit/slurmledger/slurmdb.py \ |
| 51 | + --conf /etc/slurm/slurmdbd.conf \ |
| 52 | + --cluster localcluster \ |
| 53 | + --start 2024-01-01 --end 2025-12-31 \ |
| 54 | + --output - \ |
| 55 | + | python3 -m json.tool | head -40 |
| 56 | + |
| 57 | +# --------------------------------------------------------------------------- |
| 58 | +# Invoice generation smoke test |
| 59 | +# --------------------------------------------------------------------------- |
| 60 | +echo "" |
| 61 | +echo "=== Testing invoice generation ===" |
| 62 | +echo '{ |
| 63 | + "invoice_number": "TEST-001", |
| 64 | + "date": "2026-03-29", |
| 65 | + "items": [ |
| 66 | + {"description": "CPU Hours (March 2026)", "qty": 1000, "rate": 0.02, "amount": 20.00} |
| 67 | + ], |
| 68 | + "institution": { |
| 69 | + "institutionName": "Test University", |
| 70 | + "streetAddress": "123 Research Drive", |
| 71 | + "city": "Testville", |
| 72 | + "state": "CA", |
| 73 | + "postalCode": "99999" |
| 74 | + }, |
| 75 | + "bank_info": ["Test Bank — ACH routing 000000000"], |
| 76 | + "notes": "Net 30. Test invoice — not for production use.", |
| 77 | + "subtotal": 20.00, |
| 78 | + "total_due": 20.00 |
| 79 | +}' | docker compose exec -T slurmledger \ |
| 80 | + python3 /usr/share/cockpit/slurmledger/invoice.py > /dev/null \ |
| 81 | + && echo "Invoice generation: OK" \ |
| 82 | + || echo "Invoice generation: FAILED" |
| 83 | + |
| 84 | +# --------------------------------------------------------------------------- |
| 85 | +# Balance enforcer smoke test — no allocations will be overdrawn in the test |
| 86 | +# fixture, so we expect a clean exit or a "no allocations" message. |
| 87 | +# --------------------------------------------------------------------------- |
| 88 | +echo "" |
| 89 | +echo "=== Testing balance enforcer (dry-run) ===" |
| 90 | +docker compose exec -T slurmledger \ |
| 91 | + python3 /usr/share/cockpit/slurmledger/balance_enforcer.py \ |
| 92 | + --check --json 2>&1 \ |
| 93 | + || echo "(Expected: no allocations configured or scontrol unavailable in test env)" |
| 94 | + |
| 95 | +# --------------------------------------------------------------------------- |
| 96 | +# Summary |
| 97 | +# --------------------------------------------------------------------------- |
| 98 | +echo "" |
| 99 | +echo "=== Test Environment Ready ===" |
| 100 | +echo "Cockpit UI: http://localhost:9090" |
| 101 | +echo " Log in with any system user account created inside the container, or" |
| 102 | +echo " create one with: docker compose exec slurmledger useradd -m -p \$(openssl passwd -1 testpass) testuser" |
| 103 | +echo "" |
| 104 | +echo "To stop and remove containers:" |
| 105 | +echo " docker compose -f test/docker-compose.yml down" |
| 106 | +echo "" |
| 107 | +echo "To tail application logs:" |
| 108 | +echo " docker compose -f test/docker-compose.yml logs -f slurmledger" |
0 commit comments