コード例 #1
0
ファイル: test_reporting.py プロジェクト: harlowja/cloud-init
class TestReportingConfiguration(TestCase):

    @mock.patch.object(reporting, 'instantiated_handler_registry')
    def test_empty_configuration_doesnt_add_handlers(
            self, instantiated_handler_registry):
        reporting.add_configuration({})
        self.assertEqual(
            0, instantiated_handler_registry.register_item.call_count)

    @mock.patch.object(
        reporting, 'instantiated_handler_registry', reporting.DictRegistry())
    @mock.patch.object(reporting, 'available_handlers')
    def test_looks_up_handler_by_type_and_adds_it(self, available_handlers):
        handler_type_name = 'test_handler'
        handler_cls = mock.Mock()
        available_handlers.registered_items = {handler_type_name: handler_cls}
        handler_name = 'my_test_handler'
        reporting.add_configuration(
            {handler_name: {'type': handler_type_name}})
        self.assertEqual(
            {handler_name: handler_cls.return_value},
            reporting.instantiated_handler_registry.registered_items)

    @mock.patch.object(
        reporting, 'instantiated_handler_registry', reporting.DictRegistry())
    @mock.patch.object(reporting, 'available_handlers')
    def test_uses_non_type_parts_of_config_dict_as_kwargs(
            self, available_handlers):
        handler_type_name = 'test_handler'
        handler_cls = mock.Mock()
        available_handlers.registered_items = {handler_type_name: handler_cls}
        extra_kwargs = {'foo': 'bar', 'bar': 'baz'}
        handler_config = extra_kwargs.copy()
        handler_config.update({'type': handler_type_name})
        handler_name = 'my_test_handler'
        reporting.add_configuration({handler_name: handler_config})
        self.assertEqual(
            handler_cls.return_value,
            reporting.instantiated_handler_registry.registered_items[
                handler_name])
        self.assertEqual([mock.call(**extra_kwargs)],
                         handler_cls.call_args_list)

    @mock.patch.object(
        reporting, 'instantiated_handler_registry', reporting.DictRegistry())
    @mock.patch.object(reporting, 'available_handlers')
    def test_handler_config_not_modified(self, available_handlers):
        handler_type_name = 'test_handler'
        handler_cls = mock.Mock()
        available_handlers.registered_items = {handler_type_name: handler_cls}
        handler_config = {'type': handler_type_name, 'foo': 'bar'}
        expected_handler_config = handler_config.copy()
        reporting.add_configuration({'my_test_handler': handler_config})
        self.assertEqual(expected_handler_config, handler_config)
コード例 #2
0
class TestReportingConfiguration(TestCase):
    @mock.patch.object(reporting, "instantiated_handler_registry")
    def test_empty_configuration_doesnt_add_handlers(
            self, instantiated_handler_registry):
        reporting.update_configuration({})
        self.assertEqual(
            0, instantiated_handler_registry.register_item.call_count)

    @mock.patch.object(reporting, "instantiated_handler_registry",
                       reporting.DictRegistry())
    @mock.patch.object(reporting, "available_handlers")
    def test_looks_up_handler_by_type_and_adds_it(self, available_handlers):
        handler_type_name = "test_handler"
        handler_cls = mock.Mock()
        available_handlers.registered_items = {handler_type_name: handler_cls}
        handler_name = "my_test_handler"
        reporting.update_configuration(
            {handler_name: {
                "type": handler_type_name
            }})
        self.assertEqual(
            {handler_name: handler_cls.return_value},
            reporting.instantiated_handler_registry.registered_items,
        )

    @mock.patch.object(reporting, "instantiated_handler_registry",
                       reporting.DictRegistry())
    @mock.patch.object(reporting, "available_handlers")
    def test_uses_non_type_parts_of_config_dict_as_kwargs(
            self, available_handlers):
        handler_type_name = "test_handler"
        handler_cls = mock.Mock()
        available_handlers.registered_items = {handler_type_name: handler_cls}
        extra_kwargs = {"foo": "bar", "bar": "baz"}
        handler_config = extra_kwargs.copy()
        handler_config.update({"type": handler_type_name})
        handler_name = "my_test_handler"
        reporting.update_configuration({handler_name: handler_config})
        self.assertEqual(
            handler_cls.return_value,
            reporting.instantiated_handler_registry.
            registered_items[handler_name],
        )
        self.assertEqual([mock.call(**extra_kwargs)],
                         handler_cls.call_args_list)

    @mock.patch.object(reporting, "instantiated_handler_registry",
                       reporting.DictRegistry())
    @mock.patch.object(reporting, "available_handlers")
    def test_handler_config_not_modified(self, available_handlers):
        handler_type_name = "test_handler"
        handler_cls = mock.Mock()
        available_handlers.registered_items = {handler_type_name: handler_cls}
        handler_config = {"type": handler_type_name, "foo": "bar"}
        expected_handler_config = handler_config.copy()
        reporting.update_configuration({"my_test_handler": handler_config})
        self.assertEqual(expected_handler_config, handler_config)

    @mock.patch.object(reporting, "instantiated_handler_registry",
                       reporting.DictRegistry())
    @mock.patch.object(reporting, "available_handlers")
    def test_handlers_removed_if_falseish_specified(self, available_handlers):
        handler_type_name = "test_handler"
        handler_cls = mock.Mock()
        available_handlers.registered_items = {handler_type_name: handler_cls}
        handler_name = "my_test_handler"
        reporting.update_configuration(
            {handler_name: {
                "type": handler_type_name
            }})
        self.assertEqual(
            1, len(reporting.instantiated_handler_registry.registered_items))
        reporting.update_configuration({handler_name: None})
        self.assertEqual(
            0, len(reporting.instantiated_handler_registry.registered_items))