def test_schema_is_valid(self, mock_validate): rutils.load_plugins(os.path.join(self.rally_jobs_path, "plugins")) for filename in [ "rally.yaml", "rally-neutron.yaml", "rally-zaqar.yaml", "rally-designate.yaml" ]: full_path = os.path.join(self.rally_jobs_path, filename) with open(full_path) as task_file: try: args_file = os.path.join( self.rally_jobs_path, filename.rsplit(".", 1)[0] + "_args.yaml") args = {} if os.path.exists(args_file): args = yaml.safe_load(open(args_file).read()) if not isinstance(args, dict): raise TypeError( "args file %s must be dict in yaml or json " "presenatation" % args_file) task = api.Task.render_template(task_file.read(), **args) task = yaml.safe_load(task) eng = engine.BenchmarkEngine(task, mock.MagicMock()) eng.validate() except Exception: print(traceback.format_exc()) self.fail("Wrong task input file: %s" % full_path)
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.walk.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_jobs_path, "plugins")) for filename in ["rally.yaml", "rally-neutron.yaml", "rally-zaqar.yaml", "rally-designate.yaml"]: full_path = os.path.join(self.rally_jobs_path, filename) with open(full_path) as task_file: try: args_file = os.path.join(self.rally_jobs_path, filename.rsplit(".", 1)[0] + "_args.yaml") args = {} if os.path.exists(args_file): args = yaml.safe_load(open(args_file).read()) if not isinstance(args, dict): raise TypeError("args file %s must be dict in yaml or json " "presenatation" % args_file) task = api.Task.render_template(task_file.read(), **args) task = yaml.safe_load(task) eng = engine.BenchmarkEngine(task, mock.MagicMock()) eng.validate() except Exception: print(traceback.format_exc()) self.fail("Wrong task input file: %s" % full_path)
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) # NOTE(wtakase): This is for suppressing boto error logging. LOG.debug("ERROR log from boto module is hide.") boto_log = logging.getLogger("boto").logger boto_log.setLevel(logging.CRITICAL) 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 = [ encodeutils.safe_decode(arg) 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, six.string_types): v = encodeutils.safe_decode(v) fn_kwargs[k] = v # call the action with the remaining arguments # check arguments try: validate_args(fn, *fn_args, **fn_kwargs) except 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) if getattr(fn, "_suppress_warnings", False): with warnings.catch_warnings(): warnings.simplefilter("ignore") ret = fn(*fn_args, **fn_kwargs) else: ret = fn(*fn_args, **fn_kwargs) return (ret) except (IOError, TypeError, ValueError, exceptions.DeploymentNotFound, exceptions.TaskNotFound, jsonschema.ValidationError) as e: if logging.is_debug(): LOG.exception(e) print(e) return 1 except Exception: print(_("Command failed, please check log for more info")) raise
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")
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) # NOTE(wtakase): This is for suppressing boto error logging. LOG.debug("ERROR log from boto module is hide.") boto_log = logging.getLogger("boto").logger boto_log.setLevel(logging.CRITICAL) 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 = [encodeutils.safe_decode(arg) 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, six.string_types): v = encodeutils.safe_decode(v) fn_kwargs[k] = v # call the action with the remaining arguments # check arguments try: validate_args(fn, *fn_args, **fn_kwargs) except 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) if getattr(fn, "_suppress_warnings", False): with warnings.catch_warnings(): warnings.simplefilter("ignore") ret = fn(*fn_args, **fn_kwargs) else: ret = fn(*fn_args, **fn_kwargs) return(ret) except (IOError, TypeError, ValueError, exceptions.DeploymentNotFound, exceptions.TaskNotFound, jsonschema.ValidationError) as e: if logging.is_debug(): LOG.exception(e) print(e) return 1 except Exception: print(_("Command failed, please check log for more info")) raise
def test_load_plugins_from_nonexisting_file(self, mock_isfile): # test no fails for nonexisting file utils.load_plugins("/somewhere/plugin.py")
def test_load_plugins_from_file_fails(self, mock_isfile): utils.load_plugins("/somwhere/plugin.py")
def test_load_plugins_from_file_successful(self, mock_load_source, mock_isfile): utils.load_plugins("/somewhere/plugin.py") expected = [mock.call("plugin", "/somewhere/plugin.py")] self.assertEqual(expected, mock_load_source.mock_calls)