Exemple #1
0
    def create(self, factory_ctx):
        """
        Automatically called to create the check function's object
        :param factory_ctx: (dict) names available for Function instantiation
        :return: an object that implements a check function
        """
        # load plugins dependencies and store them locally for efficiency
        if not self.http_factory:
            self.http_factory = plugin_manager.get_plugin_obj_by_name(
                'http', 'Function')

        if not self.jmx_factory:
            self.jmx_factory = plugin_manager.get_plugin_obj_by_name(
                'jmx', 'Function')

        if not self.counter_factory:
            self.counter_factory = plugin_manager.get_plugin_obj_by_name(
                'counter', 'Function')

        return propartial(ZomcatWrapper,
                          host=factory_ctx['host'],
                          instance=factory_ctx['instance'],
                          http=self.http_factory.create(factory_ctx),
                          jmx=self.jmx_factory.create(factory_ctx),
                          counter=self.counter_factory.create(factory_ctx))
    def test_load_plugins_extras(self):
        """
        Test is we can load correctly the extra plugins. Also loads builtins and simple test plugins.
        Notice we put two folders to ZMON_PLUGINS env var, separated by ':'
        """
        # reload the plugin
        reload(plugin_manager)

        # Lets create a category filter that includes our builtin plugin type and 2 types we defines for our tests
        category_filter = {
            'Function': IFunctionFactoryPlugin,
            'Color': IColorPlugin,
            'Temperature': ITemperaturePlugin,
        }

        # init the plugin manager
        plugin_manager.init_plugin_manager(category_filter=category_filter)

        # collect builtins and explore folder in env var, e.g. ZMON_PLUGINS="/path/one:path/two:/path/three"
        plugin_manager.collect_plugins(load_builtins=True, load_env=True, additional_dirs=None)

        # check categories
        all_categories = plugin_manager.get_all_categories()
        seen_categories = plugin_manager.get_loaded_plugins_categories()
        self.assertEqual(set(all_categories), set(category_filter.keys()), 'All defined categories are stored')

        self.assertTrue(len(seen_categories) >= 2 and set(seen_categories).issubset(set(all_categories)),
                        'found at least 2 categories and they all belong to all defined categories')

        # check known test plugins are loaded
        extra_plugins = ['exacrm', 'job_lock', 'nagios', 'snmp', 'mssql']  # non exhaustive list
        known_plugin_names = extra_plugins + ['http', 'color_spain', 'color_germany', 'temperature_fridge']
        plugin_names = plugin_manager.get_all_plugin_names()
        self.assertTrue(set(known_plugin_names).issubset(plugin_names), 'All known test plugins are loaded')

        # check extra plugins
        for name, category in zip(extra_plugins, ['Function']*len(extra_plugins)):

            p = plugin_manager.get_plugin_by_name(name, category)
            p_obj = plugin_manager.get_plugin_obj_by_name(name, category)
            self.assertEqual(id(p.plugin_object), id(p_obj), 'locate plugin object works')

            self.assertTrue(p.is_activated)
            self.assertEqual(p.plugin_object.is_activated, p.is_activated, 'plugin is activated')

            self.assertTrue(isinstance(p_obj, IFunctionFactoryPlugin),
                            'plugin object is instance of IFunctionFactoryPlugin')

        # test extra plugin are configured according to config file
        self.assertEqual(plugin_manager.get_plugin_obj_by_name('exacrm', 'Function')._exacrm_cluster, '--secret--',
                         'exacrm object is configured')
    def test_global_config(self):
        """
        Test that the plugin can configure itself from the global config and that global config
        takes precedence over local config
        """
        # reload the plugin
        reload(plugin_manager)

        # Lets create a category filter that includes our builtin plugin type and 2 types we defines for our tests
        category_filter = {
            'Function': IFunctionFactoryPlugin,
            'Color': IColorPlugin,
            'Temperature': ITemperaturePlugin,
        }

        # init the plugin manager
        plugin_manager.init_plugin_manager(category_filter=category_filter)

        # inject as global conf to color_german plugin fashion sites different from the local conf
        global_conf = {
            'plugin.color_germany.fashion_sites': 'superfashion.de hypefashion.de',
            'plugin.other_plugin.otherkey': 'this will not be passed to color_germany.configure',
        }

        # collect plugins builtin and explore env_var: ZMON_PLUGINS="/.../tests/plugins/simple_plugins"
        plugin_manager.collect_plugins(load_builtins=True, load_env=True, additional_dirs=None,
                                       global_config=global_conf)

        # test that color_german plugin was configured according to the global conf

        global_conf_sites = ['superfashion.de', 'hypefashion.de']

        color_ger_obj = plugin_manager.get_plugin_obj_by_name('color_germany', 'Color')

        self.assertTrue(set(global_conf_sites) == set(color_ger_obj.main_fashion_sites), 'object is configured')
    def test_load_builtin_plugins(self):
        """
        Test that city plugin can be fully loaded
        """
        # reload the plugin
        reload(plugin_manager)

        plugin_manager.init_plugin_manager()  # init the plugin manager

        # collect only builtin plugins
        plugin_manager.collect_plugins(load_builtins=True, load_env=False, additional_dirs=None)

        # city plugin is a builtin and is in category entity
        plugin_name = 'http'
        plugin_category = 'Function'

        self.assertIn(plugin_name, plugin_manager.get_all_plugin_names(), 'http plugin name must be found')

        http_plugin = plugin_manager.get_plugin_by_name(plugin_name, plugin_category)

        self.assertIsNotNone(http_plugin, 'http plugin must be under category Function')
        self.assertEqual(http_plugin.name, plugin_name, 'check plugin name field')

        # check city plugin object
        self.assertTrue(hasattr(http_plugin, 'plugin_object'), 'http.plugin_object exists')
        self.assertIsNotNone(http_plugin.plugin_object, 'http.plugin_object is not None')
        self.assertTrue(isinstance(plugin_manager.get_plugin_obj_by_name(plugin_name, plugin_category), IFunctionFactoryPlugin),
                        'the entity plugin object is instance of IFunctionFactoryPlugin')

        # check that city plugin object is activated
        self.assertTrue(http_plugin.is_activated)
        self.assertEqual(http_plugin.plugin_object.is_activated, http_plugin.is_activated)
 def create(self, factory_ctx):
     """
     Automatically called to create the check function's object
     :param factory_ctx: (dict) names available for Function instantiation
     :return: an object that implements a check function
     """
     # load plugins dependencies and store them locally for efficiency
     if not self.counter_factory:
         self.counter_factory = plugin_manager.get_plugin_obj_by_name('counter', 'Function')
     return propartial(MemcachedWrapper, counter=self.counter_factory.create(factory_ctx), host=factory_ctx['host'])
