2016-05-11 17:35:36 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2020-06-16 04:29:03 +00:00
|
|
|
# Copyright (C) 2020 GNS3 Technologies Inc.
|
2016-05-11 17:35:36 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
2020-06-16 04:29:03 +00:00
|
|
|
|
2017-09-05 09:07:10 +00:00
|
|
|
from collections import OrderedDict
|
2016-05-11 17:35:36 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
import asyncio
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
from tests.utils import asyncio_patch, AsyncioMagicMock
|
2016-05-11 17:35:36 +00:00
|
|
|
|
|
|
|
from gns3server.compute.vpcs.vpcs_vm import VPCSVM
|
|
|
|
from gns3server.compute.docker.docker_vm import DockerVM
|
2016-06-07 13:34:04 +00:00
|
|
|
from gns3server.compute.error import NodeError
|
2016-05-11 17:35:36 +00:00
|
|
|
from gns3server.compute.vpcs import VPCS
|
2017-06-30 08:22:30 +00:00
|
|
|
from gns3server.compute.nios.nio_udp import NIOUDP
|
2016-05-11 17:35:36 +00:00
|
|
|
|
|
|
|
|
2016-10-24 19:39:35 +00:00
|
|
|
@pytest.fixture(scope="function")
|
2020-06-16 04:29:03 +00:00
|
|
|
async def manager(loop, port_manager):
|
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
m = VPCS.instance()
|
|
|
|
m.port_manager = port_manager
|
|
|
|
return m
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
2020-06-16 04:29:03 +00:00
|
|
|
def node(compute_project, manager):
|
|
|
|
|
|
|
|
return VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", compute_project, manager)
|
2016-05-11 17:35:36 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
def test_temporary_directory(compute_project, manager):
|
|
|
|
|
|
|
|
node = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", compute_project, manager)
|
2016-05-11 17:35:36 +00:00
|
|
|
assert isinstance(node.temporary_directory, str)
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
def test_console(compute_project, manager):
|
|
|
|
|
|
|
|
node = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", compute_project, manager)
|
2016-05-11 17:35:36 +00:00
|
|
|
node.console = 5011
|
|
|
|
assert node.console == 5011
|
|
|
|
node.console = None
|
|
|
|
assert node.console is None
|
|
|
|
|
|
|
|
|
|
|
|
def test_change_console_port(node, port_manager):
|
2020-06-16 04:29:03 +00:00
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
port1 = port_manager.get_free_tcp_port(node.project)
|
|
|
|
port2 = port_manager.get_free_tcp_port(node.project)
|
|
|
|
port_manager.release_tcp_port(port1, node.project)
|
|
|
|
port_manager.release_tcp_port(port2, node.project)
|
|
|
|
node.console = port1
|
|
|
|
node.console = port2
|
|
|
|
assert node.console == port2
|
|
|
|
port_manager.reserve_tcp_port(port1, node.project)
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
def test_console_vnc_invalid(compute_project, manager):
|
|
|
|
|
|
|
|
node = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", compute_project, manager)
|
2016-10-24 10:21:57 +00:00
|
|
|
node._console_type = "vnc"
|
2016-05-11 17:35:36 +00:00
|
|
|
with pytest.raises(NodeError):
|
|
|
|
node.console = 2012
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_close(node, port_manager):
|
2016-05-11 17:35:36 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
assert node.console is not None
|
2016-05-11 17:35:36 +00:00
|
|
|
aux = port_manager.get_free_tcp_port(node.project)
|
|
|
|
port_manager.release_tcp_port(aux, node.project)
|
|
|
|
|
|
|
|
node.aux = aux
|
|
|
|
port = node.console
|
2020-06-16 04:29:03 +00:00
|
|
|
assert await node.close()
|
2016-05-11 17:35:36 +00:00
|
|
|
# Raise an exception if the port is not free
|
|
|
|
port_manager.reserve_tcp_port(port, node.project)
|
|
|
|
# Raise an exception if the port is not free
|
|
|
|
port_manager.reserve_tcp_port(aux, node.project)
|
|
|
|
assert node.console is None
|
|
|
|
assert node.aux is None
|
|
|
|
|
|
|
|
# Called twice closed should return False
|
2020-06-16 04:29:03 +00:00
|
|
|
assert await node.close() is False
|
2016-05-11 17:35:36 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
def test_aux(compute_project, manager, port_manager):
|
2016-05-11 17:35:36 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
aux = port_manager.get_free_tcp_port(compute_project)
|
|
|
|
port_manager.release_tcp_port(aux, compute_project)
|
|
|
|
|
2020-07-29 06:53:51 +00:00
|
|
|
node = DockerVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", compute_project, manager, "ubuntu", aux=aux, aux_type="telnet")
|
2016-05-11 17:35:36 +00:00
|
|
|
assert node.aux == aux
|
|
|
|
node.aux = None
|
|
|
|
assert node.aux is None
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
def test_allocate_aux(compute_project, manager):
|
|
|
|
|
|
|
|
node = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", compute_project, manager)
|
2016-05-11 17:35:36 +00:00
|
|
|
assert node.aux is None
|
|
|
|
|
|
|
|
# Docker has an aux port by default
|
2020-07-29 06:53:51 +00:00
|
|
|
node = DockerVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", compute_project, manager, "ubuntu", aux_type="telnet")
|
2016-05-11 17:35:36 +00:00
|
|
|
assert node.aux is not None
|
|
|
|
|
|
|
|
|
2020-07-29 06:53:51 +00:00
|
|
|
def test_change_aux_port(compute_project, manager, port_manager):
|
2020-06-16 04:29:03 +00:00
|
|
|
|
2020-07-29 06:53:51 +00:00
|
|
|
node = DockerVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", compute_project, manager, "ubuntu", aux_type="telnet")
|
2016-05-11 17:35:36 +00:00
|
|
|
port1 = port_manager.get_free_tcp_port(node.project)
|
|
|
|
port2 = port_manager.get_free_tcp_port(node.project)
|
|
|
|
port_manager.release_tcp_port(port1, node.project)
|
|
|
|
port_manager.release_tcp_port(port2, node.project)
|
|
|
|
node.aux = port1
|
|
|
|
node.aux = port2
|
|
|
|
assert node.aux == port2
|
|
|
|
port_manager.reserve_tcp_port(port1, node.project)
|
2017-06-30 08:22:30 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_update_ubridge_udp_connection(node):
|
|
|
|
|
2017-07-17 12:22:05 +00:00
|
|
|
filters = {
|
|
|
|
"latency": [10]
|
|
|
|
}
|
2017-06-30 08:22:30 +00:00
|
|
|
|
2018-03-19 11:25:56 +00:00
|
|
|
snio = NIOUDP(1245, "localhost", 1246)
|
|
|
|
dnio = NIOUDP(1245, "localhost", 1244)
|
|
|
|
dnio.filters = filters
|
2017-06-30 08:22:30 +00:00
|
|
|
with asyncio_patch("gns3server.compute.base_node.BaseNode._ubridge_apply_filters") as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
await node.update_ubridge_udp_connection('VPCS-10', snio, dnio)
|
2017-06-30 08:22:30 +00:00
|
|
|
mock.assert_called_with("VPCS-10", filters)
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_ubridge_apply_filters(node):
|
|
|
|
|
2017-09-05 09:07:10 +00:00
|
|
|
filters = OrderedDict((
|
|
|
|
('latency', [10]),
|
|
|
|
('bpf', ["icmp[icmptype] == 8\ntcp src port 53"])
|
|
|
|
))
|
2017-06-30 08:22:30 +00:00
|
|
|
node._ubridge_send = AsyncioMagicMock()
|
2020-06-16 04:29:03 +00:00
|
|
|
await node._ubridge_apply_filters("VPCS-10", filters)
|
2017-06-30 08:22:30 +00:00
|
|
|
node._ubridge_send.assert_any_call("bridge reset_packet_filters VPCS-10")
|
|
|
|
node._ubridge_send.assert_any_call("bridge add_packet_filter VPCS-10 filter0 latency 10")
|
2017-07-11 15:30:29 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_ubridge_apply_bpf_filters(node):
|
|
|
|
|
2017-07-11 15:30:29 +00:00
|
|
|
filters = {
|
|
|
|
"bpf": ["icmp[icmptype] == 8\ntcp src port 53"]
|
|
|
|
}
|
|
|
|
node._ubridge_send = AsyncioMagicMock()
|
2020-06-16 04:29:03 +00:00
|
|
|
await node._ubridge_apply_filters("VPCS-10", filters)
|
2017-07-11 15:30:29 +00:00
|
|
|
node._ubridge_send.assert_any_call("bridge reset_packet_filters VPCS-10")
|
|
|
|
node._ubridge_send.assert_any_call("bridge add_packet_filter VPCS-10 filter0 bpf \"icmp[icmptype] == 8\"")
|
|
|
|
node._ubridge_send.assert_any_call("bridge add_packet_filter VPCS-10 filter1 bpf \"tcp src port 53\"")
|