def validate(self, deployment, config, task_instance=None, task=None): """Validate a task config against specified deployment. :param deployment: UUID or name of the deployment (will be ignored in case of transmitting task_instance or task arguments) :param config: a dict with a task configuration :param task_instance: DEPRECATED. Use "task" argument to transmit task uuid instead """ if task_instance is not None: LOG.warning("Transmitting task object in `task validate` is " "deprecated since Rally 0.10. To use pre-created " "task, transmit task UUID instead via `task` " "argument.") task = objects.Task.get(task_instance["uuid"]) deployment = task["deployment_uuid"] elif task: task = objects.Task.get(task) deployment = task["deployment_uuid"] else: task = objects.Task(deployment_uuid=deployment, temporary=True) deployment = objects.Deployment.get(deployment) benchmark_engine = engine.TaskEngine(config, task, deployment) benchmark_engine.validate()
def start(cls, deployment, config, task=None, abort_on_sla_failure=False): """Start a task. Task is a list of benchmarks that will be called one by one, results of execution will be stored in DB. :param deployment: UUID or name of the deployment :param config: a dict with a task configuration :param task: Task object. If None, it will be created :param abort_on_sla_failure: if True, the execution of a benchmark scenario will stop when any SLA check for it fails """ deployment = objects.Deployment.get(deployment) task = task or objects.Task(deployment_uuid=deployment["uuid"]) if task.is_temporary: raise ValueError( _("Unable to run a temporary task. Please check your code.")) LOG.info("Benchmark Task %s on Deployment %s" % (task["uuid"], deployment["uuid"])) benchmark_engine = engine.BenchmarkEngine( config, task, admin=deployment["admin"], users=deployment["users"], abort_on_sla_failure=abort_on_sla_failure) try: benchmark_engine.run() except Exception: deployment.update_status(consts.DeployStatus.DEPLOY_INCONSISTENT) raise
def test_create_and_delete_status(self, mock_task_create, mock_task_delete): mock_task_create.return_value = self.task task = objects.Task() task.delete(status=consts.TaskStatus.FINISHED) mock_task_delete.assert_called_once_with( self.task["uuid"], status=consts.TaskStatus.FINISHED)
def start(cls, deployment, config, task=None, abort_on_sla_failure=False): """Start a task. Task is a list of benchmarks that will be called one by one, results of execution will be stored in DB. :param deployment: UUID or name of the deployment :param config: a dict with a task configuration :param task: Task object. If None, it will be created :param abort_on_sla_failure: if True, the execution of a benchmark scenario will stop when any SLA check for it fails """ deployment = objects.Deployment.get(deployment) task = task or objects.Task(deployment_uuid=deployment["uuid"]) LOG.info("Benchmark Task %s on Deployment %s" % (task["uuid"], deployment["uuid"])) benchmark_engine = engine.BenchmarkEngine( config, task, admin=deployment["admin"], users=deployment["users"], abort_on_sla_failure=abort_on_sla_failure) try: benchmark_engine.validate() benchmark_engine.run() except exceptions.InvalidTaskException: # NOTE(boris-42): We don't log anything, because it's a normal # situation when a user puts a wrong config. pass except Exception: deployment.update_status(consts.DeployStatus.DEPLOY_INCONSISTENT) raise
def start(self, deployment, config, task=None, abort_on_sla_failure=False): """Validate and start a task. Task is a list of benchmarks that will be called one by one, results of execution will be stored in DB. :param deployment: UUID or name of the deployment (will be ignored in case of transmitting existing task) :param config: a dict with a task configuration :param task: Task UUID to use pre-created task. If None, new task will be created :param abort_on_sla_failure: If set to True, the task execution will stop when any SLA check for it fails """ if task and isinstance(task, objects.Task): LOG.warning("Transmitting task object in `task start` is " "deprecated since Rally 0.10. To use pre-created " "task, transmit task UUID instead.") if task.is_temporary: raise ValueError(_( "Unable to run a temporary task. Please check your code.")) task = objects.Task.get(task["uuid"]) elif task is not None: task = objects.Task.get(task) if task is not None: deployment = task["deployment_uuid"] deployment = objects.Deployment.get(deployment) if deployment["status"] != consts.DeployStatus.DEPLOY_FINISHED: raise exceptions.DeploymentNotFinishedStatus( name=deployment["name"], uuid=deployment["uuid"], status=deployment["status"]) if task is None: task = objects.Task(deployment_uuid=deployment["uuid"]) benchmark_engine = engine.TaskEngine( config, task, deployment, abort_on_sla_failure=abort_on_sla_failure) benchmark_engine.validate() LOG.info("Task %s config is valid." % task["uuid"]) LOG.info("Benchmark Task %s on Deployment %s" % (task["uuid"], deployment["uuid"])) benchmark_engine = engine.TaskEngine( config, task, deployment, abort_on_sla_failure=abort_on_sla_failure) try: benchmark_engine.run() except Exception: deployment.update_status(consts.DeployStatus.DEPLOY_INCONSISTENT) raise return task["uuid"], task.get_status(task["uuid"])
def test_update(self, mock_task_create, mock_task_update): mock_task_create.return_value = self.task mock_task_update.return_value = {"opt": "val2"} deploy = objects.Task(opt="val1") deploy._update({"opt": "val2"}) mock_task_update.assert_called_once_with(self.task["uuid"], {"opt": "val2"}) self.assertEqual(deploy["opt"], "val2")
def test_add_subtask(self, mock_subtask): task = objects.Task(task=self.task) subtask = task.add_subtask(title="foo") mock_subtask.assert_called_once_with(self.task["uuid"], title="foo", context=None, description=None) self.assertIs(subtask, mock_subtask.return_value)
def test_set_failed(self, mock_task_update): mock_task_update.return_value = self.task task = objects.Task(task=self.task) task.set_failed() mock_task_update.assert_called_once_with( self.task["uuid"], {"status": consts.TaskStatus.FAILED, "verification_log": "\"\""}, )
def test_update_verification_log(self, mock_task_update): mock_task_update.return_value = self.task task = objects.Task(task=self.task) task.update_verification_log({"a": "fake"}) mock_task_update.assert_called_once_with( self.task["uuid"], {"verification_log": json.dumps({"a": "fake"})} )
def test_update_status(self, mock_task_update): mock_task_update.return_value = self.task task = objects.Task(task=self.task) task.update_status(consts.TaskStatus.FINISHED) mock_task_update.assert_called_once_with( self.task["uuid"], {"status": consts.TaskStatus.FINISHED}, )
def test_abort_with_init_and_verifying_states(self, soft, status): task = objects.Task(mock.MagicMock(), fake=True) task.get_status = mock.MagicMock(side_effect=(status, status, "running")) task._update_status_in_abort = mock.MagicMock() self.assertRaises(exceptions.RallyException, task.abort, soft) self.assertEqual(1, task.get_status.call_count) self.assertFalse(task._update_status_in_abort.called)
def test_update_verification_log(self, mock_task_update): mock_task_update.return_value = self.task task = objects.Task(task=self.task) task.set_validation_failed({"a": "fake"}) mock_task_update.assert_called_once_with( self.task["uuid"], {"status": consts.TaskStatus.VALIDATION_FAILED, "validation_result": {"a": "fake"}} )
def test_abort_with_finished_states(self, soft, status): task = objects.Task(mock.MagicMock(), fake=True) task.get_status = mock.MagicMock(return_value=status) task.update_status = mock.MagicMock() self.assertRaises(exceptions.RallyException, task.abort, soft) self.assertEqual(1, task.get_status.call_count) self.assertFalse(task.update_status.called)
def test_set_failed(self, mock_task_update): mock_task_update.return_value = self.task task = objects.Task(task=self.task) task.set_failed("foo_type", "foo_error_message", "foo_trace") mock_task_update.assert_called_once_with( self.task["uuid"], {"status": consts.TaskStatus.CRASHED, "validation_result": {"etype": "foo_type", "msg": "foo_error_message", "trace": "foo_trace"}}, )
def validate(cls, deployment, config, task_instance=None): """Validate a task config against specified deployment. :param deployment: UUID or name of the deployment :param config: a dict with a task configuration """ deployment = objects.Deployment.get(deployment) task = task_instance or objects.Task( deployment_uuid=deployment["uuid"], temporary=True) benchmark_engine = engine.TaskEngine(config, task, deployment) benchmark_engine.validate()
def test_get_detailed(self, mock_task_get_detailed): task = objects.Task(task=self.task) mock_task_get_detailed.return_value = { "results": [{ "created_at": dt.datetime.now(), "updated_at": dt.datetime.now() }] } task_detailed = task.get_detailed(task_id="task_id") mock_task_get_detailed.assert_called_once_with("task_id") self.assertEqual(mock_task_get_detailed.return_value, task_detailed)
def test_result_has_valid_schema(self, mock_validate_output, mock_log, data, expected=False, validate_output_return_value=None, validate_output_calls=None): task = objects.Task(task=self.task) mock_validate_output.return_value = validate_output_return_value self.assertEqual(expected, task.result_has_valid_schema(data), message=repr(data)) if validate_output_calls: mock_validate_output.assert_has_calls( [mock.call(*args) for args in validate_output_calls], any_order=True)
def create(cls, deployment, tag): """Create a task without starting it. Task is a list of benchmarks that will be called one by one, results of execution will be stored in DB. :param deployment: UUID or name of the deployment :param tag: tag for this task :returns: Task object """ deployment_uuid = objects.Deployment.get(deployment)["uuid"] return objects.Task(deployment_uuid=deployment_uuid, tag=tag)
def validate(cls, deployment, config): """Validate a task config against specified deployment. :param deployment: UUID or name of the deployment :param config: a dict with a task configuration """ deployment = objects.Deployment.get(deployment) task = objects.Task(deployment_uuid=deployment["uuid"], fake=True) benchmark_engine = engine.BenchmarkEngine(config, task, admin=deployment["admin"], users=deployment["users"]) benchmark_engine.validate()
def test_update_status(self, mock_task_update, mock_task_update_status, status, allowed_statuses): task = objects.Task(task=self.task) task.update_status(consts.TaskStatus.FINISHED, allowed_statuses) if allowed_statuses: self.assertFalse(mock_task_update.called) mock_task_update_status.assert_called_once_with( self.task["uuid"], consts.TaskStatus.FINISHED, allowed_statuses) else: self.assertFalse(mock_task_update_status.called) mock_task_update.assert_called_once_with( self.task["uuid"], {"status": consts.TaskStatus.FINISHED}, )
def test_abort_with_running_state(self, soft): task = objects.Task(mock.MagicMock(), fake=True) task.get_status = mock.MagicMock(return_value="running") task.update_status = mock.MagicMock() task.abort(soft) if soft: status = consts.TaskStatus.SOFT_ABORTING else: status = consts.TaskStatus.ABORTING task.update_status.assert_called_once_with( status, allowed_statuses=(consts.TaskStatus.RUNNING, consts.TaskStatus.SOFT_ABORTING))
def test_set_failed(self, mock_task_update): mock_task_update.return_value = self.task task = objects.Task(task=self.task) task.set_failed("foo_type", "foo_error_message", "foo_trace") mock_task_update.assert_called_once_with( self.task["uuid"], { "status": consts.TaskStatus.FAILED, "verification_log": json.dumps({ "etype": "foo_type", "msg": "foo_error_message", "trace": "foo_trace" }) }, )
def test_to_dict(self, mock_env_get): workloads = [{"created_at": dt.datetime.now(), "updated_at": dt.datetime.now()}] self.task.update({"env_uuid": "deployment_uuid", "deployment_uuid": "deployment_uuid", "created_at": dt.datetime.now(), "updated_at": dt.datetime.now()}) mock_env_get.return_value = {"name": "deployment_name"} task = objects.Task(task=self.task) serialized_task = task.to_dict() mock_env_get.assert_called_once_with(self.task["env_uuid"]) self.assertEqual(self.task, serialized_task) self.task["subtasks"] = [{"workloads": workloads}]
def create(cls, deployment, tag): """Create a task without starting it. Task is a list of benchmarks that will be called one by one, results of execution will be stored in DB. :param deployment: UUID or name of the deployment :param tag: tag for this task :returns: Task object """ deployment = objects.Deployment.get(deployment) if deployment["status"] != consts.DeployStatus.DEPLOY_FINISHED: raise exceptions.DeploymentNotFinishedStatus( name=deployment["name"], uuid=deployment["uuid"], status=deployment["status"]) return objects.Task(deployment_uuid=deployment["uuid"], tag=tag)
def test_to_dict(self, mock_get_results, mock_deployment_get): results = [{ "created_at": dt.datetime.now(), "updated_at": dt.datetime.now() }] self.task.update({ "deployment_uuid": "deployment_uuid", "deployment_name": "deployment_name", "created_at": dt.datetime.now(), "updated_at": dt.datetime.now(), "results": results }) mock_get_results.return_value = results mock_deployment_get.return_value = {"name": "deployment_name"} task = objects.Task(task=self.task) serialized_task = task.to_dict() mock_get_results.assert_called_once_with() mock_deployment_get.assert_called_once_with( self.task["deployment_uuid"]) self.assertEqual(self.task, serialized_task)
def test_get_status(self, mock_task_get_status): task = objects.Task(task=self.task) status = task.get_status(task["uuid"]) self.assertEqual(status, mock_task_get_status.return_value)
def test_init_with_fake_true(self, mock_task_create, mock_uuid4): task = objects.Task(temporary=True) self.assertFalse(mock_task_create.called) self.assertTrue(mock_uuid4.called) self.assertEqual(task["uuid"], mock_uuid4.return_value)
def test_init_without_create(self, mock_task_create): task = objects.Task(task=self.task) self.assertFalse(mock_task_create.called) self.assertEqual(task["uuid"], self.task["uuid"])
def test_init_with_create(self, mock_task_create): mock_task_create.return_value = self.task task = objects.Task(status=consts.TaskStatus.FAILED) mock_task_create.assert_called_once_with( {"status": consts.TaskStatus.FAILED}) self.assertEqual(task["uuid"], self.task["uuid"])
def test_append_results(self, mock_task_result_create): task = objects.Task(task=self.task) task.append_results("opt", "val") mock_task_result_create.assert_called_once_with( self.task["uuid"], "opt", "val")