mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-12-28 15:11:18 +01:00
updated python tests using simulators
This commit is contained in:
8
.github/workflows/run_tests.yaml
vendored
8
.github/workflows/run_tests.yaml
vendored
@@ -40,10 +40,10 @@ jobs:
|
||||
# Build your program with the given configuration
|
||||
run: cmake --build ${{github.workspace}}/build -j4 --config ${{env.BUILD_TYPE}}
|
||||
|
||||
#- name: Python unit tests
|
||||
#working-directory: ${{github.workspace}}/build/bin
|
||||
#run: |
|
||||
#python -m pytest ${{github.workspace}}/python/tests
|
||||
- name: Python unit tests with simulators
|
||||
working-directory: ${{github.workspace}}/build/bin
|
||||
run: |
|
||||
pytest -m withdetectorsimulators ${{github.workspace}}/python/tests --with-detector-simulators
|
||||
|
||||
- name: Simulator unit tests
|
||||
working-directory: ${{github.workspace}}/build/bin
|
||||
|
||||
@@ -10,8 +10,6 @@ scripts_dir = current_dir / "tests" / "scripts"
|
||||
|
||||
sys.path.append(str(scripts_dir))
|
||||
|
||||
print(sys.path)
|
||||
|
||||
from utils_for_test import (
|
||||
Log,
|
||||
LogLevel,
|
||||
@@ -20,6 +18,8 @@ from utils_for_test import (
|
||||
startDetectorVirtualServer,
|
||||
loadConfig,
|
||||
loadBasicSettings,
|
||||
connectToVirtualServers,
|
||||
DEFAULT_UDP_DST_PORTNO,
|
||||
)
|
||||
|
||||
def pytest_addoption(parser):
|
||||
@@ -39,47 +39,46 @@ def pytest_collection_modifyitems(config, items):
|
||||
item.add_marker(skip)
|
||||
|
||||
#helper fixture for servers
|
||||
@pytest.fixture
|
||||
def servers(request):
|
||||
@pytest.fixture(scope='module')
|
||||
def setup_parameters(request): # only setup once per module if same parameters used for the scopes
|
||||
try:
|
||||
return request.param # comes from @pytest.mark.parametrize(..., indirect=True)
|
||||
servers, nmods = request.param # comes from @pytest.mark.parametrize(..., indirect=True)
|
||||
return servers, nmods
|
||||
except AttributeError:
|
||||
# fallback default if the test did not parametrize
|
||||
return ['eiger', 'jungfrau', 'mythen3', 'gotthard2', 'ctb', 'moench', 'xilinx_ctb']
|
||||
return request.param
|
||||
return (['eiger', 'jungfrau', 'mythen3', 'gotthard2', 'ctb', 'moench', 'xilinx_ctb'], 2)
|
||||
|
||||
@pytest.fixture
|
||||
def test_with_simulators(servers):
|
||||
@pytest.fixture(scope='module')
|
||||
def test_with_simulators(setup_parameters):
|
||||
""" Fixture to automatically setup virtual detector servers for testing. """
|
||||
|
||||
LOG_PREFIX_FNAME = '/tmp/slsDetectorPackage_virtual_PythonAPI_test'
|
||||
MAIN_LOG_FNAME = LOG_PREFIX_FNAME + '_log.txt'
|
||||
fp = sys.stdout
|
||||
|
||||
with open(MAIN_LOG_FNAME, 'w') as fp:
|
||||
try:
|
||||
nmods = 2
|
||||
for server in servers:
|
||||
for ninterfaces in range(1,2):
|
||||
if ninterfaces == 2 and server != 'jungfrau' and server != 'moench':
|
||||
continue
|
||||
servers, nmods = setup_parameters
|
||||
print("servers:", servers)
|
||||
print("nmods:", nmods)
|
||||
try:
|
||||
for server in servers:
|
||||
for ninterfaces in range(1,2):
|
||||
if ninterfaces == 2 and server != 'jungfrau' and server != 'moench':
|
||||
continue
|
||||
|
||||
msg = f'Starting Python API Tests for {server}'
|
||||
msg = f'Starting Python API Tests for {server}'
|
||||
|
||||
if server == 'jungfrau' or server == 'moench':
|
||||
msg += f' with {ninterfaces} interfaces'
|
||||
if server == 'jungfrau' or server == 'moench':
|
||||
msg += f' with {ninterfaces} interfaces'
|
||||
|
||||
Log(LogLevel.INFOBLUE, msg, fp)
|
||||
cleanup(fp)
|
||||
startDetectorVirtualServer(server, nmods, fp)
|
||||
startReceiver(nmods, fp)
|
||||
d = loadConfig(name=server, log_file_fp=fp, num_mods=nmods, num_frames=1, num_interfaces=ninterfaces)
|
||||
loadBasicSettings(name=server, d=d, fp=fp)
|
||||
yield # run test
|
||||
cleanup(fp) # teardown
|
||||
except Exception as e:
|
||||
with open(MAIN_LOG_FNAME, 'a') as fp_error:
|
||||
traceback.print_exc(file=fp_error)
|
||||
Log(LogLevel.ERROR, f'Tests Failed.', fp)
|
||||
cleanup(fp)
|
||||
Log(LogLevel.INFOBLUE, msg, fp)
|
||||
cleanup(fp)
|
||||
startDetectorVirtualServer(server, nmods, fp)
|
||||
startReceiver(nmods, fp)
|
||||
d = loadConfig(name=server, log_file_fp=fp, num_mods=nmods, num_frames=1, num_interfaces=ninterfaces)
|
||||
#loadBasicSettings(name=server, d=d, fp=fp)
|
||||
yield # run test
|
||||
cleanup(fp) # teardown
|
||||
except Exception as e:
|
||||
traceback.print_exc(file=fp)
|
||||
Log(LogLevel.ERROR, f'Tests Failed.', fp)
|
||||
cleanup(fp)
|
||||
|
||||
|
||||
|
||||
@@ -7,6 +7,15 @@ Run this using: pytest -s test_free.py
|
||||
|
||||
import pytest, sys
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
current_dir = Path(__file__).resolve().parents[2]
|
||||
|
||||
scripts_dir = current_dir / "tests" / "scripts"
|
||||
|
||||
sys.path.append(str(scripts_dir))
|
||||
|
||||
|
||||
from slsdet import Detector, Ctb, freeSharedMemory
|
||||
from utils_for_test import (
|
||||
Log,
|
||||
@@ -46,7 +55,7 @@ def setup_simulator(det_config):
|
||||
cleanup(fp)
|
||||
|
||||
|
||||
|
||||
@pytest.mark.withdetectorsimulators
|
||||
def test_exptime_after_free_should_raise(setup_simulator):
|
||||
Log(LogLevel.INFOBLUE, f'\nRunning test_exptime_after_free_should_raise')
|
||||
|
||||
@@ -64,14 +73,11 @@ def test_exptime_after_free_should_raise(setup_simulator):
|
||||
assert str(exc_info.value) == "Shared memory is invalid or freed. Close resources before access."
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def free_and_create_shm():
|
||||
k = Ctb() # opens existing shm if it exists
|
||||
k.hostname = f"localhost:{SERVER_START_PORTNO}" # free and recreate shm, maps to local shm struct
|
||||
|
||||
|
||||
@pytest.mark.withdetectorsimulators
|
||||
def test_exptime_after_not_passing_var_should_raise(setup_simulator):
|
||||
Log(LogLevel.INFOBLUE, f'\nRunning test_exptime_after_not_passing_var_should_raise')
|
||||
|
||||
@@ -95,7 +101,7 @@ def free_and_create_shm_passing_ctb_var(k):
|
||||
k = Ctb() # opens existing shm if it exists (disregards k as its new Ctb only local to this function)
|
||||
k.hostname = f"localhost:{SERVER_START_PORTNO}" # free and recreate shm, maps to local shm struct
|
||||
|
||||
|
||||
@pytest.mark.withdetectorsimulators
|
||||
def test_exptime_after_passing_ctb_var_should_raise(setup_simulator):
|
||||
Log(LogLevel.INFOBLUE, f'\nRunning test_exptime_after_passing_ctb_var_should_raise')
|
||||
|
||||
@@ -118,7 +124,7 @@ def free_and_create_shm_returning_ctb():
|
||||
k.hostname = f"localhost:{SERVER_START_PORTNO}" # free and recreate shm, maps to local shm struct
|
||||
return k
|
||||
|
||||
|
||||
@pytest.mark.withdetectorsimulators
|
||||
def test_exptime_after_returning_ctb_should_raise(setup_simulator):
|
||||
Log(LogLevel.INFOBLUE, f'\nRunning test_exptime_after_returning_ctb_should_raise')
|
||||
|
||||
@@ -141,11 +147,7 @@ def test_exptime_after_returning_ctb_should_raise(setup_simulator):
|
||||
Log(LogLevel.INFOGREEN, f"✅ Test passed, exception was: {exc_info.value}")
|
||||
assert str(exc_info.value) == "Shared memory is invalid or freed. Close resources before access."
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@pytest.mark.withdetectorsimulators
|
||||
def test_hostname_twice_acess_old_should_raise(setup_simulator):
|
||||
Log(LogLevel.INFOBLUE, f'\nRunning test_hostname_twice_acess_old_should_raise')
|
||||
|
||||
|
||||
@@ -6,8 +6,8 @@ from conftest import test_with_simulators
|
||||
from slsdet import Detector
|
||||
|
||||
@pytest.mark.withdetectorsimulators
|
||||
@pytest.mark.parametrize("servers", [["moench"]], indirect=True)
|
||||
def test_rx_ROI_moench(test_with_simulators, servers):
|
||||
@pytest.mark.parametrize("setup_parameters", [(["moench"], 2)], indirect=True)
|
||||
def test_rx_ROI_moench(test_with_simulators, setup_parameters):
|
||||
""" Test setting and getting rx_ROI property of Detector class for moench. """
|
||||
|
||||
d = Detector()
|
||||
@@ -29,8 +29,8 @@ def test_rx_ROI_moench(test_with_simulators, servers):
|
||||
assert roi == [(-1,-1,-1,-1)]
|
||||
|
||||
@pytest.mark.withdetectorsimulators
|
||||
@pytest.mark.parametrize("servers", [["mythen3"]], indirect=True)
|
||||
def test_rx_ROI_mythen(test_with_simulators, servers):
|
||||
@pytest.mark.parametrize("setup_parameters", [(["mythen3"], 1)], indirect=True)
|
||||
def test_rx_ROI_mythen(test_with_simulators, setup_parameters):
|
||||
""" Test setting and getting rx_ROI property of Detector class for mythen. """
|
||||
|
||||
d = Detector()
|
||||
|
||||
@@ -48,7 +48,6 @@ def startTestsForAll(args, fp, advanced_test_settings=None):
|
||||
fname_template = LOG_PREFIX_FNAME + "_{}_{}.txt"
|
||||
|
||||
for server in args.servers:
|
||||
<<<<<<< HEAD
|
||||
for ninterfaces in range(1, 2): # always test both
|
||||
if ninterfaces == 2 and server != 'jungfrau' and server != 'moench':
|
||||
continue
|
||||
@@ -67,22 +66,6 @@ def startTestsForAll(args, fp, advanced_test_settings=None):
|
||||
runProcessWithLogFile('Tests (' + args.tests + ') for ' + server, cmd, fp, fname)
|
||||
except Exception as e:
|
||||
raise RuntimeException(f'Tests (' + args.tests + ') failed for {server}.') from e
|
||||
=======
|
||||
try:
|
||||
num_mods = 2 if server == 'eiger' else 1
|
||||
fname = CMD_TEST_LOG_PREFIX_FNAME + server + '.txt'
|
||||
cmd = ['tests', '--abort', args.markers, '-s']
|
||||
|
||||
Log(LogLevel.INFOBLUE, f'Starting Cmd Tests for {server}')
|
||||
cleanup(fp)
|
||||
startDetectorVirtualServer(name=server, num_mods=num_mods, fp=fp)
|
||||
startReceiver(num_mods, fp)
|
||||
d = loadConfig(name=server, rx_hostname=args.rx_hostname, settingsdir=args.settingspath, log_file_fp=fp, num_mods=num_mods)
|
||||
loadBasicSettings(name=server, d=d, fp=fp)
|
||||
runProcessWithLogFile('Cmd Tests (' + args.markers + ') for ' + server, cmd, fp, fname)
|
||||
except Exception as e:
|
||||
raise RuntimeException(f'Cmd Tests failed for {server}.') from e
|
||||
>>>>>>> 8e7921ae457a268ee6675435f215fb161c9ee1da
|
||||
|
||||
Log(LogLevel.INFOGREEN, 'Passed all tests for all detectors \n' + str(args.servers))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user