Py3: Make storlets/agent as py3 compatible

Change-Id: I84d10daca3335f9d9ef0d1d475aae3b7406f6dbe
This commit is contained in:
Kota Tsuyuzaki 2018-07-24 18:22:28 +09:00
parent bb405dbda4
commit d5a038f4ac
6 changed files with 25 additions and 23 deletions
storlets/agent
daemon
daemon_factory
tests/unit/agent/daemon
tox.ini

@ -18,7 +18,7 @@ import json
class StorletFile(object):
mode = 'r'
mode = 'rb'
def __init__(self, obj_fd):
self.obj_fd = obj_fd

@ -19,6 +19,7 @@ import pwd
import sys
import uuid
import signal
import importlib
from storlets.sbus import SBus
from storlets.agent.common.server import command_handler, EXIT_FAILURE, \
CommandSuccess, CommandFailure, SBusServer
@ -53,7 +54,7 @@ class StorletDaemon(SBusServer):
raise ValueError("Invalid storlet name %s" % storlet_name)
try:
module = __import__(module_name, fromlist=[cls_name])
module = importlib.import_module(module_name)
self.storlet_cls = getattr(module, cls_name)
except (ImportError, AttributeError):
raise StorletDaemonLoadError(

@ -336,7 +336,7 @@ class StorletDaemonFactory(SBusServer):
:raises SDaemonError: when failed to kill one of the storlet daemons
"""
failed = []
for storlet_name in self.storlet_name_to_pid.keys():
for storlet_name in list(self.storlet_name_to_pid):
try:
self.process_kill(storlet_name)
except SDaemonError:
@ -362,7 +362,7 @@ class StorletDaemonFactory(SBusServer):
"""
terminated = []
failed = []
for storlet_name in self.storlet_name_to_pid.keys():
for storlet_name in list(self.storlet_name_to_pid):
try:
self.shutdown_process(storlet_name)
terminated.append(storlet_name)
@ -447,7 +447,7 @@ class StorletDaemonFactory(SBusServer):
except SDaemonError as err:
self.logger.exception('Failed to start the sdaemon for {0}'
.format(storlet_name))
return CommandFailure(err.message)
return CommandFailure(err.args[0])
@command_handler
def stop_daemon(self, dtg):
@ -461,7 +461,7 @@ class StorletDaemonFactory(SBusServer):
except SDaemonError as err:
self.logger.exception('Failed to kill the storlet daemon %s' %
storlet_name)
return CommandFailure(err.message)
return CommandFailure(err.args[0])
@command_handler
def daemon_status(self, dtg):
@ -478,7 +478,7 @@ class StorletDaemonFactory(SBusServer):
except SDaemonError as err:
self.logger.exception('Failed to get status of the storlet '
'daemon %s' % storlet_name)
return CommandFailure(err.message)
return CommandFailure(err.args[0])
@command_handler
def stop_daemons(self, dtg):
@ -487,7 +487,7 @@ class StorletDaemonFactory(SBusServer):
return CommandSuccess('OK', False)
except SDaemonError as err:
self.logger.exception('Failed to stop some storlet daemons')
return CommandFailure(err.message, False)
return CommandFailure(err.args[0], False)
@command_handler
def halt(self, dtg):
@ -497,7 +497,7 @@ class StorletDaemonFactory(SBusServer):
return CommandSuccess(msg, False)
except SDaemonError as err:
self.logger.exception('Failed to halt some storlet daemons')
return CommandFailure(err.message, False)
return CommandFailure(err.args[0], False)
def _terminate(self):
pass

@ -117,44 +117,44 @@ class TestStorletOutputFile(TestStorletFile):
class TestStorletInputFile(TestStorletFile):
def setUp(self, content=None):
self.content = content or 'abcd\nefg\nhi\nj'
self.content = content or b'abcd\nefg\nhi\nj'
self.metadata = {'key1': 'value1'}
super(TestStorletInputFile, self).setUp()
def _prepare_file(self):
with open(self.fname, 'w') as f:
with open(self.fname, 'wb') as f:
f.write(self.content)
def _create_file(self):
return StorletInputFile(self.metadata, self.fd)
def test_read(self):
self.assertEqual('abcd\nefg\nhi\nj', self.sfile.read())
self.assertEqual(b'abcd\nefg\nhi\nj', self.sfile.read())
def test_read_size(self):
expects = ['abc', 'd\ne', 'fg\n', 'hi\n', 'j', '']
expects = [b'abc', b'd\ne', b'fg\n', b'hi\n', b'j', b'']
for expect in expects:
self.assertEqual(expect, self.sfile.read(3))
def test_readline(self):
expects = ['abcd\n', 'efg\n', 'hi\n', 'j', '']
expects = [b'abcd\n', b'efg\n', b'hi\n', b'j', b'']
for expect in expects:
self.assertEqual(expect, self.sfile.readline())
def test_readline_size(self):
expects = ['abc', 'd\n', 'efg', '\n', 'hi\n', 'j', '']
expects = [b'abc', b'd\n', b'efg', b'\n', b'hi\n', b'j', b'']
for expect in expects:
self.assertEqual(expect, self.sfile.readline(3))
def test_readlines(self):
self.assertEqual(['abcd\n', 'efg\n', 'hi\n', 'j'],
self.assertEqual([b'abcd\n', b'efg\n', b'hi\n', b'j'],
self.sfile.readlines())
def test_iter(self):
buf = ''
buf = b''
for rbuf in self.sfile:
buf += rbuf
self.assertEqual('abcd\nefg\nhi\nj', buf)
self.assertEqual(b'abcd\nefg\nhi\nj', buf)
def test_get_metadata(self):
self.assertEqual(self.metadata, self.sfile.get_metadata())
@ -167,7 +167,7 @@ class TestStorletInputFile(TestStorletFile):
class TestStorletRangeInputFile(TestStorletInputFile):
def setUp(self):
content = '012\n345\nabcd\nefg\nhi\nj67\n8'
content = b'012\n345\nabcd\nefg\nhi\nj67\n8'
self.start = 8
self.end = 21
super(TestStorletRangeInputFile, self).setUp(content)

@ -41,19 +41,19 @@ class TestStorletDaemon(unittest.TestCase):
with self.assertRaises(ValueError) as cm:
StorletDaemon(module_name, 'fake_path', self.logger, 16)
self.assertEqual('Invalid storlet name %s' % module_name,
cm.exception.message)
cm.exception.args[0])
def test_module_not_found(self):
with self.assertRaises(StorletDaemonLoadError) as cm:
StorletDaemon('nomodule.Nothing', 'fake_path', self.logger, 16)
self.assertEqual('Failed to load storlet nomodule.Nothing',
cm.exception.message)
cm.exception.args[0])
class TestStorletDaemonMain(test_server.TestSBusServerMain):
def _get_test_server(self):
with mock.patch('__builtin__.__import__') as fake_import:
with mock.patch('importlib.import_module') as fake_import:
fake_import.return_value = FakeModule()
server = StorletDaemon(
'fakeModule.FakeClass', self.sbus_path, self.logger, 16)

@ -35,7 +35,8 @@ commands =
nosetests -v \
tests/unit/gateway \
tests/unit/sbus \
tests/unit/tools
tests/unit/tools \
tests/unit/agent
[testenv:py36]
basepython = python3