HyperV: Add serial console handler class
This patch introduces a serial console handler responsible of spawning/stopping named pipe handlers as well as the serial console proxy. Hyper-V VMs have two serial ports (COM1 and COM2). The deployer will be able to designate serial ports for logging and interactive sessions using Glance image properties. The named pipe paths will have suffixes such as 'ro' or 'rw' in order to designate the above mentioned roles. Partially-implements: blueprint hyperv-serial-ports Change-Id: I5db3d60b4d26fbcae9c26557e2179e190de3d37d
This commit is contained in:
249
nova/tests/unit/virt/hyperv/test_serialconsolehandler.py
Normal file
249
nova/tests/unit/virt/hyperv/test_serialconsolehandler.py
Normal file
@@ -0,0 +1,249 @@
|
||||
# Copyright 2016 Cloudbase Solutions Srl
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import mock
|
||||
|
||||
from nova import exception
|
||||
from nova.tests.unit.virt.hyperv import test_base
|
||||
from nova.virt.hyperv import constants
|
||||
from nova.virt.hyperv import pathutils
|
||||
from nova.virt.hyperv import serialconsolehandler
|
||||
from nova.virt.hyperv import serialproxy
|
||||
|
||||
|
||||
class SerialConsoleHandlerTestCase(test_base.HyperVBaseTestCase):
|
||||
@mock.patch.object(pathutils.PathUtils, 'get_vm_console_log_paths')
|
||||
def setUp(self, mock_get_log_paths):
|
||||
super(SerialConsoleHandlerTestCase, self).setUp()
|
||||
|
||||
mock_get_log_paths.return_value = [mock.sentinel.log_path]
|
||||
|
||||
self._consolehandler = serialconsolehandler.SerialConsoleHandler(
|
||||
mock.sentinel.instance_name)
|
||||
|
||||
self._consolehandler._log_path = mock.sentinel.log_path
|
||||
self._consolehandler._pathutils = mock.Mock()
|
||||
self._consolehandler._vmutils = mock.Mock()
|
||||
|
||||
@mock.patch.object(serialconsolehandler.SerialConsoleHandler,
|
||||
'_setup_handlers')
|
||||
def test_start(self, mock_setup_handlers):
|
||||
mock_workers = [mock.Mock(), mock.Mock()]
|
||||
self._consolehandler._workers = mock_workers
|
||||
|
||||
self._consolehandler.start()
|
||||
|
||||
mock_setup_handlers.assert_called_once_with()
|
||||
for worker in mock_workers:
|
||||
worker.start.assert_called_once_with()
|
||||
|
||||
@mock.patch('nova.console.serial.release_port')
|
||||
def test_stop(self, mock_release_port):
|
||||
mock_serial_proxy = mock.Mock()
|
||||
mock_workers = [mock_serial_proxy, mock.Mock()]
|
||||
|
||||
self._consolehandler._serial_proxy = mock_serial_proxy
|
||||
self._consolehandler._listen_host = mock.sentinel.host
|
||||
self._consolehandler._listen_port = mock.sentinel.port
|
||||
self._consolehandler._workers = mock_workers
|
||||
|
||||
self._consolehandler.stop()
|
||||
|
||||
mock_release_port.assert_called_once_with(mock.sentinel.host,
|
||||
mock.sentinel.port)
|
||||
for worker in mock_workers:
|
||||
worker.stop.assert_called_once_with()
|
||||
|
||||
@mock.patch.object(serialconsolehandler.SerialConsoleHandler,
|
||||
'_setup_named_pipe_handlers')
|
||||
@mock.patch.object(serialconsolehandler.SerialConsoleHandler,
|
||||
'_setup_serial_proxy_handler')
|
||||
def _test_setup_handlers(self, mock_setup_proxy, mock_setup_pipe_handlers,
|
||||
serial_console_enabled=True):
|
||||
self.flags(enabled=serial_console_enabled, group='serial_console')
|
||||
|
||||
self._consolehandler._setup_handlers()
|
||||
|
||||
self.assertEqual(serial_console_enabled, mock_setup_proxy.called)
|
||||
mock_setup_pipe_handlers.assert_called_once_with()
|
||||
|
||||
def test_setup_handlers(self):
|
||||
self._test_setup_handlers()
|
||||
|
||||
def test_setup_handlers_console_disabled(self):
|
||||
self._test_setup_handlers(serial_console_enabled=False)
|
||||
|
||||
@mock.patch.object(serialproxy, 'SerialProxy')
|
||||
@mock.patch('nova.console.serial.acquire_port')
|
||||
@mock.patch.object(serialconsolehandler.threading, 'Event')
|
||||
@mock.patch.object(serialconsolehandler.ioutils, 'IOQueue')
|
||||
def test_setup_serial_proxy_handler(self, mock_io_queue, mock_event,
|
||||
mock_acquire_port,
|
||||
mock_serial_proxy_class):
|
||||
mock_input_queue = mock.sentinel.input_queue
|
||||
mock_output_queue = mock.sentinel.output_queue
|
||||
mock_client_connected = mock_event.return_value
|
||||
mock_io_queue.side_effect = [mock_input_queue, mock_output_queue]
|
||||
mock_serial_proxy = mock_serial_proxy_class.return_value
|
||||
|
||||
mock_acquire_port.return_value = mock.sentinel.port
|
||||
self.flags(proxyclient_address=mock.sentinel.host,
|
||||
group='serial_console')
|
||||
|
||||
self._consolehandler._setup_serial_proxy_handler()
|
||||
|
||||
mock_serial_proxy_class.assert_called_once_with(
|
||||
mock.sentinel.instance_name,
|
||||
mock.sentinel.host, mock.sentinel.port,
|
||||
mock_input_queue,
|
||||
mock_output_queue,
|
||||
mock_client_connected)
|
||||
|
||||
self.assertIn(mock_serial_proxy, self._consolehandler._workers)
|
||||
|
||||
@mock.patch.object(serialconsolehandler.SerialConsoleHandler,
|
||||
'_get_named_pipe_handler')
|
||||
@mock.patch.object(serialconsolehandler.SerialConsoleHandler,
|
||||
'_get_vm_serial_port_mapping')
|
||||
def _mock_setup_named_pipe_handlers(self, mock_get_port_mapping,
|
||||
mock_get_pipe_handler,
|
||||
serial_port_mapping=None):
|
||||
mock_get_port_mapping.return_value = serial_port_mapping
|
||||
|
||||
self._consolehandler._setup_named_pipe_handlers()
|
||||
|
||||
expected_workers = [mock_get_pipe_handler.return_value
|
||||
for port in serial_port_mapping]
|
||||
|
||||
self.assertEqual(expected_workers, self._consolehandler._workers)
|
||||
|
||||
return mock_get_pipe_handler
|
||||
|
||||
def test_setup_ro_pipe_handler(self):
|
||||
serial_port_mapping = {
|
||||
constants.SERIAL_PORT_TYPE_RW: mock.sentinel.pipe_path
|
||||
}
|
||||
|
||||
mock_get_handler = self._mock_setup_named_pipe_handlers(
|
||||
serial_port_mapping=serial_port_mapping)
|
||||
|
||||
mock_get_handler.assert_called_once_with(
|
||||
mock.sentinel.pipe_path,
|
||||
pipe_type=constants.SERIAL_PORT_TYPE_RW,
|
||||
enable_logging=True)
|
||||
|
||||
def test_setup_pipe_handlers(self):
|
||||
serial_port_mapping = {
|
||||
constants.SERIAL_PORT_TYPE_RO: mock.sentinel.ro_pipe_path,
|
||||
constants.SERIAL_PORT_TYPE_RW: mock.sentinel.rw_pipe_path
|
||||
}
|
||||
|
||||
mock_get_handler = self._mock_setup_named_pipe_handlers(
|
||||
serial_port_mapping=serial_port_mapping)
|
||||
|
||||
expected_calls = [mock.call(mock.sentinel.ro_pipe_path,
|
||||
pipe_type=constants.SERIAL_PORT_TYPE_RO,
|
||||
enable_logging=True),
|
||||
mock.call(mock.sentinel.rw_pipe_path,
|
||||
pipe_type=constants.SERIAL_PORT_TYPE_RW,
|
||||
enable_logging=False)]
|
||||
mock_get_handler.assert_has_calls(expected_calls, any_order=True)
|
||||
|
||||
@mock.patch.object(serialconsolehandler.utilsfactory,
|
||||
'get_named_pipe_handler')
|
||||
def _test_get_named_pipe_handler(self, mock_get_pipe_handler,
|
||||
pipe_type=None, enable_logging=False):
|
||||
expected_args = {}
|
||||
|
||||
if pipe_type == constants.SERIAL_PORT_TYPE_RW:
|
||||
self._consolehandler._input_queue = mock.sentinel.input_queue
|
||||
self._consolehandler._output_queue = mock.sentinel.output_queue
|
||||
self._consolehandler._client_connected = (
|
||||
mock.sentinel.connect_event)
|
||||
expected_args.update({
|
||||
'input_queue': mock.sentinel.input_queue,
|
||||
'output_queue': mock.sentinel.output_queue,
|
||||
'connect_event': mock.sentinel.connect_event})
|
||||
|
||||
if enable_logging:
|
||||
expected_args['log_file'] = mock.sentinel.log_path
|
||||
|
||||
ret_val = self._consolehandler._get_named_pipe_handler(
|
||||
mock.sentinel.pipe_path, pipe_type, enable_logging)
|
||||
|
||||
self.assertEqual(mock_get_pipe_handler.return_value, ret_val)
|
||||
mock_get_pipe_handler.assert_called_once_with(
|
||||
mock.sentinel.pipe_path,
|
||||
**expected_args)
|
||||
|
||||
def test_get_ro_named_pipe_handler(self):
|
||||
self._test_get_named_pipe_handler(
|
||||
pipe_type=constants.SERIAL_PORT_TYPE_RO,
|
||||
enable_logging=True)
|
||||
|
||||
def test_get_rw_named_pipe_handler(self):
|
||||
self._test_get_named_pipe_handler(
|
||||
pipe_type=constants.SERIAL_PORT_TYPE_RW,
|
||||
enable_logging=False)
|
||||
|
||||
def _mock_get_port_connections(self, port_connections):
|
||||
get_port_connections = (
|
||||
self._consolehandler._vmutils.get_vm_serial_port_connections)
|
||||
get_port_connections.return_value = port_connections
|
||||
|
||||
def test_get_vm_serial_port_mapping_having_tagged_pipes(self):
|
||||
ro_pipe_path = 'fake_pipe_ro'
|
||||
rw_pipe_path = 'fake_pipe_rw'
|
||||
self._mock_get_port_connections([ro_pipe_path, rw_pipe_path])
|
||||
|
||||
ret_val = self._consolehandler._get_vm_serial_port_mapping()
|
||||
|
||||
expected_mapping = {
|
||||
constants.SERIAL_PORT_TYPE_RO: ro_pipe_path,
|
||||
constants.SERIAL_PORT_TYPE_RW: rw_pipe_path
|
||||
}
|
||||
|
||||
self.assertEqual(expected_mapping, ret_val)
|
||||
|
||||
def test_get_vm_serial_port_mapping_untagged_pipe(self):
|
||||
pipe_path = 'fake_pipe_path'
|
||||
self._mock_get_port_connections([pipe_path])
|
||||
|
||||
ret_val = self._consolehandler._get_vm_serial_port_mapping()
|
||||
|
||||
expected_mapping = {constants.SERIAL_PORT_TYPE_RW: pipe_path}
|
||||
self.assertEqual(expected_mapping, ret_val)
|
||||
|
||||
def test_get_vm_serial_port_mapping_exception(self):
|
||||
self._mock_get_port_connections([])
|
||||
self.assertRaises(exception.NovaException,
|
||||
self._consolehandler._get_vm_serial_port_mapping)
|
||||
|
||||
@mock.patch('nova.console.type.ConsoleSerial')
|
||||
def test_get_serial_console(self, mock_serial_console):
|
||||
self.flags(enabled=True, group='serial_console')
|
||||
self._consolehandler._listen_host = mock.sentinel.host
|
||||
self._consolehandler._listen_port = mock.sentinel.port
|
||||
|
||||
ret_val = self._consolehandler.get_serial_console()
|
||||
self.assertEqual(mock_serial_console.return_value, ret_val)
|
||||
mock_serial_console.assert_called_once_with(
|
||||
host=mock.sentinel.host,
|
||||
port=mock.sentinel.port)
|
||||
|
||||
def test_get_serial_console_disabled(self):
|
||||
self.flags(enabled=False, group='serial_console')
|
||||
self.assertRaises(exception.ConsoleTypeUnavailable,
|
||||
self._consolehandler.get_serial_console)
|
||||
@@ -69,3 +69,6 @@ VM_GEN_1 = 1
|
||||
VM_GEN_2 = 2
|
||||
|
||||
SERIAL_CONSOLE_BUFFER_SIZE = 4 * units.Ki
|
||||
|
||||
SERIAL_PORT_TYPE_RO = 'ro'
|
||||
SERIAL_PORT_TYPE_RW = 'rw'
|
||||
|
||||
161
nova/virt/hyperv/serialconsolehandler.py
Normal file
161
nova/virt/hyperv/serialconsolehandler.py
Normal file
@@ -0,0 +1,161 @@
|
||||
# Copyright 2016 Cloudbase Solutions Srl
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from eventlet import patcher
|
||||
from os_win.utils.io import ioutils
|
||||
from os_win import utilsfactory
|
||||
from oslo_config import cfg
|
||||
from oslo_log import log as logging
|
||||
|
||||
from nova.console import serial as serial_console
|
||||
from nova.console import type as ctype
|
||||
from nova import exception
|
||||
from nova.i18n import _, _LI
|
||||
from nova.virt.hyperv import constants
|
||||
from nova.virt.hyperv import pathutils
|
||||
from nova.virt.hyperv import serialproxy
|
||||
|
||||
CONF = cfg.CONF
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
threading = patcher.original('threading')
|
||||
|
||||
|
||||
class SerialConsoleHandler(object):
|
||||
"""Handles serial console ops related to a given instance."""
|
||||
def __init__(self, instance_name):
|
||||
self._vmutils = utilsfactory.get_vmutils()
|
||||
self._pathutils = pathutils.PathUtils()
|
||||
|
||||
self._instance_name = instance_name
|
||||
self._log_path = self._pathutils.get_vm_console_log_paths(
|
||||
self._instance_name)[0]
|
||||
|
||||
self._client_connected = None
|
||||
self._input_queue = None
|
||||
self._output_queue = None
|
||||
|
||||
self._serial_proxy = None
|
||||
self._workers = []
|
||||
|
||||
def start(self):
|
||||
self._setup_handlers()
|
||||
|
||||
for worker in self._workers:
|
||||
worker.start()
|
||||
|
||||
def stop(self):
|
||||
for worker in self._workers:
|
||||
worker.stop()
|
||||
|
||||
if self._serial_proxy:
|
||||
serial_console.release_port(self._listen_host,
|
||||
self._listen_port)
|
||||
|
||||
def _setup_handlers(self):
|
||||
if CONF.serial_console.enabled:
|
||||
self._setup_serial_proxy_handler()
|
||||
|
||||
self._setup_named_pipe_handlers()
|
||||
|
||||
def _setup_serial_proxy_handler(self):
|
||||
self._listen_host = (
|
||||
CONF.serial_console.proxyclient_address)
|
||||
self._listen_port = serial_console.acquire_port(
|
||||
self._listen_host)
|
||||
|
||||
LOG.info(_LI('Initializing serial proxy on '
|
||||
'%(addr)s:%(port)s, handling connections '
|
||||
'to instance %(instance_name)s.'),
|
||||
{'addr': self._listen_host,
|
||||
'port': self._listen_port,
|
||||
'instance_name': self._instance_name})
|
||||
|
||||
# Use this event in order to manage
|
||||
# pending queue operations.
|
||||
self._client_connected = threading.Event()
|
||||
self._input_queue = ioutils.IOQueue(
|
||||
client_connected=self._client_connected)
|
||||
self._output_queue = ioutils.IOQueue(
|
||||
client_connected=self._client_connected)
|
||||
|
||||
self._serial_proxy = serialproxy.SerialProxy(
|
||||
self._instance_name, self._listen_host,
|
||||
self._listen_port, self._input_queue,
|
||||
self._output_queue, self._client_connected)
|
||||
|
||||
self._workers.append(self._serial_proxy)
|
||||
|
||||
def _setup_named_pipe_handlers(self):
|
||||
# At most 2 named pipes will be used to access the vm serial ports.
|
||||
#
|
||||
# The named pipe having the 'ro' suffix will be used only for logging
|
||||
# while the 'rw' pipe will be used for interactive sessions, logging
|
||||
# only when there is no 'ro' pipe.
|
||||
serial_port_mapping = self._get_vm_serial_port_mapping()
|
||||
log_rw_pipe_output = not serial_port_mapping.get(
|
||||
constants.SERIAL_PORT_TYPE_RO)
|
||||
|
||||
for pipe_type, pipe_path in serial_port_mapping.items():
|
||||
enable_logging = (pipe_type == constants.SERIAL_PORT_TYPE_RO or
|
||||
log_rw_pipe_output)
|
||||
handler = self._get_named_pipe_handler(
|
||||
pipe_path,
|
||||
pipe_type=pipe_type,
|
||||
enable_logging=enable_logging)
|
||||
self._workers.append(handler)
|
||||
|
||||
def _get_named_pipe_handler(self, pipe_path, pipe_type,
|
||||
enable_logging):
|
||||
kwargs = {}
|
||||
if pipe_type == constants.SERIAL_PORT_TYPE_RW:
|
||||
kwargs = {'input_queue': self._input_queue,
|
||||
'output_queue': self._output_queue,
|
||||
'connect_event': self._client_connected}
|
||||
if enable_logging:
|
||||
kwargs['log_file'] = self._log_path
|
||||
|
||||
handler = utilsfactory.get_named_pipe_handler(pipe_path, **kwargs)
|
||||
return handler
|
||||
|
||||
def _get_vm_serial_port_mapping(self):
|
||||
serial_port_conns = self._vmutils.get_vm_serial_port_connections(
|
||||
self._instance_name)
|
||||
|
||||
if not serial_port_conns:
|
||||
err_msg = _("No suitable serial port pipe was found "
|
||||
"for instance %(instance_name)s")
|
||||
raise exception.NovaException(
|
||||
err_msg % {'instance_name': self._instance_name})
|
||||
|
||||
serial_port_mapping = {}
|
||||
# At the moment, we tag the pipes by using a pipe path suffix
|
||||
# as we can't use the serial port ElementName attribute because of
|
||||
# a Hyper-V bug.
|
||||
for pipe_path in serial_port_conns:
|
||||
port_type = pipe_path[-2:]
|
||||
if port_type in [constants.SERIAL_PORT_TYPE_RO,
|
||||
constants.SERIAL_PORT_TYPE_RW]:
|
||||
serial_port_mapping[port_type] = pipe_path
|
||||
else:
|
||||
serial_port_mapping[constants.SERIAL_PORT_TYPE_RW] = pipe_path
|
||||
|
||||
return serial_port_mapping
|
||||
|
||||
def get_serial_console(self):
|
||||
if not CONF.serial_console.enabled:
|
||||
raise exception.ConsoleTypeUnavailable(console_type='serial')
|
||||
return ctype.ConsoleSerial(host=self._listen_host,
|
||||
port=self._listen_port)
|
||||
Reference in New Issue
Block a user