Exemple #6
0
    def create(self, factory_ctx):
        """
        Automatically called to create the check function's object
        :param factory_ctx: (dict) names available for Function instantiation
        :return: an object that implements a check function
        """
        if not self.counter_factory:
            self.counter_factory = plugin_manager.get_plugin_obj_by_name('counter', 'Function')

        return propartial(LdapWrapper, user=self._ldapuser, password=self._ldappass, host=factory_ctx['host'],
                          counter=self.counter_factory.create(factory_ctx))
Exemple #7
0
    def create(self, factory_ctx):
        """
        Automatically called to create the check function's object
        :param factory_ctx: (dict) names available for Function instantiation
        :return: an object that implements a check function
        """
        if not self.counter_factory:
            self.counter_factory = plugin_manager.get_plugin_obj_by_name('counter', 'Function')

        return propartial(LdapWrapper, user=self._ldapuser, password=self._ldappass, host=factory_ctx['host'],
                          counter=self.counter_factory.create(factory_ctx))
Exemple #8
0
    def create(self, factory_ctx):
        """
        Automatically called to create the check function's object
        :param factory_ctx: (dict) names available for Function instantiation
        :return: an object that implements a check function
        """

        # load plugins dependencies and store them locally for efficiency
        if not self.http_factory:
            self.http_factory = plugin_manager.get_plugin_obj_by_name('http', 'Function')

        return propartial(EventLogWrapper,
                          http_wrapper=self.http_factory.create(factory_ctx),
                          url=self.eventlog_url)
