#2887 - Additional test failure fixes

This commit is contained in:
Charlie Crane
2025-01-23 17:07:15 +00:00
parent b9d2cd25f3
commit 30c177c272
14 changed files with 88 additions and 84 deletions

View File

@@ -1760,7 +1760,7 @@ class Node(SimComponent, ABC):
self.software_manager.install(application_class) self.software_manager.install(application_class)
application_instance = self.software_manager.software.get(application_name) application_instance = self.software_manager.software.get(application_name)
self.applications[application_instance.uuid] = application_instance self.applications[application_instance.uuid] = application_instance
_LOGGER.debug(f"Added application {application_instance.name} to node {self.hostname}") _LOGGER.debug(f"Added application {application_instance.name} to node {self.config.hostname}")
self._application_request_manager.add_request( self._application_request_manager.add_request(
application_name, RequestType(func=application_instance._request_manager) application_name, RequestType(func=application_instance._request_manager)
) )

View File

@@ -126,16 +126,16 @@ class WirelessRouter(Router, identifier="wireless_router"):
config: "WirelessRouter.ConfigSchema" = Field(default_factory=lambda: WirelessRouter.ConfigSchema()) config: "WirelessRouter.ConfigSchema" = Field(default_factory=lambda: WirelessRouter.ConfigSchema())
class ConfigSchema(Router.ConfigSChema): class ConfigSchema(Router.ConfigSchema):
"""Configuration Schema for WirelessRouter nodes within PrimAITE.""" """Configuration Schema for WirelessRouter nodes within PrimAITE."""
hostname: str = "WirelessRouter" hostname: str = "WirelessRouter"
def __init__(self, hostname: str, airspace: AirSpace, **kwargs): def __init__(self, **kwargs):
super().__init__(hostname=hostname, num_ports=0, airspace=airspace, **kwargs) super().__init__(hostname=kwargs["config"].hostname, num_ports=0, airspace=kwargs["config"].airspace, **kwargs)
self.connect_nic( self.connect_nic(
WirelessAccessPoint(ip_address="127.0.0.1", subnet_mask="255.0.0.0", gateway="0.0.0.0", airspace=airspace) WirelessAccessPoint(ip_address="127.0.0.1", subnet_mask="255.0.0.0", gateway="0.0.0.0", airspace=kwargs["config"].airspace)
) )
self.connect_nic(RouterInterface(ip_address="127.0.0.1", subnet_mask="255.0.0.0", gateway="0.0.0.0")) self.connect_nic(RouterInterface(ip_address="127.0.0.1", subnet_mask="255.0.0.0", gateway="0.0.0.0"))

View File

