Example #1
0
 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)
Example #2
0
 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)
 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)
Example #4
0
 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)
Example #5
0
 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)
Example #6
0
 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))
Example #7
0
 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))
Example #8
0
 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)
Example #9
0
def handle_args(name, args):
    # Note that if an exception happens between now and when logging is
    # setup, we'll only see it in the journal
    hotplug_reporter = events.ReportEventStack(name,
                                               __doc__,
                                               reporting_enabled=True)

    hotplug_init = Init(ds_deps=[], reporter=hotplug_reporter)
    hotplug_init.read_cfg()

    log.setupLogging(hotplug_init.cfg)
    if "reporting" in hotplug_init.cfg:
        reporting.update_configuration(hotplug_init.cfg.get("reporting"))
    # Logging isn't going to be setup until now
    LOG.debug(
        "%s called with the following arguments: {"
        "hotplug_action: %s, subsystem: %s, udevaction: %s, devpath: %s}",
        name,
        args.hotplug_action,
        args.subsystem,
        args.udevaction if "udevaction" in args else None,
        args.devpath if "devpath" in args else None,
    )

    with hotplug_reporter:
        try:
            if args.hotplug_action == "query":
                try:
                    datasource = initialize_datasource(hotplug_init,
                                                       args.subsystem)
                except DataSourceNotFoundException:
                    print("Unable to determine hotplug state. No datasource "
                          "detected")
                    sys.exit(1)
                print("enabled" if datasource else "disabled")
            else:
                handle_hotplug(
                    hotplug_init=hotplug_init,
                    devpath=args.devpath,
                    subsystem=args.subsystem,
                    udevaction=args.udevaction,
                )
        except Exception:
            LOG.exception("Received fatal exception handling hotplug!")
            raise

    LOG.debug("Exiting hotplug handler")
    reporting.flush_events()
Example #10
0
 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)
 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)
Example #12
0
def handle_args(name, args):
    # Note that if an exception happens between now and when logging is
    # setup, we'll only see it in the journal
    hotplug_reporter = events.ReportEventStack(name,
                                               __doc__,
                                               reporting_enabled=True)

    hotplug_init = Init(ds_deps=[], reporter=hotplug_reporter)
    hotplug_init.read_cfg()

    log.setupLogging(hotplug_init.cfg)
    if 'reporting' in hotplug_init.cfg:
        reporting.update_configuration(hotplug_init.cfg.get('reporting'))

    # Logging isn't going to be setup until now
    LOG.debug(
        '%s called with the following arguments: {udevaction: %s, '
        'subsystem: %s, devpath: %s}', name, args.udevaction, args.subsystem,
        args.devpath)
    LOG.debug(
        '%s called with the following arguments:\n'
        'udevaction: %s\n'
        'subsystem: %s\n'
        'devpath: %s', name, args.udevaction, args.subsystem, args.devpath)

    with hotplug_reporter:
        try:
            handle_hotplug(
                hotplug_init=hotplug_init,
                devpath=args.devpath,
                subsystem=args.subsystem,
                udevaction=args.udevaction,
            )
        except Exception:
            LOG.exception('Received fatal exception handling hotplug!')
            raise

    LOG.debug('Exiting hotplug handler')
    reporting.flush_events()
Example #13
0
def apply_reporting_cfg(cfg):
    if cfg.get('reporting'):
        reporting.update_configuration(cfg.get('reporting'))
Example #14
0
 def test_empty_configuration_doesnt_add_handlers(
         self, instantiated_handler_registry):
     reporting.update_configuration({})
     self.assertEqual(
         0, instantiated_handler_registry.register_item.call_count)
Example #15
0
 def test_empty_configuration_doesnt_add_handlers(
         self, instantiated_handler_registry):
     reporting.update_configuration({})
     self.assertEqual(
         0, instantiated_handler_registry.register_item.call_count)
Example #16
0
def apply_reporting_cfg(cfg):
    if cfg.get('reporting'):
        reporting.update_configuration(cfg.get('reporting'))