Exemple #9
0
    def create(self, factory_ctx):
        """
        Automatically called to create the check function's object
        :param factory_ctx: (dict) names available for Function instantiation
        :return: an object that implements a check function
        """

        # load plugins dependencies and store them locally for efficiency
        if not self.http_factory:
            self.http_factory = plugin_manager.get_plugin_obj_by_name(
                'http', 'Function')

        return propartial(JobsWrapper,
                          http_wrapper=self.http_factory.create(factory_ctx),
                          project=factory_ctx['entity'].get('name'))
    def create(self, factory_ctx):
        """
        Automatically called to create the check function's object
        :param factory_ctx: (dict) names available for Function instantiation
        :return: an object that implements a check function
        """
        # load plugins dependencies and store them locally for efficiency
        if not self.counter_factory:
            self.counter_factory = plugin_manager.get_plugin_obj_by_name(
                'counter', 'Function')

        return propartial(RedisWrapper,
                          counter=self.counter_factory.create(factory_ctx),
                          host=factory_ctx['host'],
                          password=self.__password)
    def test_load_builtin_plugins(self):
        """
        Test that city plugin can be fully loaded
        """
        # reload the plugin
        reload(plugin_manager)

        plugin_manager.init_plugin_manager()  # init the plugin manager

        # collect only builtin plugins
        plugin_manager.collect_plugins(load_builtins=True,
                                       load_env=False,
                                       additional_dirs=None)

        # city plugin is a builtin and is in category entity
        plugin_name = 'http'
        plugin_category = 'Function'

        self.assertIn(plugin_name, plugin_manager.get_all_plugin_names(),
                      'http plugin name must be found')

        http_plugin = plugin_manager.get_plugin_by_name(
            plugin_name, plugin_category)

        self.assertIsNotNone(http_plugin,
                             'http plugin must be under category Function')
        self.assertEqual(http_plugin.name, plugin_name,
                         'check plugin name field')

        # check city plugin object
        self.assertTrue(hasattr(http_plugin, 'plugin_object'),
                        'http.plugin_object exists')
        self.assertIsNotNone(http_plugin.plugin_object,
                             'http.plugin_object is not None')
        self.assertTrue(
            isinstance(
                plugin_manager.get_plugin_obj_by_name(plugin_name,
                                                      plugin_category),
                IFunctionFactoryPlugin),
            'the entity plugin object is instance of IFunctionFactoryPlugin')

        # check that city plugin object is activated
        self.assertTrue(http_plugin.is_activated)
        self.assertEqual(http_plugin.plugin_object.is_activated,
                         http_plugin.is_activated)
Exemple #12
0
    def create(self, factory_ctx):
        """
        Automatically called to create the check function's object
        :param factory_ctx: (dict) names available for Function instantiation
        :return: an object that implements a check function
        """
        entity = factory_ctx['entity']
        project = entity['name'] if entity['type'] == 'project' else None

        # load plugins dependencies and store them locally for efficiency
        if not self.http_factory:
            self.http_factory = plugin_manager.get_plugin_obj_by_name('http', 'Function')

        return propartial(ExceptionsWrapper,
                          http_wrapper=self.http_factory.create(factory_ctx),
                          host=factory_ctx['host'],
                          instance=factory_ctx['instance'],
                          project=project)
    def test_global_config(self):
        """
        Test that the plugin can configure itself from the global config and that global config
        takes precedence over local config
        """
        # reload the plugin
        reload(plugin_manager)

        # Lets create a category filter that includes our builtin plugin type and 2 types we defines for our tests
        category_filter = {
            'Function': IFunctionFactoryPlugin,
            'Color': IColorPlugin,
            'Temperature': ITemperaturePlugin,
        }

        # init the plugin manager
        plugin_manager.init_plugin_manager(category_filter=category_filter)

        # inject as global conf to color_german plugin fashion sites different from the local conf
        global_conf = {
            'plugin.color_germany.fashion_sites':
            'superfashion.de hypefashion.de',
            'plugin.other_plugin.otherkey':
            'this will not be passed to color_germany.configure',
        }

        # collect plugins builtin and explore env_var: ZMON_PLUGINS="/.../tests/plugins/simple_plugins"
        plugin_manager.collect_plugins(load_builtins=True,
                                       load_env=True,
                                       additional_dirs=None,
                                       global_config=global_conf)

        # test that color_german plugin was configured according to the global conf

        global_conf_sites = ['superfashion.de', 'hypefashion.de']

        color_ger_obj = plugin_manager.get_plugin_obj_by_name(
            'color_germany', 'Color')

        self.assertTrue(
            set(global_conf_sites) == set(color_ger_obj.main_fashion_sites),
            'object is configured')
Exemple #14
0
        params['time_to'] = time_to
        params['group_by'] = group_by
        result = self.__request('count', params)
        if len(event_type_ids) == 1 and not group_by:
            return result.get(params['event_type_ids'], 0)
        else:
            return result