@@ -348,29 +348,29 @@ def install_stuff_to_sim(sim: Simulation):
# 1: Set up network hardware # 1: Set up network hardware
# 1.1: Configure the router # 1.1: Configure the router
router = Router(hostname="router", num_ports=3, start_up_duration=0) router = Router.from_config(config={"type":"router", "hostname":"router", "num_ports":3, "start_up_duration":0})
router.power_on() router.power_on()
router.configure_port(port=1, ip_address="10.0.1.1", subnet_mask="255.255.255.0") router.configure_port(port=1, ip_address="10.0.1.1", subnet_mask="255.255.255.0")
router.configure_port(port=2, ip_address="10.0.2.1", subnet_mask="255.255.255.0") router.configure_port(port=2, ip_address="10.0.2.1", subnet_mask="255.255.255.0")
# 1.2: Create and connect switches # 1.2: Create and connect switches
switch_1 = Switch(hostname="switch_1", num_ports=6, start_up_duration=0) switch_1 = Switch.from_config(config={"type":"switch", "hostname":"switch_1", "num_ports":6, "start_up_duration":0})
switch_1.power_on() switch_1.power_on()
network.connect(endpoint_a=router.network_interface[1], endpoint_b=switch_1.network_interface[6]) network.connect(endpoint_a=router.network_interface[1], endpoint_b=switch_1.network_interface[6])
router.enable_port(1) router.enable_port(1)
switch_2 = Switch(hostname="switch_2", num_ports=6, start_up_duration=0) switch_2 = Switch.from_config(config={"type":"switch", "hostname":"switch_2", "num_ports":6, "start_up_duration":0})
switch_2.power_on() switch_2.power_on()
network.connect(endpoint_a=router.network_interface[2], endpoint_b=switch_2.network_interface[6]) network.connect(endpoint_a=router.network_interface[2], endpoint_b=switch_2.network_interface[6])
router.enable_port(2) router.enable_port(2)
# 1.3: Create and connect computer # 1.3: Create and connect computer
client_1 = Computer( client_1_cfg = {"type": "computer",
hostname="client_1", "hostname": "client_1",
ip_address="10.0.1.2", "ip_address":"10.0.1.2",
subnet_mask="255.255.255.0", "subnet_mask":"255.255.255.0",
default_gateway="10.0.1.1", "default_gateway": "10.0.1.1",
start_up_duration=0, "start_up_duration":0}
) client_1: Computer = Computer.from_config(config=client_1_cfg)
client_1.power_on() client_1.power_on()
network.connect( network.connect(
endpoint_a=client_1.network_interface[1], endpoint_a=client_1.network_interface[1],
@@ -378,23 +378,26 @@ def install_stuff_to_sim(sim: Simulation):
) )
# 1.4: Create and connect servers # 1.4: Create and connect servers
server_1 = Server( server_1_cfg = {"type": "server",
hostname="server_1", "hostname":"server_1",
ip_address="10.0.2.2", "ip_address": "10.0.2.2",
subnet_mask="255.255.255.0", "subnet_mask":"255.255.255.0",
default_gateway="10.0.2.1", "default_gateway":"10.0.2.1",
start_up_duration=0, "start_up_duration": 0}
)
server_1: Server = Server.from_config(config=server_1_cfg)
server_1.power_on() server_1.power_on()
network.connect(endpoint_a=server_1.network_interface[1], endpoint_b=switch_2.network_interface[1]) network.connect(endpoint_a=server_1.network_interface[1], endpoint_b=switch_2.network_interface[1])
server_2_cfg = {"type": "server",
"hostname":"server_2",
"ip_address": "10.0.2.3",
"subnet_mask":"255.255.255.0",
"default_gateway":"10.0.2.1",
"start_up_duration": 0}
server_2 = Server(
hostname="server_2", server_2: Server = Server.from_config(config=server_2_cfg)
ip_address="10.0.2.3",
subnet_mask="255.255.255.0",
default_gateway="10.0.2.1",
start_up_duration=0,
)
server_2.power_on() server_2.power_on()
network.connect(endpoint_a=server_2.network_interface[1], endpoint_b=switch_2.network_interface[2]) network.connect(endpoint_a=server_2.network_interface[1], endpoint_b=switch_2.network_interface[2])
@@ -442,18 +445,18 @@ def install_stuff_to_sim(sim: Simulation):
assert acl_rule is None assert acl_rule is None
# 5.2: Assert the client is correctly configured # 5.2: Assert the client is correctly configured
c: Computer = [node for node in sim.network.nodes.values() if node.hostname == "client_1"][0] c: Computer = [node for node in sim.network.nodes.values() if node.config.hostname == "client_1"][0]
assert c.software_manager.software.get("WebBrowser") is not None assert c.software_manager.software.get("WebBrowser") is not None
assert c.software_manager.software.get("DNSClient") is not None assert c.software_manager.software.get("DNSClient") is not None
assert str(c.network_interface[1].ip_address) == "10.0.1.2" assert str(c.network_interface[1].ip_address) == "10.0.1.2"
# 5.3: Assert that server_1 is correctly configured # 5.3: Assert that server_1 is correctly configured
s1: Server = [node for node in sim.network.nodes.values() if node.hostname == "server_1"][0] s1: Server = [node for node in sim.network.nodes.values() if node.config.hostname == "server_1"][0]
assert str(s1.network_interface[1].ip_address) == "10.0.2.2" assert str(s1.network_interface[1].ip_address) == "10.0.2.2"
assert s1.software_manager.software.get("DNSServer") is not None assert s1.software_manager.software.get("DNSServer") is not None
# 5.4: Assert that server_2 is correctly configured # 5.4: Assert that server_2 is correctly configured
s2: Server = [node for node in sim.network.nodes.values() if node.hostname == "server_2"][0] s2: Server = [node for node in sim.network.nodes.values() if node.config.hostname == "server_2"][0]
assert str(s2.network_interface[1].ip_address) == "10.0.2.3" assert str(s2.network_interface[1].ip_address) == "10.0.2.3"
assert s2.software_manager.software.get("WebServer") is not None assert s2.software_manager.software.get("WebServer") is not None

