Add tests for network node adding/removal
This commit is contained in:
@@ -52,7 +52,9 @@ class NetworkContainer(SimComponent):
|
|||||||
:type node: Node
|
:type node: Node
|
||||||
"""
|
"""
|
||||||
if node in self:
|
if node in self:
|
||||||
_LOGGER.warning(f"Can't add node {node}. It is already in the network.")
|
msg = f"Can't add node {node}. It is already in the network."
|
||||||
|
_LOGGER.warning(msg)
|
||||||
|
raise RuntimeWarning(msg)
|
||||||
self.nodes[node.uuid] = node
|
self.nodes[node.uuid] = node
|
||||||
node.parent = self
|
node.parent = self
|
||||||
|
|
||||||
@@ -64,7 +66,9 @@ class NetworkContainer(SimComponent):
|
|||||||
:type node: Node
|
:type node: Node
|
||||||
"""
|
"""
|
||||||
if node not in self:
|
if node not in self:
|
||||||
_LOGGER.warning(f"Can't remove node {node}. It's not in the network.")
|
msg = f"Can't remove node {node}. It's not in the network."
|
||||||
|
_LOGGER.warning(msg)
|
||||||
|
raise RuntimeWarning(msg)
|
||||||
del self.nodes[node.uuid]
|
del self.nodes[node.uuid]
|
||||||
del node.parent # misleading?
|
del node.parent # misleading?
|
||||||
|
|
||||||
|
|||||||
38
tests/integration_tests/network/test_network_creation.py
Normal file
38
tests/integration_tests/network/test_network_creation.py
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
from primaite.simulator.network.container import NetworkContainer
|
||||||
|
from primaite.simulator.network.hardware.base import Node
|
||||||
|
|
||||||
|
|
||||||
|
def test_adding_removing_nodes():
|
||||||
|
"""Check that we can create and add a node to a network."""
|
||||||
|
net = NetworkContainer()
|
||||||
|
n1 = Node(hostname="computer")
|
||||||
|
net.add_node(n1)
|
||||||
|
assert n1.parent is net
|
||||||
|
assert n1 in net
|
||||||
|
|
||||||
|
net.remove_node(n1)
|
||||||
|
assert n1.parent is None
|
||||||
|
assert n1 not in net
|
||||||
|
|
||||||
|
|
||||||
|
def test_readding_node():
|
||||||
|
"""Check that warning is raised when readding a node."""
|
||||||
|
net = NetworkContainer()
|
||||||
|
n1 = Node(hostname="computer")
|
||||||
|
net.add_node(n1)
|
||||||
|
with pytest.raises(RuntimeWarning):
|
||||||
|
net.add_node(n1)
|
||||||
|
assert n1.parent is net
|
||||||
|
assert n1 in net
|
||||||
|
|
||||||
|
|
||||||
|
def test_removing_nonexistent_node():
|
||||||
|
"""Check that warning is raised when trying to remove a node that is not in the network."""
|
||||||
|
net = NetworkContainer()
|
||||||
|
n1 = Node(hostname="computer")
|
||||||
|
with pytest.raises(RuntimeWarning):
|
||||||
|
net.remove_node(n1)
|
||||||
|
assert n1.parent is None
|
||||||
|
assert n1 not in net
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
from primaite.simulator.network.container import NetworkContainer
|
||||||
|
|
||||||
|
|
||||||
|
def test_creating_container():
|
||||||
|
"""Check that we can create a network container"""
|
||||||
|
net = NetworkContainer()
|
||||||
|
assert net.nodes and net.links
|
||||||
|
|
||||||
|
|
||||||
|
def test_describe_state():
|
||||||
|
"""Check that we can describe network state without raising errors, and that the result is JSON serialisable."""
|
||||||
|
net = NetworkContainer()
|
||||||
|
state = net.describe_state()
|
||||||
|
json.dumps(state) # if this function call raises an error, the test fails, state was not JSON-serialisable
|
||||||
Reference in New Issue
Block a user