def test_start(loop, vm):
    process = MagicMock()
    process.returncode = None
    queue = vm.project.get_listen_queue()

    with asyncio_patch("gns3server.modules.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
        with asyncio_patch("asyncio.create_subprocess_exec", return_value=process) as mock_exec:
            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)
            loop.run_until_complete(asyncio.async(vm.start()))
            assert mock_exec.call_args[0] == (vm.vpcs_path,
                                              '-p',
                                              str(vm.console),
                                              '-m', '1',
                                              '-i',
                                              '1',
                                              '-F',
                                              '-R',
                                              '-s',
                                              '4242',
                                              '-c',
                                              '4243',
                                              '-t',
                                              '127.0.0.1')
            assert vm.is_running()
            assert vm.command_line == ' '.join(mock_exec.call_args[0])
    (action, event) = queue.get_nowait()
    assert action == "vm.started"
    assert event == vm
Esempio n. 2
0
def test_start_0_6_1(loop, vm):
    """
    Version 0.6.1 doesn't have the -R options. It's not require
    because GNS3 provide a patch for this.
    """
    process = MagicMock()
    process.returncode = None
    queue = vm.project.get_listen_queue()
    vm._vpcs_version = parse_version("0.6.1")

    with asyncio_patch(
            "gns3server.modules.vpcs.vpcs_vm.VPCSVM._check_requirements",
            return_value=True):
        with asyncio_patch("asyncio.create_subprocess_exec",
                           return_value=process) as mock_exec:
            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)
            loop.run_until_complete(asyncio. async (vm.start()))
            assert mock_exec.call_args[0] == (vm.vpcs_path, '-p',
                                              str(vm.console), '-m', '1', '-i',
                                              '1', '-F', '-s', '4242', '-c',
                                              '4243', '-t', '127.0.0.1')
            assert vm.is_running()
    (action, event) = queue.get_nowait()
    assert action == "vm.started"
    assert event == vm
Esempio n. 3
0
def test_reload(loop, vm):
    process = MagicMock()

    # Wait process kill success
    future = asyncio.Future()
    future.set_result(True)
    process.wait.return_value = future
    process.returncode = None

    with asyncio_patch(
            "gns3server.modules.vpcs.vpcs_vm.VPCSVM._check_requirements",
            return_value=True):
        with asyncio_patch("asyncio.create_subprocess_exec",
                           return_value=process):
            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)
            loop.run_until_complete(asyncio. async (vm.start()))
            assert vm.is_running()

            with asyncio_patch(
                    "gns3server.utils.asyncio.wait_for_process_termination"):
                loop.run_until_complete(asyncio. async (vm.reload()))
            assert vm.is_running() is True

            if sys.platform.startswith("win"):
                process.send_signal.assert_called_with(1)
            else:
                process.terminate.assert_called_with()
def test_start_0_6_1(loop, vm):
    """
    Version 0.6.1 doesn't have the -R options. It's not require
    because GNS3 provide a patch for this.
    """
    process = MagicMock()
    process.returncode = None
    queue = vm.project.get_listen_queue()
    vm._vpcs_version = parse_version("0.6.1")

    with asyncio_patch("gns3server.modules.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
        with asyncio_patch("asyncio.create_subprocess_exec", return_value=process) as mock_exec:
            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)
            loop.run_until_complete(asyncio.async(vm.start()))
            assert mock_exec.call_args[0] == (vm.vpcs_path,
                                              '-p',
                                              str(vm.console),
                                              '-m', '1',
                                              '-i',
                                              '1',
                                              '-F',
                                              '-s',
                                              '4242',
                                              '-c',
                                              '4243',
                                              '-t',
                                              '127.0.0.1')
            assert vm.is_running()
    (action, event) = queue.get_nowait()
    assert action == "vm.started"
    assert event == vm
Esempio n. 5
0
def test_create_vm_old_topology(loop, project, tmpdir, port_manager):

    with patch("gns3server.modules.project.Project.is_local",
               return_value=True):
        # Create an old topology directory
        project_dir = str(tmpdir / "testold")
        vm_dir = os.path.join(project_dir, "testold-files", "vpcs", "pc-1")
        project.path = project_dir
        project.name = "testold"
        os.makedirs(vm_dir, exist_ok=True)
        with open(os.path.join(vm_dir, "startup.vpc"), "w+") as f:
            f.write("1")

        VPCS._instance = None
        vpcs = VPCS.instance()
        vpcs.port_manager = port_manager
        vm_id = 1
        vm = loop.run_until_complete(vpcs.create_vm("PC 1", project.id, vm_id))
        assert len(vm.id) == 36

        assert os.path.exists(os.path.join(project_dir,
                                           "testold-files")) is False

        vm_dir = os.path.join(project_dir, "project-files", "vpcs", vm.id)
        with open(os.path.join(vm_dir, "startup.vpc")) as f:
            assert f.read() == "1"
