def test_no_plugin_to_load(self): actions = copy.deepcopy(self.conf_actions) with pytest.raises(smoker_exceptions.NoRunningPlugins) as exc_info: server_plugins.PluginManager(plugins=dict(), templates=self.conf_templates, actions=actions) assert 'No plugins loaded!' in repr(exc_info.value)
def test_load_plugins_with_blank_actions(self): actions = { 'GetFQDN': dict() } conf = dict(copy.deepcopy(self.config), **{'actions': actions}) pluginmgr = server_plugins.PluginManager(**conf) assert not pluginmgr.plugins['Hostname'].params['Action']
def test_add_process_using_filter(self): pluginmgr = server_plugins.PluginManager(**copy.deepcopy(self.config)) filter_ = {'Category': 'system'} expected_plugins = pluginmgr.get_plugins(filter=filter_) process_id = pluginmgr.add_process(filter=filter_) added_process = pluginmgr.get_process_list()[process_id]['plugins'] assert expected_plugins == added_process
def test_load_plugins_with_template_is_true(self): # Plugin has template and template is in conf_template pluginmgr = server_plugins.PluginManager(**copy.deepcopy(self.config)) for plugin in self.conf_plugins_to_load: if 'Template' in self.conf_plugins_to_load[plugin]: assert plugin in pluginmgr.get_plugins() assert 'Template' in pluginmgr.get_plugin(plugin).params templates = pluginmgr.get_plugin(plugin).params['Template'] assert 'SystemPlugin' in templates
def test_load_plugins_with_actions(self): expected = { 'Command': 'hostname -f', 'Module': None, 'Timeout': 60 } pluginmgr = server_plugins.PluginManager(**copy.deepcopy(self.config)) action = pluginmgr.plugins['Hostname'].params['Action'] assert action == expected
def test_join_timed_plugin_workers(self): pluginmgr = server_plugins.PluginManager(**copy.deepcopy(self.config)) plugin = pluginmgr.get_plugin('Hostname') time.sleep(plugin.params['Interval'] + 0.5) plugin.run() time.sleep(0.5) assert plugin.current_run pluginmgr.join_timed_plugin_workers() assert not plugin.current_run
def test_add_process_using_plugin_list(self): pluginmgr = server_plugins.PluginManager(**copy.deepcopy(self.config)) plugin_list = ['Uname', 'CpuLoad'] expected_plugins = list() for plugin in plugin_list: expected_plugins.append(pluginmgr.get_plugin(plugin)) process_id = pluginmgr.add_process(plugins=plugin_list) added_process = pluginmgr.get_process_list()[process_id]['plugins'] assert expected_plugins == added_process
def test_join_non_timed_plugin_workers(self): # Schedule and run plugin (Plugin require 'Interval' to run) pluginmgr = server_plugins.PluginManager(**copy.deepcopy(self.config)) plugin = pluginmgr.get_plugin('Hostname') time.sleep(plugin.params['Interval'] + 0.5) plugin.run() time.sleep(0.5) assert plugin.current_run plugin.params['Interval'] = 0 pluginmgr.join_timed_plugin_workers() assert plugin.current_run
def test_load_plugins_without_baseplugin_template_will_get_error(self): templates = { 'SmokerPlugin': { 'Interval': 5, 'gid': 'default', 'uid': 'default', 'Timeout': 30, 'History': 10} } actions = copy.deepcopy(self.conf_actions) with pytest.raises(smoker_exceptions.BasePluginTemplateNotFound): server_plugins.PluginManager(plugins=self.conf_plugins, templates=templates, actions=actions)
def test_get_plugin_history(self): config = copy.deepcopy(self.config) smokerd = Smokerd(config=self.conf_dir + '/smokerd.yaml') smokerd.pluginmgr = server_plugins.PluginManager(**config) restserver.smokerd = smokerd plugin = smokerd.pluginmgr.get_plugin('Uname') for i in range(4): plugin.forced = True plugin.run() time.sleep(0.5) plugin.collect_new_result() plugin_history = restserver.get_plugin_history('Uname') assert len(plugin_history) == 4
def test_add_process_using_plugin_list_and_filter(self): pluginmgr = server_plugins.PluginManager(**copy.deepcopy(self.config)) plugin_list = ['Uname', 'CpuLoad'] filter_ = {'Category': 'system'} # Plugins from list will be added to process first, # then plugins from filter expected_plugins = list() for plugin in plugin_list: expected_plugins.append(pluginmgr.get_plugin(plugin)) expected_plugins += pluginmgr.get_plugins(filter=filter_) process_id = pluginmgr.add_process(plugins=plugin_list, filter=filter_) added_process = pluginmgr.get_process_list()[process_id]['plugins'] assert expected_plugins == added_process
def test_load_plugins_with_template_is_false(self): # Plugin has template and template is in conf_template templates = { 'BasePlugin': { 'Interval': 5, 'gid': 'default', 'uid': 'default', 'Timeout': 30, 'History': 10} } conf = dict(copy.deepcopy(self.config), **{'templates': templates}) pluginmgr = server_plugins.PluginManager(**conf) for plugin in self.conf_plugins_to_load: if 'Template' in self.conf_plugins_to_load[plugin]: assert plugin not in pluginmgr.get_plugins()
def test_reset_smokerd_instance(self): # To prevent data changed from test_get_plugin_history in smokerd config = copy.deepcopy(self.config) self.smokerd = Smokerd(config=self.conf_dir + '/smokerd.yaml') self.smokerd.pluginmgr = server_plugins.PluginManager(**config) restserver.smokerd = self.smokerd
class TestRestAPI(object): """Unit tests for the restserver functions""" component_result = { 'componentResults': { 'Unit tests': { 'status': 'OK', 'messages': { 'info': ['GoodData'], 'warn': [], 'error': [] } } } } conf_plugins = { 'Uptime ': { 'Category': 'monitoring', 'Command': 'uptime' }, 'Uname': { 'Category': 'system', 'Module': 'smoker.server.plugins.uname' }, 'Hostname': { 'Category': 'system', 'Command': 'hostname' } } conf_templates = { 'BasePlugin': { 'Interval': 1, 'gid': 'default', 'uid': 'default', 'Timeout': 30, 'History': 10 } } config = { 'plugins': conf_plugins, 'templates': conf_templates, 'actions': dict() } conf_dir = (os.path.dirname(os.path.realpath(__file__)) + '/smoker_test_resources/smokerd') smokerd = Smokerd(config=conf_dir + '/smokerd.yaml') # Map smokerd.pluginmgr to PluginManager instance # because we don't 'run' smokerd instance (work arround) smokerd.pluginmgr = server_plugins.PluginManager(**config) restserver.smokerd = smokerd def test_next_run_iso_format_boolean_input(self): assert restserver.next_run_iso_format(True) is None def test_next_run_iso_format_time_input(self): sample = datetime.datetime.now() next_run = restserver.next_run_iso_format(sample) assert next_run assert next_run == sample.isoformat() assert isinstance(next_run, str) def test_standardized_api_list_null_input(self): assert not restserver.standardized_api_list(None) def test_standardized_api_list_invalid_type_input(self): component_result = ['component', 'InvalidInput'] result = restserver.standardized_api_list(component_result) assert result assert result == component_result assert isinstance(result, list) def test_standardized_api_list_missing_componentResults_keyword(self): component_result = {'InvalidKeyword': 'Smoker'} result = restserver.standardized_api_list(component_result) assert result assert result == component_result assert isinstance(result, dict) def test_standardized_api_list_valid_input(self): expected = { 'componentResults': [{ 'componentResult': { 'status': 'OK', 'messages': { 'info': ['GoodData'], 'warn': [], 'error': [] }, 'name': 'Unit tests' } }] } result = restserver.standardized_api_list(self.component_result) assert result assert result == expected assert isinstance(result, dict) def test_print_plugin(self): plugin_result = restserver.print_plugin('Uname') assert plugin_result['plugin'] assert plugin_result['plugin']['name'] == 'Uname' assert plugin_result['plugin']['links']['self'] == '/plugins/Uname' assert plugin_result['plugin']['parameters'] def test_print_plugin_with_invalid_plugin_name(self): with pytest.raises(smoker_exceptions.NoSuchPlugin) as exc_info: restserver.print_plugin('InvalidPluginName') assert 'Plugin InvalidPluginName not found' in repr(exc_info.value) def test_forced_print_plugin_without_forced_result(self): with pytest.raises(smoker_exceptions.InProgress): restserver.print_plugin('Uname', forced=True) def test_forced_print_plugin_with_forced_result(self): plugin = self.smokerd.pluginmgr.get_plugin('Uname') plugin.forced = True plugin.run() time.sleep(0.5) plugin_result = restserver.print_plugin('Uname', forced=True) assert plugin_result['plugin'] assert plugin_result['plugin']['name'] == 'Uname' assert plugin_result['plugin']['links']['self'] == '/plugins/Uname' assert plugin_result['plugin']['parameters'] assert plugin_result['plugin']['forcedResult']['status'] == 'OK' assert plugin_result['plugin']['forcedResult']['forced'] is True assert plugin_result['plugin']['forcedResult']['messages']['info'] def test_print_plugins(self): plugins_to_print = list(self.conf_plugins.keys()) plugins_result = restserver.print_plugins(plugins_to_print) assert plugins_result['plugins'] assert len(plugins_result['plugins']['items']) == len(plugins_to_print) for plugin_name in plugins_to_print: index = plugins_to_print.index(plugin_name) plugin_result = plugins_result['plugins']['items'][index]['plugin'] assert plugin_result['name'] == plugin_name assert plugin_result['links']['self'] == '/plugins/' + plugin_name assert plugin_result['parameters'] def test_forced_print_plugins_with_forced_result(self): plugins_to_print = list(self.conf_plugins.keys()) for plugin in self.smokerd.pluginmgr.get_plugins().values(): plugin.forced = True plugin.run() time.sleep(0.5) plugins_result = restserver.print_plugins(plugins_to_print, forced=True) assert plugins_result['plugins'] assert len(plugins_result['plugins']['items']) == len(plugins_to_print) for plugin_name in plugins_to_print: index = plugins_to_print.index(plugin_name) plugin_result = plugins_result['plugins']['items'][index]['plugin'] assert plugin_result['name'] == plugin_name assert plugin_result['links']['self'] == '/plugins/' + plugin_name assert plugin_result['parameters'] assert plugin_result['forcedResult']['status'] == 'OK' assert plugin_result['forcedResult']['forced'] is True assert plugin_result['forcedResult']['messages']['info'] def test_get_plugin_history(self): config = copy.deepcopy(self.config) smokerd = Smokerd(config=self.conf_dir + '/smokerd.yaml') smokerd.pluginmgr = server_plugins.PluginManager(**config) restserver.smokerd = smokerd plugin = smokerd.pluginmgr.get_plugin('Uname') for i in range(4): plugin.forced = True plugin.run() time.sleep(0.5) plugin.collect_new_result() plugin_history = restserver.get_plugin_history('Uname') assert len(plugin_history) == 4 def test_reset_smokerd_instance(self): # To prevent data changed from test_get_plugin_history in smokerd config = copy.deepcopy(self.config) self.smokerd = Smokerd(config=self.conf_dir + '/smokerd.yaml') self.smokerd.pluginmgr = server_plugins.PluginManager(**config) restserver.smokerd = self.smokerd
def test_load_plugins_without_any_template(self): actions = copy.deepcopy(self.conf_actions) with pytest.raises(smoker_exceptions.BasePluginTemplateNotFound): server_plugins.PluginManager(plugins=self.conf_plugins, templates=dict(), actions=actions)
class TestPluginManager(object): """Unit tests for the PluginManager class""" conf_plugins_to_load = { 'CpuLoad': { 'Category': 'monitoring', 'Enabled': True, 'Command': 'top', 'Template': 'SystemPlugin' }, 'Uname': { 'Category': 'system', 'Interval': 5, 'Module': 'smoker.server.plugins.uname' }, 'Hostname': { 'Category': 'system', 'Command': 'hostname', 'Action': 'GetFQDN' } } conf_plugins_with_enabled_is_false = { 'Memory': { 'Category': 'system', 'Enabled': False, 'Interval': 15, 'Command': 'echo "$(top -l 1 | awk \'/PhysMem/\';)"', 'Timeout': 5 } } conf_plugins_with_template_is_false = { 'Uptime': { 'Category': 'system', 'Enabled': True, 'Interval': 5, 'Command': 'uptime', 'Timeout': 5, 'Template': 'InvalidTemplate' } } conf_plugins = dict( list(conf_plugins_to_load.items()) + list(conf_plugins_with_enabled_is_false.items()) + list(conf_plugins_with_template_is_false.items())) conf_templates = { 'BasePlugin': { 'Interval': 2, 'gid': 'default', 'uid': 'default', 'Timeout': 30, 'History': 10 }, 'SystemPlugin': { 'Interval': 3, 'Timeout': 5 } } conf_actions = {'GetFQDN': {'Command': 'hostname -f'}} config = { 'plugins': conf_plugins, 'templates': conf_templates, 'actions': conf_actions } # Due to 'load_plugin' will override 'conf_actions', please deepcopy # 'conf_actions' or whole 'config' before using. # It'll be fixed if any issue affect smoker itself pluginmgr = server_plugins.PluginManager(**copy.deepcopy(config)) loaded_plugins = pluginmgr.get_plugins() def test_disabled_plugins_should_not_be_loaded(self): plugins = iter(self.conf_plugins_with_template_is_false.items()) for plugin, options in plugins: if 'Enabled' in options and not options['Enabled']: assert plugin not in self.loaded_plugins.keys() def test_enabled_plugins_should_be_loaded(self): for plugin, options in self.conf_plugins_to_load.items(): if 'Enabled' in options and options['Enabled']: assert plugin in self.loaded_plugins.keys() def test_plugins_without_enabled_option_should_be_loaded(self): for plugin, options in self.conf_plugins_to_load.items(): if 'Enabled' not in options: assert plugin in self.loaded_plugins.keys() def test_plugins_are_rightly_loaded(self): assert len(self.loaded_plugins) == len(self.conf_plugins_to_load) def test_plugins_not_in_config_should_not_be_loaded(self): for plugin in self.loaded_plugins: assert plugin in self.conf_plugins def test_load_plugins_without_baseplugin_template_will_get_error(self): templates = { 'SmokerPlugin': { 'Interval': 5, 'gid': 'default', 'uid': 'default', 'Timeout': 30, 'History': 10 } } actions = copy.deepcopy(self.conf_actions) with pytest.raises(smoker_exceptions.BasePluginTemplateNotFound): server_plugins.PluginManager(plugins=self.conf_plugins, templates=templates, actions=actions) def test_no_plugin_to_load(self): actions = copy.deepcopy(self.conf_actions) with pytest.raises(smoker_exceptions.NoRunningPlugins) as exc_info: server_plugins.PluginManager(plugins=dict(), templates=self.conf_templates, actions=actions) assert 'No plugins loaded!' in repr(exc_info.value) def test_load_plugins_without_any_template(self): actions = copy.deepcopy(self.conf_actions) with pytest.raises(smoker_exceptions.BasePluginTemplateNotFound): server_plugins.PluginManager(plugins=self.conf_plugins, templates=dict(), actions=actions) def test_load_plugins_with_template_is_true(self): # Plugin has template and template is in conf_template pluginmgr = server_plugins.PluginManager(**copy.deepcopy(self.config)) for plugin in self.conf_plugins_to_load: if 'Template' in self.conf_plugins_to_load[plugin]: assert plugin in pluginmgr.get_plugins() assert 'Template' in pluginmgr.get_plugin(plugin).params templates = pluginmgr.get_plugin(plugin).params['Template'] assert 'SystemPlugin' in templates def test_load_plugins_with_template_is_false(self): # Plugin has template and template is in conf_template templates = { 'BasePlugin': { 'Interval': 5, 'gid': 'default', 'uid': 'default', 'Timeout': 30, 'History': 10 } } conf = dict(copy.deepcopy(self.config), **{'templates': templates}) pluginmgr = server_plugins.PluginManager(**conf) for plugin in self.conf_plugins_to_load: if 'Template' in self.conf_plugins_to_load[plugin]: assert plugin not in pluginmgr.get_plugins() def test_load_plugins_with_actions(self): expected = {'Command': 'hostname -f', 'Module': None, 'Timeout': 60} pluginmgr = server_plugins.PluginManager(**copy.deepcopy(self.config)) action = pluginmgr.plugins['Hostname'].params['Action'] assert action == expected def test_load_plugins_with_blank_actions(self): actions = {'GetFQDN': dict()} conf = dict(copy.deepcopy(self.config), **{'actions': actions}) pluginmgr = server_plugins.PluginManager(**conf) assert not pluginmgr.plugins['Hostname'].params['Action'] def test_plugins_without_params_will_use_params_from_base_plugin(self): expected_interval = self.conf_templates['BasePlugin']['Interval'] for plugin_name, plugin in self.conf_plugins_to_load.items(): if not plugin.get('Interval') and not plugin.get('Template'): assert (self.loaded_plugins[plugin_name].params['Interval'] == expected_interval) def test_get_plugins_with_filter(self): filter_ = {'Category': 'system'} expected_plugins = ['Uname', 'Hostname'] load_plugins_with_filter = self.pluginmgr.get_plugins(filter=filter_) assert len(expected_plugins) == len(load_plugins_with_filter) for plugin in load_plugins_with_filter: assert plugin.name in expected_plugins def test_get_plugins_with_invalid_name(self): with pytest.raises(smoker_exceptions.NoSuchPlugin) as exc_info: self.pluginmgr.get_plugin('InvalidPlugin') assert 'Plugin InvalidPlugin not found' in repr(exc_info.value) def test_get_non_exist_template(self): with pytest.raises(smoker_exceptions.TemplateNotFound) as exc_info: self.pluginmgr.get_template('InvalidTemplate') expected = 'Can\'t find configured template InvalidTemplate' assert expected in repr(exc_info.value) def test_get_template(self): assert (self.pluginmgr.get_template('BasePlugin') == self.conf_templates['BasePlugin']) def test_add_process_using_plugin_list(self): pluginmgr = server_plugins.PluginManager(**copy.deepcopy(self.config)) plugin_list = ['Uname', 'CpuLoad'] expected_plugins = list() for plugin in plugin_list: expected_plugins.append(pluginmgr.get_plugin(plugin)) process_id = pluginmgr.add_process(plugins=plugin_list) added_process = pluginmgr.get_process_list()[process_id]['plugins'] assert expected_plugins == added_process def test_add_process_using_filter(self): pluginmgr = server_plugins.PluginManager(**copy.deepcopy(self.config)) filter_ = {'Category': 'system'} expected_plugins = pluginmgr.get_plugins(filter=filter_) process_id = pluginmgr.add_process(filter=filter_) added_process = pluginmgr.get_process_list()[process_id]['plugins'] assert expected_plugins == added_process def test_add_process_using_plugin_list_and_filter(self): pluginmgr = server_plugins.PluginManager(**copy.deepcopy(self.config)) plugin_list = ['Uname', 'CpuLoad'] filter_ = {'Category': 'system'} # Plugins from list will be added to process first, # then plugins from filter expected_plugins = list() for plugin in plugin_list: expected_plugins.append(pluginmgr.get_plugin(plugin)) expected_plugins += pluginmgr.get_plugins(filter=filter_) process_id = pluginmgr.add_process(plugins=plugin_list, filter=filter_) added_process = pluginmgr.get_process_list()[process_id]['plugins'] assert expected_plugins == added_process def test_add_process_without_any_plugin(self): conf = copy.deepcopy(self.config) with pytest.raises(smoker_exceptions.NoPluginsFound): server_plugins.PluginManager(**conf).add_process() def test_add_process_with_invalid_plugin_name(self): conf = copy.deepcopy(self.config) with pytest.raises(smoker_exceptions.NoSuchPlugin) as exc_info: pluginmgr = server_plugins.PluginManager(**conf) pluginmgr.add_process(plugins=['InvalidPlugin']) assert 'Plugin InvalidPlugin not found' in repr(exc_info.value) def test_get_process_with_invalid_process_id(self): with pytest.raises(IndexError): self.pluginmgr.get_process(9999) # Should equal None assert not self.pluginmgr.get_process(0) def test_run_plugins_with_interval(self): pluginmgr = server_plugins.PluginManager(**copy.deepcopy(self.config)) time.sleep(5.5) pluginmgr.run_plugins_with_interval() for plugin in pluginmgr.plugins.values(): assert plugin.current_run def test_get_action(self): assert (self.pluginmgr.get_action('GetFQDN') == self.conf_actions['GetFQDN']) def test_get_non_exist_action(self): with pytest.raises(smoker_exceptions.ActionNotFound) as exc_info: self.pluginmgr.get_action('InvalidAction') expected = 'Can\'t find configured action InvalidAction' assert expected in repr(exc_info.value) def test_join_timed_plugin_workers(self): pluginmgr = server_plugins.PluginManager(**copy.deepcopy(self.config)) plugin = pluginmgr.get_plugin('Hostname') time.sleep(plugin.params['Interval'] + 0.5) plugin.run() time.sleep(0.5) assert plugin.current_run pluginmgr.join_timed_plugin_workers() assert not plugin.current_run def test_join_non_timed_plugin_workers(self): # Schedule and run plugin (Plugin require 'Interval' to run) pluginmgr = server_plugins.PluginManager(**copy.deepcopy(self.config)) plugin = pluginmgr.get_plugin('Hostname') time.sleep(plugin.params['Interval'] + 0.5) plugin.run() time.sleep(0.5) assert plugin.current_run plugin.params['Interval'] = 0 pluginmgr.join_timed_plugin_workers() assert plugin.current_run
def test_run_plugins_with_interval(self): pluginmgr = server_plugins.PluginManager(**copy.deepcopy(self.config)) time.sleep(5.5) pluginmgr.run_plugins_with_interval() for plugin in pluginmgr.plugins.values(): assert plugin.current_run
def test_add_process_with_invalid_plugin_name(self): conf = copy.deepcopy(self.config) with pytest.raises(smoker_exceptions.NoSuchPlugin) as exc_info: pluginmgr = server_plugins.PluginManager(**conf) pluginmgr.add_process(plugins=['InvalidPlugin']) assert 'Plugin InvalidPlugin not found' in repr(exc_info.value)
def test_add_process_without_any_plugin(self): conf = copy.deepcopy(self.config) with pytest.raises(smoker_exceptions.NoPluginsFound): server_plugins.PluginManager(**conf).add_process()