2015-01-14 19:52:02 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2015 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
|
2015-01-23 03:04:24 +02:00
|
|
|
import aiohttp
|
2015-01-15 14:02:43 +02:00
|
|
|
import asyncio
|
2015-01-21 17:43:34 +02:00
|
|
|
import os
|
2015-01-21 23:32:33 +02:00
|
|
|
from tests.utils import asyncio_patch
|
|
|
|
|
2015-01-15 14:02:43 +02:00
|
|
|
|
2015-01-15 15:27:33 +02:00
|
|
|
from unittest.mock import patch, MagicMock
|
2015-01-20 14:12:26 +02:00
|
|
|
from gns3server.modules.vpcs.vpcs_vm import VPCSVM
|
2015-01-14 19:52:02 +02:00
|
|
|
from gns3server.modules.vpcs.vpcs_error import VPCSError
|
2015-01-19 12:22:24 +02:00
|
|
|
from gns3server.modules.vpcs import VPCS
|
|
|
|
|
2015-01-20 13:46:15 +02:00
|
|
|
|
2015-01-19 12:22:24 +02:00
|
|
|
@pytest.fixture(scope="module")
|
2015-01-21 23:21:01 +02:00
|
|
|
def manager(port_manager):
|
2015-01-19 12:22:24 +02:00
|
|
|
m = VPCS.instance()
|
2015-01-21 23:21:01 +02:00
|
|
|
m.port_manager = port_manager
|
2015-01-19 12:22:24 +02:00
|
|
|
return m
|
2015-01-14 19:52:02 +02:00
|
|
|
|
2015-01-20 13:46:15 +02:00
|
|
|
|
2015-01-21 16:50:35 +02:00
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def vm(project, manager):
|
|
|
|
return VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
|
|
|
|
|
|
|
|
|
2015-01-20 15:31:47 +02:00
|
|
|
def test_vm(project, manager):
|
2015-01-20 14:12:26 +02:00
|
|
|
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
|
2015-01-14 19:52:02 +02:00
|
|
|
assert vm.name == "test"
|
2015-01-20 10:58:58 +02:00
|
|
|
assert vm.uuid == "00010203-0405-0607-0809-0a0b0c0d0e0f"
|
2015-01-14 19:52:02 +02:00
|
|
|
|
2015-01-20 14:04:20 +02:00
|
|
|
|
2015-01-21 16:50:35 +02:00
|
|
|
def test_vm_invalid_vpcs_version(loop, project, manager):
|
2015-01-21 00:27:28 +02:00
|
|
|
with asyncio_patch("gns3server.modules.vpcs.vpcs_vm.VPCSVM._get_vpcs_welcome", return_value="Welcome to Virtual PC Simulator, version 0.1"):
|
|
|
|
with pytest.raises(VPCSError):
|
|
|
|
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
|
2015-01-23 03:04:24 +02:00
|
|
|
nio = manager.create_nio(vm.vpcs_path, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
|
|
|
|
vm.port_add_nio_binding(0, nio)
|
2015-01-21 00:27:28 +02:00
|
|
|
loop.run_until_complete(asyncio.async(vm.start()))
|
|
|
|
assert vm.name == "test"
|
|
|
|
assert vm.uuid == "00010203-0405-0607-0809-0a0b0c0d0e0f"
|
2015-01-14 19:52:02 +02:00
|
|
|
|
2015-01-20 14:04:20 +02:00
|
|
|
|
2015-02-03 04:41:26 +02:00
|
|
|
@patch("gns3server.config.Config.get_section_config", return_value={"vpcs_path": "/bin/test_fake"})
|
2015-01-20 23:50:26 +02:00
|
|
|
def test_vm_invalid_vpcs_path(project, manager, loop):
|
2015-01-14 19:52:02 +02:00
|
|
|
with pytest.raises(VPCSError):
|
2015-01-21 16:50:35 +02:00
|
|
|
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0e", project, manager)
|
2015-01-23 03:04:24 +02:00
|
|
|
nio = manager.create_nio(vm.vpcs_path, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
|
|
|
|
vm.port_add_nio_binding(0, nio)
|
2015-01-20 23:50:26 +02:00
|
|
|
loop.run_until_complete(asyncio.async(vm.start()))
|
2015-01-14 19:52:02 +02:00
|
|
|
assert vm.name == "test"
|
2015-01-21 16:50:35 +02:00
|
|
|
assert vm.uuid == "00010203-0405-0607-0809-0a0b0c0d0e0e"
|
2015-01-14 19:52:02 +02:00
|
|
|
|
2015-01-20 14:04:20 +02:00
|
|
|
|
2015-01-21 16:50:35 +02:00
|
|
|
def test_start(loop, vm):
|
2015-01-21 00:27:28 +02:00
|
|
|
with asyncio_patch("gns3server.modules.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
|
2015-01-23 03:04:24 +02:00
|
|
|
nio = VPCS.instance().create_nio(vm.vpcs_path, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
|
|
|
|
vm.port_add_nio_binding(0, nio)
|
2015-01-21 00:27:28 +02:00
|
|
|
loop.run_until_complete(asyncio.async(vm.start()))
|
|
|
|
assert vm.is_running()
|
2015-01-20 14:04:20 +02:00
|
|
|
|
2015-01-15 15:27:33 +02:00
|
|
|
|
2015-01-21 16:50:35 +02:00
|
|
|
def test_stop(loop, vm):
|
2015-01-15 15:27:33 +02:00
|
|
|
process = MagicMock()
|
2015-01-21 00:27:28 +02:00
|
|
|
with asyncio_patch("gns3server.modules.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
|
2015-01-23 03:04:24 +02:00
|
|
|
nio = VPCS.instance().create_nio(vm.vpcs_path, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
|
|
|
|
vm.port_add_nio_binding(0, nio)
|
2015-01-21 00:27:28 +02:00
|
|
|
loop.run_until_complete(asyncio.async(vm.start()))
|
|
|
|
assert vm.is_running()
|
|
|
|
loop.run_until_complete(asyncio.async(vm.stop()))
|
|
|
|
assert vm.is_running() is False
|
|
|
|
process.terminate.assert_called_with()
|
2015-01-15 14:02:43 +02:00
|
|
|
|
2015-01-20 14:04:20 +02:00
|
|
|
|
2015-01-22 11:55:11 +02:00
|
|
|
def test_reload(loop, vm):
|
|
|
|
process = MagicMock()
|
|
|
|
with asyncio_patch("gns3server.modules.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
|
2015-01-23 03:04:24 +02:00
|
|
|
nio = VPCS.instance().create_nio(vm.vpcs_path, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
|
|
|
|
vm.port_add_nio_binding(0, nio)
|
2015-01-22 11:55:11 +02:00
|
|
|
loop.run_until_complete(asyncio.async(vm.start()))
|
|
|
|
assert vm.is_running()
|
|
|
|
loop.run_until_complete(asyncio.async(vm.reload()))
|
|
|
|
assert vm.is_running() is True
|
|
|
|
process.terminate.assert_called_with()
|
|
|
|
|
|
|
|
|
2015-01-21 16:50:35 +02:00
|
|
|
def test_add_nio_binding_udp(vm):
|
2015-01-23 03:04:24 +02:00
|
|
|
nio = VPCS.instance().create_nio(vm.vpcs_path, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
|
|
|
|
vm.port_add_nio_binding(0, nio)
|
2015-01-16 17:20:10 +02:00
|
|
|
assert nio.lport == 4242
|
|
|
|
|
2015-01-20 14:04:20 +02:00
|
|
|
|
2015-01-21 16:50:35 +02:00
|
|
|
def test_add_nio_binding_tap(vm):
|
2015-01-23 03:04:24 +02:00
|
|
|
with patch("gns3server.modules.base_manager.BaseManager._has_privileged_access", return_value=True):
|
|
|
|
nio = VPCS.instance().create_nio(vm.vpcs_path, {"type": "nio_tap", "tap_device": "test"})
|
|
|
|
vm.port_add_nio_binding(0, nio)
|
2015-01-16 17:20:10 +02:00
|
|
|
assert nio.tap_device == "test"
|
|
|
|
|
2015-01-20 14:04:20 +02:00
|
|
|
|
2015-01-21 16:50:35 +02:00
|
|
|
def test_add_nio_binding_tap_no_privileged_access(vm):
|
2015-01-23 03:04:24 +02:00
|
|
|
with patch("gns3server.modules.base_manager.BaseManager._has_privileged_access", return_value=False):
|
|
|
|
with pytest.raises(aiohttp.web.HTTPForbidden):
|
|
|
|
nio = VPCS.instance().create_nio(vm.vpcs_path, {"type": "nio_tap", "tap_device": "test"})
|
|
|
|
vm.port_add_nio_binding(0, nio)
|
2015-01-19 12:28:51 +02:00
|
|
|
assert vm._ethernet_adapter.ports[0] is None
|
2015-01-16 22:39:58 +02:00
|
|
|
|
2015-01-20 14:04:20 +02:00
|
|
|
|
2015-01-21 16:50:35 +02:00
|
|
|
def test_port_remove_nio_binding(vm):
|
2015-01-23 03:04:24 +02:00
|
|
|
nio = VPCS.instance().create_nio(vm.vpcs_path, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
|
|
|
|
vm.port_add_nio_binding(0, nio)
|
2015-01-16 22:39:58 +02:00
|
|
|
vm.port_remove_nio_binding(0)
|
2015-01-19 12:28:51 +02:00
|
|
|
assert vm._ethernet_adapter.ports[0] is None
|
2015-01-21 17:43:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_update_startup_script(vm):
|
|
|
|
content = "echo GNS3 VPCS\nip 192.168.1.2\n"
|
|
|
|
vm.startup_script = content
|
|
|
|
filepath = os.path.join(vm.working_dir, 'startup.vpcs')
|
|
|
|
assert os.path.exists(filepath)
|
|
|
|
with open(filepath) as f:
|
|
|
|
assert f.read() == content
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_startup_script(vm):
|
|
|
|
content = "echo GNS3 VPCS\nip 192.168.1.2\n"
|
|
|
|
vm.startup_script = content
|
|
|
|
filepath = os.path.join(vm.working_dir, 'startup.vpcs')
|
|
|
|
assert os.path.exists(filepath)
|
|
|
|
with open(filepath) as f:
|
|
|
|
assert f.read() == content
|
|
|
|
|
|
|
|
|
2015-02-03 22:48:20 +02:00
|
|
|
def test_update_startup_script_h(vm):
|
|
|
|
content = "setname %h\n"
|
|
|
|
vm.name = "pc1"
|
|
|
|
vm.startup_script = content
|
|
|
|
assert os.path.exists(vm.script_file)
|
|
|
|
with open(vm.script_file) as f:
|
|
|
|
assert f.read() == "setname pc1\n"
|
|
|
|
|
|
|
|
|
2015-01-21 17:43:34 +02:00
|
|
|
def test_get_startup_script(vm):
|
|
|
|
content = "echo GNS3 VPCS\nip 192.168.1.2\n"
|
|
|
|
vm.startup_script = content
|
|
|
|
assert vm.startup_script == content
|
2015-01-21 22:46:16 +02:00
|
|
|
|
|
|
|
|
2015-01-30 15:57:25 +02:00
|
|
|
def test_get_startup_script_using_default_script(vm):
|
|
|
|
content = "echo GNS3 VPCS\nip 192.168.1.2\n"
|
|
|
|
|
|
|
|
# Reset script file location
|
2015-02-03 22:48:20 +02:00
|
|
|
vm._script_file = None
|
2015-01-30 15:57:25 +02:00
|
|
|
|
|
|
|
filepath = os.path.join(vm.working_dir, 'startup.vpc')
|
|
|
|
with open(filepath, 'w+') as f:
|
|
|
|
assert f.write(content)
|
|
|
|
|
|
|
|
assert vm.startup_script == content
|
|
|
|
assert vm.script_file == filepath
|
|
|
|
|
|
|
|
|
2015-01-21 23:06:25 +02:00
|
|
|
def test_change_console_port(vm, port_manager):
|
|
|
|
port1 = port_manager.get_free_console_port()
|
|
|
|
port2 = port_manager.get_free_console_port()
|
|
|
|
port_manager.release_console_port(port1)
|
|
|
|
port_manager.release_console_port(port2)
|
2015-01-21 23:21:01 +02:00
|
|
|
print(vm.console)
|
|
|
|
print(port1)
|
2015-01-21 23:06:25 +02:00
|
|
|
vm.console = port1
|
|
|
|
vm.console = port2
|
|
|
|
assert vm.console == port2
|
2015-01-21 23:21:01 +02:00
|
|
|
port_manager.reserve_console_port(port1)
|
2015-01-21 22:46:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_change_name(vm, tmpdir):
|
|
|
|
path = os.path.join(str(tmpdir), 'startup.vpcs')
|
|
|
|
vm.name = "world"
|
|
|
|
with open(path, 'w+') as f:
|
|
|
|
f.write("name world")
|
2015-02-03 22:48:20 +02:00
|
|
|
vm._script_file = path
|
2015-01-21 22:46:16 +02:00
|
|
|
vm.name = "hello"
|
|
|
|
assert vm.name == "hello"
|
|
|
|
with open(path) as f:
|
|
|
|
assert f.read() == "name hello"
|
|
|
|
|
|
|
|
|
2015-01-23 04:06:17 +02:00
|
|
|
def test_close(vm, port_manager):
|
2015-01-22 12:34:10 +02:00
|
|
|
with asyncio_patch("gns3server.modules.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
|
|
|
|
vm.start()
|
|
|
|
port = vm.console
|
2015-01-23 04:06:17 +02:00
|
|
|
vm.close()
|
2015-01-22 12:34:10 +02:00
|
|
|
# Raise an exception if the port is not free
|
|
|
|
port_manager.reserve_console_port(port)
|
|
|
|
assert vm.is_running() is False
|