Ejemplo n.º 1
0
    def testNoDisableMissing__run_option_hooks(self):
        """ registration.run_option_hooks: runs the parsing methods for
            registered plugins using option hooks; not disabled if missing
            options in the parser.
            """
        parser = self.MockParser()
        parser.blocks = (('apps', (('sup', ('val1', 'val2')), )), )

        # fake option registration for plugin
        plugin = MockPlugin()
        registration._registered.register(plugin.name, lambda: plugin,
                                          {'option': True})
        registration._option_hooks.register('apps_sup', lambda: plugin, {})

        # run option hooks for plugin, check if it works
        registration.run_option_hooks(parser, disable_missing=False)
        self.assertTrue(hasattr(plugin, 'test__option'))
        self.assertEqual(plugin.test__option,
                         [('sup', 'apps', ('val1', 'val2'))])

        item = registration._registered.get(plugin.name)
        self.assertIsNotNone(item)
        plugin, props = item
        self.assertFalse(props.get('disabled', False))

        # run option hooks again for plugin using empty parser, confirm test
        # plugin has not been disabled
        registration.run_option_hooks(self.MockParser(), disable_missing=False)

        self.assertFalse(props.get('disabled'))
Ejemplo n.º 2
0
    def test__setup_sudo_access(self):
        """ registration.setup_sudo_access: installs a sudo-based `run_root`
            method onto the provided plugin.
            """
        plugin = MockPlugin()
        registration.setup_sudo_access(plugin)

        # check if method was injected
        method = getattr(plugin, 'run_root', None)
        self.assertIsNotNone(method)
        self.assertTrue(callable(method))
Ejemplo n.º 3
0
    def setUp(self):
        super(TestTaskRunner, self).setUp()
        self.task = MockTask()
        self.task.load()
        self.pipe = MockPipe()
        self.task_runner = daemon.TaskRunner(self.task, os.getpid(), self.pipe)
        self.task_runner._rlock = self.MockLock()

        # fake event plugin registration
        self.plugin = MockPlugin()
        self.plugin.needs_root = True
        for event in ("task_start", "task_run", "task_end"):
            registration._event_hooks[event] = [(self.plugin.name, lambda: self.plugin)]

        registration._registered.register(self.plugin.name, lambda: self.plugin, {"event": True})
Ejemplo n.º 4
0
    def test__run_event_hooks(self):
        """ registration.run_event_hooks: runs the task event methods for
            registered event plugins.
            """
        plugin = MockPlugin()

        # fake event registration for plugin
        for event in ('task_start', 'task_run', 'task_end'):
            registration._event_hooks[event] = [(plugin.name, lambda: plugin)]

            # run event hooks for plugin, check if it works
            registration.run_event_hooks(event, MockTask())

            if event == 'task_start':
                self.assertTrue(hasattr(plugin, 'test__task_started'))

            elif event == 'task_run':
                self.assertTrue(hasattr(plugin, 'test__task_ran'))
                self.assertEqual(plugin.test__task_ran, 1)

            elif event == 'task_end':
                self.assertTrue(hasattr(plugin, 'test__task_ended'))
Ejemplo n.º 5
0
class TestTaskRunner(FocusTestCase):
    class MockLock(object):
        def acquire(self):
            pass

        def release(self):
            pass

    def setUp(self):
        super(TestTaskRunner, self).setUp()
        self.task = MockTask()
        self.task.load()
        self.pipe = MockPipe()
        self.task_runner = daemon.TaskRunner(self.task, os.getpid(), self.pipe)
        self.task_runner._rlock = self.MockLock()

        # fake event plugin registration
        self.plugin = MockPlugin()
        self.plugin.needs_root = True
        for event in ("task_start", "task_run", "task_end"):
            registration._event_hooks[event] = [(self.plugin.name, lambda: self.plugin)]

        registration._registered.register(self.plugin.name, lambda: self.plugin, {"event": True})

    def tearDown(self):
        self.plugin = None
        self.task_runner = None
        self.pipe = None
        self.task = None

        # unregister
        registration._event_hooks = {}
        registration._registered.clear()

        super(TestTaskRunner, self).tearDown()

    def testElapsedShutdown___run(self):
        """ TaskRunner._run: Shuts down if task has elapsed.
            """
        self.task_runner._task.elapsed = True
        with self.assertRaises(SystemExit):
            self.task_runner._run()

    def test___setup_root_plugins(self):
        """ TaskRunner._setup_root_plugins: installs root plugin methods.
            """
        self.task_runner._setup_root_plugins()

        # setup properly
        method = getattr(self.plugin, "run_root", None)
        self.assertIsNotNone(method)
        self.assertTrue(callable(method))

        # send command, received "OK" from server
        self.pipe.send_bytes("OK")  # fake response from command server
        self.assertTrue(self.plugin.run_root("omg-llama"))

        # verify packet that was sent to command server
        self.assertEqual(self.pipe.recv_bytes(), "SHL\x80omg-llama")

        # send again, this time with "FAIL" sent from server
        self.pipe.send_bytes("FAIL")
        self.assertFalse(self.plugin.run_root("omg-llama"))
        self.assertEqual(self.pipe.recv_bytes(), "SHL\x80omg-llama")

        # test TRM sentinel value received, shuts down this process
        self.pipe.send_bytes("TRM")
        with self.assertRaises(SystemExit):
            self.plugin.run_root("omg-llama")

    def testTaskEnd___run_events(self):
        """ TaskRunner._run_events: runs task_end events.
            """
        self.task_runner._run_events(shutdown=True)
        self.assertTrue(hasattr(self.plugin, "test__task_started"))
        self.assertTrue(hasattr(self.plugin, "test__task_ended"))
        self.assertFalse(hasattr(self.plugin, "test__task_ran"))

    def testTaskRun___run_events(self):
        """ TaskRunner._run_events: runs task_run events.
            """
        self.task_runner._run_events(shutdown=False)
        self.assertTrue(hasattr(self.plugin, "test__task_started"))
        self.assertTrue(hasattr(self.plugin, "test__task_ran"))
        self.assertFalse(hasattr(self.plugin, "test__task_ended"))

        # test running non-shutdown multiple times, should call task_run
        # multiple times
        for i in range(10):
            self.assertEqual(self.plugin.test__task_ran, i + 1)
            self.task_runner._run_events(shutdown=False)

    def testSkipHooks__shutdown(self):
        """ TaskRunner.shutdown: shuts down process, skipping event hooks.
            """
        with self.assertRaises(SystemExit):
            self.task_runner.shutdown(skip_hooks=True)

        # check if end hooks were not called
        self.assertFalse(hasattr(self.plugin, "test__task_ended"))

        _check_pipe_shutdown(self, self.pipe)
        self.assertFalse(self.task.active)

    def testNoSkipHooks__shutdown(self):
        """ TaskRunner.shutdown: shuts down process, without skipping event
            hooks.
            """
        with self.assertRaises(SystemExit):
            self.task_runner.shutdown(skip_hooks=False)

        # check if end hooks were called
        self.assertTrue(hasattr(self.plugin, "test__task_ended"))

        _check_pipe_shutdown(self, self.pipe)
        self.assertFalse(self.task.active)