2016-03-11 19:13:52 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import asyncio
|
2016-03-14 16:40:27 +00:00
|
|
|
import aiohttp
|
2016-03-11 19:13:52 +00:00
|
|
|
from unittest.mock import MagicMock
|
2016-09-02 12:39:38 +00:00
|
|
|
from tests.utils import asyncio_patch, AsyncioMagicMock
|
2016-03-11 19:13:52 +00:00
|
|
|
|
|
|
|
from gns3server.controller.project import Project
|
|
|
|
from gns3server.controller.udp_link import UDPLink
|
2016-09-15 12:51:40 +00:00
|
|
|
from gns3server.controller.ports.ethernet_port import EthernetPort
|
2016-05-11 17:35:36 +00:00
|
|
|
from gns3server.controller.node import Node
|
2016-03-11 19:13:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2016-05-18 16:37:18 +00:00
|
|
|
def project(controller):
|
2016-07-11 13:36:52 +00:00
|
|
|
return Project(controller=controller, name="Test")
|
2016-03-11 19:13:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_create(async_run, project):
|
2016-04-15 15:57:06 +00:00
|
|
|
compute1 = MagicMock()
|
|
|
|
compute2 = MagicMock()
|
2016-03-11 19:13:52 +00:00
|
|
|
|
2016-06-11 23:31:30 +00:00
|
|
|
node1 = Node(project, compute1, "node1", node_type="vpcs")
|
2016-09-15 12:51:40 +00:00
|
|
|
node1._ports = [EthernetPort("E0", 0, 0, 4)]
|
2016-06-11 23:31:30 +00:00
|
|
|
node2 = Node(project, compute2, "node2", node_type="vpcs")
|
2016-09-15 12:51:40 +00:00
|
|
|
node2._ports = [EthernetPort("E0", 0, 3, 1)]
|
2016-03-11 19:13:52 +00:00
|
|
|
|
2016-08-25 17:14:29 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def subnet_callback(compute2):
|
|
|
|
"""
|
|
|
|
Fake subnet callback
|
|
|
|
"""
|
|
|
|
return ("192.168.1.1", "192.168.1.2")
|
|
|
|
|
|
|
|
compute1.get_ip_on_same_subnet.side_effect = subnet_callback
|
|
|
|
|
2016-03-11 19:13:52 +00:00
|
|
|
link = UDPLink(project)
|
2016-05-11 17:35:36 +00:00
|
|
|
async_run(link.add_node(node1, 0, 4))
|
2016-03-11 19:13:52 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
2016-11-08 08:54:59 +00:00
|
|
|
def compute1_callback(path, data={}, **kwargs):
|
2016-03-11 19:13:52 +00:00
|
|
|
"""
|
|
|
|
Fake server
|
|
|
|
"""
|
|
|
|
if "/ports/udp" in path:
|
|
|
|
response = MagicMock()
|
|
|
|
response.json = {"udp_port": 1024}
|
|
|
|
return response
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2016-11-08 08:54:59 +00:00
|
|
|
def compute2_callback(path, data={}, **kwargs):
|
2016-03-11 19:13:52 +00:00
|
|
|
"""
|
|
|
|
Fake server
|
|
|
|
"""
|
|
|
|
if "/ports/udp" in path:
|
|
|
|
response = MagicMock()
|
|
|
|
response.json = {"udp_port": 2048}
|
|
|
|
return response
|
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
compute1.post.side_effect = compute1_callback
|
|
|
|
compute1.host = "example.com"
|
|
|
|
compute2.post.side_effect = compute2_callback
|
|
|
|
compute2.host = "example.org"
|
2016-09-02 12:39:38 +00:00
|
|
|
async_run(link.add_node(node2, 3, 1))
|
2016-03-11 19:13:52 +00:00
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
compute1.post.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/nio".format(project.id, node1.id), data={
|
2016-03-11 19:13:52 +00:00
|
|
|
"lport": 1024,
|
2016-08-25 17:14:29 +00:00
|
|
|
"rhost": "192.168.1.2",
|
2016-03-11 19:13:52 +00:00
|
|
|
"rport": 2048,
|
|
|
|
"type": "nio_udp"
|
2016-11-08 08:54:59 +00:00
|
|
|
}, timeout=120)
|
2016-05-11 17:35:36 +00:00
|
|
|
compute2.post.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/3/ports/1/nio".format(project.id, node2.id), data={
|
2016-03-11 19:13:52 +00:00
|
|
|
"lport": 2048,
|
2016-08-25 17:14:29 +00:00
|
|
|
"rhost": "192.168.1.1",
|
2016-03-11 19:13:52 +00:00
|
|
|
"rport": 1024,
|
|
|
|
"type": "nio_udp"
|
2016-11-08 08:54:59 +00:00
|
|
|
}, timeout=120)
|
2016-03-14 16:40:27 +00:00
|
|
|
|
|
|
|
|
2016-10-27 17:09:27 +00:00
|
|
|
def test_create_one_side_failure(async_run, project):
|
|
|
|
compute1 = MagicMock()
|
|
|
|
compute2 = MagicMock()
|
|
|
|
|
|
|
|
node1 = Node(project, compute1, "node1", node_type="vpcs")
|
|
|
|
node1._ports = [EthernetPort("E0", 0, 0, 4)]
|
|
|
|
node2 = Node(project, compute2, "node2", node_type="vpcs")
|
|
|
|
node2._ports = [EthernetPort("E0", 0, 3, 1)]
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def subnet_callback(compute2):
|
|
|
|
"""
|
|
|
|
Fake subnet callback
|
|
|
|
"""
|
|
|
|
return ("192.168.1.1", "192.168.1.2")
|
|
|
|
|
|
|
|
compute1.get_ip_on_same_subnet.side_effect = subnet_callback
|
|
|
|
|
|
|
|
link = UDPLink(project)
|
|
|
|
async_run(link.add_node(node1, 0, 4))
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2016-11-08 08:54:59 +00:00
|
|
|
def compute1_callback(path, data={}, **kwargs):
|
2016-10-27 17:09:27 +00:00
|
|
|
"""
|
|
|
|
Fake server
|
|
|
|
"""
|
|
|
|
if "/ports/udp" in path:
|
|
|
|
response = MagicMock()
|
|
|
|
response.json = {"udp_port": 1024}
|
|
|
|
return response
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2016-11-08 08:54:59 +00:00
|
|
|
def compute2_callback(path, data={}, **kwargs):
|
2016-10-27 17:09:27 +00:00
|
|
|
"""
|
|
|
|
Fake server
|
|
|
|
"""
|
|
|
|
if "/ports/udp" in path:
|
|
|
|
response = MagicMock()
|
|
|
|
response.json = {"udp_port": 2048}
|
|
|
|
return response
|
|
|
|
elif "/adapters" in path:
|
|
|
|
raise aiohttp.web.HTTPConflict(text="Error when creating the NIO")
|
|
|
|
|
|
|
|
compute1.post.side_effect = compute1_callback
|
|
|
|
compute1.host = "example.com"
|
|
|
|
compute2.post.side_effect = compute2_callback
|
|
|
|
compute2.host = "example.org"
|
|
|
|
with pytest.raises(aiohttp.web.HTTPConflict):
|
|
|
|
async_run(link.add_node(node2, 3, 1))
|
|
|
|
|
|
|
|
compute1.post.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/nio".format(project.id, node1.id), data={
|
|
|
|
"lport": 1024,
|
|
|
|
"rhost": "192.168.1.2",
|
|
|
|
"rport": 2048,
|
|
|
|
"type": "nio_udp"
|
2016-11-08 08:54:59 +00:00
|
|
|
}, timeout=120)
|
2016-10-27 17:09:27 +00:00
|
|
|
compute2.post.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/3/ports/1/nio".format(project.id, node2.id), data={
|
|
|
|
"lport": 2048,
|
|
|
|
"rhost": "192.168.1.1",
|
|
|
|
"rport": 1024,
|
|
|
|
"type": "nio_udp"
|
2016-11-08 08:54:59 +00:00
|
|
|
}, timeout=120)
|
2016-10-27 17:09:27 +00:00
|
|
|
# The link creation has failed we rollback the nio
|
2016-11-08 08:54:59 +00:00
|
|
|
compute1.delete.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/nio".format(project.id, node1.id), timeout=120)
|
2016-10-27 17:09:27 +00:00
|
|
|
|
|
|
|
|
2016-03-14 16:40:27 +00:00
|
|
|
def test_delete(async_run, project):
|
2016-04-15 15:57:06 +00:00
|
|
|
compute1 = MagicMock()
|
|
|
|
compute2 = MagicMock()
|
2016-03-14 16:40:27 +00:00
|
|
|
|
2016-06-11 23:31:30 +00:00
|
|
|
node1 = Node(project, compute1, "node1", node_type="vpcs")
|
2016-09-15 12:51:40 +00:00
|
|
|
node1._ports = [EthernetPort("E0", 0, 0, 4)]
|
2016-06-11 23:31:30 +00:00
|
|
|
node2 = Node(project, compute2, "node2", node_type="vpcs")
|
2016-09-15 12:51:40 +00:00
|
|
|
node2._ports = [EthernetPort("E0", 0, 3, 1)]
|
2016-03-14 16:40:27 +00:00
|
|
|
|
|
|
|
link = UDPLink(project)
|
2016-09-02 12:39:38 +00:00
|
|
|
link.create = AsyncioMagicMock()
|
2016-05-11 17:35:36 +00:00
|
|
|
async_run(link.add_node(node1, 0, 4))
|
|
|
|
async_run(link.add_node(node2, 3, 1))
|
2016-03-14 16:40:27 +00:00
|
|
|
|
|
|
|
async_run(link.delete())
|
|
|
|
|
2016-11-08 08:54:59 +00:00
|
|
|
compute1.delete.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/nio".format(project.id, node1.id), timeout=120)
|
|
|
|
compute2.delete.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/3/ports/1/nio".format(project.id, node2.id), timeout=120)
|
2016-04-21 10:14:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_choose_capture_side(async_run, project):
|
|
|
|
"""
|
|
|
|
The link capture should run on the optimal node
|
|
|
|
"""
|
|
|
|
compute1 = MagicMock()
|
|
|
|
compute2 = MagicMock()
|
|
|
|
compute2.id = "local"
|
|
|
|
|
2016-06-11 23:31:30 +00:00
|
|
|
node_vpcs = Node(project, compute1, "node1", node_type="vpcs")
|
2016-09-15 12:51:40 +00:00
|
|
|
node_vpcs._ports = [EthernetPort("E0", 0, 0, 4)]
|
2016-06-11 23:31:30 +00:00
|
|
|
node_iou = Node(project, compute2, "node2", node_type="iou")
|
2016-09-15 12:51:40 +00:00
|
|
|
node_iou._ports = [EthernetPort("E0", 0, 3, 1)]
|
2016-04-21 10:14:09 +00:00
|
|
|
|
|
|
|
link = UDPLink(project)
|
2016-09-02 12:39:38 +00:00
|
|
|
link.create = AsyncioMagicMock()
|
2016-05-11 17:35:36 +00:00
|
|
|
async_run(link.add_node(node_vpcs, 0, 4))
|
|
|
|
async_run(link.add_node(node_iou, 3, 1))
|
2016-04-21 10:14:09 +00:00
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
assert link._choose_capture_side()["node"] == node_iou
|
2016-04-21 10:14:09 +00:00
|
|
|
|
2016-07-18 17:09:08 +00:00
|
|
|
node_vpcs = Node(project, compute1, "node3", node_type="vpcs")
|
2016-09-15 12:51:40 +00:00
|
|
|
node_vpcs._ports = [EthernetPort("E0", 0, 0, 4)]
|
2016-07-18 17:09:08 +00:00
|
|
|
node_vpcs2 = Node(project, compute1, "node4", node_type="vpcs")
|
2016-09-15 12:51:40 +00:00
|
|
|
node_vpcs2._ports = [EthernetPort("E0", 0, 3, 1)]
|
2016-04-21 10:14:09 +00:00
|
|
|
|
|
|
|
link = UDPLink(project)
|
2016-09-02 12:39:38 +00:00
|
|
|
link.create = AsyncioMagicMock()
|
2016-05-11 17:35:36 +00:00
|
|
|
async_run(link.add_node(node_vpcs, 0, 4))
|
|
|
|
async_run(link.add_node(node_vpcs2, 3, 1))
|
2016-04-21 10:14:09 +00:00
|
|
|
|
|
|
|
# Capture should run on the local node
|
2016-07-18 17:09:08 +00:00
|
|
|
node_iou = Node(project, compute1, "node5", node_type="iou")
|
2016-09-15 12:51:40 +00:00
|
|
|
node_iou._ports = [EthernetPort("E0", 0, 0, 4)]
|
2016-07-18 17:09:08 +00:00
|
|
|
node_iou2 = Node(project, compute2, "node6", node_type="iou")
|
2016-09-15 12:51:40 +00:00
|
|
|
node_iou2._ports = [EthernetPort("E0", 0, 3, 1)]
|
2016-04-21 10:14:09 +00:00
|
|
|
|
|
|
|
link = UDPLink(project)
|
2016-09-02 12:39:38 +00:00
|
|
|
link.create = AsyncioMagicMock()
|
2016-05-11 17:35:36 +00:00
|
|
|
async_run(link.add_node(node_iou, 0, 4))
|
|
|
|
async_run(link.add_node(node_iou2, 3, 1))
|
2016-04-21 10:14:09 +00:00
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
assert link._choose_capture_side()["node"] == node_iou2
|
2016-04-21 10:14:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_capture(async_run, project):
|
|
|
|
compute1 = MagicMock()
|
|
|
|
|
2016-06-11 23:31:30 +00:00
|
|
|
node_vpcs = Node(project, compute1, "V1", node_type="vpcs")
|
2016-09-15 12:51:40 +00:00
|
|
|
node_vpcs._ports = [EthernetPort("E0", 0, 0, 4)]
|
2016-06-11 23:31:30 +00:00
|
|
|
node_iou = Node(project, compute1, "I1", node_type="iou")
|
2016-09-15 12:51:40 +00:00
|
|
|
node_iou._ports = [EthernetPort("E0", 0, 3, 1)]
|
2016-04-21 10:14:09 +00:00
|
|
|
|
|
|
|
link = UDPLink(project)
|
2016-09-02 12:39:38 +00:00
|
|
|
link.create = AsyncioMagicMock()
|
2016-05-11 17:35:36 +00:00
|
|
|
async_run(link.add_node(node_vpcs, 0, 4))
|
|
|
|
async_run(link.add_node(node_iou, 3, 1))
|
2016-04-21 10:14:09 +00:00
|
|
|
|
|
|
|
capture = async_run(link.start_capture())
|
2016-04-21 14:11:42 +00:00
|
|
|
assert link.capturing
|
2016-04-21 10:14:09 +00:00
|
|
|
|
2016-06-23 10:10:18 +00:00
|
|
|
compute1.post.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/start_capture".format(project.id, node_vpcs.id), data={
|
2016-04-26 15:10:33 +00:00
|
|
|
"capture_file_name": link.default_capture_file_name(),
|
2016-04-21 10:14:09 +00:00
|
|
|
"data_link_type": "DLT_EN10MB"
|
|
|
|
})
|
|
|
|
|
|
|
|
capture = async_run(link.stop_capture())
|
2016-04-21 14:11:42 +00:00
|
|
|
assert link.capturing is False
|
2016-04-21 10:14:09 +00:00
|
|
|
|
2016-06-23 10:10:18 +00:00
|
|
|
compute1.post.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/stop_capture".format(project.id, node_vpcs.id))
|
2016-04-22 14:22:03 +00:00
|
|
|
|
|
|
|
|
2016-04-26 15:10:33 +00:00
|
|
|
def test_read_pcap_from_source(project, async_run):
|
2016-04-22 14:22:03 +00:00
|
|
|
compute1 = MagicMock()
|
|
|
|
|
2016-10-03 10:31:01 +00:00
|
|
|
node_vpcs = Node(project, compute1, "V1", node_type="vpcs")
|
|
|
|
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)]
|
|
|
|
|
2016-04-22 14:22:03 +00:00
|
|
|
link = UDPLink(project)
|
2016-09-02 12:39:38 +00:00
|
|
|
link.create = AsyncioMagicMock()
|
2016-10-03 10:31:01 +00:00
|
|
|
async_run(link.add_node(node_vpcs, 0, 4))
|
|
|
|
async_run(link.add_node(node_iou, 3, 1))
|
2016-04-22 14:22:03 +00:00
|
|
|
|
|
|
|
capture = async_run(link.start_capture())
|
2016-05-11 17:35:36 +00:00
|
|
|
assert link._capture_node is not None
|
2016-04-22 14:22:03 +00:00
|
|
|
|
2016-04-26 15:10:33 +00:00
|
|
|
async_run(link.read_pcap_from_source())
|
2016-07-21 18:17:36 +00:00
|
|
|
link._capture_node["node"].compute.stream_file.assert_called_with(project, "tmp/captures/" + link._capture_file_name)
|