Skip to content

Commit

Permalink
tests: Move container setup into fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-nitrokey committed Apr 30, 2024
1 parent 6f23f13 commit a90f719
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 112 deletions.
26 changes: 19 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import dataclass
from os import environ
from typing import Iterator, Literal
from typing import TYPE_CHECKING, Iterator, Literal

import pytest

Expand All @@ -14,6 +14,9 @@
UnattendedBootStatus,
)

if TYPE_CHECKING:
from utilities import Container


@dataclass
class UserData:
Expand Down Expand Up @@ -127,19 +130,28 @@ class Constants:


@pytest.fixture(scope="module")
def nethsm() -> Iterator[NetHSM]:
def container() -> Iterator["Container"]:
from utilities import KeyfenderManager

container = KeyfenderManager.get().spawn()
try:
container.start()
container.wait()
yield container
finally:
container.kill()


@pytest.fixture(scope="module")
def nethsm(container: "Container") -> Iterator[NetHSM]:
"""Start Docker container with Nethsm image and connect to Nethsm
This Pytest Fixture will run before the tests to provide the tests with
a nethsm instance via Docker container, also the first provision of the
NetHSM will be done in here"""

from utilities import connect, provision, start_nethsm

container = start_nethsm()
from utilities import connect, provision

with connect(Constants.ADMIN_USER) as nethsm:
provision(nethsm)
yield nethsm

container.kill()
18 changes: 3 additions & 15 deletions tests/test_nethsm_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import docker # type: ignore
import pytest
from conftest import Constants as C
from utilities import add_user, connect, lock, provision, start_nethsm, unlock
from utilities import Container, add_user, connect, lock, provision, unlock

import nethsm as nethsm_sdk
from nethsm import NetHSM
Expand All @@ -18,38 +18,26 @@


@pytest.fixture(scope="module")
def nethsm_no_provision() -> Iterator[NetHSM]:
def nethsm_no_provision(container: Container) -> Iterator[NetHSM]:
"""Start Docker container with Nethsm image and connect to Nethsm
This Pytest Fixture will run before the tests to provide the tests with
a nethsm instance via Docker container"""
container = start_nethsm()

with connect(C.ADMIN_USER) as nethsm:
yield nethsm

try:
container.kill()
except docker.errors.APIError:
pass


@pytest.fixture(scope="module")
def nethsm_no_provision_no_auth() -> Iterator[NetHSM]:
def nethsm_no_provision_no_auth(container: Container) -> Iterator[NetHSM]:
"""Start Docker container with Nethsm image and connect to Nethsm
This Pytest Fixture will run before the tests to provide the tests with
a nethsm instance via Docker container"""
container = start_nethsm()

with nethsm_sdk.connect(C.HOST, verify_tls=C.VERIFY_TLS) as nethsm:
yield nethsm

try:
container.kill()
except docker.errors.APIError:
pass


"""######################### Start of Tests #########################"""

Expand Down
28 changes: 14 additions & 14 deletions tests/test_nethsm_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
from conftest import Constants as C
from test_nethsm_keys import generate_key
from utilities import (
Container,
add_user,
connect,
encrypt_rsa,
provision,
set_backup_passphrase,
start_nethsm,
update,
)

Expand Down Expand Up @@ -73,21 +73,21 @@ def test_passphrase_add_user_retrieve_backup(nethsm: NetHSM) -> None:
assert False


def test_factory_reset(nethsm: NetHSM) -> None:
def test_factory_reset(container: Container, nethsm: NetHSM) -> None:
"""Perform a factory reset for a NetHSM instance.
This command requires authentication as a user with the Administrator
role."""
nethsm.factory_reset()
start_nethsm()
container.restart()

# make sure that we really cleared the data
assert nethsm.get_state().value == "Unprovisioned"
provision(nethsm)
assert nethsm.list_keys() == []

nethsm.factory_reset()
start_nethsm()
container.restart()


def test_state_restore(nethsm: NetHSM) -> None:
Expand Down Expand Up @@ -135,62 +135,62 @@ def test_state_restore(nethsm: NetHSM) -> None:
assert decrypt.decode().decode() == C.DATA


def test_state_provision_update(nethsm: NetHSM) -> None:
def test_state_provision_update(container: Container, nethsm: NetHSM) -> None:
"""Load an update to a NetHSM instance.
This command requires authentication as a user with the Administrator
role."""
start_nethsm()
container.restart()

provision(nethsm)

update(nethsm)


def test_state_provision_update_cancel_update(nethsm: NetHSM) -> None:
def test_state_provision_update_cancel_update(container: Container, nethsm: NetHSM) -> None:
"""Cancel a queued update on a NetHSM instance.
This command requires authentication as a user with the Administrator
role."""
start_nethsm()
container.restart()

provision(nethsm)

update(nethsm)
nethsm.cancel_update()


def test_update_commit_update(nethsm: NetHSM) -> None:
def test_update_commit_update(container: Container, nethsm: NetHSM) -> None:
"""Commit a queued update on a NetHSM instance.
This command requires authentication as a user with the Administrator
role."""
start_nethsm()
container.restart()

provision(nethsm)

update(nethsm)
nethsm.commit_update()


def test_provision_reboot(nethsm: NetHSM) -> None:
def test_provision_reboot(container: Container, nethsm: NetHSM) -> None:
"""Reboot a NetHSM instance.
This command requires authentication as a user with the Administrator
role."""
start_nethsm()
container.restart()

provision(nethsm)

nethsm.reboot()


def test_provision_shutdown(nethsm: NetHSM) -> None:
def test_provision_shutdown(container: Container, nethsm: NetHSM) -> None:
"""Shutdown a NetHSM instance.
This command requires authentication as a user with the Administrator
role."""
start_nethsm()
container.restart()

provision(nethsm)

Expand Down
Loading

0 comments on commit a90f719

Please sign in to comment.