# vim: tabstop=4 shiftwidth=4 softtabstop=4 # Copyright 2014 Cloudbase Solutions Srl # # 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 importlib import mock import unittest from oslo.config import cfg CONF = cfg.CONF class SerialPortHandlerTests(unittest.TestCase): def setUp(self): self._serial = mock.MagicMock() self._module_patcher = mock.patch.dict( 'sys.modules', {'serial': self._serial}) self._module_patcher.start() self.log = importlib.import_module("cloudbaseinit.utils.log") CONF.set_override('logging_serial_port_settings', "COM1,115200,N,8") self._serial_port_handler = self.log.SerialPortHandler() self._serial_port_handler._port = mock.MagicMock() def tearDown(self): self._module_patcher.stop() def test_init(self): mock_Serial = self._serial.Serial mock_Serial.return_value.isOpen.return_value = False self.log.SerialPortHandler() mock_Serial.assert_called_with(bytesize=8, baudrate=115200, port='COM1', parity='N') mock_Serial.return_value.isOpen.assert_called_with() mock_Serial.return_value.open.assert_called_once_with() def test_close(self): self._serial_port_handler._port.isOpen.return_value = True self._serial_port_handler.close() self._serial_port_handler._port.isOpen.assert_called_once_with() self._serial_port_handler._port.close.assert_called_once_with() @mock.patch('cloudbaseinit.openstack.common.log.setup') @mock.patch('cloudbaseinit.openstack.common.log.getLogger') @mock.patch('cloudbaseinit.utils.log.SerialPortHandler') @mock.patch('cloudbaseinit.openstack.common.log.ContextFormatter') def test_setup(self, mock_ContextFormatter, mock_SerialPortHandler, mock_getLogger, mock_setup): self.log.setup(product_name='fake name') mock_setup.assert_called_once_with('fake name') mock_getLogger.assert_called_once_with('fake name') mock_getLogger().logger.addHandler.assert_called_once_with( mock_SerialPortHandler()) mock_ContextFormatter.assert_called_once_with( project='fake name', datefmt=CONF.log_date_format) mock_SerialPortHandler().setFormatter.assert_called_once_with( mock_ContextFormatter())