Debian Codegraph @

tests/device/conftest.py

raw
1 from yubihsm import YubiHsm
2 from yubihsm.exceptions import YubiHsmDeviceError
3 from time import sleep
4 from functools import partial
5 from . import DEFAULT_KEY
6 import pytest
7 from typing import List
8
9
10 @pytest.fixture(scope="session")
11 def connect_hsm(pytestconfig):
12 backend_uri = pytestconfig.getoption("backend")
13 return partial(YubiHsm.connect, backend_uri)
14
15
16 @pytest.fixture(scope="module")
17 def hsm(connect_hsm):
18 with connect_hsm() as hsm:
19 yield hsm
20
21
22 @pytest.fixture(scope="module")
23 def info(hsm):
24 return hsm.get_device_info()
25
26
27 @pytest.fixture(scope="module")
28 def session(hsm):
29 with hsm.create_session_derived(1, DEFAULT_KEY) as session:
30 yield session
31
32
33 _logged_version: List[bool] = []
34
35
36 @pytest.fixture(scope="module", autouse=True)
37 def _hsm_info(info, session, request):
38 if not _logged_version: # Run only once
39 name = "YubiHSM "
40 try:
41 session.get_fips_status()
42 name += "FIPS "
43 except YubiHsmDeviceError:
44 pass
45 name += "v" + (".".join(str(v) for v in info.version))
46
47 capmanager = request.config.pluginmanager.getplugin("capturemanager")
48 with capmanager.global_and_fixture_disabled():
49 print()
50 print()
51 print("ℹ️ Running tests on", name)
52 print()
53 _logged_version.append(True)
54
55
56 @pytest.fixture(autouse=True, scope="session")
57 def _reset_hsm(connect_hsm):
58 with connect_hsm() as hsm:
59 with hsm.create_session_derived(1, DEFAULT_KEY) as session:
60 session.reset_device()
61 sleep(5.0)