Esempio n. 6
0
def test_stop(loop, vm):
    process = MagicMock()

    # Wait process kill success
    future = asyncio.Future()
    future.set_result(True)
    process.wait.return_value = future
    process.returncode = None

    with asyncio_patch("gns3server.modules.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
        with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
            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)

            loop.run_until_complete(asyncio.async(vm.start()))
            assert vm.is_running()

            with asyncio_patch("gns3server.utils.asyncio.wait_for_process_termination"):
                loop.run_until_complete(asyncio.async(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()
Esempio n. 7
0
def test_port_remove_nio_binding(vm):
    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)
    vm.port_remove_nio_binding(0)
    assert vm._ethernet_adapter.ports[0] is None
Esempio n. 8
0
def test_start(loop, vm):
    process = MagicMock()
    process.returncode = None
    queue = vm.project.get_listen_queue()

    with asyncio_patch(
            "gns3server.modules.vpcs.vpcs_vm.VPCSVM._check_requirements",
            return_value=True):
        with asyncio_patch("asyncio.create_subprocess_exec",
                           return_value=process) as mock_exec:
            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)
            loop.run_until_complete(asyncio. async (vm.start()))
            assert mock_exec.call_args[0] == (vm.vpcs_path, '-p',
                                              str(vm.console), '-m', '1', '-i',
                                              '1', '-F', '-R', '-s', '4242',
                                              '-c', '4243', '-t', '127.0.0.1')
            assert vm.is_running()
            assert vm.command_line == ' '.join(mock_exec.call_args[0])
    (action, event) = queue.get_nowait()
    assert action == "vm.started"
    assert event == vm
Esempio n. 9
0
def test_create_vm_new_topology_without_uuid(loop, project, port_manager):

    VPCS._instance = None
    vpcs = VPCS.instance()
    vpcs.port_manager = port_manager
    vm = loop.run_until_complete(vpcs.create_vm("PC 1", project.id, None))
    assert vm in project.vms
    assert len(vm.id) == 36
Esempio n. 10
0
def test_create_vm_new_topology(loop, project, port_manager):

    VPCS._instance = None
    vpcs = VPCS.instance()
    vpcs.port_manager = port_manager
    vm_id = str(uuid.uuid4())
    vm = loop.run_until_complete(vpcs.create_vm("PC 1", project.id, vm_id))
    assert vm in project.vms
Esempio n. 11
0
def test_create_vm_new_topology_without_uuid(loop, project, port_manager):

    VPCS._instance = None
    vpcs = VPCS.instance()
    vpcs.port_manager = port_manager
    vm = loop.run_until_complete(vpcs.create_vm("PC 1", project.id, None))
    assert vm in project.vms
    assert len(vm.id) == 36
Esempio n. 12
0
def test_create_vm_new_topology(loop, project, port_manager):

    VPCS._instance = None
    vpcs = VPCS.instance()
    vpcs.port_manager = port_manager
    vm_id = str(uuid.uuid4())
    vm = loop.run_until_complete(vpcs.create_vm("PC 1", project.id, vm_id))
    assert vm in project.vms
Esempio n. 13
0
def test_add_nio_binding_udp(vm):
    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)
    assert nio.lport == 4242
Esempio n. 14
0
def test_start(loop, vm):
    process = MagicMock()
    process.returncode = None

    with asyncio_patch("gns3server.modules.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
        with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
            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)
            loop.run_until_complete(asyncio.async(vm.start()))
            assert vm.is_running()
Esempio n. 15
0
def test_port_remove_nio_binding(vm):
    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)
    vm.port_remove_nio_binding(0)
    assert vm._ethernet_adapter.ports[0] is None
Esempio n. 16
0
def test_add_nio_binding_tap(vm, ethernet_device):
    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": ethernet_device
        })
        vm.port_add_nio_binding(0, nio)
        assert nio.tap_device == ethernet_device
Esempio n. 17
0
def test_get_mac_id_no_id_available(loop, project, port_manager):
    # Cleanup the VPCS object
    VPCS._instance = None
    vpcs = VPCS.instance()
    vpcs.port_manager = port_manager
    with pytest.raises(VPCSError):
        for i in range(0, 256):
            vm_id = str(uuid.uuid4())
            loop.run_until_complete(vpcs.create_vm("PC {}".format(i), project.id, vm_id))
            assert vpcs.get_mac_id(vm_id) == i
Esempio n. 18
0
def test_start(loop, vm):
    process = MagicMock()
    process.returncode = None

    with asyncio_patch("gns3server.modules.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
        with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
            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)
            loop.run_until_complete(asyncio.async(vm.start()))
            assert vm.is_running()
Esempio n. 19
0
def test_add_nio_binding_tap_no_privileged_access(vm):
    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)
    assert vm._ethernet_adapter.ports[0] is None
