diff --git a/storlets/gateway/common/logger.py b/storlets/gateway/common/logger.py index 5b7ae7a4..bd4f57e1 100644 --- a/storlets/gateway/common/logger.py +++ b/storlets/gateway/common/logger.py @@ -28,20 +28,16 @@ class StorletLogger(object): if self._file is not None: raise StorletLoggerError('StorletLogger is already open') - try: - log_dir_path = os.path.dirname(self.log_path) - if not os.path.exists(log_dir_path): - os.makedirs(log_dir_path, 0o700) + log_dir_path = os.path.dirname(self.log_path) + if not os.path.exists(log_dir_path): + os.makedirs(log_dir_path, 0o700) - self._file = open(self.log_path, 'a') - os.chmod(self.log_path, 0o600) - except Exception: - raise + self._file = open(self.log_path, 'a') + os.chmod(self.log_path, 0o600) def getfd(self): if self._file is None: - # TODO(kota_): Is it safe to return None? - return None + raise StorletLoggerError('StorletLogger is not open') return self._file.fileno() def getsize(self): @@ -52,12 +48,8 @@ class StorletLogger(object): if self._file is None: raise StorletLoggerError('StorletLogger is not open') - try: - self._file.close() - except Exception: - raise - else: - self._file = None + self._file.close() + self._file = None @contextmanager def activate(self): diff --git a/tests/unit/gateway/common/test_logger.py b/tests/unit/gateway/common/test_logger.py index aaf92bfc..9b472313 100644 --- a/tests/unit/gateway/common/test_logger.py +++ b/tests/unit/gateway/common/test_logger.py @@ -64,7 +64,9 @@ class TestStorletLogger(unittest.TestCase): self.logger.close() def test_getfd(self): - self.assertIsNone(self.logger.getfd()) + with self.assertRaises(StorletLoggerError): + self.logger.getfd() + self.logger.open() self.assertIsNotNone(self.logger.getfd()) self.logger.close() diff --git a/tests/unit/gateway/gateways/docker/test_runtime.py b/tests/unit/gateway/gateways/docker/test_runtime.py index f21bc4a5..fd423ab7 100644 --- a/tests/unit/gateway/gateways/docker/test_runtime.py +++ b/tests/unit/gateway/gateways/docker/test_runtime.py @@ -513,26 +513,30 @@ class TestStorletInvocationProtocol(unittest.TestCase): with mock.patch('storlets.gateway.gateways.docker.runtime.SBusClient.' 'execute') as execute: execute.return_value = SBusResponse(True, 'OK', 'someid') - self.protocol._send_execute_command() + with self.protocol.storlet_logger.activate(): + self.protocol._send_execute_command() self.assertEqual('someid', self.protocol.task_id) with mock.patch('storlets.gateway.gateways.docker.runtime.SBusClient.' 'execute') as execute: execute.return_value = SBusResponse(True, 'OK') with self.assertRaises(StorletRuntimeException): - self.protocol._send_execute_command() + with self.protocol.storlet_logger.activate(): + self.protocol._send_execute_command() with mock.patch('storlets.gateway.gateways.docker.runtime.SBusClient.' 'execute') as execute: execute.return_value = SBusResponse(False, 'NG', 'someid') with self.assertRaises(StorletRuntimeException): - self.protocol._send_execute_command() + with self.protocol.storlet_logger.activate(): + self.protocol._send_execute_command() with mock.patch('storlets.gateway.gateways.docker.runtime.SBusClient.' 'execute') as execute: execute.side_effect = SBusClientIOError() with self.assertRaises(StorletRuntimeException): - self.protocol._send_execute_command() + with self.protocol.storlet_logger.activate(): + self.protocol._send_execute_command() def test_invocation_protocol(self): # os.pipe will be called 3 times @@ -572,7 +576,8 @@ class TestStorletInvocationProtocol(unittest.TestCase): self.storlet_id, {}, {}, iter(StringIO()), options=self.options) protocol = StorletInvocationProtocol( storlet_request, self.pipe_path, self.log_file, 1, self.logger) - self.assertEqual(4, len(protocol.remote_fds)) + with protocol.storlet_logger.activate(): + self.assertEqual(4, len(protocol.remote_fds)) # extra_resources expands the remote_fds storlet_request = DockerStorletRequest( @@ -580,7 +585,8 @@ class TestStorletInvocationProtocol(unittest.TestCase): protocol = StorletInvocationProtocol( storlet_request, self.pipe_path, self.log_file, 1, self.logger, extra_sources=[storlet_request]) - self.assertEqual(5, len(protocol.remote_fds)) + with protocol.storlet_logger.activate(): + self.assertEqual(5, len(protocol.remote_fds)) # 2 more extra_resources expands the remote_fds storlet_request = DockerStorletRequest( @@ -588,7 +594,8 @@ class TestStorletInvocationProtocol(unittest.TestCase): protocol = StorletInvocationProtocol( storlet_request, self.pipe_path, self.log_file, 1, self.logger, extra_sources=[storlet_request] * 3) - self.assertEqual(7, len(protocol.remote_fds)) + with protocol.storlet_logger.activate(): + self.assertEqual(7, len(protocol.remote_fds)) def test_open_writer_with_invalid_fd(self): invalid_fds = (