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)
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):

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,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 = (