Files
PrimAITE/tests/test_resetting_node.py

87 lines
2.9 KiB
Python
Raw Normal View History

# © Crown-owned copyright 2023, Defence Science and Technology Laboratory UK
2023-06-02 14:54:23 +01:00
"""Used to test Active Node functions."""
import pytest
from primaite.common.enums import FileSystemState, HardwareState, NodeType, Priority, SoftwareState
2023-06-02 16:13:16 +01:00
from primaite.common.service import Service
2023-06-12 16:59:31 +01:00
from primaite.config.training_config import TrainingConfig
2023-06-02 14:54:23 +01:00
from primaite.nodes.active_node import ActiveNode
from primaite.nodes.service_node import ServiceNode
@pytest.mark.parametrize(
"starting_operating_state, expected_operating_state",
2023-06-27 13:06:10 +01:00
[(HardwareState.RESETTING, HardwareState.ON)],
2023-06-02 14:54:23 +01:00
)
def test_node_resets_correctly(starting_operating_state, expected_operating_state):
2023-06-27 13:06:10 +01:00
"""Tests that a node resets correctly."""
2023-06-02 14:54:23 +01:00
active_node = ActiveNode(
2023-06-27 13:06:10 +01:00
node_id="0",
name="node",
node_type=NodeType.COMPUTER,
priority=Priority.P1,
hardware_state=starting_operating_state,
ip_address="192.168.0.1",
software_state=SoftwareState.COMPROMISED,
file_system_state=FileSystemState.CORRUPT,
config_values=TrainingConfig(),
2023-06-02 14:54:23 +01:00
)
for x in range(5):
active_node.update_resetting_status()
2023-06-02 16:13:16 +01:00
assert active_node.software_state == SoftwareState.GOOD
assert active_node.file_system_state_actual == FileSystemState.GOOD
2023-06-02 14:54:23 +01:00
assert active_node.hardware_state == expected_operating_state
2023-06-27 13:06:10 +01:00
2023-06-02 14:54:23 +01:00
@pytest.mark.parametrize(
"operating_state, expected_operating_state",
2023-06-27 13:06:10 +01:00
[(HardwareState.BOOTING, HardwareState.ON)],
2023-06-02 14:54:23 +01:00
)
def test_node_boots_correctly(operating_state, expected_operating_state):
2023-06-27 13:06:10 +01:00
"""Tests that a node boots correctly."""
2023-06-02 16:13:16 +01:00
service_node = ServiceNode(
2023-06-27 13:06:10 +01:00
node_id=0,
name="node",
node_type="COMPUTER",
priority="1",
hardware_state=operating_state,
ip_address="192.168.0.1",
software_state=SoftwareState.GOOD,
file_system_state="GOOD",
config_values=1,
2023-06-02 14:54:23 +01:00
)
service_attributes = Service(name="node", port="80", software_state=SoftwareState.COMPROMISED)
2023-06-27 13:06:10 +01:00
service_node.add_service(service_attributes)
2023-06-02 14:54:23 +01:00
for x in range(5):
2023-06-02 16:13:16 +01:00
service_node.update_booting_status()
2023-06-02 14:54:23 +01:00
2023-06-02 16:13:16 +01:00
assert service_attributes.software_state == SoftwareState.GOOD
assert service_node.hardware_state == expected_operating_state
2023-06-02 14:54:23 +01:00
2023-06-27 13:06:10 +01:00
2023-06-02 14:54:23 +01:00
@pytest.mark.parametrize(
"operating_state, expected_operating_state",
2023-06-27 13:06:10 +01:00
[(HardwareState.SHUTTING_DOWN, HardwareState.OFF)],
2023-06-02 14:54:23 +01:00
)
def test_node_shutdown_correctly(operating_state, expected_operating_state):
2023-06-27 13:06:10 +01:00
"""Tests that a node shutdown correctly."""
2023-06-02 14:54:23 +01:00
active_node = ActiveNode(
2023-06-27 13:06:10 +01:00
node_id=0,
name="node",
node_type="COMPUTER",
priority="1",
hardware_state=operating_state,
ip_address="192.168.0.1",
software_state=SoftwareState.GOOD,
file_system_state="GOOD",
config_values=1,
2023-06-02 14:54:23 +01:00
)
for x in range(5):
active_node.update_shutdown_status()
assert active_node.hardware_state == expected_operating_state