gns3-server/tests/compute/vpcs/test_vpcs_vm.py

338 lines
13 KiB
Python
Raw Normal View History

2015-01-14 19:52:02 +02:00
# -*- coding: utf-8 -*-
#
# Copyright (C) 2020 GNS3 Technologies Inc.
2015-01-14 19:52:02 +02: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/>.
import pytest
import asyncio
2015-01-21 17:43:34 +02:00
import os
2015-04-28 00:12:13 +03:00
import sys
from tests.utils import asyncio_patch, AsyncioMagicMock
from gns3server.utils import parse_version
from unittest.mock import patch, MagicMock, ANY
2015-11-09 11:25:20 +02:00
2016-04-15 18:57:06 +03:00
from gns3server.compute.vpcs.vpcs_vm import VPCSVM
from gns3server.compute.vpcs.vpcs_error import VPCSError
from gns3server.compute.vpcs import VPCS
from gns3server.compute.notification_manager import NotificationManager
2015-01-19 12:22:24 +02:00
2015-01-20 13:46:15 +02:00
@pytest.fixture
2020-10-02 09:37:50 +03:00
@pytest.mark.asyncio
async 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")
2020-10-02 09:37:50 +03:00
@pytest.mark.asyncio
async def vm(compute_project, manager, tmpdir, ubridge_path):
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", compute_project, manager)
2015-11-09 11:25:20 +02:00
vm._vpcs_version = parse_version("0.9")
vm._start_ubridge = AsyncioMagicMock()
2017-07-11 14:42:47 +03:00
vm._ubridge_hypervisor = MagicMock()
vm._ubridge_hypervisor.is_running.return_value = True
2015-11-09 11:25:20 +02:00
return vm
2015-01-21 16:50:35 +02:00
2020-10-02 09:37:50 +03:00
@pytest.mark.asyncio
async def test_vm(compute_project, manager):
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", compute_project, manager)
2015-01-14 19:52:02 +02:00
assert vm.name == "test"
assert vm.id == "00010203-0405-0607-0809-0a0b0c0d0e0f"
2015-01-14 19:52:02 +02:00
2015-01-20 14:04:20 +02:00
2020-10-02 09:37:50 +03:00
@pytest.mark.asyncio
async def test_vm_check_vpcs_version(vm):
2016-04-15 18:57:06 +03:00
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.subprocess_check_output", return_value="Welcome to Virtual PC Simulator, version 0.9"):
2020-06-19 12:35:23 +03:00
await vm._check_vpcs_version()
2015-11-09 11:25:20 +02:00
assert vm._vpcs_version == parse_version("0.9")
2020-10-02 09:37:50 +03:00
@pytest.mark.asyncio
async def test_vm_check_vpcs_version_0_6_1(vm):
2016-04-15 18:57:06 +03:00
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.subprocess_check_output", return_value="Welcome to Virtual PC Simulator, version 0.6.1"):
await vm._check_vpcs_version()
2015-11-11 22:57:58 +02:00
assert vm._vpcs_version == parse_version("0.6.1")
2020-10-02 09:37:50 +03:00
@pytest.mark.asyncio
async def test_vm_invalid_vpcs_version(vm, manager):
2016-04-15 18:57:06 +03:00
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.subprocess_check_output", return_value="Welcome to Virtual PC Simulator, version 0.1"):
2015-01-21 00:27:28 +02:00
with pytest.raises(VPCSError):
2017-07-11 14:42:47 +03:00
nio = manager.create_nio({"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1", "filters": {}})
2020-06-19 12:35:23 +03:00
await vm.port_add_nio_binding(0, nio)
await vm._check_vpcs_version()
2015-01-21 00:27:28 +02:00
assert vm.name == "test"
assert vm.id == "00010203-0405-0607-0809-0a0b0c0d0e0f"
2015-01-14 19:52:02 +02:00
2015-01-20 14:04:20 +02:00
2020-10-02 09:37:50 +03:00
@pytest.mark.asyncio
async def test_vm_invalid_vpcs_path(vm, manager):
2017-02-20 11:56:48 +02:00
with patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM._vpcs_path", return_value="/tmp/fake/path/vpcs"):
2015-11-09 11:25:20 +02:00
with pytest.raises(VPCSError):
nio = manager.create_nio({"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
2020-06-19 12:35:23 +03:00
await vm.port_add_nio_binding(0, nio)
await vm.start()
2015-11-09 11:25:20 +02:00
assert vm.name == "test"
assert vm.id == "00010203-0405-0607-0809-0a0b0c0d0e0e"
2015-01-14 19:52:02 +02:00
2015-01-20 14:04:20 +02:00
2020-10-02 09:37:50 +03:00
@pytest.mark.asyncio
async def test_start(vm):
2015-03-26 18:43:00 +02:00
process = MagicMock()
process.returncode = None
2016-03-17 16:15:30 +02:00
with NotificationManager.instance().queue() as queue:
await queue.get(1) # Ping
2016-03-17 16:15:30 +02:00
2016-04-15 18:57:06 +03:00
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
2016-03-17 16:15:30 +02:00
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process) as mock_exec:
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.start_wrap_console"):
await vm.start()
2017-02-20 11:56:48 +02:00
assert mock_exec.call_args[0] == (vm._vpcs_path(),
'-p',
str(vm._internal_console_port),
'-m', '1',
'-i',
'1',
'-F',
'-R',
'-s',
ANY,
'-c',
ANY,
'-t',
'127.0.0.1')
2016-03-17 16:15:30 +02:00
assert vm.is_running()
assert vm.command_line == ' '.join(mock_exec.call_args[0])
(action, event, kwargs) = await queue.get(1)
2016-05-13 18:49:28 +03:00
assert action == "node.updated"
2016-03-17 16:15:30 +02:00
assert event == vm
2015-11-09 11:25:20 +02:00
2020-10-02 09:37:50 +03:00
@pytest.mark.asyncio
async def test_start_0_6_1(vm):
2015-11-09 11:25:20 +02:00
"""
Version 0.6.1 doesn't have the -R options. It's not require
because GNS3 provide a patch for this.
"""
2015-11-09 11:25:20 +02:00
process = MagicMock()
process.returncode = None
vm._vpcs_version = parse_version("0.6.1")
2016-04-15 18:57:06 +03:00
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.start_wrap_console"):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process) as mock_exec:
2017-07-11 14:42:47 +03:00
nio = VPCS.instance().create_nio({"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1", "filters": {}})
await vm.port_add_nio_binding(0, nio)
await vm.start()
2017-02-20 11:56:48 +02:00
assert mock_exec.call_args[0] == (vm._vpcs_path(),
'-p',
str(vm._internal_console_port),
'-m', '1',
'-i',
'1',
'-F',
'-s',
ANY,
'-c',
ANY,
'-t',
'127.0.0.1')
assert vm.is_running()
2015-01-20 14:04:20 +02:00
2015-01-15 15:27:33 +02:00
2020-10-02 09:37:50 +03:00
@pytest.mark.asyncio
async def test_stop(vm):
2015-02-10 18:24:38 +02:00
process = MagicMock()
2015-02-10 18:24:38 +02:00
# Wait process kill success
future = asyncio.Future()
future.set_result(True)
process.wait.return_value = future
2015-03-26 18:43:00 +02:00
process.returncode = None
2015-02-10 18:24:38 +02:00
2016-03-17 16:15:30 +02:00
with NotificationManager.instance().queue() as queue:
2016-04-15 18:57:06 +03:00
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.start_wrap_console"):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
2017-07-11 14:42:47 +03:00
nio = VPCS.instance().create_nio({"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1", "filters": {}})
await vm.port_add_nio_binding(0, nio)
2016-03-17 16:15:30 +02:00
await vm.start()
assert vm.is_running()
2015-03-26 18:43:00 +02:00
with asyncio_patch("gns3server.utils.asyncio.wait_for_process_termination"):
2020-06-19 12:35:23 +03:00
await vm.stop()
assert vm.is_running() is False
if sys.platform.startswith("win"):
process.send_signal.assert_called_with(1)
else:
process.terminate.assert_called_with()
2015-04-28 00:12:13 +03:00
await queue.get(1) #  Ping
await queue.get(1) #  Started
(action, event, kwargs) = await queue.get(1)
assert action == "node.updated"
assert event == vm
2015-01-20 14:04:20 +02:00
2020-10-02 09:37:50 +03:00
@pytest.mark.asyncio
async def test_reload(vm):
2015-02-10 18:24:38 +02:00
process = MagicMock()
2015-02-10 18:24:38 +02:00
# Wait process kill success
future = asyncio.Future()
future.set_result(True)
process.wait.return_value = future
2015-03-26 18:43:00 +02:00
process.returncode = None
2015-02-10 18:24:38 +02:00
2016-04-15 18:57:06 +03:00
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.start_wrap_console"):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
2017-07-11 14:42:47 +03:00
nio = VPCS.instance().create_nio({"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1", "filters": {}})
await vm.port_add_nio_binding(0, nio)
await vm.start()
assert vm.is_running()
2015-04-27 11:14:46 +03:00
2017-07-11 14:42:47 +03:00
vm._ubridge_send = AsyncioMagicMock()
with asyncio_patch("gns3server.utils.asyncio.wait_for_process_termination"):
await vm.reload()
assert vm.is_running() is True
2015-04-28 00:12:13 +03:00
if sys.platform.startswith("win"):
process.send_signal.assert_called_with(1)
else:
process.terminate.assert_called_with()
2015-01-22 11:55:11 +02:00
2020-10-02 09:37:50 +03:00
@pytest.mark.asyncio
async def test_add_nio_binding_udp(vm):
2017-07-11 14:42:47 +03:00
nio = VPCS.instance().create_nio({"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1", "filters": {}})
await 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-12-22 14:15:28 +02:00
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
2020-10-02 09:37:50 +03:00
@pytest.mark.asyncio
async def test_add_nio_binding_tap(vm, ethernet_device):
2016-04-15 18:57:06 +03:00
with patch("gns3server.compute.base_manager.BaseManager.has_privileged_access", return_value=True):
nio = VPCS.instance().create_nio({"type": "nio_tap", "tap_device": ethernet_device})
2020-06-19 12:35:23 +03:00
await vm.port_add_nio_binding(0, nio)
2015-06-10 15:33:44 +03:00
assert nio.tap_device == ethernet_device
2015-01-16 17:20:10 +02:00
2015-01-20 14:04:20 +02:00
2020-10-02 09:37:50 +03:00
@pytest.mark.asyncio
async def test_port_remove_nio_binding(vm):
nio = VPCS.instance().create_nio({"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
2020-06-19 12:35:23 +03:00
await vm.port_add_nio_binding(0, nio)
await 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):
2015-01-21 17:43:34 +02:00
content = "echo GNS3 VPCS\nip 192.168.1.2\n"
vm.startup_script = content
filepath = os.path.join(vm.working_dir, 'startup.vpc')
2015-01-21 17:43:34 +02:00
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 = "set pcname %h\n"
2015-02-03 22:48:20 +02:00
vm.name = "pc1"
vm.startup_script = content
assert os.path.exists(vm.script_file)
with open(vm.script_file) as f:
assert f.read() == "set pcname pc1\n"
2015-02-03 22:48:20 +02:00
2017-06-08 16:23:18 +03:00
def test_update_startup_script_with_escaping_characters_in_name(vm):
2017-06-08 16:23:18 +03:00
vm.startup_script = "set pcname initial-name\n"
vm.name = "test\\"
assert vm.startup_script == "set pcname test{}".format(os.linesep)
2017-06-08 16:23:18 +03:00
2015-01-21 17:43:34 +02:00
def test_get_startup_script(vm):
2015-05-26 12:51:24 +03:00
content = "echo GNS3 VPCS\nip 192.168.1.2"
2015-01-21 17:43:34 +02:00
vm.startup_script = content
2015-06-10 15:35:53 +03:00
assert vm.startup_script == os.linesep.join(["echo GNS3 VPCS", "ip 192.168.1.2"])
2015-01-21 22:46:16 +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
filepath = os.path.join(vm.working_dir, 'startup.vpc')
2015-04-28 00:28:12 +03:00
with open(filepath, 'wb+') as f:
assert f.write(content.encode("utf-8"))
assert vm.startup_script == content
assert vm.script_file == filepath
def test_change_name(vm):
path = os.path.join(vm.working_dir, 'startup.vpc')
2015-01-21 22:46:16 +02:00
vm.name = "world"
with open(path, 'w+') as f:
f.write("set pcname world")
2015-01-21 22:46:16 +02:00
vm.name = "hello"
assert vm.name == "hello"
with open(path) as f:
assert f.read() == "set pcname hello"
# Support when the name is not sync with config
with open(path, 'w+') as f:
f.write("set pcname alpha")
vm.name = "beta"
assert vm.name == "beta"
with open(path) as f:
assert f.read() == "set pcname beta"
2015-01-21 22:46:16 +02:00
2020-10-02 09:37:50 +03:00
@pytest.mark.asyncio
async def test_close(vm):
2016-04-15 18:57:06 +03:00
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
2015-01-22 12:34:10 +02:00
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.start_wrap_console"):
2020-06-19 12:35:23 +03:00
await vm.start()
await vm.close()
assert vm.is_running() is False