CI / lint (push) Skipped
CI / test (3.12) (push) Skipped
CI / test (3.13) (push) Skipped
160 lines
5.3 KiB
Python
160 lines
5.3 KiB
Python
from unittest.mock import MagicMock, patch
|
|
|
|
import aareDB
|
|
import pytest
|
|
import urllib3
|
|
from aarecommon.errors.exception_handler import AareDBCommunicationError
|
|
from aareDB.models.puck_with_tell_position import PuckWithTellPosition
|
|
from aareDB.models.sample import Sample
|
|
|
|
from aare.daq.aaredb import AareWrapper
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_bl():
|
|
bl = MagicMock()
|
|
bl.value = "X10SA"
|
|
bl.name = "X10SA"
|
|
return bl
|
|
|
|
|
|
@pytest.fixture
|
|
def wrapper(mock_bl):
|
|
with (
|
|
patch("aare.daq.aaredb.aareDB.ApiClient"),
|
|
patch("aare.daq.aaredb.aareDB.TellsRunnerApi") as tell_api,
|
|
):
|
|
w = AareWrapper(mock_bl)
|
|
w._tell_api = tell_api.return_value
|
|
yield w
|
|
|
|
|
|
def _puck(*sample_ids, pgroup="p12345"):
|
|
puck = MagicMock()
|
|
puck.pgroup = pgroup
|
|
puck.samples = [MagicMock(id=i, pgroup=pgroup) for i in sample_ids]
|
|
return puck
|
|
|
|
|
|
def test_find_loaded_sample_found(wrapper):
|
|
wrapper._tell_api.get_pucks_with_tell_position.return_value = [
|
|
_puck(1, 2),
|
|
_puck(3, pgroup="p67890"),
|
|
]
|
|
sample = wrapper.find_loaded_sample(3)
|
|
assert sample is not None
|
|
assert sample.pgroup == "p67890"
|
|
wrapper._tell_api.get_pucks_with_tell_position.assert_called_once_with(tell="X10SA")
|
|
|
|
|
|
def test_find_loaded_sample_missing(wrapper):
|
|
wrapper._tell_api.get_pucks_with_tell_position.return_value = [_puck(1, 2)]
|
|
assert wrapper.find_loaded_sample(99) is None
|
|
|
|
|
|
def test_find_loaded_sample_puck_without_samples(wrapper):
|
|
puck = MagicMock()
|
|
puck.samples = None
|
|
wrapper._tell_api.get_pucks_with_tell_position.return_value = [puck]
|
|
assert wrapper.find_loaded_sample(1) is None
|
|
|
|
|
|
def _real_sample(sample_id, pgroup):
|
|
return Sample(
|
|
id=sample_id,
|
|
pgroup=pgroup,
|
|
sample_name=f"s{sample_id}",
|
|
position=1,
|
|
data_collection_parameters=None,
|
|
)
|
|
|
|
|
|
def test_get_sample_info(wrapper):
|
|
puck = PuckWithTellPosition(
|
|
id=10,
|
|
puck_name="PUCK-A",
|
|
puck_type="unipuck",
|
|
pgroup="p67890",
|
|
dewar_name="DEWAR-1",
|
|
tell_position="B3",
|
|
samples=[_real_sample(5, "p67890"), _real_sample(6, "p67890")],
|
|
)
|
|
wrapper._tell_api.get_pucks_with_tell_position.return_value = [puck]
|
|
|
|
info = wrapper.get_sample_info(6)
|
|
assert info is not None
|
|
assert info.sample.id == 6
|
|
assert info.sample.pgroup == "p67890"
|
|
assert info.puck_name == "PUCK-A"
|
|
assert info.puck_pgroup == "p67890"
|
|
assert info.dewar_name == "DEWAR-1"
|
|
assert info.tell_position == "B3"
|
|
|
|
|
|
def test_get_sample_info_missing(wrapper):
|
|
wrapper._tell_api.get_pucks_with_tell_position.return_value = [_puck(1, 2)]
|
|
assert wrapper.get_sample_info(99) is None
|
|
|
|
|
|
def test_env_overrides_connection_settings(mock_bl, tmp_path, monkeypatch):
|
|
ca, crt, key = (tmp_path / n for n in ("ca.pem", "client.crt", "client.key"))
|
|
for f in (ca, crt, key):
|
|
f.write_text("x")
|
|
monkeypatch.setenv("AAREDB_HOST", "https://aaredb-test.example/dispatcher")
|
|
monkeypatch.setenv("AAREDB_CA_CERT", str(ca))
|
|
monkeypatch.setenv("AAREDB_CLIENT_CERT", str(crt))
|
|
monkeypatch.setenv("AAREDB_CLIENT_KEY", str(key))
|
|
with patch("aare.daq.aaredb.aareDB.ApiClient") as api_client:
|
|
AareWrapper(mock_bl)
|
|
cfg = api_client.call_args.args[0]
|
|
assert cfg.host == "https://aaredb-test.example/dispatcher"
|
|
assert cfg.ssl_ca_cert == str(ca)
|
|
assert cfg.cert_file == str(crt)
|
|
assert cfg.key_file == str(key)
|
|
assert cfg.verify_ssl is True
|
|
|
|
|
|
def test_default_paths_follow_beamline(mock_bl, monkeypatch):
|
|
for var in ("AAREDB_HOST", "AAREDB_CA_CERT", "AAREDB_CLIENT_CERT", "AAREDB_CLIENT_KEY"):
|
|
monkeypatch.delenv(var, raising=False)
|
|
with patch("aare.daq.aaredb.aareDB.ApiClient") as api_client:
|
|
AareWrapper(mock_bl)
|
|
cfg = api_client.call_args.args[0]
|
|
assert cfg.host == "https://mx-aaredb-dmz-01.psi.ch/dispatcher"
|
|
# No pinned bundle: urllib3 falls back to the OS trust store, which has the DigiCert roots
|
|
assert cfg.ssl_ca_cert is None
|
|
assert cfg.verify_ssl is True
|
|
assert cfg.cert_file == "/etc/ssl/certs/secrets/mx-x10sa-queue-01_from_dmz-01.crt"
|
|
assert cfg.key_file == "/etc/ssl/certs/secrets/mx-x10sa-queue-01_from_dmz-01.key"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"raised",
|
|
[
|
|
urllib3.exceptions.MaxRetryError(
|
|
None, "/x", reason=urllib3.exceptions.SSLError("bad cert")
|
|
),
|
|
urllib3.exceptions.SSLError("CERTIFICATE_VERIFY_FAILED"),
|
|
FileNotFoundError("/etc/ssl/certs/secrets/missing.pem"),
|
|
ConnectionRefusedError(),
|
|
],
|
|
)
|
|
def test_transport_errors_become_aaredb_communication_error(wrapper, raised):
|
|
wrapper._tell_api.get_pucks_with_tell_position.side_effect = raised
|
|
with pytest.raises(AareDBCommunicationError) as e:
|
|
wrapper.get_sample_info(1)
|
|
assert e.value.critical is True
|
|
assert e.value.base_url == "https://mx-aaredb-dmz-01.psi.ch/dispatcher"
|
|
assert "tell=X10SA" in e.value.endpoint
|
|
assert "Could not reach aareDB" in str(e.value)
|
|
|
|
|
|
def test_api_error_becomes_aaredb_communication_error(wrapper):
|
|
wrapper._tell_api.get_pucks_with_tell_position.side_effect = aareDB.ApiException(
|
|
status=401, reason="Unauthorized"
|
|
)
|
|
with pytest.raises(AareDBCommunicationError) as e:
|
|
wrapper.find_loaded_sample(1)
|
|
assert e.value.status_code == 401
|
|
assert "401" in str(e.value)
|