示例#1
0
class TestStorletDaemon(unittest.TestCase):
    def setUp(self):
        self.logger = FakeLogger()
        self.pile = eventlet.greenpool.GreenPile(1)

    def test_invalid_module_name(self):
        for module_name in ('only_module',
                            'OnlyClass',
                            'nested.module.Class',
                            1, None, True, False):
            self._test_invalid_module_name(module_name)

    def _test_invalid_module_name(self, module_name):
        with self.assertRaises(ValueError) as cm:
            Daemon(module_name, 'fake_path', self.logger, 16)
        self.assertEqual('Invalid storlet name %s' % module_name,
                         cm.exception.message)

    def test_module_not_found(self):
        with self.assertRaises(StorletDaemonException) as cm:
            Daemon('nomodule.Nothing', 'fake_path', self.logger, 16)
        self.assertEqual('Failed to load storlet nomodule.Nothing',
                         cm.exception.message)

    def _test_main_loop_stop(self, stop_command):
        with mock.patch('__builtin__.__import__') as fake_import:
            fake_import.return_value = FakeModule()
            daemon = Daemon(
                'fakeModule.FakeClass', 'fake_path', self.logger, 16)
        scenario = [
            ('create', 1),
            ('listen', 1),
            ('receive', SBusDatagram(command=stop_command,
                                     fds=[], metadata=[],
                                     params=None, task_id=None)),
        ]

        fake_sbus_class = create_fake_sbus_class(scenario)
        with mock.patch('storlet_daemon.daemon.SBus', fake_sbus_class):
            with mock.patch('os.fdopen'):
                self.pile.spawn(daemon.main_loop)
                eventlet.sleep()
                ret = [ret for ret in self.pile][0]

        self.assertEqual(EXIT_SUCCESS, ret)
        # sanity for no error and no warning
        self.assertEqual([], self.logger.get_log_lines('error'))
        self.assertEqual([], self.logger.get_log_lines('warn'))

    def test_main_loop_successful_stop(self):
        # SBUS_CMD_HALT is for working to stop requested from
        # storlet_middleware
        self._test_main_loop_stop(sbus.command.SBUS_CMD_HALT)

    def test_main_loop_canceled_stop(self):
        # SBUS_CMD_CANCEL is for working to stop from sort of daemon
        # management tools
        # TODO(kota_): SBUS_CMD_CANCEL has more tasks to do for cleanup
        # so need more assertions.
        self._test_main_loop_stop(sbus.command.SBUS_CMD_CANCEL)
示例#2
0
 def setUp(self):
     self.logger = FakeLogger()
     self.pile = eventlet.greenpool.GreenPile(1)