if __name__ == '__main__':
    import sys
    import logging

    logging.basicConfig(level=logging.DEBUG)

    # init plugin manager and collect plugins, as done by Zmon when worker is starting
    plugin_manager.init_plugin_manager()
    plugin_manager.collect_plugins(load_builtins=True, load_env=True)

    eventlog_url = sys.argv[1]
    factory_ctx = {}

    http = plugin_manager.get_plugin_obj_by_name('http', 'Function').create(factory_ctx)

    # eventlog = EventLogWrapper()
    eventlog = EventLogWrapper(http_wrapper=http, url=eventlog_url)

    print eventlog.count(0x96001, time_from='-1m')
    print eventlog.count([0x96001, 0x63005], time_from='-1m')
    print eventlog.count(0x96001, time_from='-1m', group_by='appDomainId')
                "used_memory_rss": 63475712
            }
        '''

        data = self.__con.info()
        stats = {}
        for key in STATISTIC_GAUGE_KEYS:
            stats[key] = data.get(key)
        for key in STATISTIC_COUNTER_KEYS:
            stats['{}_per_sec'.format(key).replace('total_', '')] = \
                round(self._counter.key(key).per_second(data.get(key, 0)), 2)
        stats['dbsize'] = self.__con.dbsize()
        return stats


if __name__ == '__main__':
    import sys
    import json

    # init plugin manager and collect plugins, as done by Zmon when worker is starting
    plugin_manager.init_plugin_manager()
    plugin_manager.collect_plugins(load_builtins=True, load_env=True)

    factory_ctx = {
        'redis_host': 'localhost',
    }
    counter = plugin_manager.get_plugin_obj_by_name(
        'counter', 'Function').create(factory_ctx)
    wrapper = RedisWrapper(counter, sys.argv[1])
    print json.dumps(wrapper.statistics(), indent=4, sort_keys=True)
    def test_load_plugins_several_categories(self):
        """
        Test is we can load and correctly locate plugins from several categories
        First it explores folders from ZMON_PLUGINS env_var, and then from additional_dirs
        """
        for test_load_from in ('env_var', 'additional_folders'):

            # reload the plugin
            reload(plugin_manager)

            # Lets create a category filter that includes our builtin plugin type and 2 types we defines for our tests
            category_filter = {
                'Function': IFunctionFactoryPlugin,
                'Color': IColorPlugin,
                'Temperature': ITemperaturePlugin,
            }

            if test_load_from == 'env_var':
                # init the plugin manager
                plugin_manager.init_plugin_manager(category_filter=category_filter)

                # collect plugins builtin and explore env_var: ZMON_PLUGINS="/.../tests/plugins/simple_plugins"
                plugin_manager.collect_plugins(load_builtins=True, load_env=True, additional_dirs=None)

            elif test_load_from == 'additional_folders':
                # init the plugin manager
                plugin_manager.init_plugin_manager(category_filter=category_filter)

                test_plugin_dir = simple_plugin_dir_abs_path()

                # collect plugins builtin and explore  additional_dirs: /.../tests/plugins/simple_plugins
                plugin_manager.collect_plugins(load_builtins=True, load_env=False, additional_dirs=[test_plugin_dir])

            # check categories

            all_categories = plugin_manager.get_all_categories()
            seen_categories = plugin_manager.get_loaded_plugins_categories()

            self.assertEqual(set(all_categories), set(category_filter.keys()), 'All defined categories are stored')
            self.assertTrue(len(seen_categories) >= 2 and set(seen_categories).issubset(set(all_categories)),
                            'found at least 2 categories and they all belong to all defined categories')

            # check known test plugins are loaded

            known_plugin_names = ['http', 'color_spain', 'color_germany', 'temperature_fridge']
            plugin_names = plugin_manager.get_all_plugin_names()

            self.assertTrue(set(known_plugin_names).issubset(plugin_names), 'All known test plugins are loaded')

            # test get_plugin_obj_by_name() and get_plugin_objs_of_category()

            color_ger = plugin_manager.get_plugin_by_name('color_germany', 'Color')
            color_ger_obj = plugin_manager.get_plugin_obj_by_name('color_germany', 'Color')
            self.assertEqual(id(color_ger.plugin_object), id(color_ger_obj), 'locate plugin object works')
            self.assertEqual(color_ger.plugin_object.country, 'germany', 'located object field values look good')
            all_color_objs = plugin_manager.get_plugin_objs_of_category('Color')
            self.assertEqual(id(color_ger_obj), id([obj for obj in all_color_objs if obj.country == 'germany'][0]),
                             'locate the plugin object in a convoluted way works too')

            # test that color_german plugin was configured with the main fashion sites

            conf_sites_germany = ['www.big_fashion_site.de', 'www.other_fashion_site.de']

            self.assertTrue(set(conf_sites_germany) == set(color_ger_obj.main_fashion_sites), 'object is configured')

            # test that plugin objects run its logic correctly

            color_obj_de = plugin_manager.get_plugin_obj_by_name('color_germany', 'Color')
            color_obj_es = plugin_manager.get_plugin_obj_by_name('color_spain', 'Color')

            simple_colors_de = ['grey', 'white', 'black']
            simple_colors_es = ['brown', 'yellow', 'blue']

            col_names_de = color_obj_de.get_season_color_names()
            col_names_es = color_obj_es.get_season_color_names()

            self.assertEqual(col_names_de, simple_colors_de)
            self.assertEqual(col_names_es, simple_colors_es)

            # Test also the logic of temperature plugin object, this simulates a bit more complex logic
            # Temp readings are simulated as a normal distribution centered at -5 and 0.2 sigma (values from config)
            # we spawn the thread that periodically do temp reading, we wait some intervals and then get the avg temp
            # Finally we check that T avg is -5 +- 10 sigmas (see local config)

            temp_fridge = plugin_manager.get_plugin_obj_by_name('temperature_fridge', 'Temperature')
            temp_fridge.start_update()
            time.sleep(temp_fridge.interval * 20)  # we wait for some temp collection to happen
            temp_fridge.stop = True
            tavg = temp_fridge.get_temperature_average()
            # This test is non-deterministic, but probability of failure is super small, so in practice it is ok
            self.assertTrue(abs(-5.0 - tavg) < 0.2 * 10, 'the avg temperature is close to -5')

            # test subpackage dependencies can be resolved
            self.assertEqual(temp_fridge.engine.power_unit, 'Watts')
Exemple #17
0

if __name__ == '__main__':

    # init plugin manager and collect plugins, as done by Zmon when worker is starting
    plugin_manager.init_plugin_manager()
    plugin_manager.collect_plugins(load_builtins=True, load_env=True)

    host = sys.argv[1]
    instance = sys.argv[2]

    factory_ctx = {
        'base_url': 'http://{host}:3{instance}/'.format(host=host, instance=instance),
        'host': host,
        'port': ZomcatWrapper.get_jmx_port(instance),
        'instance': instance,
        'redis_host': 'localhost',
    }

    # http = partial(HttpWrapper, base_url='http://{host}:3{instance}/'.format(host=host, instance=instance))
    http = plugin_manager.get_plugin_obj_by_name('http', 'Function').create(factory_ctx)

    # jmx = partial(JmxWrapper, host=host, port=ZomcatWrapper.get_jmx_port(instance))
    jmx = plugin_manager.get_plugin_obj_by_name('jmx', 'Function').create(factory_ctx)

    # counter = partial(CounterWrapper, redis_host='localhost')
    counter = plugin_manager.get_plugin_obj_by_name('counter', 'Function').create(factory_ctx)

    zomcat = ZomcatWrapper(host, instance, http=http, jmx=jmx, counter=counter)
    print json.dumps(zomcat.health(), indent=4, sort_keys=True)
    def test_load_plugins_extras(self):
        """
        Test is we can load correctly the extra plugins. Also loads builtins and simple test plugins.
        Notice we put two folders to ZMON_PLUGINS env var, separated by ':'
        """
        # reload the plugin
        reload(plugin_manager)

        # Lets create a category filter that includes our builtin plugin type and 2 types we defines for our tests
        category_filter = {
            'Function': IFunctionFactoryPlugin,
            'Color': IColorPlugin,
            'Temperature': ITemperaturePlugin,
        }

        # init the plugin manager
        plugin_manager.init_plugin_manager(category_filter=category_filter)

        # collect builtins and explore folder in env var, e.g. ZMON_PLUGINS="/path/one:path/two:/path/three"
        plugin_manager.collect_plugins(load_builtins=True,
                                       load_env=True,
                                       additional_dirs=None)

        # check categories
        all_categories = plugin_manager.get_all_categories()
        seen_categories = plugin_manager.get_loaded_plugins_categories()
        self.assertEqual(set(all_categories), set(category_filter.keys()),
                         'All defined categories are stored')

        self.assertTrue(
            len(seen_categories) >= 2
            and set(seen_categories).issubset(set(all_categories)),
            'found at least 2 categories and they all belong to all defined categories'
        )

        # check known test plugins are loaded
        extra_plugins = ['exacrm', 'job_lock', 'nagios', 'snmp',
                         'mssql']  # non exhaustive list
        known_plugin_names = extra_plugins + [
            'http', 'color_spain', 'color_germany', 'temperature_fridge'
        ]
        plugin_names = plugin_manager.get_all_plugin_names()
        self.assertTrue(
            set(known_plugin_names).issubset(plugin_names),
            'All known test plugins are loaded')

        # check extra plugins
        for name, category in zip(extra_plugins,
                                  ['Function'] * len(extra_plugins)):

            p = plugin_manager.get_plugin_by_name(name, category)
            p_obj = plugin_manager.get_plugin_obj_by_name(name, category)
            self.assertEqual(id(p.plugin_object), id(p_obj),
                             'locate plugin object works')

            self.assertTrue(p.is_activated)
            self.assertEqual(p.plugin_object.is_activated, p.is_activated,
                             'plugin is activated')

            self.assertTrue(
                isinstance(p_obj, IFunctionFactoryPlugin),
                'plugin object is instance of IFunctionFactoryPlugin')

        # test extra plugin are configured according to config file
        self.assertEqual(
            plugin_manager.get_plugin_obj_by_name('exacrm',
                                                  'Function')._exacrm_cluster,
            '--secret--', 'exacrm object is configured')
    def test_load_plugins_several_categories(self):
        """
        Test is we can load and correctly locate plugins from several categories
        First it explores folders from ZMON_PLUGINS env_var, and then from additional_dirs
        """
        for test_load_from in ('env_var', 'additional_folders'):

            # reload the plugin
            reload(plugin_manager)

            # Lets create a category filter that includes our builtin plugin type and 2 types we defines for our tests
            category_filter = {
                'Function': IFunctionFactoryPlugin,
                'Color': IColorPlugin,
                'Temperature': ITemperaturePlugin,
            }

            if test_load_from == 'env_var':
                # init the plugin manager
                plugin_manager.init_plugin_manager(
                    category_filter=category_filter)

                # collect plugins builtin and explore env_var: ZMON_PLUGINS="/.../tests/plugins/simple_plugins"
                plugin_manager.collect_plugins(load_builtins=True,
                                               load_env=True,
                                               additional_dirs=None)

            elif test_load_from == 'additional_folders':
                # init the plugin manager
                plugin_manager.init_plugin_manager(
                    category_filter=category_filter)

                test_plugin_dir = simple_plugin_dir_abs_path()

                # collect plugins builtin and explore  additional_dirs: /.../tests/plugins/simple_plugins
                plugin_manager.collect_plugins(
                    load_builtins=True,
                    load_env=False,
                    additional_dirs=[test_plugin_dir])

            # check categories

            all_categories = plugin_manager.get_all_categories()
            seen_categories = plugin_manager.get_loaded_plugins_categories()

            self.assertEqual(set(all_categories), set(category_filter.keys()),
                             'All defined categories are stored')
            self.assertTrue(
                len(seen_categories) >= 2
                and set(seen_categories).issubset(set(all_categories)),
                'found at least 2 categories and they all belong to all defined categories'
            )

            # check known test plugins are loaded

            known_plugin_names = [
                'http', 'color_spain', 'color_germany', 'temperature_fridge'
            ]
            plugin_names = plugin_manager.get_all_plugin_names()

            self.assertTrue(
                set(known_plugin_names).issubset(plugin_names),
                'All known test plugins are loaded')

            # test get_plugin_obj_by_name() and get_plugin_objs_of_category()

            color_ger = plugin_manager.get_plugin_by_name(
                'color_germany', 'Color')
            color_ger_obj = plugin_manager.get_plugin_obj_by_name(
                'color_germany', 'Color')
            self.assertEqual(id(color_ger.plugin_object), id(color_ger_obj),
                             'locate plugin object works')
            self.assertEqual(color_ger.plugin_object.country, 'germany',
                             'located object field values look good')
            all_color_objs = plugin_manager.get_plugin_objs_of_category(
                'Color')
            self.assertEqual(
                id(color_ger_obj),
                id([obj for obj in all_color_objs
                    if obj.country == 'germany'][0]),
                'locate the plugin object in a convoluted way works too')

            # test that color_german plugin was configured with the main fashion sites

            conf_sites_germany = [
                'www.big_fashion_site.de', 'www.other_fashion_site.de'
            ]

            self.assertTrue(
                set(conf_sites_germany) == set(
                    color_ger_obj.main_fashion_sites), 'object is configured')

            # test that plugin objects run its logic correctly

            color_obj_de = plugin_manager.get_plugin_obj_by_name(
                'color_germany', 'Color')
            color_obj_es = plugin_manager.get_plugin_obj_by_name(
                'color_spain', 'Color')

            simple_colors_de = ['grey', 'white', 'black']
            simple_colors_es = ['brown', 'yellow', 'blue']

            col_names_de = color_obj_de.get_season_color_names()
            col_names_es = color_obj_es.get_season_color_names()

            self.assertEqual(col_names_de, simple_colors_de)
            self.assertEqual(col_names_es, simple_colors_es)

            # Test also the logic of temperature plugin object, this simulates a bit more complex logic
            # Temp readings are simulated as a normal distribution centered at -5 and 0.2 sigma (values from config)
            # we spawn the thread that periodically do temp reading, we wait some intervals and then get the avg temp
            # Finally we check that T avg is -5 +- 10 sigmas (see local config)

            temp_fridge = plugin_manager.get_plugin_obj_by_name(
                'temperature_fridge', 'Temperature')
            temp_fridge.start_update()
            time.sleep(temp_fridge.interval *
                       20)  # we wait for some temp collection to happen
            temp_fridge.stop = True
            tavg = temp_fridge.get_temperature_average()
            # This test is non-deterministic, but probability of failure is super small, so in practice it is ok
            self.assertTrue(
                abs(-5.0 - tavg) < 0.2 * 10,
                'the avg temperature is close to -5')

            # test subpackage dependencies can be resolved
            self.assertEqual(temp_fridge.engine.power_unit, 'Watts')
Exemple #20
0
        return pyjson.loads(self.get(key))

    def stats(self, extra_keys=[]):
        data = self.__con.stats()
        ret = {}
        for key in data:
            if key in COUNTER_KEYS:
                ret['{}_per_sec'.format(key.replace('total_', ''))] = \
                    round(self._counter.key(key).per_second(data.get(key, 0)), 2)
            elif key in VALUE_KEYS:
                ret[key] = data[key]
            elif key in extra_keys:
                ret[key] = data[key]
        return ret


if __name__ == '__main__':
    import sys
    import json

    # init plugin manager and collect plugins, as done by Zmon when worker is starting
    plugin_manager.init_plugin_manager()
    plugin_manager.collect_plugins(load_builtins=True, load_env=True)

    factory_ctx = {
            'host': 'localhost',
    }
    counter = plugin_manager.get_plugin_obj_by_name('counter', 'Function').create(factory_ctx)
    wrapper = MemcachedWrapper(counter, sys.argv[1])
    print json.dumps(wrapper.stats(), indent=4, sort_keys=True)
Exemple #21
0
    host = sys.argv[1]
    instance = sys.argv[2]

    factory_ctx = {
        'base_url':
        'http://{host}:3{instance}/'.format(host=host, instance=instance),
        'host':
        host,
        'port':
        ZomcatWrapper.get_jmx_port(instance),
        'instance':
        instance,
        'redis_host':
        'localhost',
    }

    # http = partial(HttpWrapper, base_url='http://{host}:3{instance}/'.format(host=host, instance=instance))
    http = plugin_manager.get_plugin_obj_by_name(
        'http', 'Function').create(factory_ctx)

    # jmx = partial(JmxWrapper, host=host, port=ZomcatWrapper.get_jmx_port(instance))
    jmx = plugin_manager.get_plugin_obj_by_name('jmx',
                                                'Function').create(factory_ctx)

    # counter = partial(CounterWrapper, redis_host='localhost')
    counter = plugin_manager.get_plugin_obj_by_name(
        'counter', 'Function').create(factory_ctx)

    zomcat = ZomcatWrapper(host, instance, http=http, jmx=jmx, counter=counter)
    print json.dumps(zomcat.health(), indent=4, sort_keys=True)