Esempio n. 20
0
def test_create_twice_same_vm_new_topology(loop, project, port_manager):

    project._vms = set()
    VPCS._instance = None
    vpcs = VPCS.instance()
    vpcs.port_manager = port_manager
    vm_id = str(uuid.uuid4())
    vm = loop.run_until_complete(vpcs.create_vm("PC 1", project.id, vm_id, console=2222))
    assert vm in project.vms
    assert len(project.vms) == 1
    vm = loop.run_until_complete(vpcs.create_vm("PC 2", project.id, vm_id, console=2222))
    assert len(project.vms) == 1
Esempio n. 21
0
def test_create_twice_same_vm_new_topology(loop, project, port_manager):

    project._vms = set()
    VPCS._instance = None
    vpcs = VPCS.instance()
    vpcs.port_manager = port_manager
    vm_id = str(uuid.uuid4())
    vm = loop.run_until_complete(vpcs.create_vm("PC 1", project.id, vm_id, console=2222))
    assert vm in project.vms
    assert len(project.vms) == 1
    vm = loop.run_until_complete(vpcs.create_vm("PC 2", project.id, vm_id, console=2222))
    assert len(project.vms) == 1
Esempio n. 22
0
def test_get_mac_id(loop, project, port_manager):
    # Cleanup the VPCS object
    VPCS._instance = None
    vpcs = VPCS.instance()
    vpcs.port_manager = port_manager
    vm1_id = str(uuid.uuid4())
    vm2_id = str(uuid.uuid4())
    vm3_id = str(uuid.uuid4())
    loop.run_until_complete(vpcs.create_vm("PC 1", project.id, vm1_id))
    loop.run_until_complete(vpcs.create_vm("PC 2", project.id, vm2_id))
    assert vpcs.get_mac_id(vm1_id) == 0
    assert vpcs.get_mac_id(vm1_id) == 0
    assert vpcs.get_mac_id(vm2_id) == 1
    loop.run_until_complete(vpcs.delete_vm(vm1_id))
    loop.run_until_complete(vpcs.create_vm("PC 3", project.id, vm3_id))
    assert vpcs.get_mac_id(vm3_id) == 0
Esempio n. 23
0
def test_get_mac_id_multiple_project(loop, port_manager):
    # Cleanup the VPCS object
    VPCS._instance = None
    vpcs = VPCS.instance()
    vpcs.port_manager = port_manager
    vm1_id = str(uuid.uuid4())
    vm2_id = str(uuid.uuid4())
    vm3_id = str(uuid.uuid4())
    project1 = ProjectManager.instance().create_project()
    project2 = ProjectManager.instance().create_project()
    loop.run_until_complete(vpcs.create_vm("PC 1", project1.id, vm1_id))
    loop.run_until_complete(vpcs.create_vm("PC 2", project1.id, vm2_id))
    loop.run_until_complete(vpcs.create_vm("PC 2", project2.id, vm3_id))
    assert vpcs.get_mac_id(vm1_id) == 0
    assert vpcs.get_mac_id(vm2_id) == 1
    assert vpcs.get_mac_id(vm3_id) == 0
Esempio n. 24
0
def test_create_vm_old_topology(loop, project, tmpdir, port_manager):

    with patch("gns3server.modules.project.Project.is_local", return_value=True):
        # Create an old topology directory
        project_dir = str(tmpdir / "testold")
        vm_dir = os.path.join(project_dir, "testold-files", "vpcs", "pc-1")
        project.path = project_dir
        project.name = "testold"
        os.makedirs(vm_dir, exist_ok=True)
        with open(os.path.join(vm_dir, "startup.vpc"), "w+") as f:
            f.write("1")

        VPCS._instance = None
        vpcs = VPCS.instance()
        vpcs.port_manager = port_manager
        vm_id = 1
        vm = loop.run_until_complete(vpcs.create_vm("PC 1", project.id, vm_id))
        assert len(vm.id) == 36

        assert os.path.exists(os.path.join(project_dir, "testold-files")) is False

        vm_dir = os.path.join(project_dir, "project-files", "vpcs", vm.id)
        with open(os.path.join(vm_dir, "startup.vpc")) as f:
            assert f.read() == "1"
Esempio n. 25
0
def manager(port_manager):
    m = VPCS.instance()
    m.port_manager = port_manager
    return m
Esempio n. 26
0
def vpcs(port_manager):
    VPCS._instance = None
    vpcs = VPCS.instance()
    vpcs.port_manager = port_manager
    return vpcs
Esempio n. 27
0
def vpcs(port_manager):
    VPCS._instance = None
    vpcs = VPCS.instance()
    vpcs.port_manager = port_manager
    return vpcs
Esempio n. 28
0
def manager(port_manager):
    m = VPCS.instance()
    m.port_manager = port_manager
    return m
Esempio n. 29
0
def test_add_nio_binding_tap_no_privileged_access(vm):
    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)
    assert vm._ethernet_adapter.ports[0] is None
Esempio n. 30
0
def test_add_nio_binding_tap(vm):
    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)
        assert nio.tap_device == "test"
Esempio n. 31
0
def test_add_nio_binding_udp(vm):
    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)
    assert nio.lport == 4242