mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 22:11:14 +02:00
Prevent capture on non running node
Fix https://github.com/GNS3/gns3-gui/issues/1656
This commit is contained in:
9 files changed
+158
-39
No files matched your search
@@ -97,6 +97,7 @@ def test_add_node(async_run, project, compute):
|
||||
|
||||
assert link.create.called
|
||||
link._project.controller.notification.emit.assert_called_with("link.created", link.__json__())
|
||||
assert link in node2.link
|
||||
|
||||
|
||||
def test_add_node_cloud(async_run, project, compute):
|
||||
@@ -302,3 +303,23 @@ def test_stop_capture(link, async_run, tmpdir, project, controller):
|
||||
async_run(link.stop_capture())
|
||||
assert link._capturing is False
|
||||
controller._notification.emit.assert_called_with("link.updated", link.__json__())
|
||||
|
||||
|
||||
def test_delete(async_run, project, compute):
|
||||
node1 = Node(project, compute, "node1", node_type="qemu")
|
||||
node1._ports = [EthernetPort("E0", 0, 0, 4)]
|
||||
|
||||
link = Link(project)
|
||||
link.create = AsyncioMagicMock()
|
||||
link._project.controller.notification.emit = MagicMock()
|
||||
project.dump = AsyncioMagicMock()
|
||||
async_run(link.add_node(node1, 0, 4))
|
||||
|
||||
node2 = Node(project, compute, "node2", node_type="qemu")
|
||||
node2._ports = [EthernetPort("E0", 0, 0, 4)]
|
||||
async_run(link.add_node(node2, 0, 4))
|
||||
|
||||
assert link in node2.link
|
||||
|
||||
async_run(link.delete())
|
||||
assert link not in node2.link
|
||||
@@ -474,3 +474,14 @@ def test_get_port(node):
|
||||
assert port.adapter_number == 1
|
||||
with pytest.raises(aiohttp.web.HTTPNotFound):
|
||||
port = node.get_port(42, 0)
|
||||
|
||||
|
||||
def test_parse_node_response(node, async_run):
|
||||
"""
|
||||
When a node is updated we notify the links connected to it
|
||||
"""
|
||||
link = MagicMock()
|
||||
link.node_updated = AsyncioMagicMock()
|
||||
node.add_link(link)
|
||||
async_run(node.parse_node_response({"status": "started"}))
|
||||
assert link.node_updated.called
|
||||
@@ -77,7 +77,7 @@ def test_dispatch(async_run, controller, project):
|
||||
with notif.queue(project) as queue:
|
||||
assert len(notif._listeners[project.id]) == 1
|
||||
async_run(queue.get(0.1)) # ping
|
||||
notif.dispatch("test", {}, compute_id=1)
|
||||
async_run(notif.dispatch("test", {}, compute_id=1))
|
||||
msg = async_run(queue.get(5))
|
||||
assert msg == ('test', {}, {})
|
||||
|
||||
@@ -87,7 +87,7 @@ def test_dispatch_ping(async_run, controller, project):
|
||||
with notif.queue(project) as queue:
|
||||
assert len(notif._listeners[project.id]) == 1
|
||||
async_run(queue.get(0.1)) # ping
|
||||
notif.dispatch("ping", {}, compute_id=12)
|
||||
async_run(notif.dispatch("ping", {}, compute_id=12))
|
||||
msg = async_run(queue.get(5))
|
||||
assert msg == ('ping', {'compute_id': 12}, {})
|
||||
|
||||
@@ -102,13 +102,13 @@ def test_dispatch_node_updated(async_run, controller, node, project):
|
||||
with notif.queue(project) as queue:
|
||||
assert len(notif._listeners[project.id]) == 1
|
||||
async_run(queue.get(0.1)) # ping
|
||||
notif.dispatch("node.updated", {
|
||||
async_run(notif.dispatch("node.updated", {
|
||||
"node_id": node.id,
|
||||
"project_id": project.id,
|
||||
"name": "hello",
|
||||
"startup_config": "ip 192"
|
||||
},
|
||||
compute_id=1)
|
||||
compute_id=1))
|
||||
assert node.name == "hello"
|
||||
action, event, _ = async_run(queue.get(5))
|
||||
assert action == "node.updated"
|
||||
|
||||
@@ -187,6 +187,37 @@ def test_choose_capture_side(async_run, project):
|
||||
compute2 = MagicMock()
|
||||
compute2.id = "local"
|
||||
|
||||
# Capture should run on the local node
|
||||
node_vpcs = Node(project, compute1, "node1", node_type="vpcs")
|
||||
node_vpcs._status = "started"
|
||||
node_vpcs._ports = [EthernetPort("E0", 0, 0, 4)]
|
||||
node_iou = Node(project, compute2, "node2", node_type="iou")
|
||||
node_iou._status = "started"
|
||||
node_iou._ports = [EthernetPort("E0", 0, 3, 1)]
|
||||
|
||||
link = UDPLink(project)
|
||||
link.create = AsyncioMagicMock()
|
||||
async_run(link.add_node(node_vpcs, 0, 4))
|
||||
async_run(link.add_node(node_iou, 3, 1))
|
||||
|
||||
assert link._choose_capture_side()["node"] == node_iou
|
||||
|
||||
# Capture should choose always running node
|
||||
node_iou = Node(project, compute1, "node5", node_type="iou")
|
||||
node_iou._status = "started"
|
||||
node_iou._ports = [EthernetPort("E0", 0, 0, 4)]
|
||||
node_switch = Node(project, compute1, "node6", node_type="ethernet_switch")
|
||||
node_switch._status = "started"
|
||||
node_switch._ports = [EthernetPort("E0", 0, 3, 1)]
|
||||
|
||||
link = UDPLink(project)
|
||||
link.create = AsyncioMagicMock()
|
||||
async_run(link.add_node(node_iou, 0, 4))
|
||||
async_run(link.add_node(node_switch, 3, 1))
|
||||
|
||||
assert link._choose_capture_side()["node"] == node_switch
|
||||
|
||||
# Capture should raise error if node are not started
|
||||
node_vpcs = Node(project, compute1, "node1", node_type="vpcs")
|
||||
node_vpcs._ports = [EthernetPort("E0", 0, 0, 4)]
|
||||
node_iou = Node(project, compute2, "node2", node_type="iou")
|
||||
@@ -197,36 +228,18 @@ def test_choose_capture_side(async_run, project):
|
||||
async_run(link.add_node(node_vpcs, 0, 4))
|
||||
async_run(link.add_node(node_iou, 3, 1))
|
||||
|
||||
assert link._choose_capture_side()["node"] == node_iou
|
||||
|
||||
node_vpcs = Node(project, compute1, "node3", node_type="vpcs")
|
||||
node_vpcs._ports = [EthernetPort("E0", 0, 0, 4)]
|
||||
node_vpcs2 = Node(project, compute1, "node4", node_type="vpcs")
|
||||
node_vpcs2._ports = [EthernetPort("E0", 0, 3, 1)]
|
||||
|
||||
link = UDPLink(project)
|
||||
link.create = AsyncioMagicMock()
|
||||
async_run(link.add_node(node_vpcs, 0, 4))
|
||||
async_run(link.add_node(node_vpcs2, 3, 1))
|
||||
|
||||
# Capture should run on the local node
|
||||
node_iou = Node(project, compute1, "node5", node_type="iou")
|
||||
node_iou._ports = [EthernetPort("E0", 0, 0, 4)]
|
||||
node_iou2 = Node(project, compute2, "node6", node_type="iou")
|
||||
node_iou2._ports = [EthernetPort("E0", 0, 3, 1)]
|
||||
|
||||
link = UDPLink(project)
|
||||
link.create = AsyncioMagicMock()
|
||||
async_run(link.add_node(node_iou, 0, 4))
|
||||
async_run(link.add_node(node_iou2, 3, 1))
|
||||
|
||||
assert link._choose_capture_side()["node"] == node_iou2
|
||||
with pytest.raises(aiohttp.web.HTTPConflict):
|
||||
link._choose_capture_side()
|
||||
# If you start a node you can capture on it
|
||||
node_vpcs._status = "started"
|
||||
assert link._choose_capture_side()["node"] == node_vpcs
|
||||
|
||||
|
||||
def test_capture(async_run, project):
|
||||
compute1 = MagicMock()
|
||||
|
||||
node_vpcs = Node(project, compute1, "V1", node_type="vpcs")
|
||||
node_vpcs._status = "started"
|
||||
node_vpcs._ports = [EthernetPort("E0", 0, 0, 4)]
|
||||
node_iou = Node(project, compute1, "I1", node_type="iou")
|
||||
node_iou._ports = [EthernetPort("E0", 0, 3, 1)]
|
||||
@@ -254,6 +267,7 @@ def test_read_pcap_from_source(project, async_run):
|
||||
compute1 = MagicMock()
|
||||
|
||||
node_vpcs = Node(project, compute1, "V1", node_type="vpcs")
|
||||
node_vpcs._status = "started"
|
||||
node_vpcs._ports = [EthernetPort("E0", 0, 0, 4)]
|
||||
node_iou = Node(project, compute1, "I1", node_type="iou")
|
||||
node_iou._ports = [EthernetPort("E0", 0, 3, 1)]
|
||||
@@ -268,3 +282,23 @@ def test_read_pcap_from_source(project, async_run):
|
||||
|
||||
async_run(link.read_pcap_from_source())
|
||||
link._capture_node["node"].compute.stream_file.assert_called_with(project, "tmp/captures/" + link._capture_file_name)
|
||||
|
||||
|
||||
def test_node_updated(project, async_run):
|
||||
"""
|
||||
If a node stop when capturing we stop the capture
|
||||
"""
|
||||
compute1 = MagicMock()
|
||||
node_vpcs = Node(project, compute1, "V1", node_type="vpcs")
|
||||
node_vpcs._status = "started"
|
||||
|
||||
link = UDPLink(project)
|
||||
link._capture_node = {"node": node_vpcs}
|
||||
link.stop_capture = AsyncioMagicMock()
|
||||
|
||||
async_run(link.node_updated(node_vpcs))
|
||||
assert not link.stop_capture.called
|
||||
|
||||
node_vpcs._status = "stopped"
|
||||
async_run(link.node_updated(node_vpcs))
|
||||
assert link.stop_capture.called
|
||||
Reference in new issue
Block a user