def test_deployment_use_by_name(self, m_db, m_ercde, m_uaigf, m_uodf): fake_deployment = fakes.FakeDeployment(uuid="fake_uuid", admin="fake_endpoints") m_db.deployment_list.return_value = [fake_deployment] m_db.deployment_get.return_value = fake_deployment status = self.use.deployment(deployment="fake_name") self.assertIsNone(status) m_db.deployment_get.assert_called_once_with("fake_name") m_ercde.assert_called_once_with() m_uaigf.assert_called_once_with(envutils.ENV_DEPLOYMENT, "fake_uuid") m_uodf.assert_called_once_with("fake_uuid", "fake_endpoints")
def test_required_openstack_with_users(self): validator = self._unwrap_validator(validation.required_openstack, users=True) # users presented in deployment fake_deployment = fakes.FakeDeployment(admin=None, users=["u_credential"]) self.assertTrue(validator({}, None, fake_deployment).is_valid) # admin and users presented in deployment fake_deployment = fakes.FakeDeployment(admin="a", users=["u1", "h2"]) self.assertTrue(validator({}, None, fake_deployment).is_valid) # admin and user context fake_deployment = fakes.FakeDeployment(admin="a", users=[]) context = {"context": {"users": True}} self.assertTrue(validator(context, None, fake_deployment).is_valid) # just admin presented fake_deployment = fakes.FakeDeployment(admin="a", users=[]) self.assertFalse(validator({}, None, fake_deployment).is_valid)
def test_use_by_name(self, mock_db, mock_update_openrc, mock_update_globals): fake_deployment = fakes.FakeDeployment(uuid="fake_uuid", admin="fake_endpoints") mock_db.deployment_list.return_value = [fake_deployment] mock_db.deployment_get.return_value = fake_deployment status = self.deployment.use(deployment="fake_name") self.assertIsNone(status) mock_db.deployment_get.assert_called_once_with("fake_name") mock_update_openrc.assert_called_once_with(envutils.ENV_DEPLOYMENT, "fake_uuid") mock_update_globals.assert_called_once_with("fake_uuid", "fake_endpoints")
def test_use_by_name(self, mock_api_deployment, mock_update_globals_file, mock__update_openrc_deployment_file): fake_deployment = fakes.FakeDeployment(uuid="fake_uuid", admin="fake_credentials") mock_api_deployment.list.return_value = [fake_deployment] mock_api_deployment.get.return_value = fake_deployment status = self.deployment.use(deployment="fake_name") self.assertIsNone(status) mock_api_deployment.get.assert_called_once_with("fake_name") mock_update_globals_file.assert_called_once_with( envutils.ENV_DEPLOYMENT, "fake_uuid") mock__update_openrc_deployment_file.assert_called_once_with( "fake_uuid", "fake_credentials")
def test_required_clients(self): validator = self._unwrap_validator(validation.required_clients, "keystone", "nova") clients = mock.Mock() clients.keystone.return_value = "keystone" clients.nova.return_value = "nova" deployment = fakes.FakeDeployment() result = validator({}, clients, deployment) self.assertTrue(result.is_valid, result.msg) clients.nova.side_effect = ImportError result = validator({}, clients, deployment) self.assertFalse(result.is_valid, result.msg)
def test_required_clients_with_admin(self): validator = self._unwrap_validator(validation.required_clients, "keystone", "nova", admin=True) admin = fakes.fake_credential(foo="bar") clients = admin.clients.return_value clients.keystone.return_value = "keystone" clients.nova.return_value = "nova" deployment = fakes.FakeDeployment(admin=admin) result = validator({}, clients, deployment) self.assertTrue(result.is_valid, result.msg) clients.nova.side_effect = ImportError result = validator({}, clients, deployment) self.assertFalse(result.is_valid, result.msg)
def test_required_cinder_services(self): validator = self._unwrap_validator( validation.required_cinder_services, service_name=six.text_type("cinder-service")) fake_service = mock.Mock(binary="cinder-service", state="up") admin = fakes.fake_credential(foo="bar") cinder = admin.clients.return_value.cinder.return_value cinder.services.list.return_value = [fake_service] deployment = fakes.FakeDeployment(admin=admin) result = validator({}, None, deployment) self.assertTrue(result.is_valid, result.msg) fake_service.state = "down" result = validator({}, None, deployment) self.assertFalse(result.is_valid, result.msg)
def setUp(self): super(TempestContextTestCase, self).setUp() mock.patch("rally.osclients.Clients").start() self.mock_isfile = mock.patch("os.path.isfile", return_value=True).start() self.deployment = fakes.FakeDeployment(**CREDS) cfg = {"verifier": mock.Mock(deployment=self.deployment), "verification": {"uuid": "uuid"}} cfg["verifier"].manager.home_dir = "/p/a/t/h" cfg["verifier"].manager.configfile = "/fake/path/to/config" self.context = context.TempestContext(cfg) self.context.conf.add_section("compute") self.context.conf.add_section("orchestration") self.context.conf.add_section("scenario")
def test__validate_config_semantic( self, mock_deployment_get, mock__validate_config_semantic_helper, mock_task_config, mock_context, mock_scenario_get): admin = fakes.fake_credential(foo="admin") users = [fakes.fake_credential(bar="user1")] deployment = fakes.FakeDeployment( uuid="deployment_uuid", admin=admin, users=users) class SomeScen(object): is_classbased = True @classmethod def get_namespace(cls): return "openstack" @classmethod def get_info(cls): return {"title": "foo"} mock_scenario_get.return_value = SomeScen mock_task_instance = mock.MagicMock() mock_subtask1 = mock.MagicMock() wconf1 = engine.Workload({"name": "a", "runner": "ra", "context": {"users": {}}}, 0) wconf2 = engine.Workload({"name": "a", "runner": "rb"}, 1) mock_subtask1.workloads = [wconf1, wconf2] mock_subtask2 = mock.MagicMock() wconf3 = engine.Workload({"name": "b", "runner": "ra"}, 0) mock_subtask2.workloads = [wconf3] mock_task_instance.subtasks = [mock_subtask1, mock_subtask2] fake_task = mock.MagicMock() eng = engine.TaskEngine(mock_task_instance, fake_task, deployment) eng._validate_config_semantic(mock_task_instance) user_context = mock_context.get.return_value.return_value mock__validate_config_semantic_helper.assert_has_calls([ mock.call(admin, user_context, [wconf1], "openstack"), mock.call(admin, user_context, [wconf2, wconf3], "openstack"), ], any_order=True)
def test__validate_config_semantic( self, mock_deployment_get, mock__validate_config_semantic_helper, mock_task_config, mock_context, mock_scenario_get): admin = fakes.fake_credential(foo="admin") users = [fakes.fake_credential(bar="user1")] deployment = fakes.FakeDeployment( uuid="deployment_uuid", admin=admin, users=users) # TODO(boris-42): Refactor this test case to make it # up to date with other code class SomeScen(object): is_classbased = True @classmethod def get_platform(cls): return "openstack" @classmethod def get_info(cls): return {"title": "foo"} mock_scenario_get.return_value = SomeScen mock_task_instance = mock.MagicMock() wconf1 = self._make_workload(name="a", runner="ra", context={"users": {}}) wconf2 = self._make_workload(name="a", runner="rb", position=1) subtask1 = {"workloads": [wconf1, wconf2]} wconf3 = self._make_workload(name="b", runner="ra", position=2) subtask2 = {"workloads": [wconf3]} mock_task_instance.subtasks = [subtask1, subtask2] fake_task = mock.MagicMock() eng = engine.TaskEngine(mock_task_instance, fake_task, deployment) eng._validate_config_semantic(mock_task_instance) user_context = mock_context.get.return_value.return_value mock__validate_config_semantic_helper.assert_called_once_with( admin, user_context, [wconf1, wconf2, wconf3], "openstack")
def test_required_clients_with_admin(self, mock_osclients, mock_objects): validator = self._unwrap_validator(validation.required_clients, "keystone", "nova", admin=True) clients = mock.Mock() clients.keystone.return_value = "keystone" clients.nova.return_value = "nova" mock_osclients.Clients.return_value = clients mock_objects.Credential.return_value = "foo_credential" deployment = fakes.FakeDeployment(admin={"foo": "bar"}) result = validator({}, clients, deployment) self.assertTrue(result.is_valid, result.msg) mock_objects.Credential.assert_called_once_with(foo="bar") mock_osclients.Clients.assert_called_once_with("foo_credential") clients.nova.side_effect = ImportError result = validator({}, clients, deployment) self.assertFalse(result.is_valid, result.msg)
def test_use_with_v3_auth(self, mock_update_env_file, mock_path_exists, mock_symlink, mock_remove): deployment_id = "593b683c-4b16-4b2b-a56b-e162bd60f10b" self.fake_api.deployment.get.return_value = fakes.FakeDeployment( uuid=deployment_id, admin={ "auth_url": "http://localhost:5000/v3", "username": "******", "password": "******", "tenant_name": "fake_tenant_name", "endpoint": "fake_endpoint", "region_name": None, "user_domain_name": "fake_user_domain", "project_domain_name": "fake_project_domain" }) with mock.patch("rally.cli.commands.deployment.open", mock.mock_open(), create=True) as mock_file: self.deployment.use(self.fake_api, deployment_id) self.assertEqual(2, mock_path_exists.call_count) mock_update_env_file.assert_called_once_with( os.path.expanduser("~/.rally/globals"), "RALLY_DEPLOYMENT", "%s\n" % deployment_id) mock_file.return_value.write.assert_any_call( "export OS_ENDPOINT='fake_endpoint'\n") mock_file.return_value.write.assert_any_call( "export OS_AUTH_URL='http://localhost:5000/v3'\n" "export OS_USERNAME='******'\n" "export OS_PASSWORD='******'\n" "export OS_TENANT_NAME='fake_tenant_name'\n") mock_file.return_value.write.assert_any_call( "export OS_USER_DOMAIN_NAME='fake_user_domain'\n" "export OS_PROJECT_DOMAIN_NAME='fake_project_domain'\n") mock_symlink.assert_called_once_with( os.path.expanduser("~/.rally/openrc-%s" % deployment_id), os.path.expanduser("~/.rally/openrc")) mock_remove.assert_called_once_with( os.path.expanduser("~/.rally/openrc"))
def test__validate_config_semantic( self, mock_deployment_get, mock__validate_config_semantic_helper, mock_task_config, mock_credential, mock_context, mock_scenario_get, mock_clients): deployment = fakes.FakeDeployment( uuid="deployment_uuid", admin={"foo": "admin"}, users=[{"bar": "user1"}]) scenario_cls = mock_scenario_get.return_value scenario_cls.get_namespace.return_value = "default" mock_task_instance = mock.MagicMock() mock_subtask1 = mock.MagicMock() wconf1 = engine.Workload({"name": "a", "runner": "ra", "context": {"users": {}}}, 0) wconf2 = engine.Workload({"name": "a", "runner": "rb"}, 1) mock_subtask1.workloads = [wconf1, wconf2] mock_subtask2 = mock.MagicMock() wconf3 = engine.Workload({"name": "b", "runner": "ra"}, 0) mock_subtask2.workloads = [wconf3] mock_task_instance.subtasks = [mock_subtask1, mock_subtask2] fake_task = mock.MagicMock() eng = engine.TaskEngine(mock_task_instance, fake_task, deployment) eng._validate_config_semantic(mock_task_instance) admin = mock_credential.return_value user_context = mock_context.get.return_value.return_value mock_clients.assert_called_once_with(admin) mock_clients.return_value.verified_keystone.assert_called_once_with() mock__validate_config_semantic_helper.assert_has_calls([ mock.call(admin, user_context, [wconf1], deployment), mock.call(admin, user_context, [wconf2, wconf3], deployment), ], any_order=True)
def test_run__task_soft_aborted( self, mock_scenario_runner, mock_scenario, mock_context_manager_setup, mock_context_manager_cleanup, mock_result_consumer): scenario_cls = mock_scenario.get.return_value scenario_cls.get_namespace.return_value = "openstack" task = mock.MagicMock() mock_result_consumer.is_task_in_aborting_status.side_effect = [False, False, True] config = { "a.task": [{"runner": {"type": "a", "b": 1}, "description": "foo"}], "b.task": [{"runner": {"type": "a", "b": 1}, "description": "bar"}], "c.task": [{"runner": {"type": "a", "b": 1}, "description": "xxx"}] } fake_runner_cls = mock.MagicMock() fake_runner = mock.MagicMock() fake_runner_cls.return_value = fake_runner mock_scenario_runner.get.return_value = fake_runner_cls deployment = fakes.FakeDeployment( uuid="deployment_uuid", admin={"foo": "admin"}) eng = engine.TaskEngine(config, task, deployment) eng.run() self.assertEqual(2, fake_runner.run.call_count) self.assertEqual(mock.call(consts.TaskStatus.ABORTED), task.update_status.mock_calls[-1]) subtask_obj = task.add_subtask.return_value subtask_obj.update_status.assert_has_calls(( mock.call(consts.SubtaskStatus.FINISHED), mock.call(consts.SubtaskStatus.FINISHED), mock.call(consts.SubtaskStatus.ABORTED), ))
def setUp(self): super(TempestConfigfileManagerTestCase, self).setUp() deployment = fakes.FakeDeployment(uuid="fake_deployment", admin=fakes.fake_credential(**CRED)) self.tempest = config.TempestConfigfileManager(deployment)
class TaskAPITestCase(test.TestCase): def setUp(self): super(TaskAPITestCase, self).setUp() self.task_uuid = "b0d9cd6c-2c94-4417-a238-35c7019d0257" self.task = { "uuid": self.task_uuid, } @mock.patch("rally.api.objects.Task") @mock.patch("rally.api.objects.Deployment.get", return_value=fakes.FakeDeployment(uuid="deployment_uuid", admin=mock.MagicMock(), users=[])) @mock.patch("rally.api.engine.BenchmarkEngine") def test_validate(self, mock_benchmark_engine, mock_deployment_get, mock_task): api.Task.validate(mock_deployment_get.return_value["uuid"], "config") mock_benchmark_engine.assert_has_calls([ mock.call("config", mock_task.return_value, admin=mock_deployment_get.return_value["admin"], users=[]), mock.call().validate() ]) mock_task.assert_called_once_with( temporary=True, deployment_uuid=mock_deployment_get.return_value["uuid"]) mock_deployment_get.assert_called_once_with( mock_deployment_get.return_value["uuid"]) @mock.patch("rally.api.objects.Task") @mock.patch("rally.api.objects.Deployment", return_value=fakes.FakeDeployment(uuid="deployment_uuid", admin=mock.MagicMock(), users=[])) @mock.patch("rally.api.engine.BenchmarkEngine") def test_validate_engine_exception(self, mock_benchmark_engine, mock_deployment, mock_task): excpt = exceptions.InvalidTaskException() mock_benchmark_engine.return_value.validate.side_effect = excpt self.assertRaises(exceptions.InvalidTaskException, api.Task.validate, mock_deployment.return_value["uuid"], "config") def test_render_template(self): self.assertEqual( "3 = 3", api.Task.render_template("{{a + b}} = {{c}}", a=1, b=2, c=3)) def test_render_template_default_values(self): template = "{% set a = a or 1 %}{{a + b}} = {{c}}" self.assertEqual("3 = 3", api.Task.render_template(template, b=2, c=3)) self.assertEqual("5 = 5", api.Task.render_template(template, a=2, b=3, c=5)) def test_render_template_default_filter(self): template = "{{ c | default(3) }}" self.assertEqual("3", api.Task.render_template(template)) self.assertEqual("5", api.Task.render_template(template, c=5)) def test_render_template_builtin(self): template = "{% for i in range(4) %}{{i}}{% endfor %}" self.assertEqual("0123", api.Task.render_template(template)) def test_render_template_missing_args(self): self.assertRaises(TypeError, api.Task.render_template, "{{a}}") def test_render_template_include_other_template(self): other_template_path = os.path.join( os.path.dirname(__file__), "..", "..", "samples/tasks/scenarios/nova/boot.json") template = "{%% include \"%s\" %%}" % os.path.basename( other_template_path) with open(other_template_path) as f: other_template = f.read() expect = api.Task.render_template(other_template) actual = api.Task.render_template(template, os.path.dirname(other_template_path)) self.assertEqual(expect, actual) @mock.patch("rally.common.objects.Deployment.get", return_value={"uuid": "b0d9cd6c-2c94-4417-a238-35c7019d0257"}) @mock.patch("rally.common.objects.Task") def test_create(self, mock_task, mock_deployment_get): tag = "a" api.Task.create(mock_deployment_get.return_value["uuid"], tag) mock_task.assert_called_once_with( deployment_uuid=mock_deployment_get.return_value["uuid"], tag=tag) @mock.patch("rally.api.objects.Task", return_value=fakes.FakeTask(uuid="some_uuid")) @mock.patch("rally.api.objects.Deployment.get", return_value=fakes.FakeDeployment(uuid="deployment_uuid", admin=mock.MagicMock(), users=[])) @mock.patch("rally.api.engine.BenchmarkEngine") def test_start(self, mock_benchmark_engine, mock_deployment_get, mock_task): api.Task.start(mock_deployment_get.return_value["uuid"], "config") mock_benchmark_engine.assert_has_calls([ mock.call("config", mock_task.return_value, admin=mock_deployment_get.return_value["admin"], users=[], abort_on_sla_failure=False), mock.call().run(), ]) mock_task.assert_called_once_with( deployment_uuid=mock_deployment_get.return_value["uuid"]) mock_deployment_get.assert_called_once_with( mock_deployment_get.return_value["uuid"]) @mock.patch("rally.api.objects.Task", return_value=fakes.FakeTask(uuid="some_uuid", task={}, temporary=True)) @mock.patch("rally.api.objects.Deployment.get", return_value=fakes.FakeDeployment(uuid="deployment_uuid", admin=mock.MagicMock(), users=[])) def test_start_temporary_task(self, mock_deployment_get, mock_task): self.assertRaises(ValueError, api.Task.start, mock_deployment_get.return_value["uuid"], "config") @mock.patch("rally.api.objects.Task") @mock.patch("rally.api.objects.Deployment.get") @mock.patch("rally.api.engine.BenchmarkEngine") def test_start_exception(self, mock_benchmark_engine, mock_deployment_get, mock_task): mock_task.return_value.is_temporary = False mock_benchmark_engine.return_value.run.side_effect = TypeError self.assertRaises(TypeError, api.Task.start, "deployment_uuid", "config") mock_deployment_get().update_status.assert_called_once_with( consts.DeployStatus.DEPLOY_INCONSISTENT) @ddt.data(True, False) @mock.patch("rally.api.time") @mock.patch("rally.api.objects.Task") def test_abort_sync(self, soft, mock_task, mock_time): mock_task.get_status.side_effect = (consts.TaskStatus.INIT, consts.TaskStatus.VERIFYING, consts.TaskStatus.RUNNING, consts.TaskStatus.ABORTING, consts.TaskStatus.SOFT_ABORTING, consts.TaskStatus.ABORTED) some_uuid = "ca441749-0eb9-4fcc-b2f6-76d314c55404" api.Task.abort(some_uuid, soft=soft, async=False) mock_task.get.assert_called_once_with(some_uuid) mock_task.get.return_value.abort.assert_called_once_with(soft=soft) self.assertEqual([mock.call(some_uuid)] * 6, mock_task.get_status.call_args_list) self.assertTrue(mock_time.sleep.called) @ddt.data(True, False) @mock.patch("rally.api.time") @mock.patch("rally.api.objects.Task") def test_abort_async(self, soft, mock_task, mock_time): some_uuid = "133695fb-400d-4988-859c-30bfaa0488ce" api.Task.abort(some_uuid, soft=soft, async=True) mock_task.get.assert_called_once_with(some_uuid) mock_task.get.return_value.abort.assert_called_once_with(soft=soft) self.assertFalse(mock_task.get_status.called) self.assertFalse(mock_time.sleep.called) @mock.patch("rally.common.objects.task.db.task_delete") def test_delete(self, mock_task_delete): api.Task.delete(self.task_uuid) mock_task_delete.assert_called_once_with( self.task_uuid, status=consts.TaskStatus.FINISHED) @mock.patch("rally.common.objects.task.db.task_delete") def test_delete_force(self, mock_task_delete): api.Task.delete(self.task_uuid, force=True) mock_task_delete.assert_called_once_with(self.task_uuid, status=None)
def test_setup(self, mock_clients, mock_create_dir, mock__create_tempest_roles, mock__configure_option, mock_open): self.deployment = fakes.FakeDeployment(**CREDS) verifier = mock.Mock(deployment=self.deployment) verifier.manager.home_dir = "/p/a/t/h" # case #1: no neutron and heat mock_clients.return_value.services.return_value = {} ctx = context.TempestContext({"verifier": verifier}) ctx.conf = mock.Mock() ctx.setup() ctx.conf.read.assert_called_once_with(verifier.manager.configfile) mock_create_dir.assert_called_once_with(ctx.data_dir) mock__create_tempest_roles.assert_called_once_with() mock_open.assert_called_once_with(verifier.manager.configfile, "w") ctx.conf.write(mock_open.side_effect()) self.assertEqual( [mock.call("DEFAULT", "log_file", "/p/a/t/h/tempest.log"), mock.call("oslo_concurrency", "lock_path", "/p/a/t/h/lock_files"), mock.call("scenario", "img_dir", "/p/a/t/h"), mock.call("scenario", "img_file", ctx.image_name, helper_method=ctx._download_image), mock.call("compute", "image_ref", helper_method=ctx._discover_or_create_image), mock.call("compute", "image_ref_alt", helper_method=ctx._discover_or_create_image), mock.call("compute", "flavor_ref", helper_method=ctx._discover_or_create_flavor, flv_ram=config.CONF.tempest.flavor_ref_ram), mock.call("compute", "flavor_ref_alt", helper_method=ctx._discover_or_create_flavor, flv_ram=config.CONF.tempest.flavor_ref_alt_ram)], mock__configure_option.call_args_list) mock_create_dir.reset_mock() mock__create_tempest_roles.reset_mock() mock_open.reset_mock() mock__configure_option.reset_mock() # case #2: neutron and heat are presented mock_clients.return_value.services.return_value = { "network": "neutron", "orchestration": "heat"} ctx = context.TempestContext({"verifier": verifier}) ctx.conf = mock.Mock() ctx.setup() ctx.conf.read.assert_called_once_with(verifier.manager.configfile) mock_create_dir.assert_called_once_with(ctx.data_dir) mock__create_tempest_roles.assert_called_once_with() mock_open.assert_called_once_with(verifier.manager.configfile, "w") ctx.conf.write(mock_open.side_effect()) self.assertEqual( [mock.call("DEFAULT", "log_file", "/p/a/t/h/tempest.log"), mock.call("oslo_concurrency", "lock_path", "/p/a/t/h/lock_files"), mock.call("scenario", "img_dir", "/p/a/t/h"), mock.call("scenario", "img_file", ctx.image_name, helper_method=ctx._download_image), mock.call("compute", "image_ref", helper_method=ctx._discover_or_create_image), mock.call("compute", "image_ref_alt", helper_method=ctx._discover_or_create_image), mock.call("compute", "flavor_ref", helper_method=ctx._discover_or_create_flavor, flv_ram=config.CONF.tempest.flavor_ref_ram), mock.call("compute", "flavor_ref_alt", helper_method=ctx._discover_or_create_flavor, flv_ram=config.CONF.tempest.flavor_ref_alt_ram), mock.call("compute", "fixed_network_name", helper_method=ctx._create_network_resources), mock.call("orchestration", "instance_type", helper_method=ctx._discover_or_create_flavor, flv_ram=config.CONF.tempest.heat_instance_type_ram)], mock__configure_option.call_args_list)
class DeploymentCommandsTestCase(test.TestCase): def setUp(self): super(DeploymentCommandsTestCase, self).setUp() self.deployment = deployment.DeploymentCommands() @mock.patch.dict(os.environ, {"RALLY_DEPLOYMENT": "my_deployment_id"}) @mock.patch("rally.cli.commands.deployment.DeploymentCommands.list") @mock.patch("rally.cli.commands.deployment.api.Deployment.create") @mock.patch("rally.cli.commands.deployment.open", side_effect=mock.mock_open(read_data="{\"some\": \"json\"}"), create=True) def test_create(self, mock_open, mock_deployment_create, mock_deployment_commands_list): self.deployment.create("fake_deploy", False, "path_to_config.json") mock_deployment_create.assert_called_once_with({"some": "json"}, "fake_deploy") @mock.patch.dict( os.environ, { "OS_AUTH_URL": "fake_auth_url", "OS_USERNAME": "******", "OS_PASSWORD": "******", "OS_TENANT_NAME": "fake_tenant_name", "OS_REGION_NAME": "fake_region_name", "OS_ENDPOINT": "fake_endpoint", "OS_INSECURE": "True", "OS_CACERT": "fake_cacert", "RALLY_DEPLOYMENT": "fake_deployment_id" }) @mock.patch("rally.cli.commands.deployment.api.Deployment.create") @mock.patch("rally.cli.commands.deployment.DeploymentCommands.list") def test_createfromenv(self, mock_list, mock_deployment_create): self.deployment.create("from_env", True) mock_deployment_create.assert_called_once_with( { "type": "ExistingCloud", "auth_url": "fake_auth_url", "region_name": "fake_region_name", "endpoint": "fake_endpoint", "admin": { "username": "******", "password": "******", "tenant_name": "fake_tenant_name" }, "https_insecure": True, "https_cacert": "fake_cacert" }, "from_env") @mock.patch("rally.cli.commands.deployment.DeploymentCommands.list") @mock.patch("rally.cli.commands.deployment.DeploymentCommands.use") @mock.patch("rally.cli.commands.deployment.api.Deployment.create", return_value=dict(uuid="uuid")) @mock.patch("rally.cli.commands.deployment.open", side_effect=mock.mock_open(read_data="{\"uuid\": \"uuid\"}"), create=True) def test_create_and_use(self, mock_open, mock_deployment_create, mock_deployment_commands_use, mock_deployment_commands_list): self.deployment.create("fake_deploy", False, "path_to_config.json", True) mock_deployment_create.assert_called_once_with({"uuid": "uuid"}, "fake_deploy") mock_deployment_commands_use.assert_called_once_with("uuid") @mock.patch("rally.cli.commands.deployment.api.Deployment.recreate") def test_recreate(self, mock_deployment_recreate): deployment_id = "43924f8b-9371-4152-af9f-4cf02b4eced4" self.deployment.recreate(deployment_id) mock_deployment_recreate.assert_called_once_with(deployment_id) @mock.patch("rally.cli.commands.deployment.envutils.get_global") def test_recreate_no_deployment_id(self, mock_get_global): mock_get_global.side_effect = exceptions.InvalidArgumentsException self.assertRaises(exceptions.InvalidArgumentsException, self.deployment.recreate, None) @mock.patch("rally.cli.commands.deployment.api.Deployment.destroy") def test_destroy(self, mock_deployment_destroy): deployment_id = "53fd0273-60ce-42e5-a759-36f1a683103e" self.deployment.destroy(deployment_id) mock_deployment_destroy.assert_called_once_with(deployment_id) @mock.patch("rally.cli.commands.deployment.envutils.get_global") def test_destroy_no_deployment_id(self, mock_get_global): mock_get_global.side_effect = exceptions.InvalidArgumentsException self.assertRaises(exceptions.InvalidArgumentsException, self.deployment.destroy, None) @mock.patch("rally.cli.commands.deployment.cliutils.print_list") @mock.patch("rally.cli.commands.deployment.utils.Struct") @mock.patch("rally.cli.commands.deployment.envutils.get_global") @mock.patch("rally.cli.commands.deployment.api.Deployment.list") def test_list_different_deployment_id(self, mock_deployment_list, mock_get_global, mock_struct, mock_print_list): current_deployment_id = "26a3ce76-0efa-40e4-86e5-514574bd1ff6" mock_get_global.return_value = current_deployment_id fake_deployment_list = [{ "uuid": "fa34aea2-ae2e-4cf7-a072-b08d67466e3e", "created_at": "03-12-2014", "name": "dep1", "status": "deploy->started", "active": "False" }] mock_deployment_list.return_value = fake_deployment_list self.deployment.list() fake_deployment = fake_deployment_list[0] fake_deployment["active"] = "" mock_struct.assert_called_once_with(**fake_deployment) headers = ["uuid", "created_at", "name", "status", "active"] mock_print_list.assert_called_once_with( [mock_struct()], headers, sortby_index=headers.index("created_at")) @mock.patch("rally.cli.commands.deployment.cliutils.print_list") @mock.patch("rally.cli.commands.deployment.utils.Struct") @mock.patch("rally.cli.commands.deployment.envutils.get_global") @mock.patch("rally.cli.commands.deployment.api.Deployment.list") def test_list_current_deployment_id(self, mock_deployment_list, mock_get_global, mock_struct, mock_print_list): current_deployment_id = "64258e84-ffa1-4011-9e4c-aba07bdbcc6b" mock_get_global.return_value = current_deployment_id fake_deployment_list = [{ "uuid": current_deployment_id, "created_at": "13-12-2014", "name": "dep2", "status": "deploy->finished", "active": "True" }] mock_deployment_list.return_value = fake_deployment_list self.deployment.list() fake_deployment = fake_deployment_list[0] fake_deployment["active"] = "*" mock_struct.assert_called_once_with(**fake_deployment) headers = ["uuid", "created_at", "name", "status", "active"] mock_print_list.assert_called_once_with( [mock_struct()], headers, sortby_index=headers.index("created_at")) @mock.patch("rally.cli.commands.deployment.api.Deployment.get") @mock.patch("json.dumps") def test_config(self, mock_json_dumps, mock_deployment_get): deployment_id = "fa4a423e-f15d-4d83-971a-89574f892999" value = {"config": "config"} mock_deployment_get.return_value = value self.deployment.config(deployment_id) mock_json_dumps.assert_called_once_with(value["config"], sort_keys=True, indent=4) mock_deployment_get.assert_called_once_with(deployment_id) @mock.patch("rally.cli.commands.deployment.envutils.get_global") def test_config_no_deployment_id(self, mock_get_global): mock_get_global.side_effect = exceptions.InvalidArgumentsException self.assertRaises(exceptions.InvalidArgumentsException, self.deployment.config, None) @mock.patch("rally.cli.commands.deployment.cliutils.print_list") @mock.patch("rally.cli.commands.deployment.utils.Struct") @mock.patch("rally.cli.commands.deployment.api.Deployment.get") def test_show(self, mock_deployment_get, mock_struct, mock_print_list): deployment_id = "b1a6153e-a314-4cb3-b63b-cf08c1a416c3" value = { "admin": { "auth_url": "url", "username": "******", "password": "******", "tenant_name": "t", "region_name": "r", "endpoint_type": consts.EndpointType.INTERNAL }, "users": [] } mock_deployment_get.return_value = value self.deployment.show(deployment_id) mock_deployment_get.assert_called_once_with(deployment_id) headers = [ "auth_url", "username", "password", "tenant_name", "region_name", "endpoint_type" ] fake_data = ["url", "u", "***", "t", "r", consts.EndpointType.INTERNAL] mock_struct.assert_called_once_with(**dict(zip(headers, fake_data))) mock_print_list.assert_called_once_with([mock_struct()], headers) @mock.patch("rally.cli.commands.deployment.envutils.get_global") def test_deploy_no_deployment_id(self, mock_get_global): mock_get_global.side_effect = exceptions.InvalidArgumentsException self.assertRaises(exceptions.InvalidArgumentsException, self.deployment.show, None) @mock.patch("os.remove") @mock.patch("os.symlink") @mock.patch("rally.cli.commands.deployment.api.Deployment.get", return_value=fakes.FakeDeployment( uuid="593b683c-4b16-4b2b-a56b-e162bd60f10b")) @mock.patch("os.path.exists", return_value=True) @mock.patch("rally.common.fileutils.update_env_file") def test_use(self, mock_update_env_file, mock_path_exists, mock_deployment_get, mock_symlink, mock_remove): deployment_id = mock_deployment_get.return_value["uuid"] mock_deployment_get.return_value["admin"] = { "auth_url": "fake_auth_url", "username": "******", "password": "******", "tenant_name": "fake_tenant_name", "endpoint": "fake_endpoint", "region_name": None } with mock.patch("rally.cli.commands.deployment.open", mock.mock_open(), create=True) as mock_file: self.deployment.use(deployment_id) self.assertEqual(2, mock_path_exists.call_count) mock_update_env_file.assert_called_once_with( os.path.expanduser("~/.rally/globals"), "RALLY_DEPLOYMENT", "%s\n" % deployment_id) mock_file.return_value.write.assert_any_call( "export OS_ENDPOINT=fake_endpoint\n") mock_file.return_value.write.assert_any_call( "export OS_AUTH_URL=fake_auth_url\n" "export OS_USERNAME=fake_username\n" "export OS_PASSWORD=fake_password\n" "export OS_TENANT_NAME=fake_tenant_name\n") mock_symlink.assert_called_once_with( os.path.expanduser("~/.rally/openrc-%s" % deployment_id), os.path.expanduser("~/.rally/openrc")) mock_remove.assert_called_once_with( os.path.expanduser("~/.rally/openrc")) @mock.patch("os.remove") @mock.patch("os.symlink") @mock.patch("rally.cli.commands.deployment.api.Deployment.get", return_value=fakes.FakeDeployment( uuid="593b683c-4b16-4b2b-a56b-e162bd60f10b")) @mock.patch("os.path.exists", return_value=True) @mock.patch("rally.common.fileutils.update_env_file") def test_use_with_v3_auth(self, mock_update_env_file, mock_path_exists, mock_deployment_get, mock_symlink, mock_remove): deployment_id = mock_deployment_get.return_value["uuid"] mock_deployment_get.return_value["admin"] = { "auth_url": "http://*****:*****@mock.patch("rally.cli.commands.deployment.DeploymentCommands." "_update_openrc_deployment_file") @mock.patch("rally.common.fileutils.update_globals_file") @mock.patch("rally.cli.commands.deployment.api.Deployment") def test_use_by_name(self, mock_api_deployment, mock_update_globals_file, mock__update_openrc_deployment_file): fake_deployment = fakes.FakeDeployment(uuid="fake_uuid", admin="fake_credentials") mock_api_deployment.list.return_value = [fake_deployment] mock_api_deployment.get.return_value = fake_deployment status = self.deployment.use(deployment="fake_name") self.assertIsNone(status) mock_api_deployment.get.assert_called_once_with("fake_name") mock_update_globals_file.assert_called_once_with( envutils.ENV_DEPLOYMENT, "fake_uuid") mock__update_openrc_deployment_file.assert_called_once_with( "fake_uuid", "fake_credentials") @mock.patch("rally.cli.commands.deployment.api.Deployment.get") def test_deployment_not_found(self, mock_deployment_get): deployment_id = "e87e4dca-b515-4477-888d-5f6103f13b42" mock_deployment_get.side_effect = exceptions.DeploymentNotFound( deployment=deployment_id) self.assertEqual(1, self.deployment.use(deployment_id)) @mock.patch("rally.cli.commands.deployment.cliutils.print_list") @mock.patch("rally.cli.commands.deployment.api.Deployment.check") @mock.patch("rally.cli.commands.deployment.api.Deployment.get") def test_deployment_check(self, mock_deployment_get, mock_deployment_check, mock_print_list): deployment_id = "e87e4dca-b515-4477-888d-5f6103f13b42" sample_credential = objects.Credential("http://192.168.1.1:5000/v2.0/", "admin", "adminpass").to_dict() deployment = {"admin": sample_credential, "users": [sample_credential]} mock_deployment_get.return_value = deployment mock_deployment_check.return_value = {} self.deployment.check(deployment_id) mock_deployment_get.assert_called_once_with(deployment_id) mock_deployment_check.assert_called_once_with(deployment) headers = ["services", "type", "status"] mock_print_list.assert_called_once_with([], headers) @mock.patch("rally.cli.commands.deployment.api.Deployment.get") def test_deployment_check_not_exist(self, mock_deployment_get): deployment_id = "e87e4dca-b515-4477-888d-5f6103f13b42" mock_deployment_get.side_effect = exceptions.DeploymentNotFound( deployment=deployment_id) self.assertEqual(self.deployment.check(deployment_id), 1) @mock.patch("rally.cli.commands.deployment.api.Deployment.check") @mock.patch("rally.cli.commands.deployment.api.Deployment.get") def test_deployment_check_raise(self, mock_deployment_get, mock_deployment_check): deployment_id = "e87e4dca-b515-4477-888d-5f6103f13b42" sample_credential = objects.Credential("http://192.168.1.1:5000/v2.0/", "admin", "adminpass").to_dict() sample_credential["not-exist-key"] = "error" mock_deployment_get.return_value = {"admin": sample_credential} refused = keystone_exceptions.ConnectionRefused() mock_deployment_check.side_effect = refused self.assertEqual(self.deployment.check(deployment_id), 1)
class TaskAPITestCase(test.TestCase): def setUp(self): super(TaskAPITestCase, self).setUp() self.task_uuid = "b0d9cd6c-2c94-4417-a238-35c7019d0257" self.task = { "uuid": self.task_uuid, } @mock.patch("rally.api.objects.Task") @mock.patch("rally.api.objects.Deployment.get", return_value=fakes.FakeDeployment(uuid="deployment_uuid", admin=mock.MagicMock(), users=[])) @mock.patch("rally.api.engine.BenchmarkEngine") def test_validate(self, mock_engine, mock_deployment_get, mock_task): api.Task.validate(mock_deployment_get.return_value["uuid"], "config") mock_engine.assert_has_calls([ mock.call("config", mock_task.return_value, admin=mock_deployment_get.return_value["admin"], users=[]), mock.call().validate() ]) mock_task.assert_called_once_with( fake=True, deployment_uuid=mock_deployment_get.return_value["uuid"]) mock_deployment_get.assert_called_once_with( mock_deployment_get.return_value["uuid"]) def test_render_template(self): self.assertEqual( "3 = 3", api.Task.render_template("{{a + b}} = {{c}}", a=1, b=2, c=3)) def test_render_template_default_values(self): template = "{% set a = a or 1 %}{{a + b}} = {{c}}" self.assertEqual("3 = 3", api.Task.render_template(template, b=2, c=3)) self.assertEqual( "5 = 5", api.Task.render_template(template, a=2, b=3, c=5)) def test_render_template_builtin(self): template = "{% for i in range(4) %}{{i}}{% endfor %}" self.assertEqual("0123", api.Task.render_template(template)) def test_render_template_missing_args(self): self.assertRaises(TypeError, api.Task.render_template, "{{a}}") @mock.patch("rally.objects.Deployment.get", return_value={"uuid": "b0d9cd6c-2c94-4417-a238-35c7019d0257"}) @mock.patch("rally.objects.Task") def test_create(self, mock_task, mock_d_get): tag = "a" api.Task.create(mock_d_get.return_value["uuid"], tag) mock_task.assert_called_once_with( deployment_uuid=mock_d_get.return_value["uuid"], tag=tag) @mock.patch("rally.api.objects.Task") @mock.patch("rally.api.objects.Deployment.get", return_value=fakes.FakeDeployment(uuid="deployment_uuid", admin=mock.MagicMock(), users=[])) @mock.patch("rally.api.engine.BenchmarkEngine") def test_start(self, mock_engine, mock_deployment_get, mock_task): api.Task.start(mock_deployment_get.return_value["uuid"], "config") mock_engine.assert_has_calls([ mock.call("config", mock_task.return_value, admin=mock_deployment_get.return_value["admin"], users=[], abort_on_sla_failure=False), mock.call().validate(), mock.call().run() ]) mock_task.assert_called_once_with( deployment_uuid=mock_deployment_get.return_value["uuid"]) mock_deployment_get.assert_called_once_with( mock_deployment_get.return_value["uuid"]) @mock.patch("rally.api.objects.Task") @mock.patch("rally.api.objects.Deployment.get") @mock.patch("rally.api.engine.BenchmarkEngine") def test_start_invalid_task_ignored(self, mock_engine, mock_deployment_get, mock_task): mock_engine().run.side_effect = ( exceptions.InvalidTaskException()) # check that it doesn't raise anything api.Task.start("deployment_uuid", "config") @mock.patch("rally.api.objects.Task") @mock.patch("rally.api.objects.Deployment.get") @mock.patch("rally.api.engine.BenchmarkEngine") def test_start_exception(self, mock_engine, mock_deployment_get, mock_task): mock_engine().run.side_effect = TypeError self.assertRaises(TypeError, api.Task.start, "deployment_uuid", "config") mock_deployment_get().update_status.assert_called_once_with( consts.DeployStatus.DEPLOY_INCONSISTENT) def test_abort(self): self.assertRaises(NotImplementedError, api.Task.abort, self.task_uuid) @mock.patch("rally.objects.task.db.task_delete") def test_delete(self, mock_delete): api.Task.delete(self.task_uuid) mock_delete.assert_called_once_with( self.task_uuid, status=consts.TaskStatus.FINISHED) @mock.patch("rally.objects.task.db.task_delete") def test_delete_force(self, mock_delete): api.Task.delete(self.task_uuid, force=True) mock_delete.assert_called_once_with(self.task_uuid, status=None)
class UseCommandsTestCase(test.TestCase): def setUp(self): super(UseCommandsTestCase, self).setUp() self.use = use.UseCommands() @mock.patch("rally.cmd.commands.use.db.deployment_get", side_effect=exceptions.DeploymentNotFound()) def test_deployment_use_no_args(self, mock_d_get): status = self.use.deployment() self.assertEqual(1, status) @mock.patch(MOD + "UseCommands._update_openrc_deployment_file") @mock.patch(MOD + "UseCommands._update_attribute_in_global_file") @mock.patch(MOD + "UseCommands._ensure_rally_configuration_dir_exists") @mock.patch(MOD + "db") def test_deployment_use_by_name(self, m_db, m_ercde, m_uaigf, m_uodf): fake_deployment = fakes.FakeDeployment(uuid="fake_uuid", admin="fake_endpoints") m_db.deployment_list.return_value = [fake_deployment] m_db.deployment_get.return_value = fake_deployment status = self.use.deployment(deployment="fake_name") self.assertIsNone(status) m_db.deployment_get.assert_called_once_with("fake_name") m_ercde.assert_called_once_with() m_uaigf.assert_called_once_with(envutils.ENV_DEPLOYMENT, "fake_uuid") m_uodf.assert_called_once_with("fake_uuid", "fake_endpoints") @mock.patch("os.remove") @mock.patch("os.symlink") @mock.patch(MOD + "db.deployment_get", return_value=fakes.FakeDeployment( uuid="593b683c-4b16-4b2b-a56b-e162bd60f10b")) @mock.patch("os.path.exists", return_value=True) @mock.patch(MOD + "fileutils.update_env_file") def test_deployment(self, mock_env, mock_path, mock_deployment, mock_symlink, mock_remove): deployment_id = mock_deployment.return_value["uuid"] mock_deployment.return_value["admin"] = { "auth_url": "fake_auth_url", "username": "******", "password": "******", "tenant_name": "fake_tenant_name", "region_name": None } with mock.patch("rally.cmd.commands.use.open", mock.mock_open(), create=True) as mock_file: self.use.deployment(deployment_id) self.assertEqual(2, mock_path.call_count) mock_env.assert_called_once_with( os.path.expanduser("~/.rally/globals"), "RALLY_DEPLOYMENT", "%s\n" % deployment_id) mock_file.return_value.write.assert_called_once_with( "export OS_AUTH_URL=fake_auth_url\n" "export OS_USERNAME=fake_username\n" "export OS_PASSWORD=fake_password\n" "export OS_TENANT_NAME=fake_tenant_name\n") mock_symlink.assert_called_once_with( os.path.expanduser("~/.rally/openrc-%s" % deployment_id), os.path.expanduser("~/.rally/openrc")) mock_remove.assert_called_once_with( os.path.expanduser("~/.rally/openrc")) @mock.patch(MOD + "db.deployment_get") def test_deployment_not_found(self, mock_deployment): deployment_id = "e87e4dca-b515-4477-888d-5f6103f13b42" mock_deployment.side_effect = exceptions.DeploymentNotFound( uuid=deployment_id) self.assertEqual(1, self.use.deployment(deployment_id)) @mock.patch(MOD + "fileutils._rewrite_env_file") @mock.patch(MOD + "db.task_get", return_value=True) def test_task(self, mock_task, mock_file): task_id = "80422553-5774-44bd-98ac-38bd8c7a0feb" self.use.task(task_id) mock_file.assert_called_once_with( os.path.expanduser("~/.rally/globals"), ["RALLY_TASK=%s\n" % task_id]) @mock.patch(MOD + "db.task_get") def test_task_not_found(self, mock_task): task_id = "ddc3f8ba-082a-496d-b18f-72cdf5c10a14" mock_task.side_effect = exceptions.TaskNotFound(uuid=task_id) self.assertRaises(exceptions.TaskNotFound, self.use.task, task_id)
def setUp(self): super(FuelEngineTestCase, self).setUp() self.deployment = fakes.FakeDeployment({"config": SAMPLE_CONFIG})