diff --git a/.env.example b/.env.example index 8d79818..7676c0c 100644 --- a/.env.example +++ b/.env.example @@ -52,6 +52,14 @@ PANDA_AUTH=None # PANDA_QUEUES=PANDA_COMPOSE_LOCAL E1_BNL E1_JLAB PANDA_QUEUES=PANDA_COMPOSE_LOCAL +# Worker container image for the queue-level default. Concrete queues inherit +# the submitter block from the shared template queue, so this patches the +# template's containerImage before the harvester renders its panda_queues.cfg. +# Leave unset to keep the template default (alpine:latest). Jobs can still +# override per job with pandajob-submit --container IMAGE. +# PANDA_WORKER_IMAGE=myregistry/worker:latest +#PANDA_WORKER_IMAGE= + # ────────────────────────────────────────────── # JEDI task submission # ────────────────────────────────────────────── diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5b6cc29..8f939ae 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,6 +7,14 @@ on: branches: [main] jobs: + unit-tests: + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@v7 + - name: Run render-panda-queues unit tests + run: python3 -m unittest discover -s tests -v + smoke-test: runs-on: ubuntu-latest timeout-minutes: 30 @@ -19,6 +27,12 @@ jobs: with: ref: ${{ github.sha }} timeout: '300' + # Override the queue-default worker image for the queue-default + # test below: a small public image, deliberately different from + # the per-job image used by the first integration test + # (python:3.12-alpine), so each test proves its own path. + env-overrides: | + PANDA_WORKER_IMAGE=python:3.13-alpine - name: Verify Harvester is running run: | @@ -48,6 +62,32 @@ jobs: fi echo "Harvester is healthy." + - name: Verify rendered queue config (template inheritance) + run: | + # The harvester renders panda_queues.cfg from + # config/harvester/panda_queues.template.json via + # scripts/render-panda-queues.py. Concrete queues must inherit the + # submitter block (incl. containerImage) from the shared template + # queue through templateQueueName — this is what DockerSubmitter + # falls back to for jobs without a per-job container_name. + # This also checks the compose -> env -> render chain for + # PANDA_WORKER_IMAGE up front, in seconds, instead of letting a + # broken override surface only when the queue-default job below + # lands in the wrong container ~4 minutes later. + # Runs after the health wait: the file only appears once the + # harvester entrypoint finishes its setup and renders the config. + docker exec ${PANDA_COMPOSE_PROJECT}-harvester-1 \ + python3 -c " + import json + cfg = json.load(open('/var/lib/panda/panda_queues.cfg')) + template = cfg['panda-compose-local.template'] + assert template['submitter']['name'] == 'DockerSubmitter', template['submitter'] + assert template['submitter']['containerImage'] == 'python:3.13-alpine', template['submitter'] + queue = cfg['PANDA_COMPOSE_LOCAL'] + assert queue['templateQueueName'] == 'panda-compose-local.template', queue + print('rendered queue config OK; template containerImage =', template['submitter']['containerImage']) + " + - name: Set up Python uses: actions/setup-python@v7 with: @@ -62,8 +102,9 @@ jobs: # Use --container to specify a per-job container image, mirroring PanDA's # production container_name field. The DockerSubmitter reads this from # job.jobParams["container_name"] and prefers it over the queue-level default. - # python:3.12-alpine is deliberately different from the queue default (alpine:latest) - # to prove per-job container selection is working. + # python:3.12-alpine is deliberately different from the queue default + # (PANDA_WORKER_IMAGE=python:3.13-alpine in this run) to prove per-job + # container selection is working. JOB_ID=$(python3 $PANDA_COMPOSE_DIR/scripts/pandajob-submit \ --site PANDA_COMPOSE_LOCAL \ --container python:3.12-alpine \ @@ -149,6 +190,61 @@ jobs: exit 1 fi + - name: Integration test — queue-default container image + run: | + echo "=== Submitting job WITHOUT --container (queue default path) ===" + # The job above proves per-job container selection with --container. + # This one proves the queue-level default path: no container_name in + # the job spec, so DockerSubmitter must fall back to the + # containerImage inherited from the queue template + # (config/harvester/panda_queues.template.json), which for this run + # is overridden via PANDA_WORKER_IMAGE=python:3.13-alpine — a small + # public image, distinct from the per-job python:3.12-alpine, so + # each test proves its own path. + JOB_ID=$(python3 $PANDA_COMPOSE_DIR/scripts/pandajob-submit \ + --site PANDA_COMPOSE_LOCAL \ + --transformation sh \ + --output user.alice.ci.default-image.out.txt \ + --params "-c 'echo hello from queue-default image > user.alice.ci.default-image.out.txt; cat /etc/os-release | head -1'" \ + --name ci-queue-default-image-test) + echo "Submitted PanDA job ID: $JOB_ID" + if ! [[ "$JOB_ID" =~ ^[0-9]+$ ]]; then + echo "ERROR: expected integer job ID, got: $JOB_ID" + exit 1 + fi + + echo "=== Waiting for job to reach finished/failed (timeout 20 min) ===" + DEADLINE=$((SECONDS + 1200)) + while [[ $SECONDS -lt $DEADLINE ]]; do + JOB_STATUS=$(python3 $PANDA_COMPOSE_DIR/scripts/pandajob-status "$JOB_ID" \ + | python3 -c "import sys,json; print(json.load(sys.stdin)['jobStatus'])") + echo "[$(date -u '+%H:%M:%S')] Job status: $JOB_STATUS" + case "$JOB_STATUS" in + finished) break ;; + failed|cancelled) + echo "ERROR: job ended in terminal state '$JOB_STATUS'" + exit 1 ;; + esac + sleep 15 + done + if [[ "$JOB_STATUS" != "finished" ]]; then + echo "ERROR: job did not reach 'finished' within 20 minutes (last status: $JOB_STATUS)" + exit 1 + fi + + echo "=== Asserting the queue-default image was actually used ===" + # DockerSubmitter logs which image it picked per worker; prove the + # fallback went through the template (not a per-job override). + # The informational grep below is a pipeline whose status is + # tail's, so it must not be reused for the assertion: grep -q + # fails the step when the line is missing. + docker exec ${PANDA_COMPOSE_PROJECT}-harvester-1 \ + sh -c "grep 'using queue default container image=' /var/log/harvester/panda-docker_submitter.log | tail -1" + docker exec ${PANDA_COMPOSE_PROJECT}-harvester-1 \ + grep -q 'using queue default container image=python:3.13-alpine' /var/log/harvester/panda-docker_submitter.log \ + || { echo "ERROR: queue-default job did not use PANDA_WORKER_IMAGE"; exit 1; } + echo "OK: queue-default container path verified" + - name: Verify job output landed in Rucio run: | echo "=== RSE contents ===" @@ -168,14 +264,20 @@ jobs: 'http://localhost/dids/user.alice/dids/search?type=file') echo "$DIDS" - if [[ -z "$DIDS" || "$DIDS" == "[]" ]]; then - echo "ERROR: no output DIDs registered in Rucio scope user.alice" - echo "--- stager log ---" - docker exec ${PANDA_COMPOSE_PROJECT}-harvester-1 \ - tail -n 50 /var/log/harvester/panda-stager.log 2>/dev/null || true - exit 1 - fi - echo "OK: job output is registered in Rucio" + # Both integration jobs declare an output LFN; each must have been + # staged out and registered, otherwise the step passes on the first + # job's output alone. The search returns a bare JSON list of names, + # e.g. ["user.alice.ci.out.txt"]. + for LFN in user.alice.ci.out.txt user.alice.ci.default-image.out.txt; do + if ! grep -q "\"$LFN\"" <<<"$DIDS"; then + echo "ERROR: output DID $LFN not registered in Rucio scope user.alice" + echo "--- stager log ---" + docker exec ${PANDA_COMPOSE_PROJECT}-harvester-1 \ + tail -n 50 /var/log/harvester/panda-stager.log 2>/dev/null || true + exit 1 + fi + done + echo "OK: outputs of both jobs are registered in Rucio" - name: Dump all service logs if: always() diff --git a/.gitignore b/.gitignore index ecc495d..eba88a4 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ *.key docker-compose.override.yml .hermes/ +__pycache__/ diff --git a/README.md b/README.md index fba2f78..903b37b 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,8 @@ Static configuration is mounted read-only into the containers: The default queue `PANDA_COMPOSE_LOCAL` uses Harvester's `DockerSubmitter` — jobs run as Docker containers on the host Docker daemon. The default container image is -`alpine:latest`; override per job with `--container IMAGE`. +`alpine:latest`; override per job with `--container IMAGE`, or for all jobs on a +queue with the `PANDA_WORKER_IMAGE` environment variable (see `.env.example`). ## Relation to panda-k8s @@ -122,7 +123,7 @@ echo "Submitted PanDA job $JOB_ID" | `--site SITE` | PanDA compute site / queue name (e.g. `TEST_SITE`, `PANDA_COMPOSE_LOCAL`) | | `--transformation PATH` | Executable to run inside the worker container | | `--params STRING` | Arguments passed to the transformation (default: `""`) | -| `--container IMAGE` | Docker image for the job worker (overrides queue default `alpine:latest`) | +| `--container IMAGE` | Docker image for the job worker (overrides queue default; see `PANDA_WORKER_IMAGE`) | | `--script PATH` | Script path (must be accessible inside the worker container) | | `--name NAME` | Human-readable job name (default: `panda-compose-job`) | | `--cores N` | CPU cores requested (default: 1) | diff --git a/docker-compose.yml b/docker-compose.yml index 4ac1646..977c822 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -302,6 +302,7 @@ services: PANDA_VERIFY_HOST: "off" HARVESTER_EXPERIMENT: ${HARVESTER_EXPERIMENT:-wlcg} PANDA_QUEUES: ${PANDA_QUEUES:-PANDA_COMPOSE_LOCAL} + PANDA_WORKER_IMAGE: ${PANDA_WORKER_IMAGE:-} RUCIO_HOME: /opt/rucio RUCIO_CONFIG: /opt/rucio/etc/rucio.cfg http_proxy: ${HTTP_PROXY:-} diff --git a/scripts/render-panda-queues.py b/scripts/render-panda-queues.py index 3d2b584..df1d63b 100755 --- a/scripts/render-panda-queues.py +++ b/scripts/render-panda-queues.py @@ -45,6 +45,21 @@ def main(): if TEMPLATE_NAME not in config: sys.exit(f"template queue '{TEMPLATE_NAME}' missing from {template_path}") + # Queue-level worker image override. Concrete queues inherit the + # submitter block from the template queue via templateQueueName, so + # patching the template is enough. Without PANDA_WORKER_IMAGE the + # committed template default (alpine:latest) is kept. + worker_image = os.environ.get("PANDA_WORKER_IMAGE", "").strip() + if worker_image: + submitter = config[TEMPLATE_NAME].get("submitter") + if not isinstance(submitter, dict): + sys.exit( + f"submitter block missing from template queue '{TEMPLATE_NAME}' " + f"in {template_path}; cannot apply PANDA_WORKER_IMAGE={worker_image}" + ) + submitter["containerImage"] = worker_image + print(f"Queue worker image override: {worker_image}") + for name in names: config[name] = concrete_queue() diff --git a/tests/test_render_panda_queues.py b/tests/test_render_panda_queues.py new file mode 100644 index 0000000..0750507 --- /dev/null +++ b/tests/test_render_panda_queues.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python3 +"""Unit tests for scripts/render-panda-queues.py. + +Run with: python3 -m unittest discover -s tests -v +(or via pytest if available; stdlib-only so CI needs no extra deps) + +Covers the queue-template rendering contract: +- concrete queues are created for each PANDA_QUEUES entry +- concrete queues inherit the submitter block (incl. containerImage) from + the shared template queue via templateQueueName +- PANDA_WORKER_IMAGE patches the template's containerImage before rendering +- without PANDA_WORKER_IMAGE the committed template default is preserved +""" +import json +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path + +REPO_ROOT = Path(__file__).resolve().parent.parent +SCRIPT = REPO_ROOT / "scripts" / "render-panda-queues.py" +TEMPLATE = REPO_ROOT / "config" / "harvester" / "panda_queues.template.json" + +TEMPLATE_QUEUE = "panda-compose-local.template" + + +def run_render(env_extra, template=TEMPLATE): + """Run the render script in a temp env; return (stdout, rendered config).""" + with tempfile.TemporaryDirectory() as tmp: + out = Path(tmp) / "panda_queues.cfg" + # The env carries exactly what the script reads: the template and + # output paths plus PANDA_QUEUES and PANDA_WORKER_IMAGE. A minimal + # PATH is kept in case the script ever shells out. + env = { + "PATH": "/usr/bin:/bin", + "PANDA_QUEUES_TEMPLATE": str(template), + "PANDA_QUEUES_OUTPUT": str(out), + "PANDA_QUEUES": "PANDA_COMPOSE_LOCAL", + "PANDA_WORKER_IMAGE": "", + } + env.update(env_extra) + proc = subprocess.run( + [sys.executable, str(SCRIPT)], + env=env, + capture_output=True, + text=True, + check=True, + ) + return proc.stdout, json.loads(out.read_text()) + + +class TestRenderPandaQueues(unittest.TestCase): + def test_committed_template_has_submitter_container_image(self): + """The shipped template must carry the queue-default worker image. + + This is the value DockerSubmitter falls back to when a job has no + per-job container_name; if it is missing or malformed every job + without an override breaks. + """ + config = json.loads(TEMPLATE.read_text()) + submitter = config[TEMPLATE_QUEUE]["submitter"] + self.assertEqual(submitter["name"], "DockerSubmitter") + self.assertTrue( + submitter.get("containerImage"), "containerImage must not be empty" + ) + + def test_concrete_queue_created(self): + _, config = run_render({"PANDA_QUEUES": "PANDA_COMPOSE_LOCAL E1_BNL"}) + self.assertIn("PANDA_COMPOSE_LOCAL", config) + self.assertIn("E1_BNL", config) + for name in ("PANDA_COMPOSE_LOCAL", "E1_BNL"): + self.assertEqual(config[name]["queueStatus"], "online") + self.assertEqual(config[name]["templateQueueName"], TEMPLATE_QUEUE) + + def test_default_image_preserved_without_override(self): + """No PANDA_WORKER_IMAGE => the committed template default is kept.""" + template_default = json.loads(TEMPLATE.read_text())[TEMPLATE_QUEUE][ + "submitter" + ]["containerImage"] + _, config = run_render({"PANDA_WORKER_IMAGE": ""}) + self.assertEqual( + config[TEMPLATE_QUEUE]["submitter"]["containerImage"], + template_default, + ) + + def test_worker_image_override_applied(self): + """PANDA_WORKER_IMAGE patches the template's containerImage.""" + stdout, config = run_render( + {"PANDA_WORKER_IMAGE": "panda-compose/snakemake-worker:9.27.1"} + ) + self.assertEqual( + config[TEMPLATE_QUEUE]["submitter"]["containerImage"], + "panda-compose/snakemake-worker:9.27.1", + ) + self.assertIn("panda-compose/snakemake-worker:9.27.1", stdout) + + def test_worker_image_override_requires_submitter_block(self): + """PANDA_WORKER_IMAGE with a submitter-less template fails loudly.""" + with tempfile.TemporaryDirectory() as tmp: + bad = Path(tmp) / "template.json" + bad.write_text(json.dumps({TEMPLATE_QUEUE: {}})) + with self.assertRaises(subprocess.CalledProcessError) as ctx: + run_render({"PANDA_WORKER_IMAGE": "img:latest"}, template=bad) + self.assertIn("submitter block missing", ctx.exception.stderr) + + +if __name__ == "__main__": + unittest.main()