def _run_scenario_once(args): i, cls, method_name, admin, user, kwargs = args LOG.info("ITER: %s START" % i) # TODO(boris-42): remove context scenario = cls( context={}, admin_clients=utils.create_openstack_clients(admin["endpoint"]), clients=utils.create_openstack_clients(user["endpoint"])) try: scenario_output = None with rutils.Timer() as timer: scenario_output = getattr(scenario, method_name)(**kwargs) error = None except Exception as e: error = utils.format_exc(e) if cfg.CONF.debug: LOG.exception(e) finally: status = "Error %s: %s" % tuple(error[0:2]) if error else "OK" LOG.info("ITER: %(i)s END: %(status)s" % {"i": i, "status": status}) return {"time": timer.duration() - scenario.idle_time(), "idle_time": scenario.idle_time(), "error": error, "scenario_output": scenario_output, "atomic_actions_time": scenario.atomic_actions_time()}
def _prepare_boot(self, mock_osclients, mock_choice=None, nic=None, assert_nic=False): fake_server = mock.MagicMock() fc = fakes.FakeClients() mock_osclients.Clients.return_value = fc nova = fakes.FakeNovaClient() fc.get_nova_client = lambda: nova user_endpoint = endpoint.Endpoint("url", "user", "password", "tenant") clients = butils.create_openstack_clients(user_endpoint) scenario = servers.NovaServers(clients=clients) scenario._boot_server = mock.MagicMock(return_value=fake_server) scenario._generate_random_name = mock.MagicMock(return_value="name") kwargs = {'fakearg': 'f'} expected_kwargs = {'fakearg': 'f'} assert_nic = nic or assert_nic if nic: kwargs['nics'] = nic if assert_nic: nova.networks.create('net-1') mock_choice.return_value = nova.networks.create('net-2') expected_kwargs['nics'] = nic or [{'net-id': 'net-2'}] print(kwargs) print(expected_kwargs) return scenario, kwargs, expected_kwargs
def test_create_image_and_boot_instances(self, mock_osclients, mock_create_image, mock_boot_servers, mock_random_name): glance_scenario = images.GlanceImages() nova_scenario = servers.NovaServers() fc = fakes.FakeClients() mock_osclients.Clients.return_value = fc fake_glance = fakes.FakeGlanceClient() fc.get_glance_client = lambda: fake_glance fake_nova = fakes.FakeNovaClient() fc.get_nova_client = lambda: fake_nova user_endpoint = endpoint.Endpoint("url", "user", "password", "tenant") nova_scenario._clients = butils.create_openstack_clients(user_endpoint) fake_image = fakes.FakeImage() fake_servers = [object() for i in range(5)] mock_create_image.return_value = fake_image mock_boot_servers.return_value = fake_servers mock_random_name.return_value = "random_name" kwargs = {'fakearg': 'f'} with mock.patch("rally.benchmark.scenarios.glance.utils.time.sleep"): glance_scenario.\ create_image_and_boot_instances("cf", "url", "df", "fid", 5, **kwargs) mock_create_image.assert_called_once_with("random_name", "cf", "url", "df", **kwargs) mock_boot_servers.assert_called_once_with("random_name", "image-id-0", "fid", 5, **kwargs)
def run(self, name, kwargs): cls_name, method_name = name.split(".") cls = base.Scenario.get_by_name(cls_name) args = kwargs.get('args', {}) init_args = kwargs.get('init', {}) execution_type = kwargs.get('execution', 'continuous') config = kwargs.get('config', {}) tenants = config.get('tenants', 1) users_per_tenant = config.get('users_per_tenant', 1) temp_users = self._create_temp_tenants_and_users(tenants, users_per_tenant) global __openstack_clients__, __scenario_context__ # NOTE(msdubov): Call init() with admin openstack clients cls._clients = __admin_clients__ __scenario_context__ = cls.init(init_args) # NOTE(msdubov): Launch scenarios with non-admin openstack clients keys = ["username", "password", "tenant_name", "uri"] __openstack_clients__ = utils.create_openstack_clients(temp_users, keys) results = self._run_scenario(cls, method_name, args, execution_type, config) self._cleanup_scenario(config.get("active_users", 1)) self._delete_temp_tenants_and_users() return results
def _verify_boot_server(self, mock_choice, mock_osclients, mock_boot, mock_random_name, nic=None, assert_nic=False): assert_nic = nic or assert_nic kwargs = {'fakearg': 'f'} expected_kwargs = {'fakearg': 'f'} fc = fakes.FakeClients() mock_osclients.Clients.return_value = fc nova = fakes.FakeNovaClient() fc.get_nova_client = lambda: nova temp_keys = ["username", "password", "tenant_name", "uri"] users_endpoints = [dict(zip(temp_keys, temp_keys))] servers.NovaServers._clients = butils.create_openstack_clients( users_endpoints, temp_keys)[0] mock_boot.return_value = object() mock_random_name.return_value = "random_name" if nic: kwargs['nics'] = nic if assert_nic: nova.networks.create('net-1') network = nova.networks.create('net-2') mock_choice.return_value = network expected_kwargs['nics'] = nic or [{'net-id': 'net-2'}] servers.NovaServers.boot_server("img", 0, **kwargs) mock_boot.assert_called_once_with("random_name", "img", 0, **expected_kwargs)
def _validate_scenario_args(self, name, kwargs): cls_name, method_name = name.split(".") cls = base_scenario.Scenario.get_by_name(cls_name) method = getattr(cls, method_name) validators = getattr(method, "validators", []) args = kwargs.get("args", {}) # NOTE(msdubov): Some scenarios may require validation from admin, # while others use ordinary clients. admin_validators = [v for v in validators if v.permission == consts.EndpointPermission.ADMIN] user_validators = [v for v in validators if v.permission == consts.EndpointPermission.USER] def validate(validators, clients): for validator in validators: result = validator(clients=clients, **args) if not result.is_valid: raise exceptions.InvalidScenarioArgument( message=result.msg) # NOTE(msdubov): In case of generated users (= admin mode) - validate # first the admin validators, then the user ones # (with one temporarily created user). if self.admin_endpoint: admin_client = utils.create_openstack_clients(self.admin_endpoint) validate(admin_validators, admin_client) context = { "task": self.task, "admin": {"endpoint": self.admin_endpoint} } with users_ctx.UserGenerator(context) as generator: # TODO(boris-42): refactor this peace generator.setup() user = context["users"][0] user_client = utils.create_openstack_clients(user["endpoint"]) validate(user_validators, user_client) # NOTE(msdubov): In case of pre-created users - validate # for all of them. else: for user in self.users: user_client = utils.create_openstack_clients(user) validate(user_validators, user_client)
def __init__(self, task, cloud_config): self.task = task self.endpoints = cloud_config global __admin_clients__ keys = ["admin_username", "admin_password", "admin_tenant_name", "uri"] __admin_clients__ = utils.create_openstack_clients([self.endpoints], keys)[0] base.Scenario.register()
def _cleanup_admin_resources(self): if not self.admin: return try: admin = utils.create_openstack_clients(self.admin) utils.delete_keystone_resources(admin["keystone"]) except Exception as e: LOG.debug(_("Not all resources were cleaned."), exc_info=sys.exc_info()) LOG.warning(_('Unable to fully cleanup keystone service: %s') % (e.message))