Skip to content

Commit

Permalink
Qemu telnet support multiple client connected
Browse files Browse the repository at this point in the history
Fix #770
  • Loading branch information
julien-duponchelle committed Nov 9, 2016
1 parent 69f154d commit bddf9ec
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
7 changes: 4 additions & 3 deletions gns3server/compute/qemu/qemu_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class QemuVM(BaseNode):

def __init__(self, name, node_id, project, manager, linked_clone=True, qemu_path=None, console=None, console_type="telnet", platform=None):

super().__init__(name, node_id, project, manager, console=console, console_type=console_type)
super().__init__(name, node_id, project, manager, console=console, console_type=console_type, wrap_console=True)
server_config = manager.config.get_section_config("Server")
self._host = server_config.get("host", "127.0.0.1")
self._monitor_host = server_config.get("monitor_host", "127.0.0.1")
Expand Down Expand Up @@ -908,6 +908,7 @@ def start(self):

if "-enable-kvm" in command_string:
self._hw_virtualization = True
yield from self.start_wrap_console()

def _termination_callback(self, returncode):
"""
Expand Down Expand Up @@ -936,7 +937,6 @@ def stop(self):
self._hw_virtualization = False
if self.is_running():
log.info('Stopping QEMU VM "{}" PID={}'.format(self._name, self._process.pid))
self.status = "stopped"
try:
if self.acpi_shutdown:
yield from self._control_vm("system_powerdown")
Expand All @@ -955,6 +955,7 @@ def stop(self):
log.warn('QEMU VM "{}" PID={} is still running'.format(self._name, self._process.pid))
self._process = None
self._stop_cpulimit()
yield from super().stop()

@asyncio.coroutine
def _control_vm(self, command, expected=None):
Expand Down Expand Up @@ -1255,7 +1256,7 @@ def command(self):
def _serial_options(self):

if self._console:
return ["-serial", "telnet:{}:{},server,nowait".format(self._manager.port_manager.console_host, self._console)]
return ["-serial", "telnet:127.0.0.1:{},server,nowait".format(self._internal_console_port)]
else:
return []

Expand Down
49 changes: 26 additions & 23 deletions tests/compute/qemu/test_qemu_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,11 @@ def test_is_running(vm, running_subprocess_mock):


def test_start(loop, vm, running_subprocess_mock):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=running_subprocess_mock) as mock:
loop.run_until_complete(asyncio.async(vm.start()))
assert vm.is_running()
assert vm.command_line == ' '.join(mock.call_args[0])
with asyncio_patch("gns3server.compute.qemu.QemuVM.start_wrap_console"):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=running_subprocess_mock) as mock:
loop.run_until_complete(asyncio.async(vm.start()))
assert vm.is_running()
assert vm.command_line == ' '.join(mock.call_args[0])


def test_stop(loop, vm, running_subprocess_mock):
Expand All @@ -127,14 +128,15 @@ def test_stop(loop, vm, running_subprocess_mock):
future.set_result(True)
process.wait.return_value = future

with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
nio = Qemu.instance().create_nio({"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
vm.adapter_add_nio_binding(0, nio)
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()
with asyncio_patch("gns3server.compute.qemu.QemuVM.start_wrap_console"):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
nio = Qemu.instance().create_nio({"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
vm.adapter_add_nio_binding(0, nio)
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()


def test_termination_callback(vm, async_run):
Expand Down Expand Up @@ -213,17 +215,18 @@ def test_port_remove_nio_binding(vm, loop):


def test_close(vm, port_manager, loop):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
loop.run_until_complete(asyncio.async(vm.start()))
with asyncio_patch("gns3server.compute.qemu.QemuVM.start_wrap_console"):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
loop.run_until_complete(asyncio.async(vm.start()))

console_port = vm.console
console_port = vm.console

loop.run_until_complete(asyncio.async(vm.close()))
loop.run_until_complete(asyncio.async(vm.close()))

# Raise an exception if the port is not free
port_manager.reserve_tcp_port(console_port, vm.project)
# Raise an exception if the port is not free
port_manager.reserve_tcp_port(console_port, vm.project)

assert vm.is_running() is False
assert vm.is_running() is False


def test_set_qemu_path(vm, tmpdir, fake_qemu_binary):
Expand Down Expand Up @@ -434,7 +437,7 @@ def test_build_command(vm, loop, fake_qemu_binary, port_manager):
"-uuid",
vm.id,
"-serial",
"telnet:127.0.0.1:{},server,nowait".format(vm.console),
"telnet:127.0.0.1:{},server,nowait".format(vm._internal_console_port),
"-net",
"none",
"-device",
Expand Down Expand Up @@ -478,7 +481,7 @@ def test_build_command_kvm(linux_platform, vm, loop, fake_qemu_binary, port_mana
"-uuid",
vm.id,
"-serial",
"telnet:127.0.0.1:{},server,nowait".format(vm.console),
"telnet:127.0.0.1:{},server,nowait".format(vm._internal_console_port),
"-net",
"none",
"-device",
Expand Down Expand Up @@ -511,7 +514,7 @@ def test_build_command_kvm_2_4(linux_platform, vm, loop, fake_qemu_binary, port_
"-uuid",
vm.id,
"-serial",
"telnet:127.0.0.1:{},server,nowait".format(vm.console),
"telnet:127.0.0.1:{},server,nowait".format(vm._internal_console_port),
"-net",
"none",
"-device",
Expand Down Expand Up @@ -547,7 +550,7 @@ def test_build_command_two_adapters(vm, loop, fake_qemu_binary, port_manager):
"-uuid",
vm.id,
"-serial",
"telnet:127.0.0.1:{},server,nowait".format(vm.console),
"telnet:127.0.0.1:{},server,nowait".format(vm._internal_console_port),
"-net",
"none",
"-device",
Expand Down

0 comments on commit bddf9ec

Please sign in to comment.