def test_all_scenarios_have_docstrings(self): ignored_params = ["self", "scenario_obj"] for scenario_group in utils.itersubclasses(base.Scenario): if scenario_group.__module__.startswith("tests."): continue for method in dir(scenario_group): if base.Scenario.is_scenario(scenario_group, method): scenario = getattr(scenario_group, method) scenario_name = scenario_group.__name__ + "." + method self.assertIsNotNone(scenario.__doc__, "%s doensn't have a docstring." % scenario_name) doc = utils.parse_docstring(scenario.__doc__) short_description = doc["short_description"] self.assertIsNotNone(short_description, "Docstring for %s should have " "at least a one-line description." % scenario_name) self.assertFalse(short_description.startswith("Test"), "One-line description for %s " "should be declarative and not start " "with 'Test(s) ...'" % scenario_name) params_count = scenario.__code__.co_argcount params = scenario.__code__.co_varnames[:params_count] documented_params = [p["name"] for p in doc["params"]] for param in params: if param not in ignored_params: self.assertIn(param, documented_params, "Docstring for %(scenario)s should " "describe the '%(param)s' parameter " "in the :param <name>: clause." % {"scenario": scenario_name, "param": param})
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 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_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 _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)) sla_info = [cls.__name__ for cls in utils.itersubclasses(sla.SLA)] sla_info.extend([cls.OPTION_NAME for cls in utils.itersubclasses( sla.SLA)]) 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 + sla_info + 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 _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)) sla_info = [cls.__name__ for cls in utils.itersubclasses( sla_base.SLA)] sla_info.extend([cls.OPTION_NAME for cls in utils.itersubclasses( sla_base.SLA)]) 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 + sla_info + 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 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 get_all(cls, namespace=None): """Return all subclass plugins of plugin. All plugins that are not configured will be ignored. :param namespace: return only plugins from specified namespace. """ plugins = [] for p in utils.itersubclasses(cls): if issubclass(p, Plugin) and p._meta_is_inited(raise_exc=False): if not namespace or namespace == p.get_namespace(): plugins.append(getattr(p, "func_ref", p)) return plugins
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 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 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 test_all_scenarios_have_docstrings(self): ignored_params = ["self", "scenario_obj"] for scenario_group in utils.itersubclasses(base.Scenario): if scenario_group.__module__.startswith("tests."): continue for method in dir(scenario_group): if base.Scenario.is_scenario(scenario_group, method): scenario = getattr(scenario_group, method) scenario_name = scenario_group.__name__ + "." + method self.assertIsNotNone( scenario.__doc__, "%s doensn't have a docstring." % scenario_name) doc = utils.parse_docstring(scenario.__doc__) short_description = doc["short_description"] self.assertIsNotNone( short_description, "Docstring for %s should have " "at least a one-line description." % scenario_name) self.assertFalse( short_description.startswith("Test"), "One-line description for %s " "should be declarative and not start " "with 'Test(s) ...'" % scenario_name) params_count = scenario.__code__.co_argcount params = scenario.__code__.co_varnames[:params_count] documented_params = [p["name"] for p in doc["params"]] for param in params: if param not in ignored_params: self.assertIn( param, documented_params, "Docstring for %(scenario)s should " "describe the '%(param)s' parameter " "in the :param <name>: clause." % { "scenario": scenario_name, "param": param })
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_by_name(name): """Return Context class by name.""" for context in utils.itersubclasses(Context): if name == context.get_name(): return context raise exceptions.NoSuchContext(name=name)
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 test_all_SLA_have_docstrings(self): for s in utils.itersubclasses(sla.SLA): self._assert_class_has_docstrings(s, long_description=False)
def test_all_server_providers_have_docstrings(self): for provider in utils.itersubclasses(serverprovider.ProviderFactory): self._assert_class_has_docstrings(provider)
def test_all_SLA_have_docstrings(self): for sla in utils.itersubclasses(sla_base.SLA): self._assert_class_has_docstrings(sla, long_description=False)
def test_all_deploy_engines_have_docstrings(self): for deploy_engine in utils.itersubclasses(deploy.EngineFactory): self._assert_class_has_docstrings(deploy_engine)
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_available_engines(): """Returns a list of names of available engines.""" return [e.__name__ for e in utils.itersubclasses(EngineFactory)]
def get_all(cls): return list(utils.itersubclasses(cls))
def get_available_providers(): """Returns list of names of available engines.""" return [e.__name__ for e in utils.itersubclasses(ProviderFactory)]
def test_all_scenario_groups_have_docstrings(self): for scenario_group in utils.itersubclasses(base.Scenario): self._assert_class_has_docstrings(scenario_group, long_description=False)
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_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)