Drop redundant try-except block

We can omit try-except in case we just re-raise all exceptions.

Also, we should fail in case fd is requested but the log file is not
open.

Change-Id: I2d8588cc8356e32f5bb199416243b52edad30ef6
This commit is contained in:
Takashi Kajinami 2024-01-21 16:22:39 +09:00
parent 0b6628f972
commit 60e75cff17
3 changed files with 25 additions and 24 deletions

View File

@ -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)
self._file = open(self.log_path, 'a')
os.chmod(self.log_path, 0o600)
except Exception:
raise
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,11 +48,7 @@ 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
@contextmanager

View File

@ -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()

View File

@ -513,6 +513,7 @@ class TestStorletInvocationProtocol(unittest.TestCase):
with mock.patch('storlets.gateway.gateways.docker.runtime.SBusClient.'
'execute') as execute:
execute.return_value = SBusResponse(True, 'OK', 'someid')
with self.protocol.storlet_logger.activate():
self.protocol._send_execute_command()
self.assertEqual('someid', self.protocol.task_id)
@ -520,18 +521,21 @@ class TestStorletInvocationProtocol(unittest.TestCase):
'execute') as execute:
execute.return_value = SBusResponse(True, 'OK')
with self.assertRaises(StorletRuntimeException):
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):
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):
with self.protocol.storlet_logger.activate():
self.protocol._send_execute_command()
def test_invocation_protocol(self):
@ -572,6 +576,7 @@ 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)
with protocol.storlet_logger.activate():
self.assertEqual(4, len(protocol.remote_fds))
# extra_resources expands the remote_fds
@ -580,6 +585,7 @@ class TestStorletInvocationProtocol(unittest.TestCase):
protocol = StorletInvocationProtocol(
storlet_request, self.pipe_path, self.log_file, 1, self.logger,
extra_sources=[storlet_request])
with protocol.storlet_logger.activate():
self.assertEqual(5, len(protocol.remote_fds))
# 2 more extra_resources expands the remote_fds
@ -588,6 +594,7 @@ class TestStorletInvocationProtocol(unittest.TestCase):
protocol = StorletInvocationProtocol(
storlet_request, self.pipe_path, self.log_file, 1, self.logger,
extra_sources=[storlet_request] * 3)
with protocol.storlet_logger.activate():
self.assertEqual(7, len(protocol.remote_fds))
def test_open_writer_with_invalid_fd(self):