def _get_action_exec_db_model(self, params): actionexec_db = ActionExecutionDB() actionexec_db.status = 'initializing' actionexec_db.start_timestamp = datetime.datetime.utcnow() actionexec_db.action = ResourceReference(name=ParamsUtilsTest.action_db.name, pack=ParamsUtilsTest.action_db.pack).ref actionexec_db.parameters = params return actionexec_db
def _run_action(action_ref, parent_execution_id, params, wait_for_completion=True): execution = ActionExecutionDB(action=action_ref) execution.parameters = ActionChainRunner._cast_params(action_ref, params) execution.context = {'parent': str(parent_execution_id)} execution = action_service.schedule(execution) while (wait_for_completion and execution.status != ACTIONEXEC_STATUS_SUCCEEDED and execution.status != ACTIONEXEC_STATUS_FAILED): eventlet.sleep(1) execution = action_db_util.get_actionexec_by_id(execution.id) return execution
def _run_action(action_ref, parent_execution_id, params, wait_for_completion=True): execution = ActionExecutionDB(action=action_ref) execution.parameters = ActionChainRunner._cast_params( action_ref, params) execution.context = {'parent': str(parent_execution_id)} execution = action_service.schedule(execution) while (wait_for_completion and execution.status != ACTIONEXEC_STATUS_SUCCEEDED and execution.status != ACTIONEXEC_STATUS_FAILED): eventlet.sleep(1) execution = action_db_util.get_actionexec_by_id(execution.id) return execution
def test_basic_execution(self): action_ref = ResourceReference(name='local', pack='core') execution = ActionExecutionDB(action=action_ref.ref, parameters={'cmd': 'uname -a'}) execution = action_service.schedule(execution) execution = ActionExecution.get_by_id(str(execution.id)) self.assertEqual(execution.status, ACTIONEXEC_STATUS_SUCCEEDED) history = ActionExecutionHistory.get(execution__id=str(execution.id), raise_exception=True) self.assertDictEqual(history.trigger, {}) self.assertDictEqual(history.trigger_type, {}) self.assertDictEqual(history.trigger_instance, {}) self.assertDictEqual(history.rule, {}) action, _ = action_utils.get_action_by_dict({ 'name': action_ref.name, 'pack': action_ref.pack }) self.assertDictEqual(history.action, vars(ActionAPI.from_model(action))) runner = RunnerType.get_by_name(action.runner_type['name']) self.assertDictEqual(history.runner, vars(RunnerTypeAPI.from_model(runner))) execution = ActionExecution.get_by_id(str(execution.id)) self.assertDictEqual(history.execution, vars(ActionExecutionAPI.from_model(execution)))
def test_chained_executions(self): action_ref = ResourceReference(name='chain', pack='core') execution = ActionExecutionDB(action=action_ref.ref) execution = action_service.schedule(execution) execution = ActionExecution.get_by_id(str(execution.id)) self.assertEqual(execution.status, ACTIONEXEC_STATUS_SUCCEEDED) history = ActionExecutionHistory.get(execution__id=str(execution.id), raise_exception=True) action, _ = action_utils.get_action_by_dict({ 'name': action_ref.name, 'pack': action_ref.pack }) self.assertDictEqual(history.action, vars(ActionAPI.from_model(action))) runner = RunnerType.get_by_name(action.runner_type['name']) self.assertDictEqual(history.runner, vars(RunnerTypeAPI.from_model(runner))) execution = ActionExecution.get_by_id(str(execution.id)) self.assertDictEqual(history.execution, vars(ActionExecutionAPI.from_model(execution))) self.assertGreater(len(history.children), 0) for child in history.children: record = ActionExecutionHistory.get(id=child, raise_exception=True) self.assertEqual(record.parent, str(history.id)) self.assertEqual(record.action['name'], 'local') self.assertEqual(record.runner['name'], 'run-local')
def test_launch_workflow(self): MistralRunner.entry_point = mock.PropertyMock( return_value=WORKFLOW_YAML) execution = ActionExecutionDB(action='core.workflow-v2', parameters={'friend': 'Rocky'}) execution = action_service.schedule(execution) execution = ActionExecution.get_by_id(str(execution.id)) self.assertEqual(execution.status, ACTIONEXEC_STATUS_RUNNING)
def test_schedule_disabled_action(self): self.actiondb.enabled = False Action.add_or_update(self.actiondb) parameters = {'hosts': 'localhost', 'cmd': 'uname -a'} execution = ActionExecutionDB(action=ACTION_REF, parameters=parameters) self.assertRaises(ValueError, action_service.schedule, execution) self.actiondb.enabled = True Action.add_or_update(self.actiondb)
def _invoke_action(action, action_args, context=None): action_ref = action['ref'] execution = ActionExecutionDB(action=action_ref, context=context, parameters=action_args) execution = action_service.schedule(execution) return ({ 'id': str(execution.id) } if execution.status == ACTIONEXEC_STATUS_SCHEDULED else None)
def _get_failingaction_exec_db_model(self, params): actionexec_db = ActionExecutionDB() actionexec_db.status = 'initializing' actionexec_db.start_timestamp = datetime.datetime.now() actionexec_db.action = ResourceReference( name=RunnerContainerTest.failingaction_db.name, pack=RunnerContainerTest.failingaction_db.pack).ref actionexec_db.parameters = params actionexec_db.context = {'user': cfg.CONF.system_user.user} return actionexec_db
def setup_action_models(cls): action_db = ActionDB() action_db.name = 'action-1' action_db.description = 'awesomeness' action_db.enabled = True action_db.pack = 'wolfpack' action_db.entry_point = '' action_db.runner_type = {'name': 'test-runner'} action_db.parameters = { 'actionstr': {'type': 'string', 'position': 1, 'required': True}, 'actionint': {'type': 'number', 'default': 10, 'position': 0}, 'runnerdummy': {'type': 'string', 'default': 'actiondummy'} } ActionDBUtilsTestCase.action_db = Action.add_or_update(action_db) actionexec_db = ActionExecutionDB() actionexec_db.status = 'initializing' actionexec_db.start_timestamp = datetime.datetime.utcnow() actionexec_db.action = ResourceReference( name=ActionDBUtilsTestCase.action_db.name, pack=ActionDBUtilsTestCase.action_db.pack).ref params = { 'actionstr': 'foo', 'some_key_that_aint_exist_in_action_or_runner': 'bar', 'runnerint': 555 } actionexec_db.parameters = params ActionDBUtilsTestCase.actionexec_db = ActionExecution.add_or_update(actionexec_db)
def _get_action_exec_db_model(self, params): actionexec_db = ActionExecutionDB() actionexec_db.status = 'initializing' actionexec_db.start_timestamp = datetime.datetime.utcnow() actionexec_db.action = ResourceReference( name=ParamsUtilsTest.action_db.name, pack=ParamsUtilsTest.action_db.pack).ref actionexec_db.parameters = params return actionexec_db
def _get_failingaction_exec_db_model(self, params): actionexec_db = ActionExecutionDB() actionexec_db.status = 'initializing' actionexec_db.start_timestamp = datetime.datetime.now() actionexec_db.action = ResourceReference( name=RunnerContainerTest.failingaction_db.name, pack=RunnerContainerTest.failingaction_db.pack).ref actionexec_db.parameters = params actionexec_db.context = {'user': cfg.CONF.system_user.user} return actionexec_db
def test_schedule(self): context = {'user': USERNAME} parameters = {'hosts': 'localhost', 'cmd': 'uname -a'} request = ActionExecutionDB(action=ACTION_REF, context=context, parameters=parameters) request = action_service.schedule(request) execution = ActionExecution.get_by_id(str(request.id)) self.assertIsNotNone(execution) self.assertEqual(execution.id, request.id) action = '.'.join([self.actiondb.pack, self.actiondb.name]) actual_action = execution.action self.assertEqual(actual_action, action) self.assertEqual(execution.context['user'], request.context['user']) self.assertDictEqual(execution.parameters, request.parameters) self.assertEqual(execution.status, ACTIONEXEC_STATUS_SCHEDULED) # mongoengine DateTimeField stores datetime only up to milliseconds self.assertEqual(isotime.format(execution.start_timestamp, usec=False), isotime.format(request.start_timestamp, usec=False))
def test_callback(self): execution = ActionExecutionDB( action='core.local', parameters={'cmd': 'uname -a'}, callback={ 'source': 'mistral', 'url': 'http://localhost:8989/v2/tasks/12345' }) execution = action_service.schedule(execution) execution = ActionExecution.get_by_id(str(execution.id)) self.assertEqual(execution.status, ACTIONEXEC_STATUS_SUCCEEDED) requests.request.assert_called_with( 'PUT', execution.callback['url'], data=json.dumps({ 'state': 'SUCCESS', 'result': '{}' }), headers={'content-type': 'application/json'})
def test_basic_execution(self): execution = ActionExecutionDB(action='core.local', parameters={'cmd': 'uname -a'}) execution = action_service.schedule(execution) execution = ActionExecution.get_by_id(str(execution.id)) self.assertEqual(execution.status, ACTIONEXEC_STATUS_FAILED) history = ActionExecutionHistory.get(execution__id=str(execution.id), raise_exception=True) self.assertDictEqual(history.trigger, {}) self.assertDictEqual(history.trigger_type, {}) self.assertDictEqual(history.trigger_instance, {}) self.assertDictEqual(history.rule, {}) action = action_utils.get_action_by_ref('core.local') self.assertDictEqual(history.action, vars(ActionAPI.from_model(action))) runner = RunnerType.get_by_name(action.runner_type['name']) self.assertDictEqual(history.runner, vars(RunnerTypeAPI.from_model(runner))) execution = ActionExecution.get_by_id(str(execution.id)) self.assertDictEqual(history.execution, vars(ActionExecutionAPI.from_model(execution)))
def test_update_actionexecution_status_invalid(self): actionexec_db = ActionExecutionDB() actionexec_db.status = 'initializing' actionexec_db.start_timestamp = datetime.datetime.utcnow() actionexec_db.action = ResourceReference( name=ActionDBUtilsTestCase.action_db.name, pack=ActionDBUtilsTestCase.action_db.pack).ref params = { 'actionstr': 'foo', 'some_key_that_aint_exist_in_action_or_runner': 'bar', 'runnerint': 555 } actionexec_db.parameters = params actionexec_db = ActionExecution.add_or_update(actionexec_db) # Update by id. self.assertRaises(ValueError, action_db_utils.update_actionexecution_status, 'mea culpa', actionexec_id=actionexec_db.id)
def test_update_actionexecution_status(self): actionexec_db = ActionExecutionDB() actionexec_db.status = 'initializing' actionexec_db.start_timestamp = datetime.datetime.utcnow() actionexec_db.action = ResourceReference( name=ActionDBUtilsTestCase.action_db.name, pack=ActionDBUtilsTestCase.action_db.pack).ref params = { 'actionstr': 'foo', 'some_key_that_aint_exist_in_action_or_runner': 'bar', 'runnerint': 555 } actionexec_db.parameters = params actionexec_db = ActionExecution.add_or_update(actionexec_db) origactionexec_db = copy.copy(actionexec_db) # Update by id. newactionexec_db = action_db_utils.update_actionexecution_status( 'running', actionexec_id=actionexec_db.id) # Verify id didn't change. self.assertEqual(origactionexec_db.id, newactionexec_db.id) self.assertEqual(newactionexec_db.status, 'running')
def test_update_actionexecution_status_and_end_timestamp(self): actionexec_db = ActionExecutionDB() actionexec_db.status = 'initializing' actionexec_db.start_timestamp = datetime.datetime.utcnow() actionexec_db.action = ResourceReference( name=ActionDBUtilsTestCase.action_db.name, pack=ActionDBUtilsTestCase.action_db.pack).ref actionexec_db.parameters = {} actionexec_db = ActionExecution.add_or_update(actionexec_db) origactionexec_db = copy.copy(actionexec_db) # Update by id now = datetime.datetime.now() newactionexec_db = action_db_utils.update_actionexecution_status( status='running', end_timestamp=now, actionexec_id=actionexec_db.id) # Verify id didn't change and end_timestamp has been set self.assertEqual(origactionexec_db.id, newactionexec_db.id) self.assertEqual(newactionexec_db.status, 'running') self.assertEqual(newactionexec_db.end_timestamp, now)
def test_launch_workflow_when_workbook_not_exists(self): execution = ActionExecutionDB(action='core.workflow-v2', parameters={'friend': 'Rocky'}) execution = action_service.schedule(execution) execution = ActionExecution.get_by_id(str(execution.id)) self.assertEqual(execution.status, ACTIONEXEC_STATUS_RUNNING)
MOCK_TRIGGER = TriggerDB() MOCK_TRIGGER.id = 'trigger-test.id' MOCK_TRIGGER.name = 'trigger-test.name' MOCK_TRIGGER.pack = 'dummypack1' MOCK_TRIGGER_INSTANCE = TriggerInstanceDB() MOCK_TRIGGER_INSTANCE.id = 'triggerinstance-test' MOCK_TRIGGER_INSTANCE.trigger = reference.get_ref_from_model(MOCK_TRIGGER) MOCK_TRIGGER_INSTANCE.payload = {} MOCK_TRIGGER_INSTANCE.occurrence_time = datetime.datetime.utcnow() MOCK_ACTION = ActionDB() MOCK_ACTION.id = 'action-test-1.id' MOCK_ACTION.name = 'action-test-1.name' MOCK_ACTION_EXECUTION = ActionExecutionDB() MOCK_ACTION_EXECUTION.id = 'actionexec-test-1.id' MOCK_ACTION_EXECUTION.name = 'actionexec-test-1.name' MOCK_ACTION_EXECUTION.status = 'scheduled' MOCK_RULE_1 = RuleDB() MOCK_RULE_1.id = 'rule-test-1' MOCK_RULE_1.trigger = reference.get_str_resource_ref_from_model(MOCK_TRIGGER) MOCK_RULE_1.criteria = {} MOCK_RULE_1.action = ActionExecutionSpecDB() MOCK_RULE_1.action.ref = reference.get_ref_from_model(MOCK_ACTION) MOCK_RULE_1.enabled = True MOCK_RULE_2 = RuleDB() MOCK_RULE_2.id = 'rule-test-2' MOCK_RULE_2.trigger = reference.get_str_resource_ref_from_model(MOCK_TRIGGER)
def test_schedule_nonexistent_action(self): parameters = {'hosts': 'localhost', 'cmd': 'uname -a'} action_ref = ResourceReference(name='i.action', pack='default').ref execution = ActionExecutionDB(action=action_ref, parameters=parameters) self.assertRaises(ValueError, action_service.schedule, execution)
MOCK_TRIGGER = TriggerDB() MOCK_TRIGGER.id = 'trigger-test.id' MOCK_TRIGGER.name = 'trigger-test.name' MOCK_TRIGGER.pack = 'dummypack1' MOCK_TRIGGER_INSTANCE = TriggerInstanceDB() MOCK_TRIGGER_INSTANCE.id = 'triggerinstance-test' MOCK_TRIGGER_INSTANCE.trigger = reference.get_ref_from_model(MOCK_TRIGGER) MOCK_TRIGGER_INSTANCE.payload = {} MOCK_TRIGGER_INSTANCE.occurrence_time = datetime.datetime.utcnow() MOCK_ACTION = ActionDB() MOCK_ACTION.id = 'action-test-1.id' MOCK_ACTION.name = 'action-test-1.name' MOCK_ACTION_EXECUTION = ActionExecutionDB() MOCK_ACTION_EXECUTION.id = 'actionexec-test-1.id' MOCK_ACTION_EXECUTION.name = 'actionexec-test-1.name' MOCK_ACTION_EXECUTION.status = 'scheduled' MOCK_RULE_1 = RuleDB() MOCK_RULE_1.id = 'rule-test-1' MOCK_RULE_1.trigger = reference.get_str_resource_ref_from_model(MOCK_TRIGGER) MOCK_RULE_1.criteria = {} MOCK_RULE_1.action = ActionExecutionSpecDB() MOCK_RULE_1.action.ref = reference.get_ref_from_model(MOCK_ACTION) MOCK_RULE_1.enabled = True MOCK_RULE_2 = RuleDB() MOCK_RULE_2.id = 'rule-test-2' MOCK_RULE_2.trigger = reference.get_str_resource_ref_from_model(MOCK_TRIGGER)
def test_schedule_invalid_parameters(self): parameters = {'hosts': 'localhost', 'cmd': 'uname -a', 'a': 123} execution = ActionExecutionDB(action=ACTION_REF, parameters=parameters) self.assertRaises(jsonschema.ValidationError, action_service.schedule, execution)