From 4afa6a35e27553826fee4f871e033bdc0ca0e908 Mon Sep 17 00:00:00 2001 From: smathis Date: Wed, 23 Jul 2025 08:48:51 +0200 Subject: [PATCH] Separated IOC startup from running tests --- ioc/st.cmd | 2 +- ioc/startioc | 12 ------------ ioc/{pystartioc.py => startioc.py} | 27 +++++++++++++++++++++------ maketestenv | 0 tests/conftest.py | 22 +++------------------- 5 files changed, 25 insertions(+), 38 deletions(-) delete mode 100755 ioc/startioc rename ioc/{pystartioc.py => startioc.py} (65%) mode change 100644 => 100755 maketestenv diff --git a/ioc/st.cmd b/ioc/st.cmd index 7cd36c8..1803999 100755 --- a/ioc/st.cmd +++ b/ioc/st.cmd @@ -10,7 +10,7 @@ require masterMacs, $(MASTERMACS_VERSION) # Motors # Initialize the motors itself -< motors/turboPmac1.cmd +#< motors/turboPmac1.cmd #< motors/masterMacs1.cmd # Create the test record which is used to detect if the IOC is running diff --git a/ioc/startioc b/ioc/startioc deleted file mode 100755 index 814fa6d..0000000 --- a/ioc/startioc +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -export EPICS_BASE=/usr/local/epics/base-7.0.7 -export EPICS_HOST_ARCH=RHEL8-x86_64 - -# Get the full directory path of the script -SCRIPT_DIR=$(dirname "$(realpath "$0")") - -# Needed so st.cmd finds other .cmd files -cd "$SCRIPT_DIR" || exit 1 - -# Run the IOC shell script -st.cmd diff --git a/ioc/pystartioc.py b/ioc/startioc.py similarity index 65% rename from ioc/pystartioc.py rename to ioc/startioc.py index d20923e..678fed0 100755 --- a/ioc/pystartioc.py +++ b/ioc/startioc.py @@ -7,6 +7,8 @@ import yaml import os import subprocess +import sys +from pathlib import Path def startioc(): @@ -38,14 +40,27 @@ def startioc(): out.write( f'epicsEnvSet("MASTERMACS1_PORT", "{config["controllers"]["masterMacs1"]["port"]}")\n') - # Start the IOC itself and keep it running in the background. - # We need to start a new session so Pytest doesn't kill the process - proc = subprocess.Popen([root_dir + 'ioc/startioc'], start_new_session=True) + # Set environment variables + os.environ["EPICS_BASE"] = "/usr/local/epics/base-7.0.7" + os.environ["EPICS_HOST_ARCH"] = "RHEL8-x86_64" - print(f"Started IOC with PID {proc.pid}") + # Get the full directory path of the script + script_dir = Path(__file__).resolve().parent + + # Change working directory so st.cmd can find other .cmd files + os.chdir(script_dir) + + print("Current working directory:", os.getcwd()) + print("Script directory:", script_dir) + print("Files in directory:", os.listdir()) + + # Run the IOC shell script + try: + subprocess.run(["/usr/local/bin/iocsh", "st.cmd"], check=True) + except subprocess.CalledProcessError as e: + print(f"Failed to start IOC: {e}", file=sys.stderr) + sys.exit(e.returncode) - # Yield control back to the test - # yield if __name__ == '__main__': startioc() diff --git a/maketestenv b/maketestenv old mode 100644 new mode 100755 diff --git a/tests/conftest.py b/tests/conftest.py index ca0b9fd..a25a02c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,24 +1,21 @@ import time from caproto.sync.client import read, write -from ioc.pystartioc import startioc import pytest from common import read_config -TIMEOUT_IOC_STARTUP = 10 TIMEOUT_READ = 2 @pytest.fixture(autouse=True) -def prepare_ioc(): +def check_ioc_running(): config = read_config() pvprefix = config['pvprefix'] """ - This function checks if the test IOC is already running and attempts to - start it, if that is not the case. + This function checks if the test IOC is already running. """ try: read(f'{pvprefix}:IOCREADY', timeout=TIMEOUT_READ) @@ -27,18 +24,5 @@ def prepare_ioc(): # is running return except TimeoutError: - # Received a timeout error -> Start the IOC - startioc() - - # Check periodically if the IOC started successfully - now = time.time() - while now + TIMEOUT_IOC_STARTUP > time.time(): - try: - read(f'{pvprefix}:IOCREADY', timeout=TIMEOUT_READ) - return - except TimeoutError: - time.sleep(0.5) - # IOC startup failed in the given time -> Raise an error - raise TimeoutError( - f'Starting the IOC within {TIMEOUT_IOC_STARTUP} seconds failed.') + raise Exception('Start the test IOC first ()')