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 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_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 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')
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')