diff --git a/docker/adsim/.dockerignore b/docker/adsim/.dockerignore new file mode 100644 index 0000000..8fce603 --- /dev/null +++ b/docker/adsim/.dockerignore @@ -0,0 +1 @@ +data/ diff --git a/docker/adsim/.gitignore b/docker/adsim/.gitignore new file mode 100644 index 0000000..8fce603 --- /dev/null +++ b/docker/adsim/.gitignore @@ -0,0 +1 @@ +data/ diff --git a/docker/adsim/Dockerfile b/docker/adsim/Dockerfile new file mode 100644 index 0000000..a445c56 --- /dev/null +++ b/docker/adsim/Dockerfile @@ -0,0 +1,42 @@ +# The synApps image contains EPICS Base, ADCore R3-11, simDetector and HDF5 support. +FROM prjemian/synapps@sha256:1f0696e4262e50855197be1c73c2d7d16b645684e97814faad200c8d4008cc73 +USER root +SHELL ["/bin/bash", "-o", "pipefail", "-c"] +ARG ADZMQ_COMMIT=6c5334fd54830d93bc76cb3d8760c97001aa3024 + +# Pin the Debian release used by the base image rather than following stable. +RUN sed -i '/debian-security/d; s/stable/bullseye/g; s|http://deb.debian.org|https://deb.debian.org|g' /etc/apt/sources.list \ + && rm -rf /var/lib/apt/lists/* \ + && apt-get update \ + && apt-get install -y --no-install-recommends ca-certificates curl libzmq3-dev \ + && rm -rf /var/lib/apt/lists/* \ + && mkdir -p /opt/synApps/support/areaDetector-R3-11/ADZMQ \ + && curl --fail --location --retry 3 \ + "https://codeload.github.com/paulscherrerinstitute/ADZMQ/tar.gz/${ADZMQ_COMMIT}" \ + | tar -xz --strip-components=1 -C /opt/synApps/support/areaDetector-R3-11/ADZMQ + +# Link Debian's ZeroMQ rather than the bundled prebuilt library. +RUN cd /opt/synApps/support/areaDetector-R3-11/ADZMQ \ + && sed -i '/^LIB_INSTALLS_Linux/d' zmqApp/zmqSrc/Makefile \ + && sed -i 's/ADZMQ_LIBS += $(LIBZMQ) NDPlugin/ADZMQ_LIBS += NDPlugin\nADZMQ_SYS_LIBS += zmq/' zmqApp/src/Makefile \ + && sed -i 's/$(PROD_NAME)_LIBS += ADZMQ $(LIBZMQ)/$(PROD_NAME)_LIBS += ADZMQ\n$(PROD_NAME)_SYS_LIBS += zmq/' iocs/zmqIOC/zmqApp/src/Makefile \ + && cp /usr/include/zmq.h /usr/include/zmq_utils.h zmqApp/zmqSrc/ \ + && make -j2 +RUN printf '\nADZMQ=/opt/synApps/support/areaDetector-R3-11/ADZMQ\n' >> /opt/synApps/support/areaDetector-R3-11/ADSimDetector/iocs/simDetectorIOC/configure/RELEASE.local \ + && sed -i '/include $(ADCORE)\/ADApp\/commonDriverMakefile/i $(PROD_NAME)_DBD += ADZMQSupport.dbd\nPROD_LIBS += ADZMQ\nPROD_SYS_LIBS += zmq' /opt/synApps/support/areaDetector-R3-11/ADSimDetector/iocs/simDetectorIOC/simDetectorApp/src/Makefile \ + && cd /opt/synApps/support/areaDetector-R3-11/ADSimDetector/iocs/simDetectorIOC \ + && make -j2 + +COPY start-ioc.sh /usr/local/bin/start-adsim-ioc +COPY st.cmd /config/st.cmd +RUN chmod +x /usr/local/bin/start-adsim-ioc && mkdir -p /data +ENV PATH="/opt/base/bin/linux-x86_64:${PATH}" \ + EPICS_CA_SERVER_PORT=6064 \ + EPICS_CA_REPEATER_PORT=6065 \ + EPICS_CA_MAX_ARRAY_BYTES=10000000 +WORKDIR /opt/synApps/support/areaDetector-R3-11/ADSimDetector/iocs/simDetectorIOC/iocBoot/iocSimDetector +EXPOSE 6064/tcp 6064/udp 5555/tcp +HEALTHCHECK --interval=5s --timeout=4s --start-period=15s --retries=6 \ + CMD EPICS_CA_AUTO_ADDR_LIST=NO EPICS_CA_ADDR_LIST="127.0.0.1:${EPICS_CA_SERVER_PORT}" caget -w 2 BEC:SIM:cam1:PortName_RBV >/dev/null || exit 1 +ENTRYPOINT ["/usr/local/bin/start-adsim-ioc"] +CMD ["/opt/synApps/support/areaDetector-R3-11/ADSimDetector/iocs/simDetectorIOC/bin/linux-x86_64/simDetectorApp", "/config/st.cmd"] diff --git a/docker/adsim/README.md b/docker/adsim/README.md new file mode 100644 index 0000000..f3ab878 --- /dev/null +++ b/docker/adsim/README.md @@ -0,0 +1,91 @@ +# AreaDetector simulator + +This Docker image runs a local simDetector IOC at the `BEC:SIM:` PV prefix. It +provides two ROI/Stats chains, image transforms, ADZMQ preview and HDF5 capture. + +## Requirements + +- A running local Docker engine or Docker Desktop with Linux containers. The + image uses `linux/amd64`. On Apple Silicon, Docker Desktop must be able to run + amd64 containers through emulation. +- Network access during the first build to pull the base image and download + Debian packages and ADZMQ. +- For BEC integration, a local BEC environment and Redis. Install this + `ophyd_devices` checkout into the Python environment used by the device + server and use a BEC version with the companion ROI and signal metadata + interfaces. + +## Start the IOC + +Run these commands from the repository root: + +```bash +bash docker/adsim/setup.sh start +docker logs --tail 30 bec-adsim +docker inspect --format '{{.State.Health.Status}}' bec-adsim +``` + +Wait for the health status to become `healthy`. The default container is +`bec-adsim` and exposes Channel Access on localhost port 6064 (TCP and UDP) +and ZMQ on localhost port 5555. Acquisition starts idle and BEC can start live +acquisition when the camera connects. + +To build separately, run `bash docker/adsim/setup.sh build`. To reuse an +already built image, run `ADSIM_SKIP_BUILD=1 bash docker/adsim/setup.sh start`. +The script refuses to replace a container with the same name. + +## Connect a local BEC session + +1. To save HDF5 frames, set `ADSIM_DATA_DIR` to the absolute host directory + containing BEC's output paths **before** starting the IOC. Docker mounts + that directory at `/data`. With no override, it mounts `docker/adsim/data`. +2. Copy the [example camera config](../../ophyd_devices/configs/ad_camera_example.yaml) + to a local YAML file, such as `adsim.local.yaml`. Keep the `BEC:SIM:` prefix. + For HDF5 output, set `deviceConfig.hdf5_path_mapping` to map the same host + directory from step 1 to `/data`, for example + `{"/absolute/bec/data": "/data"}`. Set `deviceConfig.save_frame_data: false` + to run without saving frame files. +3. Start local Redis and the BEC services. Apply the simulator exports in + the environment that runs `bec-device-server`. For a local tmux setup, + use `bec-server start --interface tmux` and attach with `bec-server attach`. + In the device server pane, stop the device server with Ctrl+C and change + to the `ophyd_devices` repository root. With the BEC environment active, + restart it directly in that pane: + + ```bash + adsim_env=$(bash docker/adsim/setup.sh env) && + eval "$adsim_env" && + bec-device-server + ``` + + If the original device server used a custom service configuration, pass + the same `--config` path to `bec-device-server`. For a systemd-managed + device server, apply the generated EPICS values in its service environment + and restart the service. The exports add the IOC to the existing CA + discovery lists of the shell where they are evaluated. +4. Run `bec` from the repository root in a second terminal with the BEC + environment active. At the BEC prompt, add the camera to your local session: + + ```python + bec.config.add_to_session("./adsim.local.yaml") + dev.show_all() + ``` + +If a default port is occupied, set `ADSIM_CA_PORT` or `ADSIM_ZMQ_PORT` in +the environment. Use distinct host ports and export the same overrides +in the device server pane before running `setup.sh env` there. The CA port +must be in the range 1–65534 and cannot be 5555, which the IOC reserves for +ZMQ even when the host ZMQ port changes. +For a custom ZMQ port, set +`deviceConfig.preview_transports.preview[0].address` in your local YAML to +the URL printed as `ADSIM_ZMQ_ADDRESS`. + +## Stop the IOC + +```bash +bash docker/adsim/setup.sh stop +``` + +If you set `ADSIM_CONTAINER` to a custom name, use it again for `stop`. +The command removes the simulator container and keeps files in the mounted +host directory. Run `bash docker/adsim/setup.sh --help` for all options. diff --git a/docker/adsim/setup.sh b/docker/adsim/setup.sh new file mode 100755 index 0000000..e878986 --- /dev/null +++ b/docker/adsim/setup.sh @@ -0,0 +1,134 @@ +#!/usr/bin/env bash +# Reproduce the AD simulator locally; no Python environment or BEC service is modified. +set -euo pipefail + +adsim_script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" +adsim_action="${1:-start}" +adsim_image="${ADSIM_IMAGE:-bec-adsim-zmq:local}" +adsim_name="${ADSIM_CONTAINER:-bec-adsim}" +adsim_ca_port="${ADSIM_CA_PORT:-6064}" +adsim_zmq_port="${ADSIM_ZMQ_PORT:-5555}" +adsim_data_dir="${ADSIM_DATA_DIR:-${adsim_script_dir}/data}" + +usage() { + cat <<'HELP' +Usage: bash docker/adsim/setup.sh [start|build|env|stop] + + start Build and launch a new simulator; refuse to replace an existing container. + build Build the pinned linux/amd64 simulator image (also on Apple Silicon). + env Print additive shell exports; apply with: eval "$(bash docker/adsim/setup.sh env)" + stop Stop/remove only a container created by this script. + +Options are environment variables: + ADSIM_DATA_DIR Host directory mounted at /data; default: docker/adsim/data + For BEC, point this at its data root and configure + hdf5_path_mapping: {"/absolute/bec/data": "/data"} on the camera. + ADSIM_CONTAINER Container name, default bec-adsim + ADSIM_IMAGE Image tag, default bec-adsim-zmq:local + ADSIM_CA_PORT Channel Access TCP/UDP port, default 6064 (maximum 65534) + Port 5555 is reserved for the IOC's ZMQ listener. + ADSIM_ZMQ_PORT Host ZMQ port, default 5555 + For a custom port, set deviceConfig.preview_transports.preview[0].address + in the camera config to the URL printed as ADSIM_ZMQ_ADDRESS. + ADSIM_SKIP_BUILD Set to 1 to start an already-built image. + +The IOC exposes BEC:SIM:, two ROI/Stats pairs (2048 samples), Trans1:, image1:, +HDF1: and ZMQ1:. Acquisition starts stopped; BEC's live mode starts it. +Start Docker Desktop before building or launching. All ports bind to localhost. +HELP +} + +print_env() { + # Resolve existing lists when the printed commands are evaluated, not when + # generated. Keep gateways and automatic discovery, and append only if absent. + cat <<'ENV' +_adsim_append_address() { + local variable="$1" address="$2" current + eval 'current="${'"$variable"'-}"' + case " $(printf '%s' "$current" | tr '\t\r\n' ' ') " in + *" $address "*) + export "$variable=$current" + return + ;; + esac + export "$variable=${current:+$current }$address" +} +ENV + printf '_adsim_append_address EPICS_CA_NAME_SERVERS %q\n' "127.0.0.1:${adsim_ca_port}" + printf '_adsim_append_address EPICS_CA_ADDR_LIST %q\n' "127.0.0.1:${adsim_ca_port}" + printf 'unset -f _adsim_append_address\n' + cat <<'ENV' +if [[ -z "${EPICS_CA_MAX_ARRAY_BYTES:-}" ]] || + # Values below 10 MB have at most seven decimal digits after leading zeros. + [[ "$EPICS_CA_MAX_ARRAY_BYTES" =~ ^0*[0-9]{1,7}$ ]]; then + export EPICS_CA_MAX_ARRAY_BYTES=10000000 +fi +ENV + printf 'export ADSIM_ZMQ_ADDRESS=%q\n' "tcp://127.0.0.1:${adsim_zmq_port}" +} + +normalize_ca_port() { + if [[ "$adsim_ca_port" =~ ^0*([0-9]{1,5})$ ]]; then + adsim_ca_port=$((10#${BASH_REMATCH[1]})) + if ((adsim_ca_port == 5555)); then + echo 'ADSIM_CA_PORT cannot be 5555 (reserved for the IOC ZMQ listener).' >&2 + exit 2 + fi + if ((adsim_ca_port >= 1 && adsim_ca_port <= 65534)); then + return + fi + fi + echo 'ADSIM_CA_PORT must be a decimal port from 1 to 65534 (the repeater uses the next port).' >&2 + exit 2 +} + +case "${adsim_action}" in + -h|--help|help) usage; exit 0 ;; + env) normalize_ca_port; print_env; exit 0 ;; + start) normalize_ca_port ;; + build|stop) ;; + *) usage >&2; exit 2 ;; +esac + +command -v docker >/dev/null || { echo 'Docker is required.' >&2; exit 1; } +docker info >/dev/null + +build_image() { + docker build --platform linux/amd64 --tag "${adsim_image}" "${adsim_script_dir}" +} + +case "${adsim_action}" in + build) build_image ;; + start) + if docker container inspect "${adsim_name}" >/dev/null 2>&1; then + echo "Container ${adsim_name} already exists; choose ADSIM_CONTAINER or stop it explicitly." >&2 + exit 1 + fi + if [[ "${ADSIM_SKIP_BUILD:-0}" != 1 ]]; then build_image; fi + mkdir -p -- "${adsim_data_dir}" + adsim_data_dir="$(cd -- "${adsim_data_dir}" && pwd)" + # Docker parses --mount as CSV. Quote the entire source field. + adsim_mount_source="${adsim_data_dir//\"/\"\"}" + docker run --detach --init --interactive --tty --platform linux/amd64 \ + --name "${adsim_name}" --label org.bec-project.adsim=true \ + --env "EPICS_CA_SERVER_PORT=${adsim_ca_port}" \ + --env "EPICS_CA_REPEATER_PORT=$((adsim_ca_port + 1))" \ + --publish "127.0.0.1:${adsim_ca_port}:${adsim_ca_port}/tcp" \ + --publish "127.0.0.1:${adsim_ca_port}:${adsim_ca_port}/udp" \ + --publish "127.0.0.1:${adsim_zmq_port}:5555/tcp" \ + --mount "type=bind,\"source=${adsim_mount_source}\",target=/data" \ + "${adsim_image}" + printf '\nApply these exports in the BEC/device-server shell before starting it:\n' + print_env + printf '\nCheck startup with: docker logs %q\n' "${adsim_name}" + ;; + stop) + adsim_owned="$(docker inspect --format '{{index .Config.Labels "org.bec-project.adsim"}}' "${adsim_name}")" + if [[ "${adsim_owned}" != true ]]; then + echo "Refusing to stop ${adsim_name}: it was not created by this script." >&2 + exit 1 + fi + docker stop "${adsim_name}" + docker rm "${adsim_name}" + ;; +esac diff --git a/docker/adsim/st.cmd b/docker/adsim/st.cmd new file mode 100644 index 0000000..64239a3 --- /dev/null +++ b/docker/adsim/st.cmd @@ -0,0 +1,109 @@ +< envPaths +errlogInit(20000) +dbLoadDatabase("$(TOP)/dbd/simDetectorApp.dbd") +simDetectorApp_registerRecordDeviceDriver(pdbbase) +epicsEnvSet("PREFIX", "BEC:SIM:") +epicsEnvSet("EPICS_DB_INCLUDE_PATH", "$(ADCORE)/db") + +simDetectorConfig("SIM1", 512, 512, 1, 0, 0) +dbLoadRecords("$(ADSIMDETECTOR)/db/simDetector.template", "P=$(PREFIX),R=cam1:,PORT=SIM1,ADDR=0,TIMEOUT=1") + +NDTransformConfigure("TRANS1", 20, 0, "SIM1", 0, 0, 0, 0, 0, 2) +dbLoadRecords("NDTransform.template", "P=$(PREFIX),R=Trans1:,PORT=TRANS1,ADDR=0,TIMEOUT=1,NDARRAY_PORT=SIM1") + +NDROIConfigure("ROI1", 20, 0, "TRANS1", 0, 0, 0, 0, 0, 2) +dbLoadRecords("NDROI.template", "P=$(PREFIX),R=ROI1:,PORT=ROI1,ADDR=0,TIMEOUT=1,NDARRAY_PORT=TRANS1") + +NDStatsConfigure("STATS1", 20, 0, "ROI1", 0, 0, 0, 0, 0, 2) +dbLoadRecords("NDStats.template", "P=$(PREFIX),R=Stats1:,PORT=STATS1,ADDR=0,TIMEOUT=1,HIST_SIZE=256,XSIZE=512,YSIZE=512,NCHANS=2048,NDARRAY_PORT=ROI1") +NDTimeSeriesConfigure("STATS1_TS", 20, 0, "STATS1", 1, 23) +dbLoadRecords("NDTimeSeries.template", "P=$(PREFIX),R=Stats1:TS:,PORT=STATS1_TS,ADDR=0,TIMEOUT=1,NDARRAY_PORT=STATS1,NDARRAY_ADDR=1,NCHANS=2048,ENABLED=1") + +NDROIConfigure("ROI2", 20, 0, "TRANS1", 0, 0, 0, 0, 0, 2) +dbLoadRecords("NDROI.template", "P=$(PREFIX),R=ROI2:,PORT=ROI2,ADDR=0,TIMEOUT=1,NDARRAY_PORT=TRANS1") + +NDStatsConfigure("STATS2", 20, 0, "ROI2", 0, 0, 0, 0, 0, 2) +dbLoadRecords("NDStats.template", "P=$(PREFIX),R=Stats2:,PORT=STATS2,ADDR=0,TIMEOUT=1,HIST_SIZE=256,XSIZE=512,YSIZE=512,NCHANS=2048,NDARRAY_PORT=ROI2") +NDTimeSeriesConfigure("STATS2_TS", 20, 0, "STATS2", 1, 23) +dbLoadRecords("NDTimeSeries.template", "P=$(PREFIX),R=Stats2:TS:,PORT=STATS2_TS,ADDR=0,TIMEOUT=1,NDARRAY_PORT=STATS2,NDARRAY_ADDR=1,NCHANS=2048,ENABLED=1") + +NDStdArraysConfigure("IMAGE1", 5, 0, "TRANS1", 0) +dbLoadRecords("NDStdArrays.template", "P=$(PREFIX),R=image1:,PORT=IMAGE1,ADDR=0,TIMEOUT=1,NDARRAY_PORT=TRANS1,TYPE=Int8,FTVL=UCHAR,NELEMENTS=262144") + + +# Save the same IOC-transformed full images to HDF5 on explicit capture. +NDFileHDF5Configure("HDF1", 20, 0, "TRANS1", 0) +dbLoadRecords("NDFileHDF5.template", "P=$(PREFIX),R=HDF1:,PORT=HDF1,ADDR=0,TIMEOUT=1,XMLSIZE=2048,NDARRAY_PORT=TRANS1") + +# Publish IOC-transformed arrays directly over ZMQ. +NDZMQConfigure("ZMQ1", "tcp://*:5555", 5, 0, "TRANS1", 0, 0, 0, 0, 0) +dbLoadRecords("NDPluginBase.template", "P=$(PREFIX),R=ZMQ1:,PORT=ZMQ1,ADDR=0,TIMEOUT=1,NDARRAY_PORT=TRANS1,NDARRAY_ADDR=0") + +iocInit() +dbpf("$(PREFIX)Trans1:Type", "None") +dbpf("$(PREFIX)Trans1:EnableCallbacks", "1") +dbpf("$(PREFIX)cam1:AcquireTime", "0.1") +dbpf("$(PREFIX)cam1:AcquirePeriod", "0.2") +dbpf("$(PREFIX)cam1:ImageMode", "2") +dbpf("$(PREFIX)cam1:ArrayCallbacks", "1") +dbpf("$(PREFIX)ROI1:MinX", "100") +dbpf("$(PREFIX)ROI1:MinY", "100") +dbpf("$(PREFIX)ROI1:SizeX", "100") +dbpf("$(PREFIX)ROI1:SizeY", "100") +dbpf("$(PREFIX)ROI1:EnableX", "1") +dbpf("$(PREFIX)ROI1:EnableY", "1") +dbpf("$(PREFIX)ROI1:EnableCallbacks", "1") +dbpf("$(PREFIX)Stats1:ComputeStatistics", "1") +dbpf("$(PREFIX)Stats1:ComputeCentroid", "1") +dbpf("$(PREFIX)Stats1:EnableCallbacks", "1") +dbpf("$(PREFIX)ROI2:MinX", "300") +dbpf("$(PREFIX)ROI2:MinY", "300") +dbpf("$(PREFIX)ROI2:SizeX", "100") +dbpf("$(PREFIX)ROI2:SizeY", "100") +dbpf("$(PREFIX)ROI2:EnableX", "1") +dbpf("$(PREFIX)ROI2:EnableY", "1") +dbpf("$(PREFIX)ROI2:EnableCallbacks", "1") +dbpf("$(PREFIX)Stats2:ComputeStatistics", "1") +dbpf("$(PREFIX)Stats2:ComputeCentroid", "1") +dbpf("$(PREFIX)Stats2:EnableCallbacks", "1") +dbpf("$(PREFIX)image1:EnableCallbacks", "1") +dbpf("$(PREFIX)cam1:SimMode", "Sine") +dbpf("$(PREFIX)cam1:PeakStartX", "150") +dbpf("$(PREFIX)cam1:PeakStartY", "150") +dbpf("$(PREFIX)cam1:PeakNumX", "2") +dbpf("$(PREFIX)cam1:PeakNumY", "2") +dbpf("$(PREFIX)cam1:PeakStepX", "200") +dbpf("$(PREFIX)cam1:PeakStepY", "200") +dbpf("$(PREFIX)cam1:PeakWidthX", "15") +dbpf("$(PREFIX)cam1:PeakWidthY", "20") +dbpf("$(PREFIX)cam1:Gain", "40") +dbpf("$(PREFIX)cam1:Offset", "128") +dbpf("$(PREFIX)cam1:Noise", "0") +dbpf("$(PREFIX)cam1:PeakVariation", "0") +dbpf("$(PREFIX)cam1:GainX", "1") +dbpf("$(PREFIX)cam1:GainY", "1") +dbpf("$(PREFIX)cam1:XSineOperation", "Add") +dbpf("$(PREFIX)cam1:YSineOperation", "Add") +dbpf("$(PREFIX)cam1:XSine1Amplitude", "1") +dbpf("$(PREFIX)cam1:XSine1Frequency", "1.005") +dbpf("$(PREFIX)cam1:XSine1Phase", "0") +dbpf("$(PREFIX)cam1:XSine2Amplitude", "0") +dbpf("$(PREFIX)cam1:YSine1Amplitude", "0") +dbpf("$(PREFIX)cam1:YSine2Amplitude", "0") +dbpf("$(PREFIX)cam1:Reset", "1") +dbpf("$(PREFIX)HDF1:EnableCallbacks", "1") +dbpf("$(PREFIX)HDF1:FilePath", "/data/") +dbpf("$(PREFIX)HDF1:FileName", "adsim") +dbpf("$(PREFIX)HDF1:FileTemplate", "%s%s_%6.6d.h5") +dbpf("$(PREFIX)HDF1:AutoIncrement", "1") +dbpf("$(PREFIX)HDF1:AutoSave", "0") +dbpf("$(PREFIX)HDF1:FileWriteMode", "2") +dbpf("$(PREFIX)HDF1:NumCapture", "1") +dbpf("$(PREFIX)ZMQ1:EnableCallbacks", "1") +dbpf("$(PREFIX)cam1:Acquire", "0") + +dbpf("$(PREFIX)Trans1:BlockingCallbacks", "Yes") +dbpf("$(PREFIX)ROI1:BlockingCallbacks", "Yes") +dbpf("$(PREFIX)ROI2:BlockingCallbacks", "Yes") +dbpf("$(PREFIX)Stats1:BlockingCallbacks", "Yes") +dbpf("$(PREFIX)Stats2:BlockingCallbacks", "Yes") diff --git a/docker/adsim/start-ioc.sh b/docker/adsim/start-ioc.sh new file mode 100755 index 0000000..99843c3 --- /dev/null +++ b/docker/adsim/start-ioc.sh @@ -0,0 +1,19 @@ +#!/bin/sh +set -eu + +# libca otherwise forks caRepeater after the IOC has created ZeroMQ threads. +# That sequence can deadlock under Rosetta. Start it first and wait for its port. +/opt/base/bin/linux-x86_64/caRepeater & +repeater_pid=$! +repeater_port=$(printf '%04X' "${EPICS_CA_REPEATER_PORT:-5065}") +attempt=0 +while [ "$attempt" -lt 100 ]; do + if awk -v port=":$repeater_port" '$2 ~ port "$" { found=1 } END { exit !found }' /proc/net/udp /proc/net/udp6; then + exec "$@" + fi + kill -0 "$repeater_pid" 2>/dev/null || { echo 'caRepeater exited before binding' >&2; exit 1; } + attempt=$((attempt + 1)) + sleep 0.05 +done +echo 'Timed out waiting for caRepeater' >&2 +exit 1 diff --git a/tests/test_adsim_setup.py b/tests/test_adsim_setup.py new file mode 100644 index 0000000..7d49b99 --- /dev/null +++ b/tests/test_adsim_setup.py @@ -0,0 +1,137 @@ +"""Check simulator environment exports without Docker or EPICS.""" + +import csv +import os +import shutil +import subprocess +import tempfile +import unittest +from pathlib import Path + +SETUP = Path(__file__).resolve().parents[1] / "docker" / "adsim" / "setup.sh" + + +class TestADSimEnvironment(unittest.TestCase): + """Exports preserve existing discovery and are safe to apply repeatedly.""" + + def test_additive_exports(self) -> None: + """Exercise unset lists, gateways, exact duplicates and custom ports.""" + for shell in ("bash", "zsh"): + if shutil.which(shell) is None: + continue + for port in ("6064", "08080"): + address = f"127.0.0.1:{int(port)}" + for existing in ("", "gateway:5064 other:5064", f"gateway:5064\t{address}\n"): + for automatic in (None, "YES", "NO"): + with self.subTest( + shell=shell, port=port, existing=existing, auto=automatic + ): + env = { + key: value + for key, value in os.environ.items() + if not key.startswith("EPICS_CA_") + } + env.update(ADSIM_CA_PORT=port, SETUP=str(SETUP)) + commands = subprocess.check_output( + ["bash", str(SETUP), "env"], env=env, text=True + ) + # Supply existing values only at evaluation time. + env.update( + EXPORTS=commands, + EPICS_CA_ADDR_LIST=existing, + EPICS_CA_NAME_SERVERS=existing, + EPICS_CA_MAX_ARRAY_BYTES="20000000", + ) + if automatic is not None: + env["EPICS_CA_AUTO_ADDR_LIST"] = automatic + result = subprocess.check_output( + [ + shell, + "-c", + 'eval "$EXPORTS"; eval "$EXPORTS"; ' + 'printf "%s\\0" "$EPICS_CA_ADDR_LIST" ' + '"$EPICS_CA_NAME_SERVERS" ' + '"${EPICS_CA_AUTO_ADDR_LIST-unset}" ' + '"$EPICS_CA_MAX_ARRAY_BYTES"', + ], + env=env, + text=True, + ).split("\0") + expected = ( + existing + if address in existing.split() + else (f"{existing} {address}" if existing else address) + ) + self.assertEqual( + result[:4], [expected, expected, automatic or "unset", "20000000"] + ) + + def test_array_capacity_minimum(self) -> None: + """Set the required minimum without reducing an existing larger limit.""" + for previous in ("", "1000", "08000000", "10000000", "20000000", "020000000"): + env = dict(os.environ, EPICS_CA_MAX_ARRAY_BYTES=previous, SETUP=str(SETUP)) + result = subprocess.check_output( + [ + "bash", + "-c", + 'eval "$(bash "$SETUP" env)"; printf %s "$EPICS_CA_MAX_ARRAY_BYTES"', + ], + env=env, + text=True, + ) + expected = previous if int(previous or 0) >= 10000000 else "10000000" + self.assertEqual(result, expected) + + def test_data_mount_escapes_csv_source(self) -> None: + """Docker receives a single source field even when the path contains CSV syntax.""" + with tempfile.TemporaryDirectory() as directory: + root = Path(directory).resolve() + docker = root / "docker" + docker.write_text( + '#!/bin/sh\ncase "$1" in\n' + " info) exit 0 ;;\n" + " container) exit 1 ;;\n" + ' run) printf "%s\\n" "$@" ;;\n' + " *) exit 2 ;;\nesac\n" + ) + docker.chmod(0o755) + data = root / 'BEC, "data"' + env = dict(os.environ, PATH=f"{root}{os.pathsep}{os.environ['PATH']}") + env.update(ADSIM_DATA_DIR=str(data), ADSIM_SKIP_BUILD="1", ADSIM_CA_PORT="08080") + output = subprocess.check_output(["bash", str(SETUP), "start"], env=env, text=True) + args = output.splitlines() + self.assertIn("EPICS_CA_SERVER_PORT=8080", args) + self.assertIn("EPICS_CA_REPEATER_PORT=8081", args) + self.assertIn("127.0.0.1:8080:8080/tcp", args) + self.assertEqual( + next(csv.reader([args[args.index("--mount") + 1]])), + ["type=bind", f"source={data}", "target=/data"], + ) + + def test_invalid_ca_port_is_rejected_before_start(self) -> None: + for port in ("0", "invalid", "65535", "18446744073709559696"): + with self.subTest(port=port): + result = subprocess.run( + ["bash", str(SETUP), "start"], + env=dict(os.environ, ADSIM_CA_PORT=port), + capture_output=True, + text=True, + ) + self.assertEqual(result.returncode, 2) + self.assertIn("ADSIM_CA_PORT must be a decimal port from 1 to 65534", result.stderr) + + def test_ca_cannot_use_internal_zmq_port(self) -> None: + for action in ("start", "env"): + with self.subTest(action=action): + result = subprocess.run( + ["bash", str(SETUP), action], + env=dict(os.environ, ADSIM_CA_PORT="05555", ADSIM_ZMQ_PORT="5556"), + capture_output=True, + text=True, + ) + self.assertEqual(result.returncode, 2) + self.assertIn("ADSIM_CA_PORT cannot be 5555", result.stderr) + + +if __name__ == "__main__": + unittest.main()