def get_runner(task, endpoint, config): """Returns instance of a scenario runner for execution type.""" execution_type = config.get('execution', 'continuous') for runner in rutils.itersubclasses(ScenarioRunner): if execution_type == runner.__execution_type__: new_runner = runner(task, endpoint) return new_runner
def find_resource_managers(names=None, admin_required=None): """Returns resource managers. :param names: List of names in format <service> or <service>.<resource> that is used for filtering resource manager classes :param admin_required: None -> returns all ResourceManagers True -> returns only admin ResourceManagers False -> returns only non admin ResourceManagers """ names = set(names or []) resource_managers = [] for manager in rutils.itersubclasses(base.ResourceManager): if admin_required is not None: if admin_required != manager._admin_required: continue if (manager._service in names or "%s.%s" % (manager._service, manager._resource) in names): resource_managers.append(manager) resource_managers.sort(key=lambda x: x._order) found_names = set() for mgr in resource_managers: found_names.add(mgr._service) found_names.add("%s.%s" % (mgr._service, mgr._resource)) missing = names - found_names if missing: LOG.warning("Missing resource managers: %s" % ", ".join(missing)) return resource_managers
def get_provider(config, deployment): """Returns instance of vm provider by name.""" name = config['type'] for provider in utils.itersubclasses(ProviderFactory): if name == provider.__name__: return provider(deployment, config) raise exceptions.NoSuchVMProvider(vm_provider_name=name)
def find_benchmark_scenario(query): """Find a scenario method by query. :param query: string with the name of the benchmark scenario being searched. It can be either a full name (e.g, 'NovaServers.boot_server') or just a method name (e.g., 'boot_server') :returns: method object or None if the query doesn't match any scenario method. """ if "." in query: scenario_group, scenario_name = query.split(".", 1) else: scenario_group = None scenario_name = query if scenario_group: scenario_cls = find_benchmark_scenario_group(scenario_group) if hasattr(scenario_cls, scenario_name): return getattr(scenario_cls, scenario_name) else: return None else: for scenario_cls in utils.itersubclasses(scenarios_base.Scenario): if scenario_name in dir(scenario_cls): return getattr(scenario_cls, scenario_name) return None
def _get_descriptions(self, base_cls): descriptions = [] for entity in utils.itersubclasses(base_cls): name = entity.__name__ doc = utils.parse_docstring(entity.__doc__) description = doc["short_description"] or "" descriptions.append((name, description)) return descriptions
def get_by_name(name): """Returns Scenario class by name.""" # TODO(msdubov): support approximate string matching # (here and in other base classes). for scenario in utils.itersubclasses(Scenario): if name == scenario.__name__: return scenario raise exceptions.NoSuchScenario(name=name)
def _find_substitution(self, query): max_distance = min(3, len(query) / 4) scenarios = scenario_base.Scenario.list_benchmark_scenarios() scenario_groups = list(set(s.split(".")[0] for s in scenarios)) scenario_methods = list(set(s.split(".")[1] for s in scenarios)) deploy_engines = [cls.__name__ for cls in utils.itersubclasses( deploy.EngineFactory)] server_providers = [cls.__name__ for cls in utils.itersubclasses( serverprovider.ProviderFactory)] candidates = (scenarios + scenario_groups + scenario_methods + deploy_engines + server_providers) suggestions = [] # NOTE(msdubov): Incorrect query may either have typos or be truncated. for candidate in candidates: if ((utils.distance(query, candidate) <= max_distance or candidate.startswith(query))): suggestions.append(candidate) return suggestions
def validate(config): properties = dict([(c.OPTION_NAME, c.CONFIG_SCHEMA) for c in utils.itersubclasses(SLA)]) schema = { "type": "object", "properties": properties, "additionalProperties": False, } jsonschema.validate(config, schema)
def get_provider(config, task): """Returns instance of vm provider by name.""" name = config['name'] for provider in utils.itersubclasses(ProviderFactory): if name == provider.__name__: provider = provider(config) provider.task = task return provider raise exceptions.NoSuchVMProvider(vm_provider_name=name)
def get_engine(name, deployment): """Returns instance of a deploy engine with corresponding name.""" for engine in utils.itersubclasses(EngineFactory): if name == engine.__name__: new_engine = engine(deployment) return new_engine LOG.error(_('Deployment %(uuid)s: Deploy engine for %(name)s ' 'does not exist.') % {'uuid': deployment['uuid'], 'name': name}) deployment.update_status(consts.DeployStatus.DEPLOY_FAILED) raise exceptions.NoSuchEngine(engine_name=name)
def _find_substitution(self, query): max_distance = min(3, len(query) / 4) scenarios = scenario_base.Scenario.list_benchmark_scenarios() scenario_groups = list(set(s.split(".")[0] for s in scenarios)) scenario_methods = list(set(s.split(".")[1] for s in scenarios)) deploy_engines = [ cls.__name__ for cls in utils.itersubclasses(deploy.EngineFactory) ] server_providers = [ cls.__name__ for cls in utils.itersubclasses(serverprovider.ProviderFactory) ] candidates = (scenarios + scenario_groups + scenario_methods + deploy_engines + server_providers) suggestions = [] # NOTE(msdubov): Incorrect query may either have typos or be truncated. for candidate in candidates: if ((utils.distance(query, candidate) <= max_distance or candidate.startswith(query))): suggestions.append(candidate) return suggestions
def _get_descriptions(self, base_cls, subclass_filter=None): descriptions = [] subclasses = utils.itersubclasses(base_cls) if subclass_filter: subclasses = filter(subclass_filter, subclasses) for entity in subclasses: name = entity.__name__ doc = utils.parse_docstring(entity.__doc__) description = doc["short_description"] or "" descriptions.append((name, description)) descriptions.sort(key=lambda d: d[0]) return descriptions
def test_itersubclasses(self): class A(object): pass class B(A): pass class C(A): pass class D(C): pass self.assertEqual([B, C, D], list(utils.itersubclasses(A)))
def list_benchmark_scenarios(): """Lists all the existing methods in the benchmark scenario classes. Returns the method names in format <Class name>.<Method name>, which is used in the test config. :returns: List of strings """ utils.import_modules_from_package("rally.benchmark.scenarios") benchmark_scenarios = [ ["%s.%s" % (scenario.__name__, method) for method in dir(scenario) if not method.startswith("_")] for scenario in utils.itersubclasses(Scenario) ] benchmark_scenarios_flattened = list(itertools.chain.from_iterable(benchmark_scenarios)) return benchmark_scenarios_flattened
def list_benchmark_scenarios(): """Lists all the existing methods in the benchmark scenario classes. Returns the method names in format <Class name>.<Method name>, which is used in the test config. :returns: List of strings """ benchmark_scenarios = [ ["%s.%s" % (scenario.__name__, func) for func in dir(scenario) if Scenario.is_scenario(scenario, func)] for scenario in utils.itersubclasses(Scenario) ] benchmark_scenarios_flattened = list(itertools.chain.from_iterable( benchmark_scenarios)) return benchmark_scenarios_flattened
def list_benchmark_scenarios(scenario_cls): """List all scenarios in the benchmark scenario class & its subclasses. Returns the method names in format <Class name>.<Method name>, which is used in the test config. :param scenario_cls: the base class for searching scenarios in :returns: List of strings """ scenario_classes = (list(utils.itersubclasses(scenario_cls)) + [scenario_cls]) benchmark_scenarios = [[ "%s.%s" % (scenario.__name__, func) for func in dir(scenario) if Scenario.is_scenario(scenario, func) ] for scenario in scenario_classes] benchmark_scenarios_flattened = list( itertools.chain.from_iterable(benchmark_scenarios)) return benchmark_scenarios_flattened
def get_scenario_by_name(name): """Return benchmark scenario method by name. :param name: name of the benchmark scenario being searched for (either a full name (e.g, 'NovaServers.boot_server') or just a method name (e.g., 'boot_server') :returns: function object """ if "." in name: scenario_group, scenario_name = name.split(".", 1) scenario_cls = Scenario.get_by_name(scenario_group) if Scenario.is_scenario(scenario_cls, scenario_name): return getattr(scenario_cls, scenario_name) else: for scenario_cls in utils.itersubclasses(Scenario): if Scenario.is_scenario(scenario_cls, name): return getattr(scenario_cls, name) raise exceptions.NoSuchScenario(name=name)
def check_all(config, result): """Check all SLA criteria. :param config: sla related config for a task :param result: Result of a task :returns: A list of sla results """ results = [] opt_name_map = dict([(c.OPTION_NAME, c) for c in utils.itersubclasses(SLA)]) for name, criterion in config.get("sla", {}).iteritems(): check_result = opt_name_map[name].check(criterion, result) results.append({'criterion': name, 'success': check_result.success, 'detail': check_result.msg}) return results
def list_benchmark_scenarios(): """Lists all the existing methods in the benchmark scenario classes. Returns the method names in format <Class name>.<Method name>, which is used in the test config. :returns: List of strings """ benchmark_scenarios = [[ "%s.%s" % (scenario.__name__, method) for method in dir(scenario) if Scenario.meta(scenario, method_name=method, attr_name="is_scenario", default=False) ] for scenario in utils.itersubclasses(Scenario)] benchmark_scenarios_flattened = list( itertools.chain.from_iterable(benchmark_scenarios)) return benchmark_scenarios_flattened
def check_all(task): """Check all SLA criteria. :param task: Task object :returns: Generator """ opt_name_map = dict([(c.OPTION_NAME, c) for c in utils.itersubclasses(SLA)]) for result in task.results: config = result['key']['kw'].get('sla', None) if config: for name, criterion in config.iteritems(): success = opt_name_map[name].check(criterion, result) yield {'benchmark': result['key']['name'], 'pos': result['key']['pos'], 'criterion': name, 'success': success}
def list_benchmark_scenarios(scenario_cls): """List all scenarios in the benchmark scenario class & its subclasses. Returns the method names in format <Class name>.<Method name>, which is used in the test config. :param scenario_cls: the base class for searching scenarios in :returns: List of strings """ scenario_classes = (list(utils.itersubclasses(scenario_cls)) + [scenario_cls]) benchmark_scenarios = [ ["%s.%s" % (scenario.__name__, func) for func in dir(scenario) if Scenario.is_scenario(scenario, func)] for scenario in scenario_classes ] benchmark_scenarios_flattened = list(itertools.chain.from_iterable( benchmark_scenarios)) return benchmark_scenarios_flattened
def list_resource_names(admin_required=None): """List all resource managers names. Returns all service names and all combination of service.resource names. :param admin_required: None -> returns all ResourceManagers True -> returns only admin ResourceManagers False -> returns only non admin ResourceManagers """ res_mgrs = rutils.itersubclasses(base.ResourceManager) if admin_required is not None: res_mgrs = filter(lambda cls: cls._admin_required == admin_required, res_mgrs) names = set() for cls in res_mgrs: names.add(cls._service) names.add("%s.%s" % (cls._service, cls._resource)) return names
def check_all(task): """Check all SLA criteria. :param task: Task object :returns: Generator """ opt_name_map = dict([(c.OPTION_NAME, c) for c in utils.itersubclasses(SLA)]) for result in task.results: config = result['key']['kw'].get('sla', None) if config: for name, criterion in config.iteritems(): check_result = opt_name_map[name].check(criterion, result) yield {'benchmark': result['key']['name'], 'pos': result['key']['pos'], 'criterion': name, 'success': check_result.success, 'detail': check_result.msg}
def test_res_manager_special_field(self): for res_mgr in utils.itersubclasses(base.ResourceManager): manager_name = "%s.%s" % (res_mgr.__module__, res_mgr.__name__) fields = filter(lambda x: not x.startswith("__"), dir(res_mgr)) available_opts = set([ "_admin_required", "_perform_for_admin_only", "_tenant_resource", "_service", "_resource", "_order", "_max_attempts", "_timeout", "_interval", "_threads", "_manager", "id", "is_deleted", "delete", "list" ]) extra_opts = set(fields) - available_opts self.assertFalse( extra_opts, ("ResourceManager %(name)s contains extra fields: %(opts)s." " Remove them to pass this test") % {"name": manager_name, "opts": ", ".join(extra_opts)})
def get_by_name(name): """Returns Scenario class by name.""" for scenario in utils.itersubclasses(Scenario): if name == scenario.__name__: return scenario raise exceptions.NoSuchScenario(name=name)
def get_engine(name, config): """Returns instance of deploy engine with corresponding name.""" for engine in utils.itersubclasses(EngineFactory): if name == engine.__name__: return engine(config) raise exceptions.NoSuchEngine(engine_name=name)
def get_available_providers(): """Returns list of names of available engines.""" return [e.__name__ for e in utils.itersubclasses(ProviderFactory)]
def get_by_name(name): """Returns SLA by name or config option name.""" for sla in utils.itersubclasses(SLA): if name == sla.__name__ or name == sla.OPTION_NAME: return sla raise exceptions.NoSuchSLA(name=name)
def get_by_name(name): """Return Engine class by name.""" for engine in utils.itersubclasses(EngineFactory): if name == engine.__name__: return engine raise exceptions.NoSuchEngine(engine_name=name)
def get_by_name(name): """Return Server Provider class by type.""" for provider in utils.itersubclasses(ProviderFactory): if name == provider.__name__: return provider raise exceptions.NoSuchVMProvider(vm_provider_name=name)
def _get_cls(runner_type): for runner in rutils.itersubclasses(ScenarioRunner): if runner_type == runner.__execution_type__: return runner raise exceptions.NoSuchRunner(type=runner_type)
def get_available_engines(): """Returns a list of names of available engines.""" return [e.__name__ for e in utils.itersubclasses(EngineFactory)]
def test_all_context_have_ctxt_name(self): for cls in utils.itersubclasses(base.Context): self.assertNotEqual(cls.get_name(), "base", str(cls))
def get_by_name(name): """Returns SLA by name.""" for sla in utils.itersubclasses(SLA): if name == sla.__name__: return sla raise exceptions.NoSuchSLA(name=name)
def get_by_name(name): """Return Context class by name.""" for context in utils.itersubclasses(Context): if name == context.__ctx_name__: return context raise exceptions.NoSuchContext(name=name)
def get_by_name(name): """Returns Context class by name.""" for context in utils.itersubclasses(Context): if name == context.__name__: return context raise exceptions.NoSuchContext(name=name)