1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-12 18:18:16 +00:00
gns3-server/tests/modules/vpcs/test_vpcs_vm.py

110 lines
4.6 KiB
Python
Raw Normal View History

2015-01-14 17:52:02 +00: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
import asyncio
from tests.utils import asyncio_patch
2015-01-20 12:04:20 +00:00
# TODO: Move loop to util
2015-01-20 11:46:15 +00:00
from tests.api.base import loop, project
from asyncio.subprocess import Process
2015-01-15 13:27:33 +00:00
from unittest.mock import patch, MagicMock
2015-01-20 12:12:26 +00:00
from gns3server.modules.vpcs.vpcs_vm import VPCSVM
2015-01-14 17:52:02 +00:00
from gns3server.modules.vpcs.vpcs_error import VPCSError
2015-01-19 10:22:24 +00:00
from gns3server.modules.vpcs import VPCS
from gns3server.modules.port_manager import PortManager
2015-01-20 11:46:15 +00:00
2015-01-19 10:22:24 +00:00
@pytest.fixture(scope="module")
def manager():
m = VPCS.instance()
m.port_manager = PortManager("127.0.0.1", False)
return m
2015-01-14 17:52:02 +00:00
2015-01-20 11:46:15 +00:00
2015-01-14 17:52:02 +00:00
@patch("subprocess.check_output", return_value="Welcome to Virtual PC Simulator, version 0.6".encode("utf-8"))
def test_vm(project, manager):
2015-01-20 12:12:26 +00:00
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
2015-01-14 17:52:02 +00:00
assert vm.name == "test"
2015-01-20 08:58:58 +00:00
assert vm.uuid == "00010203-0405-0607-0809-0a0b0c0d0e0f"
2015-01-14 17:52:02 +00:00
2015-01-20 12:04:20 +00:00
2015-01-14 17:52:02 +00:00
@patch("subprocess.check_output", return_value="Welcome to Virtual PC Simulator, version 0.1".encode("utf-8"))
2015-01-20 11:46:15 +00:00
def test_vm_invalid_vpcs_version(project, manager):
2015-01-14 17:52:02 +00:00
with pytest.raises(VPCSError):
2015-01-20 12:12:26 +00:00
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
2015-01-14 17:52:02 +00:00
assert vm.name == "test"
2015-01-20 08:58:58 +00:00
assert vm.uuid == "00010203-0405-0607-0809-0a0b0c0d0e0f"
2015-01-14 17:52:02 +00:00
2015-01-20 12:04:20 +00:00
@patch("gns3server.config.Config.get_section_config", return_value={"path": "/bin/test_fake"})
2015-01-20 11:46:15 +00:00
def test_vm_invalid_vpcs_path(project, manager):
2015-01-14 17:52:02 +00:00
with pytest.raises(VPCSError):
2015-01-20 12:12:26 +00:00
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
2015-01-14 17:52:02 +00:00
assert vm.name == "test"
2015-01-20 08:58:58 +00:00
assert vm.uuid == "00010203-0405-0607-0809-0a0b0c0d0e0f"
2015-01-14 17:52:02 +00:00
2015-01-20 12:04:20 +00:00
2015-01-20 11:46:15 +00:00
def test_start(project, loop, manager):
2015-01-15 13:27:33 +00:00
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
2015-01-20 12:12:26 +00:00
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
nio = vm.port_add_nio_binding(0, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
2015-01-15 13:27:33 +00:00
loop.run_until_complete(asyncio.async(vm.start()))
2015-01-20 12:04:20 +00:00
assert vm.is_running()
2015-01-15 13:27:33 +00:00
2015-01-20 11:46:15 +00:00
def test_stop(project, loop, manager):
2015-01-15 13:27:33 +00:00
process = MagicMock()
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
2015-01-20 12:12:26 +00:00
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
nio = vm.port_add_nio_binding(0, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
loop.run_until_complete(asyncio.async(vm.start()))
2015-01-20 12:04:20 +00:00
assert vm.is_running()
2015-01-15 13:27:33 +00:00
loop.run_until_complete(asyncio.async(vm.stop()))
2015-01-20 12:04:20 +00:00
assert vm.is_running() is False
2015-01-15 13:27:33 +00:00
process.terminate.assert_called_with()
2015-01-20 12:04:20 +00:00
def test_add_nio_binding_udp(manager, project):
2015-01-20 12:12:26 +00:00
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
2015-01-16 15:20:10 +00:00
nio = vm.port_add_nio_binding(0, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
assert nio.lport == 4242
2015-01-20 12:04:20 +00:00
2015-01-20 11:46:15 +00:00
def test_add_nio_binding_tap(project, manager):
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
2015-01-20 12:12:26 +00:00
with patch("gns3server.modules.vpcs.vpcs_vm.has_privileged_access", return_value=True):
2015-01-16 15:20:10 +00:00
nio = vm.port_add_nio_binding(0, {"type": "nio_tap", "tap_device": "test"})
assert nio.tap_device == "test"
2015-01-20 12:04:20 +00:00
def test_add_nio_binding_tap_no_privileged_access(manager, project):
2015-01-20 12:12:26 +00:00
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
with patch("gns3server.modules.vpcs.vpcs_vm.has_privileged_access", return_value=False):
2015-01-16 15:20:10 +00:00
with pytest.raises(VPCSError):
vm.port_add_nio_binding(0, {"type": "nio_tap", "tap_device": "test"})
2015-01-19 10:28:51 +00:00
assert vm._ethernet_adapter.ports[0] is None
2015-01-16 20:39:58 +00:00
2015-01-20 12:04:20 +00:00
def test_port_remove_nio_binding(manager, project):
2015-01-20 12:12:26 +00:00
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
2015-01-16 20:39:58 +00:00
nio = vm.port_add_nio_binding(0, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
vm.port_remove_nio_binding(0)
2015-01-19 10:28:51 +00:00
assert vm._ethernet_adapter.ports[0] is None