File: conftest.py

package info (click to toggle)
python-yubihsm 3.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 452 kB
  • sloc: python: 4,882; makefile: 4
file content (61 lines) | stat: -rw-r--r-- 1,584 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from yubihsm import YubiHsm
from yubihsm.exceptions import YubiHsmDeviceError
from time import sleep
from functools import partial
from . import DEFAULT_KEY
import pytest
from typing import List


@pytest.fixture(scope="session")
def connect_hsm(pytestconfig):
    backend_uri = pytestconfig.getoption("backend")
    return partial(YubiHsm.connect, backend_uri)


@pytest.fixture(scope="module")
def hsm(connect_hsm):
    with connect_hsm() as hsm:
        yield hsm


@pytest.fixture(scope="module")
def info(hsm):
    return hsm.get_device_info()


@pytest.fixture(scope="module")
def session(hsm):
    with hsm.create_session_derived(1, DEFAULT_KEY) as session:
        yield session


_logged_version: List[bool] = []


@pytest.fixture(scope="module", autouse=True)
def _hsm_info(info, session, request):
    if not _logged_version:  # Run only once
        name = "YubiHSM "
        try:
            session.get_fips_status()
            name += "FIPS "
        except YubiHsmDeviceError:
            pass
        name += "v" + (".".join(str(v) for v in info.version))

        capmanager = request.config.pluginmanager.getplugin("capturemanager")
        with capmanager.global_and_fixture_disabled():
            print()
            print()
            print("ℹ️  Running tests on", name)
            print()
        _logged_version.append(True)


@pytest.fixture(autouse=True, scope="session")
def _reset_hsm(connect_hsm):
    with connect_hsm() as hsm:
        with hsm.create_session_derived(1, DEFAULT_KEY) as session:
            session.reset_device()
    sleep(5.0)