View File

@@ -25,7 +25,8 @@ def check_default_rules(acl_obs):
def test_firewall_observation(): def test_firewall_observation():
"""Test adding/removing acl rules and enabling/disabling ports.""" """Test adding/removing acl rules and enabling/disabling ports."""
net = Network() net = Network()
firewall = Firewall(hostname="firewall", operating_state=NodeOperatingState.ON) firewall_cfg = {"type": "firewall", "hostname": "firewall", "opertating_state": NodeOperatingState.ON}
firewall = Firewall.from_config(config=firewall_cfg)
firewall_observation = FirewallObservation( firewall_observation = FirewallObservation(
where=[], where=[],
num_rules=7, num_rules=7,
@@ -116,7 +117,7 @@ def test_firewall_observation():
assert all(observation["PORTS"][i]["operating_status"] == 2 for i in range(1, 4)) assert all(observation["PORTS"][i]["operating_status"] == 2 for i in range(1, 4))
# connect a switch to the firewall and check that only the correct port is updated # connect a switch to the firewall and check that only the correct port is updated
switch = Switch(hostname="switch", num_ports=1, operating_state=NodeOperatingState.ON) switch: Switch = Switch.from_config(config={"type": "switch", "hostname":"switch", "num_ports":1, "operating_state":NodeOperatingState.ON})
link = net.connect(firewall.network_interface[1], switch.network_interface[1]) link = net.connect(firewall.network_interface[1], switch.network_interface[1])
assert firewall.network_interface[1].enabled assert firewall.network_interface[1].enabled
observation = firewall_observation.observe(firewall.describe_state()) observation = firewall_observation.observe(firewall.describe_state())

View File

@@ -56,12 +56,12 @@ def test_link_observation():
"""Check the shape and contents of the link observation.""" """Check the shape and contents of the link observation."""
net = Network() net = Network()
sim = Simulation(network=net) sim = Simulation(network=net)
switch = Switch(hostname="switch", num_ports=5, operating_state=NodeOperatingState.ON) switch: Switch = Switch.from_config(config={"type":"switch", "hostname":"switch", "num_ports":5, "operating_state":NodeOperatingState.ON})
computer_1 = Computer( computer_1: Computer = Computer.from_config(config={"type": "computer",
hostname="computer_1", ip_address="10.0.0.1", subnet_mask="255.255.255.0", start_up_duration=0 "hostname":"computer_1", "ip_address":"10.0.0.1", "subnet_mask":"255.255.255.0", "start_up_duration":0}
) )
computer_2 = Computer( computer_2: Computer = Computer.from_config(config={"type":"computer",
hostname="computer_2", ip_address="10.0.0.2", subnet_mask="255.255.255.0", start_up_duration=0 "hostname":"computer_2", "ip_address":"10.0.0.2", "subnet_mask":"255.255.255.0", "start_up_duration":0}
) )
computer_1.power_on() computer_1.power_on()
computer_2.power_on() computer_2.power_on()

View File

@@ -75,7 +75,7 @@ def test_nic(simulation):
nic: NIC = pc.network_interface[1] nic: NIC = pc.network_interface[1]
nic_obs = NICObservation(where=["network", "nodes", pc.hostname, "NICs", 1], include_nmne=True) nic_obs = NICObservation(where=["network", "nodes", pc.config.hostname, "NICs", 1], include_nmne=True)
# Set the NMNE configuration to capture DELETE/ENCRYPT queries as MNEs # Set the NMNE configuration to capture DELETE/ENCRYPT queries as MNEs
nmne_config = { nmne_config = {
@@ -108,7 +108,7 @@ def test_nic_categories(simulation):
"""Test the NIC observation nmne count categories.""" """Test the NIC observation nmne count categories."""
pc: Computer = simulation.network.get_node_by_hostname("client_1") pc: Computer = simulation.network.get_node_by_hostname("client_1")
nic_obs = NICObservation(where=["network", "nodes", pc.hostname, "NICs", 1], include_nmne=True) nic_obs = NICObservation(where=["network", "nodes", pc.config.hostname, "NICs", 1], include_nmne=True)
assert nic_obs.high_nmne_threshold == 10 # default assert nic_obs.high_nmne_threshold == 10 # default
assert nic_obs.med_nmne_threshold == 5 # default assert nic_obs.med_nmne_threshold == 5 # default
@@ -163,7 +163,7 @@ def test_nic_monitored_traffic(simulation):
pc2: Computer = simulation.network.get_node_by_hostname("client_2") pc2: Computer = simulation.network.get_node_by_hostname("client_2")
nic_obs = NICObservation( nic_obs = NICObservation(
where=["network", "nodes", pc.hostname, "NICs", 1], include_nmne=False, monitored_traffic=monitored_traffic where=["network", "nodes", pc.config.hostname, "NICs", 1], include_nmne=False, monitored_traffic=monitored_traffic
) )
simulation.pre_timestep(0) # apply timestep to whole sim simulation.pre_timestep(0) # apply timestep to whole sim

View File

@@ -25,7 +25,7 @@ def test_host_observation(simulation):
pc: Computer = simulation.network.get_node_by_hostname("client_1") pc: Computer = simulation.network.get_node_by_hostname("client_1")
host_obs = HostObservation( host_obs = HostObservation(
where=["network", "nodes", pc.hostname], where=["network", "nodes", pc.config.hostname],
num_applications=0, num_applications=0,
num_files=1, num_files=1,
num_folders=1, num_folders=1,
@@ -56,7 +56,7 @@ def test_host_observation(simulation):
observation_state = host_obs.observe(simulation.describe_state()) observation_state = host_obs.observe(simulation.describe_state())
assert observation_state.get("operating_status") == 4 # shutting down assert observation_state.get("operating_status") == 4 # shutting down
for i in range(pc.shut_down_duration + 1): for i in range(pc.config.shut_down_duration + 1):
pc.apply_timestep(i) pc.apply_timestep(i)
observation_state = host_obs.observe(simulation.describe_state()) observation_state = host_obs.observe(simulation.describe_state())

View File

@@ -16,7 +16,7 @@ from primaite.utils.validation.port import PORT_LOOKUP
def test_router_observation(): def test_router_observation():
"""Test adding/removing acl rules and enabling/disabling ports.""" """Test adding/removing acl rules and enabling/disabling ports."""
net = Network() net = Network()
router = Router(hostname="router", num_ports=5, operating_state=NodeOperatingState.ON) router = Router.from_config(config={"type": "router", "hostname":"router", "num_ports":5, "operating_state":NodeOperatingState.ON})
ports = [PortObservation(where=["NICs", i]) for i in range(1, 6)] ports = [PortObservation(where=["NICs", i]) for i in range(1, 6)]
acl = ACLObservation( acl = ACLObservation(
@@ -89,7 +89,7 @@ def test_router_observation():
assert all(observed_output["PORTS"][i]["operating_status"] == 2 for i in range(1, 6)) assert all(observed_output["PORTS"][i]["operating_status"] == 2 for i in range(1, 6))
# connect a switch to the router and check that only the correct port is updated # connect a switch to the router and check that only the correct port is updated
switch = Switch(hostname="switch", num_ports=1, operating_state=NodeOperatingState.ON) switch: Switch = Switch.from_config(config={"type": "switch", "hostname":"switch", "num_ports":1, "operating_state":NodeOperatingState.ON})
link = net.connect(router.network_interface[1], switch.network_interface[1]) link = net.connect(router.network_interface[1], switch.network_interface[1])
assert router.network_interface[1].enabled assert router.network_interface[1].enabled
observed_output = router_observation.observe(router.describe_state()) observed_output = router_observation.observe(router.describe_state())

View File

@@ -29,7 +29,7 @@ def test_service_observation(simulation):
ntp_server = pc.software_manager.software.get("NTPServer") ntp_server = pc.software_manager.software.get("NTPServer")
assert ntp_server assert ntp_server
service_obs = ServiceObservation(where=["network", "nodes", pc.hostname, "services", "NTPServer"]) service_obs = ServiceObservation(where=["network", "nodes", pc.config.hostname, "services", "NTPServer"])
assert service_obs.space["operating_status"] == spaces.Discrete(7) assert service_obs.space["operating_status"] == spaces.Discrete(7)
assert service_obs.space["health_status"] == spaces.Discrete(5) assert service_obs.space["health_status"] == spaces.Discrete(5)
@@ -54,7 +54,7 @@ def test_application_observation(simulation):
web_browser: WebBrowser = pc.software_manager.software.get("WebBrowser") web_browser: WebBrowser = pc.software_manager.software.get("WebBrowser")
assert web_browser assert web_browser
app_obs = ApplicationObservation(where=["network", "nodes", pc.hostname, "applications", "WebBrowser"]) app_obs = ApplicationObservation(where=["network", "nodes", pc.config.hostname, "applications", "WebBrowser"])
web_browser.close() web_browser.close()
observation_state = app_obs.observe(simulation.describe_state()) observation_state = app_obs.observe(simulation.describe_state())

View File

@@ -3,6 +3,7 @@ from primaite.session.environment import PrimaiteGymEnv
from primaite.simulator.network.hardware.node_operating_state import NodeOperatingState from primaite.simulator.network.hardware.node_operating_state import NodeOperatingState
from primaite.simulator.network.hardware.nodes.host.host_node import HostNode from primaite.simulator.network.hardware.nodes.host.host_node import HostNode
from primaite.simulator.system.services.service import ServiceOperatingState from primaite.simulator.system.services.service import ServiceOperatingState
from primaite.simulator.network.hardware.nodes.network.wireless_router import WirelessRouter
from tests.conftest import TEST_ASSETS_ROOT from tests.conftest import TEST_ASSETS_ROOT
CFG_PATH = TEST_ASSETS_ROOT / "configs/test_primaite_session.yaml" CFG_PATH = TEST_ASSETS_ROOT / "configs/test_primaite_session.yaml"

View File

@@ -17,6 +17,7 @@ from typing import Tuple
import pytest import pytest
import yaml import yaml
from primaite.simulator.network.hardware.nodes.network.firewall import Firewall
from primaite.game.agent.interface import ProxyAgent from primaite.game.agent.interface import ProxyAgent
from primaite.game.game import PrimaiteGame from primaite.game.game import PrimaiteGame
from primaite.session.environment import PrimaiteGymEnv from primaite.session.environment import PrimaiteGymEnv

View File

@@ -2,6 +2,7 @@
import yaml import yaml
from primaite.game.game import PrimaiteGame from primaite.game.game import PrimaiteGame
from primaite.simulator.network.hardware.nodes.network.wireless_router import WirelessRouter
from tests import TEST_ASSETS_ROOT from tests import TEST_ASSETS_ROOT

View File

@@ -84,44 +84,47 @@ class BroadcastTestClient(Application, identifier="BroadcastTestClient"):
def broadcast_network() -> Network: def broadcast_network() -> Network:
network = Network() network = Network()
client_1 = Computer( client_1_cfg = {"type": "computer",
hostname="client_1", "hostname": "client_1",
ip_address="192.168.1.2", "ip_address":"192.168.1.2",
subnet_mask="255.255.255.0", "subnet_mask": "255.255.255.0",
default_gateway="192.168.1.1", "default_gateway": "192.168.1.1",
start_up_duration=0, "start_up_duration":0}
)
client_1: Computer = Computer.from_config(config=client_1_cfg)
client_1.power_on() client_1.power_on()
client_1.software_manager.install(BroadcastTestClient) client_1.software_manager.install(BroadcastTestClient)
application_1 = client_1.software_manager.software["BroadcastTestClient"] application_1 = client_1.software_manager.software["BroadcastTestClient"]
application_1.run() application_1.run()
client_2_cfg = {"type": "computer",
"hostname": "client_2",
"ip_address":"192.168.1.3",
"subnet_mask": "255.255.255.0",
"default_gateway": "192.168.1.1",
"start_up_duration":0}
client_2 = Computer( client_2: Computer = Computer.from_config(config=client_2_cfg)
hostname="client_2",
ip_address="192.168.1.3",
subnet_mask="255.255.255.0",
default_gateway="192.168.1.1",
start_up_duration=0,
)
client_2.power_on() client_2.power_on()
client_2.software_manager.install(BroadcastTestClient) client_2.software_manager.install(BroadcastTestClient)
application_2 = client_2.software_manager.software["BroadcastTestClient"] application_2 = client_2.software_manager.software["BroadcastTestClient"]
application_2.run() application_2.run()
server_1 = Server( server_1_cfg = {"type": "server",
hostname="server_1", "hostname": "server_1",
ip_address="192.168.1.1", "ip_address":"192.168.1.1",
subnet_mask="255.255.255.0", "subnet_mask": "255.255.255.0",
default_gateway="192.168.1.1", "default_gateway":"192.168.1.1",
start_up_duration=0, "start_up_duration": 0}
)
server_1 :Server = Server.from_config(config=server_1_cfg)
server_1.power_on() server_1.power_on()
server_1.software_manager.install(BroadcastTestService) server_1.software_manager.install(BroadcastTestService)
service: BroadcastTestService = server_1.software_manager.software["BroadcastService"] service: BroadcastTestService = server_1.software_manager.software["BroadcastService"]
service.start() service.start()
switch_1 = Switch(hostname="switch_1", num_ports=6, start_up_duration=0) switch_1: Switch = Switch.from_config(config={"type": "switch", "hostname":"switch_1", "num_ports":6, "start_up_duration":0})
switch_1.power_on() switch_1.power_on()
network.connect(endpoint_a=client_1.network_interface[1], endpoint_b=switch_1.network_interface[1]) network.connect(endpoint_a=client_1.network_interface[1], endpoint_b=switch_1.network_interface[1])

View File

@@ -12,13 +12,10 @@ from primaite.utils.validation.port import PORT_LOOKUP
@pytest.fixture(scope="function") @pytest.fixture(scope="function")
def web_browser() -> WebBrowser: def web_browser() -> WebBrowser:
computer = Computer( computer_cfg = {"type": "computer", "hostname": "web_client", "ip_address": "192.168.1.11", "subnet_mask": "255.255.255.0", "default_gateway": "192.168.1.1", "start_up_duration": 0}
hostname="web_client",
ip_address="192.168.1.11", computer: Computer = Computer.from_config(config=computer_cfg)
subnet_mask="255.255.255.0",
default_gateway="192.168.1.1",
start_up_duration=0,
)
computer.power_on() computer.power_on()
# Web Browser should be pre-installed in computer # Web Browser should be pre-installed in computer
web_browser: WebBrowser = computer.software_manager.software.get("WebBrowser") web_browser: WebBrowser = computer.software_manager.software.get("WebBrowser")
@@ -28,13 +25,10 @@ def web_browser() -> WebBrowser:
def test_create_web_client(): def test_create_web_client():
computer = Computer( computer_cfg = {"type": "computer", "hostname": "web_client", "ip_address": "192.168.1.11", "subnet_mask": "255.255.255.0", "default_gateway": "192.168.1.1", "start_up_duration": 0}
hostname="web_client",
ip_address="192.168.1.11", computer: Computer = Computer.from_config(config=computer_cfg)
subnet_mask="255.255.255.0",
default_gateway="192.168.1.1",
start_up_duration=0,
)
computer.power_on() computer.power_on()
# Web Browser should be pre-installed in computer # Web Browser should be pre-installed in computer
web_browser: WebBrowser = computer.software_manager.software.get("WebBrowser") web_browser: WebBrowser = computer.software_manager.software.get("WebBrowser")