Merge "Flush console log file before retrieving content" into stable/queens

This commit is contained in:
Zuul 2018-03-13 19:04:13 +00:00 committed by Gerrit Code Review
commit 74daeb2219
4 changed files with 29 additions and 1 deletions

View File

@ -49,6 +49,7 @@ class SerialConsoleHandler(object):
self._serial_proxy = None self._serial_proxy = None
self._workers = [] self._workers = []
self._log_handler = None
def start(self): def start(self):
self._setup_handlers() self._setup_handlers()
@ -117,6 +118,9 @@ class SerialConsoleHandler(object):
enable_logging=enable_logging) enable_logging=enable_logging)
self._workers.append(handler) self._workers.append(handler)
if enable_logging:
self._log_handler = handler
def _get_named_pipe_handler(self, pipe_path, pipe_type, def _get_named_pipe_handler(self, pipe_path, pipe_type,
enable_logging): enable_logging):
kwargs = {} kwargs = {}
@ -161,3 +165,9 @@ class SerialConsoleHandler(object):
raise exception.ConsoleTypeUnavailable(console_type='serial') raise exception.ConsoleTypeUnavailable(console_type='serial')
return ctype.ConsoleSerial(host=self._listen_host, return ctype.ConsoleSerial(host=self._listen_host,
port=self._listen_port) port=self._listen_port)
def flush_console_log(self):
if self._log_handler:
LOG.debug("Flushing instance %s console log.",
self._instance_name)
self._log_handler.flush_log_file()

View File

@ -111,6 +111,10 @@ class SerialConsoleOps(object):
console_log_paths = self._pathutils.get_vm_console_log_paths( console_log_paths = self._pathutils.get_vm_console_log_paths(
instance_name) instance_name)
handler = _console_handlers.get(instance_name)
if handler:
handler.flush_console_log()
try: try:
log = b'' log = b''
# Start with the oldest console log file. # Start with the oldest console log file.

View File

@ -135,7 +135,7 @@ class SerialConsoleHandlerTestCase(test_base.HyperVBaseTestCase):
return mock_get_pipe_handler return mock_get_pipe_handler
def test_setup_ro_pipe_handler(self): def test_setup_rw_pipe_handler(self):
serial_port_mapping = { serial_port_mapping = {
constants.SERIAL_PORT_TYPE_RW: mock.sentinel.pipe_path constants.SERIAL_PORT_TYPE_RW: mock.sentinel.pipe_path
} }
@ -147,6 +147,8 @@ class SerialConsoleHandlerTestCase(test_base.HyperVBaseTestCase):
mock.sentinel.pipe_path, mock.sentinel.pipe_path,
pipe_type=constants.SERIAL_PORT_TYPE_RW, pipe_type=constants.SERIAL_PORT_TYPE_RW,
enable_logging=True) enable_logging=True)
self.assertEqual(mock_get_handler.return_value,
self._consolehandler._log_handler)
def test_setup_pipe_handlers(self): def test_setup_pipe_handlers(self):
serial_port_mapping = { serial_port_mapping = {
@ -251,3 +253,12 @@ class SerialConsoleHandlerTestCase(test_base.HyperVBaseTestCase):
self.flags(enabled=False, group='serial_console') self.flags(enabled=False, group='serial_console')
self.assertRaises(exception.ConsoleTypeUnavailable, self.assertRaises(exception.ConsoleTypeUnavailable,
self._consolehandler.get_serial_console) self._consolehandler.get_serial_console)
def test_flush_console_log(self):
self._consolehandler._log_handler = None
self._consolehandler.flush_console_log()
mock_handler = mock.Mock()
self._consolehandler._log_handler = mock_handler
self._consolehandler.flush_console_log()
mock_handler.flush_log_file.assert_called_once_with()

View File

@ -109,6 +109,8 @@ class SerialConsoleOpsTestCase(test_base.HyperVBaseTestCase):
@mock.patch.object(builtins, 'open') @mock.patch.object(builtins, 'open')
@mock.patch("os.path.exists") @mock.patch("os.path.exists")
def test_get_console_output_exception(self, fake_path_exists, fake_open): def test_get_console_output_exception(self, fake_path_exists, fake_open):
mock_handler = self._setup_console_handler_mock()
self._serialops._vmutils.is_secure_vm.return_value = False self._serialops._vmutils.is_secure_vm.return_value = False
self._serialops._pathutils.get_vm_console_log_paths.return_value = [ self._serialops._pathutils.get_vm_console_log_paths.return_value = [
mock.sentinel.log_path_1, mock.sentinel.log_path_2] mock.sentinel.log_path_1, mock.sentinel.log_path_2]
@ -118,6 +120,7 @@ class SerialConsoleOpsTestCase(test_base.HyperVBaseTestCase):
self.assertRaises(exception.ConsoleLogOutputException, self.assertRaises(exception.ConsoleLogOutputException,
self._serialops.get_console_output, self._serialops.get_console_output,
mock.sentinel.instance_name) mock.sentinel.instance_name)
mock_handler.flush_console_log.assert_called_once_with()
fake_open.assert_called_once_with(mock.sentinel.log_path_2, 'rb') fake_open.assert_called_once_with(mock.sentinel.log_path_2, 'rb')
def test_get_console_output_secure_vm(self): def test_get_console_output_secure_vm(self):