Files
slsDetectorPackage/python/tests/test_det_api.py
AliceMazzoleni99 28b2aa9673
All checks were successful
Build on RHEL9 / build (push) Successful in 3m50s
Build on RHEL8 / build (push) Successful in 4m46s
Run Simulator Tests on local RHEL9 / build (push) Successful in 14m37s
Build on local RHEL9 / build (push) Successful in 1m26s
Run Simulator Tests on local RHEL8 / build (push) Successful in 16m57s
Build on local RHEL8 / build (push) Successful in 3m33s
Dev/add simulator tests in GitHub workflows (#1337)
* added simulator tests in github workflows

* indentation error

* typo

* debug

* Logging for debugging

* added more debug lines

* more debugging

* debug

* debug

* debug

* dont throw if process does not exist

* debug

* added absolute path to sls_detector commands

* some refactoring in test scripts

* added absolute path to all slsdet command

* typo

* ../tests/scripts/test_frame_synchronizer.py

* raise exception upon failure for github workflows

* removed hidden tags

* some refactoring in test scripts

* some refactoring

* fixed CMakeLists

* fixed unsuccesful merge

* updated python tests using simulators

* debug import error

* debug module import

* python -m runs module pytest as script - everything in path available

* removed integartion tests

* enable file write not to log files

* run tests without log files

* increased sleep time for udp packets

* added logg level variable to cmake

* added testing policies to documenattion

* disabled check for num_frames for jungfrau & xilinx

* set log level as cmake cached variable

* disable tests for jungfrau and xilinx_ctb

* check frames for HDF5

* updated Documentation of Testing

* changed withdetectorsimulators to detectorintegration

* replaced [.cmdcall] with [.detectorintegration]

* check_file_size only disabled for jungfrau - disable for all roi tests

* changed time to wait after receive to 5 ms

* take into account half modules of eiger

* num udp interfaces needs to be consistent across modules

* suppressed warning enclosing if

* config added 2 udp ports per default for moench and jungfrau

* write detector output to console

* allow jungfrau to tests num frames, remove unused variable (numinterfaces), add comment for future to handle traceback to know which calling function threw the files unmatched, added documentation for tests (examples for .detectoritnegration and how to disable marked tests, removed addditional argumetns to disable for test_simulator as one can just use ~, removed the check that checks for jungfrau checking number of frames at master attributes and at rx test, removed unused advanced_test_settings in test_simulator script, the num_mods check for multiple modules is removed and default num  modules set to 1 for test_simulator (to be increased later), back to raising exception for killprocess

* removed integration tests from cmakelists.txt and cmk.sh, modified the tests workflow command to reflect the disable argument and removed xilinx_ctb from test (fix fromdeveloper merge to be done)

* filtering by actual name for disable certain tests on github workflow

* minor refactor

* wip

* wip

* changes to run on local rh9 runner instead of github workfloa

* modified yml to remove some leftover from github workflow

* test

* fix build_dir in scripts (github workflow) and pytest dir in gitea workflow

* making the local machine use python3.13 binary

* pythonpath added

* changes for build_Dir back

* allowing ctb api tests

* allowed ctb api tests and set up slsdetname envt variable for shared memory being reserved just for these tests

* added rh8 workflow for local runner on gitea

* remnants from rh9 local runner

* remnants from rh9 local runner

* conda env for all shell for local runner

* allowing hdf5 to build on local runner

* run all tests for both the runners

* refactored fixtures a bit and merged some tests that use one session for entire server

* test fail

* test fix

* adding github workflow to test without data file checks and without logs

* documentation changes

* unnecessary import in conftest

* allowing the session_simulator to test for multiple modules and interfaces etc

* allow test_simulator script to run for 2 modules for all modules except ctb and xilinx ctb

* run upon push

* removing the disable file check on github workflow

* minor adjustment

* testing without synch

* reverting to previous

* with log file

* without the space

* summary from file and more error extracts from file to terminal

* minor

* trying nlf for more details

* updated with no log file to print everything to screen  also for det and rxr

* trying a no throw

* stoi was more about indent in yaml

* tries

* wip

* debug

* number of frames inconsistent fix=>just take first one, only test xilinx

* jungfrau tests without frames caught check

* extend the disable file check to everywhere that creates files

* specify path for test_simulator

* withoutprinting ==

* wip

* back with printing===, but not parsing file for errors anymore

* lang?

* wip

* safe log?

* wip2

* wip

* dont split error as its streaming live, just raise

* with log files

* lang?

* last resort

* wip

* test no det with general tests

* show tests live

* also include hidden integration tests

* without extra summary?

* revert

* last resort again

* tsquash on int64_t?

* tsquash on int64_t? mroe print

* writing to /tmp?

* all tests

* might be the fix?

* write to file

* fixed a few quiet mode no log file tests

* work on any branch for github tests, work on also release candidates for gitea tests

* added frame synchronizer tests to github workflow

* moved tests to run_tests.yaml from cmake.yaml

* documentation

* disabled general tests

---------

Co-authored-by: Dhanya Thattil <dhanya.thattil@psi.ch>
2026-02-03 11:45:12 +01:00

397 lines
14 KiB
Python

import pytest, sys, traceback
from pathlib import Path
current_dir = Path(__file__).resolve().parents[2]
scripts_dir = current_dir / "tests" / "scripts"
sys.path.append(str(scripts_dir))
print(sys.path)
from utils_for_test import (
Log,
LogLevel,
)
from slsdet import Detector
@pytest.mark.detectorintegration
def test_define_reg(session_simulator, request):
""" Test setting define_reg for ctb and xilinx_ctb."""
det_type, num_interfaces, num_mods, d = session_simulator
assert d is not None
from slsdet import RegisterAddress
if det_type in ['ctb', 'xilinx_ctb']:
prev_reg_defs = d.getRegisterDefinitions()
prev_bit_defs = d.getBitDefinitions()
d.clearRegisterDefinitions()
d.clearBitDefinitions()
addr1 = RegisterAddress(0x201)
addr2 = RegisterAddress(0x202)
d.define_reg(name="test_reg", addr=RegisterAddress(0x200)) # valid
d.define_reg(name="test_reg", addr=addr1) # takes a register address
d.define_reg(name="test_reg2", addr=0x202) # takes an int
# not using keyword arguments
with pytest.raises(TypeError) as exc_info:
d.define_reg("randomreg", 0x203)
# invalid value type
with pytest.raises(Exception) as exc_info:
d.define_reg(name="test_reg3", addr='0x203')
assert "addr must int or RegisterAddress" in str(exc_info.value)
# defining with duplicate value
with pytest.raises(Exception) as exc_info:
d.define_reg(name="test_reg3", addr=addr1)
assert "Value already assigned" in str(exc_info.value)
assert(d.getRegisterAddress("test_reg") == addr1)
assert(d.getRegisterName(addr1) == "test_reg")
# accessing non existent reg name
with pytest.raises(Exception) as exc_info:
d.reg['random_reg']
assert "No entry found for key" in str(exc_info.value)
# get non existing reg address
with pytest.raises(Exception) as exc_info:
d.getRegisterName(RegisterAddress(0x300))
assert "No entry found for value" in str(exc_info.value)
d.clearRegisterDefinitions()
d.setRegisterDefinitions(prev_reg_defs)
d.setBitDefinitions(prev_bit_defs)
else:
with pytest.raises(Exception) as exc_info:
d.define_reg(name="test_reg", addr=0x201)
assert "Register Definitions only for CTB" in str(exc_info.value)
Log(LogLevel.INFOGREEN, f"{request.node.name} passed")
@pytest.mark.detectorintegration
def test_define_bit(session_simulator, request):
""" Test setting define_bit for ctb and xilinx_ctb."""
det_type, num_interfaces, num_mods, d = session_simulator
assert d is not None
from slsdet import RegisterAddress, BitAddress
if det_type in ['ctb', 'xilinx_ctb']:
prev_reg_defs = d.getRegisterDefinitions()
prev_bit_defs = d.getBitDefinitions()
d.clearRegisterDefinitions()
d.clearBitDefinitions()
addr1 = RegisterAddress(0x201)
addr2 = RegisterAddress(0x202)
d.define_reg(name="test_reg1", addr=addr1)
d.define_reg(name="test_reg2", addr=addr2)
# not using keyword arguments
with pytest.raises(TypeError) as exc_info:
d.define_bit("randombit", 0x203, 1)
# invalid value type (bit=string)
with pytest.raises(ValueError) as exc_info:
d.define_bit(name="test_bit1", addr='test_reg1', bit_position='1')
# invalid bit_position
with pytest.raises(Exception) as exc_info:
d.define_bit(name="test_bit1", addr='test_reg1', bit_position=32)
assert "Bit position must be between 0 and 31" in str(exc_info.value)
# defining with random reg value
with pytest.raises(Exception) as exc_info:
d.define_bit(name='test_bit1', addr='random_reg', bit_position=1)
assert "No entry found for key" in str(exc_info.value)
bit1 = BitAddress(addr1, 2)
bit2 = BitAddress(addr1, 4)
bit3 = BitAddress(addr2, 3)
# defining bit address with bit_position as well
with pytest.raises(ValueError) as exc_info:
d.define_bit(name='test_bit1', addr=bit1, bit_position=1)
assert "bit_position must be None" in str(exc_info.value)
d.define_bit(name="test_bit1", addr='test_reg2', bit_position=1)
d.define_bit(name="test_bit1", addr='test_reg1', bit_position=1) # modify reg
d.define_bit(name='test_bit1', addr=bit1) # modify pos
d.define_bit(name="test_bit2", addr=0x201, bit_position=4) # int addr
d.define_bit(name="test_bit3", addr=addr2, bit_position=3) # RegisterAddress addr
assert(d.getBitAddress('test_bit1') == bit1)
assert(d.getBitAddress('test_bit2') == bit2)
assert(d.getBitAddress('test_bit3') == bit3)
assert(d.getBitAddress('test_bit1').address() == addr1)
assert(d.getBitAddress('test_bit1').bitPosition() == 2)
assert(d.getBitAddress('test_bit2') == BitAddress(addr1, 4))
assert(d.getBitName(bit1) == 'test_bit1')
assert(d.getBitName(bit2) == 'test_bit2')
assert(d.getBitName(bit3) == 'test_bit3')
assert(d.getBitName(BitAddress(addr2,3)) == 'test_bit3')
# bit doesnt exist for that reg
with pytest.raises(Exception) as exc_info:
d.getBitName(BitAddress(addr1, 5))
assert "No entry found for value" in str(exc_info.value)
# addr doesnt exist for that reg
with pytest.raises(Exception) as exc_info:
d.getBitName(BitAddress(RegisterAddress(0x300), 5))
assert "No entry found for value" in str(exc_info.value)
d.clearRegisterDefinitions()
d.clearBitDefinitions()
d.setRegisterDefinitions(prev_reg_defs)
d.setBitDefinitions(prev_bit_defs)
else:
with pytest.raises(Exception) as exc_info:
d.define_bit(name="test_bit", addr=0x300, bit_position=1)
assert "Bit Definitions only for CTB" in str(exc_info.value)
Log(LogLevel.INFOGREEN, f"{request.node.name} passed")
@pytest.mark.detectorintegration
def test_using_defined_reg_and_bit(session_simulator, request):
""" Test using defined reg and bit define_bit for ctb and xilinx_ctb."""
det_type, num_interfaces, num_mods, d = session_simulator
assert d is not None
from slsdet import RegisterAddress, BitAddress, RegisterValue
if det_type in ['ctb', 'xilinx_ctb']:
prev_reg_defs = d.getRegisterDefinitions()
prev_bit_defs = d.getBitDefinitions()
d.clearRegisterDefinitions()
d.clearBitDefinitions()
addr1 = RegisterAddress(0x201)
addr2 = RegisterAddress(0x202)
d.setRegisterDefinition('test_reg1', addr1)
d.setRegisterDefinition('test_reg2', addr2)
bit1 = BitAddress(addr1, 2)
bit2 = BitAddress(addr1, 4)
bit3 = BitAddress(addr2, 3)
d.setBitDefinition('test_bit1', bit1)
d.setBitDefinition('test_bit2', bit2)
d.setBitDefinition('test_bit3', bit3)
prev_val_addr1 = d.reg[addr1]
prev_val_addr2 = d.reg[addr2]
# reg name doesnt exist
with pytest.raises(Exception) as exc_info:
d.reg['random_reg']
assert "No entry found for key" in str(exc_info.value)
with pytest.raises(Exception) as exc_info:
d.setBit('random_reg')
assert "No entry found for key" in str(exc_info.value)
with pytest.raises(Exception) as exc_info:
d.clearBit('random_reg')
assert "No entry found for key" in str(exc_info.value)
with pytest.raises(Exception) as exc_info:
d.getBit('random_reg')
assert "No entry found for key" in str(exc_info.value)
# bit name doesnt exist
with pytest.raises(Exception) as exc_info:
d.setBit('test_bit1', bit_position=5)
assert "bit_position must be None" in str(exc_info.value)
with pytest.raises(Exception) as exc_info:
d.clearBit('test_bit1', bit_position=5)
assert "bit_position must be None" in str(exc_info.value)
with pytest.raises(Exception) as exc_info:
d.getBit('test_bit1', bit_position=5)
assert "bit_position must be None" in str(exc_info.value)
d.reg['test_reg1'] = RegisterValue(0x0)
assert(d.reg['test_reg1'].value() == 0x0)
d.reg['test_reg1'] = RegisterValue(0x10)
assert(d.reg['test_reg1'].value() == 0x10)
d.setBit('test_bit1')
assert(d.reg['test_reg1'].value() == 0x14) # 0x10 | (1 << 2)
d.clearBit('test_bit1')
assert(d.reg['test_reg1'].value() == 0x10)
assert(d.getBit('test_bit1') == 0)
# restore previous values
d.reg[addr1] = prev_val_addr1
d.reg[addr2] = prev_val_addr2
d.clearRegisterDefinitions()
d.clearBitDefinitions()
d.setRegisterDefinitions(prev_reg_defs)
d.setBitDefinitions(prev_bit_defs)
else:
with pytest.raises(Exception) as exc_info:
d.define_bit(name="test_bit", addr=0x300, bit_position=1)
assert "Bit Definitions only for CTB" in str(exc_info.value)
Log(LogLevel.INFOGREEN, f"{request.node.name} passed")
@pytest.mark.detectorintegration
def test_definelist_reg(session_simulator, request):
""" Test using definelist_reg for ctb and xilinx_ctb."""
det_type, num_interfaces, num_mods, d = session_simulator
assert d is not None
from slsdet import RegisterAddress, BitAddress, RegisterValue
if det_type in ['ctb', 'xilinx_ctb']:
prev_reg_defs = d.getRegisterDefinitions()
prev_bit_defs = d.getBitDefinitions()
d.clearRegisterDefinitions()
d.clearBitDefinitions()
addr1 = RegisterAddress(0x201)
addr2 = RegisterAddress(0x202)
bit1 = BitAddress(addr1, 2)
bit2 = BitAddress(addr1, 4)
bit3 = BitAddress(addr2, 3)
d.setRegisterDefinitions({
'test_reg1': RegisterAddress(0x201),
'test_reg2': RegisterAddress(0x202)
})
res = d.getRegisterDefinitions()
assert(res['test_reg1'] == addr1)
assert(res['test_reg2'] == addr2)
assert(len(res) == 2)
d.clearRegisterDefinitions()
d.clearBitDefinitions()
d.setRegisterDefinitions(prev_reg_defs)
d.setBitDefinitions(prev_bit_defs)
else:
with pytest.raises(Exception) as exc_info:
d.define_bit(name="test_bit", addr=0x300, bit_position=1)
assert "Bit Definitions only for CTB" in str(exc_info.value)
Log(LogLevel.INFOGREEN, f"{request.node.name} passed")
@pytest.mark.detectorintegration
def test_definelist_bit(session_simulator, request):
""" Test using definelist_bit for ctb and xilinx_ctb."""
det_type, num_interfaces, num_mods, d = session_simulator
assert d is not None
from slsdet import RegisterAddress, BitAddress, RegisterValue
if det_type in ['ctb', 'xilinx_ctb']:
prev_reg_defs = d.getRegisterDefinitions()
prev_bit_defs = d.getBitDefinitions()
d.clearRegisterDefinitions()
d.clearBitDefinitions()
addr1 = RegisterAddress(0x201)
addr2 = RegisterAddress(0x202)
bit1 = BitAddress(addr1, 2)
bit2 = BitAddress(addr1, 4)
bit3 = BitAddress(addr2, 3)
d.setRegisterDefinitions({
'test_reg1': RegisterAddress(0x201),
'test_reg2': RegisterAddress(0x202)
})
d.setBitDefinitions({
'test_bit1': BitAddress(addr1, 2),
'test_bit2': BitAddress(addr1, 4),
'test_bit3': BitAddress(addr2, 3)
})
res = d.getBitDefinitions()
assert(len(res) == 3)
assert(res['test_bit1'] == bit1)
assert(res['test_bit2'] == bit2)
assert(res['test_bit3'] == bit3)
assert(res['test_bit2'].address() == addr1)
assert(res['test_bit2'].bitPosition() == 4)
d.clearRegisterDefinitions()
d.clearBitDefinitions()
d.setRegisterDefinitions(prev_reg_defs)
d.setBitDefinitions(prev_bit_defs)
else:
with pytest.raises(Exception) as exc_info:
d.define_bit(name="test_bit", addr=0x300, bit_position=1)
assert "Bit Definitions only for CTB" in str(exc_info.value)
Log(LogLevel.INFOGREEN, f"{request.node.name} passed")
@pytest.mark.detectorintegration
def test_parameters_file(session_simulator, request):
""" Test using test_parameters_file."""
det_type, num_interfaces, num_mods, d = session_simulator
assert d is not None
with open("/tmp/params.det", "w") as f:
f.write("frames 2\n")
f.write("fwrite 1\n")
# this should not throw
d.parameters = "/tmp/params.det"
assert d.frames == 2
assert d.fwrite == 1
Log(LogLevel.INFOGREEN, f"✅ Test passed. Command: parameters")
@pytest.mark.detectorintegration
def test_include_file(session_simulator, request):
""" Test using test_include_file."""
det_type, num_interfaces, num_mods, d = session_simulator
assert d is not None
with open("/tmp/params.det", "w") as f:
f.write("frames 3\n")
f.write("fwrite 0\n")
# this should not throw
d.include = "/tmp/params.det"
assert d.frames == 3
assert d.fwrite == 0
Log(LogLevel.INFOGREEN, f"✅ Test passed. Command: include")
@pytest.mark.detectorintegration
def test_patternstart(session_simulator, request):
""" Test using patternstart for ctb, xilinx_ctb and mythen3."""
det_type, num_interfaces, num_mods, d = session_simulator
assert d is not None
if det_type in ['ctb', 'xilinx_ctb', 'mythen3']:
d.patternstart()
else:
with pytest.raises(Exception) as exc_info:
d.patternstart()
assert "not implemented" in str(exc_info.value)
Log(LogLevel.INFOGREEN, f"{request.node.name} passed")