def test_load_plugins_fails(self, mock_oslistdir, mock_ospath, mock_load_module, mock_find_module): mock_oslistdir.return_value = ["somebrokenplugin.py", ] mock_load_module.side_effect = Exception() # test no fails if module is broken # TODO(olkonami): check exception is handled correct utils.load_plugins("/somwhere")
def test_load_plugins_from_nonexisting_and_empty_dir(self, mock_os): # test no fails for nonexisting directory mock_os.path.exists.return_value = False utils.load_plugins("/somewhere") # test no fails for empty directory mock_os.path.exists.return_value = True mock_os.listdir.return_value = [] utils.load_plugins("/somewhere")
def test_load_plugins_successfull(self, mock_exists, mock_oswalk, mock_find_module, mock_load_module): test_path = "/somewhere" utils.load_plugins(test_path) expected = [ mock.call("plugin1", ["/somewhere"]), mock.call("plugin2", ["/somewhere/subdir"]), mock.call("plugin3", ["/somewhere/subdir/subsubdir"]) ] self.assertEqual(mock_find_module.mock_calls, expected) self.assertEqual(len(mock_load_module.mock_calls), 3)
def test_schema_is_valid(self, mock_validate): rutils.load_plugins(os.path.join(self.rally_scenarios_path, "plugins")) for filename in ["rally.yaml", "rally-neutron.yaml"]: full_path = os.path.join(self.rally_scenarios_path, filename) with open(full_path) as task_file: try: task_config = yaml.safe_load(task_file.read()) eng = engine.BenchmarkEngine(task_config, mock.MagicMock()) eng.validate() except Exception: print(traceback.format_exc()) self.fail("Wrong scenario config %s" % full_path)
def test_load_plugins_successfull(self, mock_listdir, mock_isfile, mock_exists, mock_find_module, mock_load_module): mock_listdir.return_value = ["plugin1.py", "plugin2.py", "somethingnotpythonmodule", "somestrangedir.py"] # check we don't try to load something that is not file def isfile_side_effect(*args): return args[0] != "/somewhere/somestrangedir.py" mock_isfile.side_effect = isfile_side_effect mock_find_module.return_value = (mock.MagicMock(), None, None) test_path = "/somewhere" utils.load_plugins(test_path) expected = [mock.call("plugin1", ["/somewhere"]), mock.call("plugin2", ["/somewhere"])] self.assertEqual(mock_find_module.mock_calls, expected) self.assertEqual(len(mock_load_module.mock_calls), 2)
def test_load_plugins_successfull(self, mock_listdir, mock_isfile, mock_exists, mock_find_module, mock_load_module): mock_listdir.return_value = ["plugin1.py", "plugin2.py", "somethingnotpythonmodule", "somestrangedir.py"] # check we don't try to load something that is not file def isfile_side_effect(*args): return args[0] != "/somewhere/somestrangedir.py" mock_isfile.side_effect = isfile_side_effect mock_find_module.return_value = (mock.MagicMock(), None, None) test_path = "/somewhere" utils.load_plugins(test_path) expected = [ mock.call("plugin1", ["/somewhere"]), mock.call("plugin2", ["/somewhere"]) ] self.assertEqual(mock_find_module.mock_calls, expected) self.assertEqual(len(mock_load_module.mock_calls), 2)
def test_load_plugins_fails(self, mock_oswalk, mock_ospath, mock_load_module, mock_find_module): # test no fails if module is broken # TODO(olkonami): check exception is handled correct utils.load_plugins("/somwhere")
# Copyright 2014: Mirantis Inc. # All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. import os from rally import utils as rutils rutils.import_modules_from_package("rally.benchmark.context") rutils.import_modules_from_package("rally.benchmark.runners") rutils.import_modules_from_package("rally.benchmark.scenarios") rutils.load_plugins("/etc/rally/plugins/scenarios/") rutils.load_plugins(os.path.expanduser("~/.rally/plugins/scenarios/"))
def run(argv, categories): parser = lambda subparsers: _add_command_parsers(categories, subparsers) category_opt = cfg.SubCommandOpt('category', title='Command categories', help='Available categories', handler=parser) CONF.register_cli_opt(category_opt) try: CONF(argv[1:], project='rally', version=version.version_string()) logging.setup("rally") if not CONF.get('log_config_append'): # The below two lines are to disable noise from request module. The # standard way should be we make such lots of settings on the root # rally. However current oslo codes doesn't support such interface. # So I choose to use a 'hacking' way to avoid INFO logs from # request module where user didn't give specific log configuration. # And we could remove this hacking after oslo.log has such # interface. LOG.debug("INFO logs from urllib3 and requests module are hide.") requests_log = logging.getLogger("requests").logger requests_log.setLevel(logging.WARNING) urllib3_log = logging.getLogger("urllib3").logger urllib3_log.setLevel(logging.WARNING) except cfg.ConfigFilesNotFoundError: cfgfile = CONF.config_file[-1] if CONF.config_file else None if cfgfile and not os.access(cfgfile, os.R_OK): st = os.stat(cfgfile) print(_("Could not read %s. Re-running with sudo") % cfgfile) try: os.execvp('sudo', ['sudo', '-u', '#%s' % st.st_uid] + sys.argv) except Exception: print(_('sudo failed, continuing as if nothing happened')) print(_('Please re-run %s as root.') % argv[0]) return(2) if CONF.category.name == "version": print(version.version_string()) return(0) if CONF.category.name == "bash-completion": print(_generate_bash_completion_script()) return(0) fn = CONF.category.action_fn fn_args = [arg.decode('utf-8') for arg in CONF.category.action_args] fn_kwargs = {} for k in CONF.category.action_kwargs: v = getattr(CONF.category, 'action_kwarg_' + k) if v is None: continue if isinstance(v, basestring): v = v.decode('utf-8') fn_kwargs[k] = v # call the action with the remaining arguments # check arguments try: cliutils.validate_args(fn, *fn_args, **fn_kwargs) except cliutils.MissingArgs as e: # NOTE(mikal): this isn't the most helpful error message ever. It is # long, and tells you a lot of things you probably don't want to know # if you just got a single arg wrong. print(fn.__doc__) CONF.print_help() print("Missing arguments:") for missing in e.missing: for arg in fn.args: if arg[1].get('dest', '').endswith(missing): print(" " + arg[0][0]) break return(1) try: utils.load_plugins("/opt/rally/plugins/") utils.load_plugins(os.path.expanduser("~/.rally/plugins/")) validate_deprecated_args(argv, fn) ret = fn(*fn_args, **fn_kwargs) return(ret) except (IOError, TypeError, exceptions.DeploymentNotFound) as e: if CONF.debug: raise print(e) return 1 except Exception: print(_("Command failed, please check log for more info")) raise
def run(argv, categories): parser = lambda subparsers: _add_command_parsers(categories, subparsers) category_opt = cfg.SubCommandOpt('category', title='Command categories', help='Available categories', handler=parser) CONF.register_cli_opt(category_opt) try: CONF(argv[1:], project='rally', version=version.version_string()) logging.setup("rally") if not CONF.get('log_config_append'): # The below two lines are to disable noise from request module. The # standard way should be we make such lots of settings on the root # rally. However current oslo codes doesn't support such interface. # So I choose to use a 'hacking' way to avoid INFO logs from # request module where user didn't give specific log configuration. # And we could remove this hacking after oslo.log has such # interface. LOG.debug("INFO logs from urllib3 and requests module are hide.") requests_log = logging.getLogger("requests").logger requests_log.setLevel(logging.logging.WARNING) urllib3_log = logging.getLogger("urllib3").logger urllib3_log.setLevel(logging.logging.WARNING) except cfg.ConfigFilesNotFoundError: cfgfile = CONF.config_file[-1] if CONF.config_file else None if cfgfile and not os.access(cfgfile, os.R_OK): st = os.stat(cfgfile) print(_("Could not read %s. Re-running with sudo") % cfgfile) try: os.execvp('sudo', ['sudo', '-u', '#%s' % st.st_uid] + sys.argv) except Exception: print(_('sudo failed, continuing as if nothing happened')) print(_('Please re-run %s as root.') % argv[0]) return(2) if CONF.category.name == "version": print(version.version_string()) return(0) if CONF.category.name == "bash-completion": print(_generate_bash_completion_script()) return(0) fn = CONF.category.action_fn fn_args = [arg.decode('utf-8') for arg in CONF.category.action_args] fn_kwargs = {} for k in CONF.category.action_kwargs: v = getattr(CONF.category, 'action_kwarg_' + k) if v is None: continue if isinstance(v, basestring): v = v.decode('utf-8') fn_kwargs[k] = v # call the action with the remaining arguments # check arguments try: cliutils.validate_args(fn, *fn_args, **fn_kwargs) except exceptions.MissingArgs as e: # NOTE(mikal): this isn't the most helpful error message ever. It is # long, and tells you a lot of things you probably don't want to know # if you just got a single arg wrong. print(fn.__doc__) CONF.print_help() print("Missing arguments:") for missing in e.missing: for arg in fn.args: if arg[1].get('dest', '').endswith(missing): print(" " + arg[0][0]) break return(1) try: utils.load_plugins("/opt/rally/plugins/") utils.load_plugins(os.path.expanduser("~/.rally/plugins/")) ret = fn(*fn_args, **fn_kwargs) return(ret) except IOError as e: if CONF.debug: raise print(e) return 1 except Exception: print(_("Command failed, please check log for more info")) raise