Beispiel #1
0
 def test_register_post_import_hook_before_import(self):
     """
     Test that a hook is fired after registering.
     """
     test_hook = mock.MagicMock()
     register_post_import_hook('tests.utils.test_module', test_hook)
     import tests.utils.test_module  # noqa
     test_hook.assert_called_once()
Beispiel #2
0
 def test_register_post_import_hook_reimport(self):
     """
     Test that a hook is fired when the module is reimported.
     """
     test_hook = mock.MagicMock()
     register_post_import_hook('tests.utils.test_module', test_hook)
     import tests.utils.test_module
     reload_module(tests.utils.test_module)
     self.assertEqual(test_hook.call_count, 2)
Beispiel #3
0
    def test_hook_called_with_module(self):
        """
        Test that a hook is called with the module that it is hooked on.
        """
        def test_hook(module):
            self.assertTrue(hasattr(module, 'A'))

        register_post_import_hook('tests.utils.test_module', test_hook)
        import tests.utils.test_module  # noqa
Beispiel #4
0
 def test_register_post_import_hook_multiple(self):
     """
     Test that multiple hooks are fired after registering.
     """
     test_hook = mock.MagicMock()
     test_hook2 = mock.MagicMock()
     register_post_import_hook('tests.utils.test_module', test_hook)
     register_post_import_hook('tests.utils.test_module', test_hook2)
     import tests.utils.test_module  # noqa
     test_hook.assert_called_once()
     test_hook2.assert_called_once()
Beispiel #5
0
 def test_register_post_import_hook_different_modules(self):
     """
     Test that multiple hooks hooked on different modules are fired after registering.
     """
     test_hook = mock.MagicMock()
     test_hook_redis = mock.MagicMock()
     register_post_import_hook('tests.utils.test_module', test_hook)
     register_post_import_hook('oteltrace.contrib.redis', test_hook_redis)
     import tests.utils.test_module  # noqa
     import oteltrace.contrib.redis  # noqa
     test_hook.assert_called_once()
     test_hook_redis.assert_called_once()
Beispiel #6
0
 def test_deregister_post_import_hook_after_register(self):
     """
     Test that import hooks can be deregistered after being registered.
     """
     test_hook = mock.MagicMock()
     register_post_import_hook('tests.utils.test_module', test_hook)
     outcome = deregister_post_import_hook('tests.utils.test_module',
                                           test_hook)
     self.assertTrue(outcome)
     import tests.utils.test_module  # noqa
     self.assertEqual(
         test_hook.call_count, 0,
         'hook has been deregistered and should have been removed')
Beispiel #7
0
    def test_deregister_post_import_hook_after_import(self):
        """
        Test that import hooks can be deregistered after being registered.
        """
        test_hook = mock.MagicMock()
        register_post_import_hook('tests.utils.test_module', test_hook)

        import tests.utils.test_module
        test_hook.assert_called_once()
        outcome = deregister_post_import_hook('tests.utils.test_module',
                                              test_hook)
        self.assertTrue(outcome)
        reload_module(tests.utils.test_module)
        self.assertEqual(test_hook.call_count, 1,
                         'hook should only be called once')
Beispiel #8
0
 def test_register_post_import_hook_after_import(self):
     """
     Test that a hook is fired when the module is imported with an
     appropriate log debug message.
     """
     test_hook = mock.MagicMock()
     with mock.patch('oteltrace.utils.hook.log') as log_mock:
         import tests.utils.test_module  # noqa
         register_post_import_hook('tests.utils.test_module', test_hook)
         test_hook.assert_called_once()
         calls = [
             mock.call(
                 'module "tests.utils.test_module" already imported, firing hook'
             )
         ]
         log_mock.debug.assert_has_calls(calls)
Beispiel #9
0
    def test_hook_exception(self):
        """
        Test that when a hook throws an exception that it is caught and logged
        as a warning.
        """
        def test_hook(module):
            raise Exception('test_hook_failed')

        register_post_import_hook('tests.utils.test_module', test_hook)

        with mock.patch('oteltrace.utils.hook.log') as log_mock:
            import tests.utils.test_module  # noqa
            calls = [
                mock.call(
                    'hook "{}" for module "tests.utils.test_module" failed: test_hook_failed'
                    .format(test_hook))
            ]
            log_mock.warning.assert_has_calls(calls)
Beispiel #10
0
    def test_deregister_post_import_hook_after_register_multiple(self):
        """
        Test that only the specified import hook can be deregistered after being registered.
        """
        # Enforce a spec so that hasattr doesn't vacuously return True.
        test_hook = mock.MagicMock(spec=[])
        test_hook2 = mock.MagicMock(spec=[])
        register_post_import_hook('tests.utils.test_module', test_hook)
        register_post_import_hook('tests.utils.test_module', test_hook2)

        outcome = deregister_post_import_hook('tests.utils.test_module',
                                              test_hook)
        self.assertTrue(outcome)
        import tests.utils.test_module  # noqa
        self.assertEqual(test_hook.call_count, 0,
                         'hook has been deregistered and should be removed')
        self.assertEqual(test_hook2.call_count, 1,
                         'hook should have been called')
Beispiel #11
0
    def test_register_post_import_hook_duplicate_register(self):
        """
        Test that a function can be registered as a hook twice.
        """
        test_hook = mock.MagicMock()
        with mock.patch('oteltrace.utils.hook.log') as log_mock:
            register_post_import_hook('tests.utils.test_module', test_hook)
            register_post_import_hook('tests.utils.test_module', test_hook)
            import tests.utils.test_module  # noqa

            # Since the log message will contain the id (non-deterministic) of the hook
            # we just check to see if the important parts of the log message are included
            # in the message. Those being the name and the module to be hooked.
            class Matcher(object):
                def __eq__(self, other):
                    return 'MagicMock' in other and 'already exists on module "tests.utils.test_module"' in other

            calls = [mock.call(Matcher())]
            self.assertEqual(test_hook.call_count, 1)
            log_mock.debug.assert_has_calls(calls)
Beispiel #12
0
    def test_deregister_post_import_hook_after_register_multiple_all(self):
        """
        Test that multiple import hooks can be deregistered.
        """
        test_hook = mock.MagicMock()
        test_hook2 = mock.MagicMock()
        register_post_import_hook('tests.utils.test_module', test_hook)
        register_post_import_hook('tests.utils.test_module', test_hook2)

        outcome = deregister_post_import_hook('tests.utils.test_module',
                                              test_hook)
        self.assertTrue(outcome)
        outcome = deregister_post_import_hook('tests.utils.test_module',
                                              test_hook2)
        self.assertTrue(outcome)
        import tests.utils.test_module  # noqa
        self.assertEqual(test_hook.call_count, 0,
                         'hook has been deregistered and should be removed')
        self.assertEqual(test_hook2.call_count, 0,
                         'hook has been deregistered and should be removed')