def setUpClass(cls): super(TestActionExecutionService, cls).setUpClass() cls.runner = RunnerTypeAPI(**RUNNER) cls.runnerdb = RunnerType.add_or_update( RunnerTypeAPI.to_model(cls.runner)) runner_api = RunnerTypeAPI(**RUNNER_ACTION_CHAIN) RunnerType.add_or_update(RunnerTypeAPI.to_model(runner_api)) cls.actions = { ACTION['name']: ActionAPI(**ACTION), ACTION_WORKFLOW['name']: ActionAPI(**ACTION_WORKFLOW), ACTION_OVR_PARAM['name']: ActionAPI(**ACTION_OVR_PARAM), ACTION_OVR_PARAM_MUTABLE['name']: ActionAPI(**ACTION_OVR_PARAM_MUTABLE), ACTION_OVR_PARAM_IMMUTABLE['name']: ActionAPI(**ACTION_OVR_PARAM_IMMUTABLE), ACTION_OVR_PARAM_BAD_ATTR['name']: ActionAPI(**ACTION_OVR_PARAM_BAD_ATTR), ACTION_OVR_PARAM_BAD_ATTR_NOOP['name']: ActionAPI(**ACTION_OVR_PARAM_BAD_ATTR_NOOP) } cls.actiondbs = { name: Action.add_or_update(ActionAPI.to_model(action)) for name, action in six.iteritems(cls.actions) } cls.container = RunnerContainer()
def setup_runner(cls): test_runner = { 'name': 'test-runner', 'description': 'A test runner.', 'enabled': True, 'runner_parameters': { 'runnerstr': { 'description': 'Foo str param.', 'type': 'string', 'default': 'defaultfoo' }, 'runnerint': { 'description': 'Foo int param.', 'type': 'number' }, 'runnerdummy': { 'description': 'Dummy param.', 'type': 'string', 'default': 'runnerdummy' } }, 'runner_module': 'tests.test_runner' } runnertype_api = RunnerTypeAPI(**test_runner) ActionDBUtilsTestCase.runnertype_db = RunnerType.add_or_update( RunnerTypeAPI.to_model(runnertype_api))
def _setup_runner_models(cls): test_runner = { 'name': 'test-runner', 'description': 'A test runner.', 'enabled': True, 'runner_parameters': { 'runnerstr': { 'description': 'Foo str param.', 'type': 'string', 'default': 'defaultfoo' }, 'runnerint': { 'description': 'Foo int param.', 'type': 'number' }, 'runnerdummy': { 'description': 'Dummy param.', 'type': 'string', 'default': 'runnerdummy' }, 'runnerimmutable': { 'description': 'Immutable param.', 'type': 'string', 'default': 'runnerimmutable', 'immutable': True } }, 'runner_module': 'tests.test_runner' } runnertype_api = RunnerTypeAPI(**test_runner) ParamsUtilsTest.runnertype_db = RunnerTypeAPI.to_model(runnertype_api)
def register_runner_types(): LOG.debug('Start : register default RunnerTypes.') for runnertype in RUNNER_TYPES: try: runnertype_db = get_runnertype_by_name(runnertype['name']) update = True except StackStormDBObjectNotFoundError: runnertype_db = None update = False runnertype_api = RunnerTypeAPI(**runnertype) runnertype_api.validate() runner_type_model = RunnerTypeAPI.to_model(runnertype_api) if runnertype_db: runner_type_model.id = runnertype_db.id try: runnertype_db = RunnerType.add_or_update(runner_type_model) extra = {'runnertype_db': runnertype_db} if update: LOG.audit('RunnerType updated. RunnerType %s', runnertype_db, extra=extra) else: LOG.audit('RunnerType created. RunnerType %s', runnertype_db, extra=extra) except Exception: LOG.exception('Unable to register runner type %s.', runnertype['name']) LOG.debug('End : register default RunnerTypes.')
def setUpClass(cls): super(TestActionExecutionService, cls).setUpClass() cls.runner = RunnerTypeAPI(**RUNNER) cls.runnerdb = RunnerType.add_or_update( RunnerTypeAPI.to_model(cls.runner)) cls.action = ActionAPI(**ACTION) cls.actiondb = Action.add_or_update(ActionAPI.to_model(cls.action))
def setup_runner(cls): test_runner = { "name": "test-runner", "description": "A test runner.", "enabled": True, "runner_parameters": { "runnerstr": { "description": "Foo str param.", "type": "string", "default": "defaultfoo", }, "runnerint": { "description": "Foo int param.", "type": "number" }, "runnerdummy": { "description": "Dummy param.", "type": "string", "default": "runnerdummy", }, }, "runner_module": "tests.test_runner", } runnertype_api = RunnerTypeAPI(**test_runner) ActionDBUtilsTestCase.runnertype_db = RunnerType.add_or_update( RunnerTypeAPI.to_model(runnertype_api))
def register_runner(runner_type, experimental): # For backward compatibility reasons, we also register runners under the old names runner_names = [runner_type['name']] + runner_type.get('aliases', []) for runner_name in runner_names: runner_type['name'] = runner_name runner_experimental = runner_type.get('experimental', False) if runner_experimental and not experimental: LOG.debug('Skipping experimental runner "%s"' % (runner_name)) continue # Remove additional, non db-model attributes non_db_attributes = ['experimental', 'aliases'] for attribute in non_db_attributes: if attribute in runner_type: del runner_type[attribute] try: runner_type_db = get_runnertype_by_name(runner_name) update = True except StackStormDBObjectNotFoundError: runner_type_db = None update = False # Note: We don't want to overwrite "enabled" attribute which is already in the database # (aka we don't want to re-enable runner which has been disabled by the user) if runner_type_db and runner_type_db['enabled'] != runner_type[ 'enabled']: runner_type['enabled'] = runner_type_db['enabled'] # If package is not provided, assume it's the same as module name for backward # compatibility reasons if not runner_type.get('runner_package', None): runner_type['runner_package'] = runner_type['runner_module'] runner_type_api = RunnerTypeAPI(**runner_type) runner_type_api.validate() runner_type_model = RunnerTypeAPI.to_model(runner_type_api) if runner_type_db: runner_type_model.id = runner_type_db.id try: runner_type_db = RunnerType.add_or_update(runner_type_model) extra = {'runner_type_db': runner_type_db} if update: LOG.audit('RunnerType updated. RunnerType %s', runner_type_db, extra=extra) else: LOG.audit('RunnerType created. RunnerType %s', runner_type_db, extra=extra) except Exception: LOG.exception('Unable to register runner type %s.', runner_type['name']) return 0 return 1
def setUpClass(cls): super(TestActionAPIValidator, cls).setUpClass() runner_api_dict = fixture.ARTIFACTS['runners']['run-local'] runner_api = RunnerTypeAPI(**runner_api_dict) runner_model = RunnerTypeAPI.to_model(runner_api) RunnerType.add_or_update(runner_model)
def setUpClass(cls): super(ActionParamsUtilsTest, cls).setUpClass() cls.runnertype = RunnerTypeAPI( **FIXTURES['runners']['testrunner1.yaml']) cls.runnertype_db = RunnerType.add_or_update( RunnerTypeAPI.to_model(cls.runnertype)) cls.action = ActionAPI(**FIXTURES['actions']['action1.yaml']) cls.action_db = Action.add_or_update(ActionAPI.to_model(cls.action))
def setUpClass(cls): super(TestStreamController, cls).setUpClass() instance = RunnerTypeAPI(**RUNNER_TYPE_1) RunnerType.add_or_update(RunnerTypeAPI.to_model(instance)) instance = ActionAPI(**ACTION_1) Action.add_or_update(ActionAPI.to_model(instance))
def register_runner_types(experimental=False): """ :param experimental: True to also register experimental runners. :type experimental: ``bool`` """ LOG.debug('Start : register default RunnerTypes.') for runner_type in RUNNER_TYPES: runner_type = copy.deepcopy(runner_type) # For backward compatibility reasons, we also register runners under the old names runner_names = [runner_type['name']] + runner_type.get('aliases', []) for runner_name in runner_names: runner_type['name'] = runner_name runner_experimental = runner_type.get('experimental', False) if runner_experimental and not experimental: LOG.debug('Skipping experimental runner "%s"' % (runner_name)) continue # Remove additional, non db-model attributes non_db_attributes = ['experimental', 'aliases'] for attribute in non_db_attributes: if attribute in runner_type: del runner_type[attribute] try: runner_type_db = get_runnertype_by_name(runner_name) update = True except StackStormDBObjectNotFoundError: runner_type_db = None update = False runner_type_api = RunnerTypeAPI(**runner_type) runner_type_api.validate() runner_type_model = RunnerTypeAPI.to_model(runner_type_api) if runner_type_db: runner_type_model.id = runner_type_db.id try: runner_type_db = RunnerType.add_or_update(runner_type_model) extra = {'runner_type_db': runner_type_db} if update: LOG.audit('RunnerType updated. RunnerType %s', runner_type_db, extra=extra) else: LOG.audit('RunnerType created. RunnerType %s', runner_type_db, extra=extra) except Exception: LOG.exception('Unable to register runner type %s.', runner_type['name']) LOG.debug('End : register default RunnerTypes.')
def register_runner_types(experimental=False): """ :param experimental: True to also register experimental runners. :type experimental: ``bool`` """ LOG.debug('Start : register default RunnerTypes.') for runner_type in RUNNER_TYPES: runner_type = copy.deepcopy(runner_type) # For backward compatibility reasons, we also register runners under the old names runner_names = [runner_type['name']] + runner_type.get('aliases', []) for runner_name in runner_names: runner_type['name'] = runner_name runner_experimental = runner_type.get('experimental', False) if runner_experimental and not experimental: LOG.debug('Skipping experimental runner "%s"' % (runner_name)) continue # Remove additional, non db-model attributes non_db_attributes = ['experimental', 'aliases'] for attribute in non_db_attributes: if attribute in runner_type: del runner_type[attribute] try: runner_type_db = get_runnertype_by_name(runner_name) update = True except StackStormDBObjectNotFoundError: runner_type_db = None update = False # Note: We don't want to overwrite "enabled" attribute which is already in the database # (aka we don't want to re-enable runner which has been disabled by the user) if runner_type_db and runner_type_db['enabled'] != runner_type['enabled']: runner_type['enabled'] = runner_type_db['enabled'] runner_type_api = RunnerTypeAPI(**runner_type) runner_type_api.validate() runner_type_model = RunnerTypeAPI.to_model(runner_type_api) if runner_type_db: runner_type_model.id = runner_type_db.id try: runner_type_db = RunnerType.add_or_update(runner_type_model) extra = {'runner_type_db': runner_type_db} if update: LOG.audit('RunnerType updated. RunnerType %s', runner_type_db, extra=extra) else: LOG.audit('RunnerType created. RunnerType %s', runner_type_db, extra=extra) except Exception: LOG.exception('Unable to register runner type %s.', runner_type['name']) LOG.debug('End : register default RunnerTypes.')
def setUpClass(cls): super(SchedulerTest, cls).setUpClass() for _, fixture in six.iteritems(FIXTURES['runners']): instance = RunnerTypeAPI(**fixture) RunnerType.add_or_update(RunnerTypeAPI.to_model(instance)) for _, fixture in six.iteritems(FIXTURES['actions']): instance = ActionAPI(**fixture) Action.add_or_update(ActionAPI.to_model(instance))
def register_runner(runner_type, experimental): # For backward compatibility reasons, we also register runners under the old names runner_names = [runner_type['name']] + runner_type.get('aliases', []) for runner_name in runner_names: runner_type['name'] = runner_name runner_experimental = runner_type.get('experimental', False) if runner_experimental and not experimental: LOG.debug('Skipping experimental runner "%s"' % (runner_name)) continue # Remove additional, non db-model attributes non_db_attributes = ['experimental', 'aliases'] for attribute in non_db_attributes: if attribute in runner_type: del runner_type[attribute] try: runner_type_db = get_runnertype_by_name(runner_name) update = True except StackStormDBObjectNotFoundError: runner_type_db = None update = False # Note: We don't want to overwrite "enabled" attribute which is already in the database # (aka we don't want to re-enable runner which has been disabled by the user) if runner_type_db and runner_type_db['enabled'] != runner_type['enabled']: runner_type['enabled'] = runner_type_db['enabled'] # If package is not provided, assume it's the same as module name for backward # compatibility reasons if not runner_type.get('runner_package', None): runner_type['runner_package'] = runner_type['runner_module'] runner_type_api = RunnerTypeAPI(**runner_type) runner_type_api.validate() runner_type_model = RunnerTypeAPI.to_model(runner_type_api) if runner_type_db: runner_type_model.id = runner_type_db.id try: runner_type_db = RunnerType.add_or_update(runner_type_model) extra = {'runner_type_db': runner_type_db} if update: LOG.audit('RunnerType updated. RunnerType %s', runner_type_db, extra=extra) else: LOG.audit('RunnerType created. RunnerType %s', runner_type_db, extra=extra) except Exception: LOG.exception('Unable to register runner type %s.', runner_type['name']) return 0 return 1
def setUpClass(cls): super(DSLTransformTestCase, cls).setUpClass() runners_registrar.register_runner_types() for _, fixture in six.iteritems(FIXTURES['runners']): instance = RunnerTypeAPI(**fixture) RunnerType.add_or_update(RunnerTypeAPI.to_model(instance)) for _, fixture in six.iteritems(FIXTURES['actions']): instance = ActionAPI(**fixture) Action.add_or_update(ActionAPI.to_model(instance))
def setUpClass(cls): super(MistralValidationTest, cls).setUpClass() for _, fixture in six.iteritems(FIXTURES['runners']): instance = RunnerTypeAPI(**fixture) RunnerType.add_or_update(RunnerTypeAPI.to_model(instance)) for _, fixture in six.iteritems(FIXTURES['actions']): instance = ActionAPI(**fixture) Action.add_or_update(ActionAPI.to_model(instance)) cls.validator = wf_validation_utils.get_validator()
def register_runner_types(experimental=False): """ :param experimental: True to also register experimental runners. :type experimental: ``bool`` """ LOG.debug("Start : register default RunnerTypes.") for runner_type in RUNNER_TYPES: runner_type = copy.deepcopy(runner_type) # For backward compatibility reasons, we also register runners under the old names runner_names = [runner_type["name"]] + runner_type.get("aliases", []) for runner_name in runner_names: runner_type["name"] = runner_name runner_experimental = runner_type.get("experimental", False) if runner_experimental and not experimental: LOG.debug('Skipping experimental runner "%s"' % (runner_name)) continue # Remove additional, non db-model attributes non_db_attributes = ["experimental", "aliases"] for attribute in non_db_attributes: if attribute in runner_type: del runner_type[attribute] try: runner_type_db = get_runnertype_by_name(runner_name) update = True except StackStormDBObjectNotFoundError: runner_type_db = None update = False runner_type_api = RunnerTypeAPI(**runner_type) runner_type_api.validate() runner_type_model = RunnerTypeAPI.to_model(runner_type_api) if runner_type_db: runner_type_model.id = runner_type_db.id try: runner_type_db = RunnerType.add_or_update(runner_type_model) extra = {"runner_type_db": runner_type_db} if update: LOG.audit("RunnerType updated. RunnerType %s", runner_type_db, extra=extra) else: LOG.audit("RunnerType created. RunnerType %s", runner_type_db, extra=extra) except Exception: LOG.exception("Unable to register runner type %s.", runner_type["name"]) LOG.debug("End : register default RunnerTypes.")
def setup_runner(cls): test_runner = { 'name': 'test-runner', 'description': 'A test runner.', 'enabled': True, 'runner_parameters': { 'runnerstr': { 'description': 'Foo str param.', 'type': 'string', 'default': 'defaultfoo' }, 'runnerint': { 'description': 'Foo int param.', 'type': 'number' }, 'runnerdummy': { 'description': 'Dummy param.', 'type': 'string', 'default': 'runnerdummy' }, 'runnerimmutable': { 'description': 'Immutable param.', 'type': 'string', 'default': 'runnerimmutable', 'immutable': True } }, 'runner_module': 'tests.test_runner' } runnertype_api = RunnerTypeAPI(**test_runner) RunnerContainerTest.runnertype_db = RunnerType.add_or_update( RunnerTypeAPI.to_model(runnertype_api)) test_failingrunner = { 'name': 'test-failingrunner', 'description': 'A failing test runner.', 'enabled': True, 'runner_parameters': { 'raise': { 'description': 'Foo str param.', 'type': 'boolean', 'default': True, 'immutable': True } }, 'runner_module': 'tests.test_runner' } runnertype_api = RunnerTypeAPI(**test_failingrunner) RunnerContainerTest.runnertype_db = RunnerType.add_or_update( RunnerTypeAPI.to_model(runnertype_api))
def test_basic_execution(self): liveaction = LiveActionDB(action="executions.local", parameters={"cmd": "uname -a"}) liveaction, _ = action_service.request(liveaction) liveaction = self._wait_on_status( liveaction, action_constants.LIVEACTION_STATUS_FAILED) execution = self._get_action_execution(liveaction__id=str( liveaction.id), raise_exception=True) self.assertDictEqual(execution.trigger, {}) self.assertDictEqual(execution.trigger_type, {}) self.assertDictEqual(execution.trigger_instance, {}) self.assertDictEqual(execution.rule, {}) action = action_utils.get_action_by_ref("executions.local") self.assertDictEqual(execution.action, vars(ActionAPI.from_model(action))) runner = RunnerType.get_by_name(action.runner_type["name"]) self.assertDictEqual(execution.runner, vars(RunnerTypeAPI.from_model(runner))) liveaction = LiveAction.get_by_id(str(liveaction.id)) self.assertEqual(execution.start_timestamp, liveaction.start_timestamp) # NOTE: Timestamp of liveaction and execution may be a bit different, depending on how long # it takes to persist each object in the database self.assertEqual( execution.end_timestamp.replace(microsecond=0), liveaction.end_timestamp.replace(microsecond=0), ) self.assertEqual(execution.result, liveaction.result) self.assertEqual(execution.status, liveaction.status) self.assertEqual(execution.context, liveaction.context) self.assertEqual(execution.liveaction["callback"], liveaction.callback) self.assertEqual(execution.liveaction["action"], liveaction.action)
def test_chained_executions(self): liveaction = LiveActionDB(action='core.chain') liveaction, _ = action_service.request(liveaction) liveaction = LiveAction.get_by_id(str(liveaction.id)) self.assertEqual(liveaction.status, action_constants.LIVEACTION_STATUS_FAILED) execution = self._get_action_execution(liveaction__id=str(liveaction.id), raise_exception=True) action = action_utils.get_action_by_ref('core.chain') self.assertDictEqual(execution.action, vars(ActionAPI.from_model(action))) runner = RunnerType.get_by_name(action.runner_type['name']) self.assertDictEqual(execution.runner, vars(RunnerTypeAPI.from_model(runner))) liveaction = LiveAction.get_by_id(str(liveaction.id)) self.assertEqual(execution.start_timestamp, liveaction.start_timestamp) self.assertEqual(execution.end_timestamp, liveaction.end_timestamp) self.assertEqual(execution.result, liveaction.result) self.assertEqual(execution.status, liveaction.status) self.assertEqual(execution.context, liveaction.context) self.assertEqual(execution.liveaction['callback'], liveaction.callback) self.assertEqual(execution.liveaction['action'], liveaction.action) self.assertGreater(len(execution.children), 0) for child in execution.children: record = ActionExecution.get(id=child, raise_exception=True) self.assertEqual(record.parent, str(execution.id)) self.assertEqual(record.action['name'], 'local') self.assertEqual(record.runner['name'], 'run-local')
def test_chained_executions(self): with mock.patch("st2common.runners.register_runner", mock.MagicMock(return_value=action_chain_runner)): liveaction = LiveActionDB(action="executions.chain") liveaction, _ = action_service.request(liveaction) liveaction = LiveAction.get_by_id(str(liveaction.id)) self.assertEqual(liveaction.status, action_constants.LIVEACTION_STATUS_FAILED) execution = self._get_action_execution(liveaction__id=str(liveaction.id), raise_exception=True) action = action_utils.get_action_by_ref("executions.chain") self.assertDictEqual(execution.action, vars(ActionAPI.from_model(action))) runner = RunnerType.get_by_name(action.runner_type["name"]) self.assertDictEqual(execution.runner, vars(RunnerTypeAPI.from_model(runner))) liveaction = LiveAction.get_by_id(str(liveaction.id)) self.assertEqual(execution.start_timestamp, liveaction.start_timestamp) self.assertEqual(execution.end_timestamp, liveaction.end_timestamp) self.assertEqual(execution.result, liveaction.result) self.assertEqual(execution.status, liveaction.status) self.assertEqual(execution.context, liveaction.context) self.assertEqual(execution.liveaction["callback"], liveaction.callback) self.assertEqual(execution.liveaction["action"], liveaction.action) self.assertGreater(len(execution.children), 0) for child in execution.children: record = ActionExecution.get(id=child, raise_exception=True) self.assertEqual(record.parent, str(execution.id)) self.assertEqual(record.action["name"], "local") self.assertEqual(record.runner["name"], "run-local")
def put(self, runner_type_api, name_or_id, requester_user): # Note: We only allow "enabled" attribute of the runner to be changed runner_type_db = self._get_by_name_or_id(name_or_id=name_or_id) permission_type = PermissionType.RUNNER_MODIFY rbac_utils.assert_user_has_resource_db_permission(user_db=requester_user, resource_db=runner_type_db, permission_type=permission_type) old_runner_type_db = runner_type_db LOG.debug('PUT /runnertypes/ lookup with id=%s found object: %s', name_or_id, runner_type_db) try: if runner_type_api.id and runner_type_api.id != name_or_id: LOG.warning('Discarding mismatched id=%s found in payload and using uri_id=%s.', runner_type_api.id, name_or_id) runner_type_db.enabled = runner_type_api.enabled runner_type_db = RunnerType.add_or_update(runner_type_db) except (ValidationError, ValueError) as e: LOG.exception('Validation failed for runner type data=%s', runner_type_api) abort(http_client.BAD_REQUEST, six.text_type(e)) return extra = {'old_runner_type_db': old_runner_type_db, 'new_runner_type_db': runner_type_db} LOG.audit('Runner Type updated. RunnerType.id=%s.' % (runner_type_db.id), extra=extra) runner_type_api = RunnerTypeAPI.from_model(runner_type_db) return runner_type_api
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_execution_creation_action_triggered_by_rule(self): # Wait for the action execution to complete and then confirm outcome. trigger_type = self.MODELS['triggertypes']['triggertype2.yaml'] trigger = self.MODELS['triggers']['trigger2.yaml'] trigger_instance = self.MODELS['triggerinstances']['trigger_instance_1.yaml'] test_liveaction = self.FIXTURES['liveactions']['liveaction3.yaml'] rule = self.MODELS['rules']['rule3.yaml'] # Setup LiveAction to point to right rule and trigger_instance. # XXX: We need support for dynamic fixtures. test_liveaction['context']['rule']['id'] = str(rule.id) test_liveaction['context']['trigger_instance']['id'] = str(trigger_instance.id) test_liveaction_api = LiveActionAPI(**test_liveaction) test_liveaction = LiveAction.add_or_update(LiveActionAPI.to_model(test_liveaction_api)) liveaction = LiveAction.get(context__trigger_instance__id=str(trigger_instance.id)) self.assertIsNotNone(liveaction) self.assertEqual(liveaction.status, action_constants.LIVEACTION_STATUS_REQUESTED) executions_util.create_execution_object(liveaction) execution = self._get_action_execution(liveaction__id=str(liveaction.id), raise_exception=True) self.assertDictEqual(execution.trigger, vars(TriggerAPI.from_model(trigger))) self.assertDictEqual(execution.trigger_type, vars(TriggerTypeAPI.from_model(trigger_type))) self.assertDictEqual(execution.trigger_instance, vars(TriggerInstanceAPI.from_model(trigger_instance))) self.assertDictEqual(execution.rule, vars(RuleAPI.from_model(rule))) action = action_utils.get_action_by_ref(liveaction.action) self.assertDictEqual(execution.action, vars(ActionAPI.from_model(action))) runner = RunnerType.get_by_name(action.runner_type['name']) self.assertDictEqual(execution.runner, vars(RunnerTypeAPI.from_model(runner))) liveaction = LiveAction.get_by_id(str(liveaction.id)) self.assertEquals(execution.liveaction['id'], str(liveaction.id))
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 setUpClass(cls): super(ActionParamsUtilsTest, cls).setUpClass() cls.runnertype_dbs = {} cls.action_dbs = {} for _, fixture in six.iteritems(FIXTURES['runners']): instance = RunnerTypeAPI(**fixture) runnertype_db = RunnerType.add_or_update( RunnerTypeAPI.to_model(instance)) cls.runnertype_dbs[runnertype_db.name] = runnertype_db for _, fixture in six.iteritems(FIXTURES['actions']): instance = ActionAPI(**fixture) action_db = Action.add_or_update(ActionAPI.to_model(instance)) cls.action_dbs[action_db.name] = action_db
def setUpClass(cls): super(TestActionExecutionService, cls).setUpClass() cls.runner = RunnerTypeAPI(**RUNNER) cls.runnerdb = RunnerType.add_or_update(RunnerTypeAPI.to_model(cls.runner)) cls.action = ActionAPI(**ACTION) cls.actiondb = Action.add_or_update(ActionAPI.to_model(cls.action)) cls.container = RunnerContainer()
def put(self, name_or_id, runner_type_api): # Note: We only allow "enabled" attribute of the runner to be changed runner_type_db = self._get_by_name_or_id(name_or_id=name_or_id) old_runner_type_db = runner_type_db LOG.debug('PUT /runnertypes/ lookup with id=%s found object: %s', name_or_id, runner_type_db) try: if runner_type_api.id and runner_type_api.id != name_or_id: LOG.warning( 'Discarding mismatched id=%s found in payload and using uri_id=%s.', runner_type_api.id, name_or_id) runner_type_db.enabled = runner_type_api.enabled runner_type_db = RunnerType.add_or_update(runner_type_db) except (ValidationError, ValueError) as e: LOG.exception('Validation failed for runner type data=%s', runner_type_api) pecan.abort(http_client.BAD_REQUEST, str(e)) return extra = { 'old_runner_type_db': old_runner_type_db, 'new_runner_type_db': runner_type_db } LOG.audit('Runner Type updated. RunnerType.id=%s.' % (runner_type_db.id), extra=extra) runner_type_api = RunnerTypeAPI.from_model(runner_type_db) return runner_type_api
def test_execution_creation_manual_action_run(self): liveaction = self.MODELS['liveactions']['liveaction1.yaml'] pre_creation_timestamp = date_utils.get_datetime_utc_now() executions_util.create_execution_object(liveaction) post_creation_timestamp = date_utils.get_datetime_utc_now() execution = self._get_action_execution(liveaction__id=str( liveaction.id), raise_exception=True) self.assertDictEqual(execution.trigger, {}) self.assertDictEqual(execution.trigger_type, {}) self.assertDictEqual(execution.trigger_instance, {}) self.assertDictEqual(execution.rule, {}) action = action_utils.get_action_by_ref('core.local') self.assertDictEqual(execution.action, vars(ActionAPI.from_model(action))) runner = RunnerType.get_by_name(action.runner_type['name']) self.assertDictEqual(execution.runner, vars(RunnerTypeAPI.from_model(runner))) liveaction = LiveAction.get_by_id(str(liveaction.id)) self.assertEquals(execution.liveaction['id'], str(liveaction.id)) self.assertEquals(len(execution.log), 1) self.assertEquals(execution.log[0]['status'], liveaction.status) self.assertGreater(execution.log[0]['timestamp'], pre_creation_timestamp) self.assertLess(execution.log[0]['timestamp'], post_creation_timestamp)
def put(self, runner_type_api, name_or_id, requester_user): # Note: We only allow "enabled" attribute of the runner to be changed runner_type_db = self._get_by_name_or_id(name_or_id=name_or_id) permission_type = PermissionType.RUNNER_MODIFY rbac_utils = get_rbac_backend().get_utils_class() rbac_utils.assert_user_has_resource_db_permission(user_db=requester_user, resource_db=runner_type_db, permission_type=permission_type) old_runner_type_db = runner_type_db LOG.debug('PUT /runnertypes/ lookup with id=%s found object: %s', name_or_id, runner_type_db) try: if runner_type_api.id and runner_type_api.id != name_or_id: LOG.warning('Discarding mismatched id=%s found in payload and using uri_id=%s.', runner_type_api.id, name_or_id) runner_type_db.enabled = runner_type_api.enabled runner_type_db = RunnerType.add_or_update(runner_type_db) except (ValidationError, ValueError) as e: LOG.exception('Validation failed for runner type data=%s', runner_type_api) abort(http_client.BAD_REQUEST, six.text_type(e)) return extra = {'old_runner_type_db': old_runner_type_db, 'new_runner_type_db': runner_type_db} LOG.audit('Runner Type updated. RunnerType.id=%s.' % (runner_type_db.id), extra=extra) runner_type_api = RunnerTypeAPI.from_model(runner_type_db) return runner_type_api
def test_basic_execution(self): liveaction = LiveActionDB(action='executions.local', parameters={'cmd': 'uname -a'}) liveaction, _ = action_service.request(liveaction) liveaction = self._wait_on_status(liveaction, action_constants.LIVEACTION_STATUS_FAILED) execution = self._get_action_execution( liveaction__id=str(liveaction.id), raise_exception=True ) self.assertDictEqual(execution.trigger, {}) self.assertDictEqual(execution.trigger_type, {}) self.assertDictEqual(execution.trigger_instance, {}) self.assertDictEqual(execution.rule, {}) action = action_utils.get_action_by_ref('executions.local') self.assertDictEqual(execution.action, vars(ActionAPI.from_model(action))) runner = RunnerType.get_by_name(action.runner_type['name']) self.assertDictEqual(execution.runner, vars(RunnerTypeAPI.from_model(runner))) liveaction = LiveAction.get_by_id(str(liveaction.id)) self.assertEqual(execution.start_timestamp, liveaction.start_timestamp) self.assertEqual(execution.end_timestamp, liveaction.end_timestamp) self.assertEqual(execution.result, liveaction.result) self.assertEqual(execution.status, liveaction.status) self.assertEqual(execution.context, liveaction.context) self.assertEqual(execution.liveaction['callback'], liveaction.callback) self.assertEqual(execution.liveaction['action'], liveaction.action)
def record_action_execution(self, body): try: history_id = bson.ObjectId() execution = ActionExecution.get_by_id(str(body.id)) action_ref = ResourceReference.from_string_reference( ref=execution.action) action_db, _ = action_utils.get_action_by_dict({ 'name': action_ref.name, 'pack': action_ref.pack }) runner = RunnerType.get_by_name(action_db.runner_type['name']) attrs = { 'id': history_id, 'action': vars(ActionAPI.from_model(action_db)), 'runner': vars(RunnerTypeAPI.from_model(runner)), 'execution': vars(ActionExecutionAPI.from_model(execution)) } if 'rule' in execution.context: rule = reference.get_model_from_ref( Rule, execution.context.get('rule', {})) attrs['rule'] = vars(RuleAPI.from_model(rule)) if 'trigger_instance' in execution.context: trigger_instance_id = execution.context.get( 'trigger_instance', {}) trigger_instance_id = trigger_instance_id.get('id', None) trigger_instance = TriggerInstance.get_by_id( trigger_instance_id) trigger = reference.get_model_by_resource_ref( db_api=Trigger, ref=trigger_instance.trigger) trigger_type = reference.get_model_by_resource_ref( db_api=TriggerType, ref=trigger.type) trigger_instance = reference.get_model_from_ref( TriggerInstance, execution.context.get('trigger_instance', {})) attrs['trigger_instance'] = vars( TriggerInstanceAPI.from_model(trigger_instance)) attrs['trigger'] = vars(TriggerAPI.from_model(trigger)) attrs['trigger_type'] = vars( TriggerTypeAPI.from_model(trigger_type)) parent = ActionExecutionHistory.get( execution__id=execution.context.get('parent', '')) if parent: attrs['parent'] = str(parent.id) if str(history_id) not in parent.children: parent.children.append(str(history_id)) ActionExecutionHistory.add_or_update(parent) history = ActionExecutionHistoryDB(**attrs) history = ActionExecutionHistory.add_or_update(history) except: LOG.exception('An unexpected error occurred while creating the ' 'action execution history.') raise
def test_triggered_execution(self): docs = { 'trigger_type': copy.deepcopy(fixture.ARTIFACTS['trigger_type']), 'trigger': copy.deepcopy(fixture.ARTIFACTS['trigger']), 'rule': copy.deepcopy(fixture.ARTIFACTS['rule']), 'trigger_instance': copy.deepcopy(fixture.ARTIFACTS['trigger_instance']) } # Trigger an action execution. trigger_type = TriggerType.add_or_update( TriggerTypeAPI.to_model(TriggerTypeAPI(**docs['trigger_type']))) trigger = Trigger.add_or_update( TriggerAPI.to_model(TriggerAPI(**docs['trigger']))) rule = RuleAPI.to_model(RuleAPI(**docs['rule'])) rule.trigger = reference.get_str_resource_ref_from_model(trigger) rule = Rule.add_or_update(rule) trigger_instance = TriggerInstance.add_or_update( TriggerInstanceAPI.to_model( TriggerInstanceAPI(**docs['trigger_instance']))) enforcer = RuleEnforcer(trigger_instance, rule) enforcer.enforce() # Wait for the action execution to complete and then confirm outcome. liveaction = LiveAction.get( context__trigger_instance__id=str(trigger_instance.id)) self.assertIsNotNone(liveaction) liveaction = LiveAction.get_by_id(str(liveaction.id)) self.assertEqual(liveaction.status, LIVEACTION_STATUS_FAILED) execution = self._get_action_execution(liveaction__id=str( liveaction.id), raise_exception=True) self.assertDictEqual(execution.trigger, vars(TriggerAPI.from_model(trigger))) self.assertDictEqual(execution.trigger_type, vars(TriggerTypeAPI.from_model(trigger_type))) self.assertDictEqual( execution.trigger_instance, vars(TriggerInstanceAPI.from_model(trigger_instance))) self.assertDictEqual(execution.rule, vars(RuleAPI.from_model(rule))) action = action_utils.get_action_by_ref(liveaction.action) self.assertDictEqual(execution.action, vars(ActionAPI.from_model(action))) runner = RunnerType.get_by_name(action.runner_type['name']) self.assertDictEqual(execution.runner, vars(RunnerTypeAPI.from_model(runner))) liveaction = LiveAction.get_by_id(str(liveaction.id)) self.assertEqual(execution.start_timestamp, liveaction.start_timestamp) self.assertEqual(execution.end_timestamp, liveaction.end_timestamp) self.assertEqual(execution.result, liveaction.result) self.assertEqual(execution.status, liveaction.status) self.assertEqual(execution.context, liveaction.context) self.assertEqual(execution.liveaction['callback'], liveaction.callback) self.assertEqual(execution.liveaction['action'], liveaction.action)
def test_triggered_execution(self): docs = { 'trigger_type': copy.deepcopy(fixture.ARTIFACTS['trigger_type']), 'trigger': copy.deepcopy(fixture.ARTIFACTS['trigger']), 'rule': copy.deepcopy(fixture.ARTIFACTS['rule']), 'trigger_instance': copy.deepcopy(fixture.ARTIFACTS['trigger_instance']) } # Trigger an action execution. trigger_type = TriggerType.add_or_update( TriggerTypeAPI.to_model(TriggerTypeAPI(**docs['trigger_type']))) trigger = Trigger.add_or_update( TriggerAPI.to_model(TriggerAPI(**docs['trigger']))) rule = RuleAPI.to_model(RuleAPI(**docs['rule'])) rule.trigger = reference.get_str_resource_ref_from_model(trigger) rule = Rule.add_or_update(rule) trigger_instance = TriggerInstance.add_or_update( TriggerInstanceAPI.to_model( TriggerInstanceAPI(**docs['trigger_instance']))) enforcer = RuleEnforcer(trigger_instance, rule) enforcer.enforce() # Wait for the action execution to complete and then confirm outcome. execution = ActionExecution.get( context__trigger_instance__id=str(trigger_instance.id)) self.assertIsNotNone(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, vars(TriggerAPI.from_model(trigger))) self.assertDictEqual(history.trigger_type, vars(TriggerTypeAPI.from_model(trigger_type))) self.assertDictEqual( history.trigger_instance, vars(TriggerInstanceAPI.from_model(trigger_instance))) self.assertDictEqual(history.rule, vars(RuleAPI.from_model(rule))) action_ref = ResourceReference.from_string_reference( ref=execution.action) 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 get_one(self, id): """ List RunnerType objects by id. Handle: GET /runnertypes/1 """ runnertype_db = RunnerTypesController.__get_by_id(id) runnertype_api = RunnerTypeAPI.from_model(runnertype_db) return runnertype_api
def setUpClass(cls): super(DSLTransformTestCase, cls).setUpClass() for _, fixture in six.iteritems(FIXTURES['runners']): instance = RunnerTypeAPI(**fixture) RunnerType.add_or_update(RunnerTypeAPI.to_model(instance)) for _, fixture in six.iteritems(FIXTURES['actions']): instance = ActionAPI(**fixture) Action.add_or_update(ActionAPI.to_model(instance))
def setUpClass(cls): super(MistralValidationControllerTest, cls).setUpClass() for _, fixture in six.iteritems(FIXTURES['runners']): instance = RunnerTypeAPI(**fixture) RunnerType.add_or_update(RunnerTypeAPI.to_model(instance)) for _, fixture in six.iteritems(FIXTURES['actions']): instance = ActionAPI(**fixture) Action.add_or_update(ActionAPI.to_model(instance))
def setUpClass(cls): EventletTestCase.setUpClass() DbTestCase.setUpClass() for _, fixture in six.iteritems(FIXTURES['runners']): instance = RunnerTypeAPI(**fixture) RunnerType.add_or_update(RunnerTypeAPI.to_model(instance)) for _, fixture in six.iteritems(FIXTURES['actions']): instance = ActionAPI(**fixture) Action.add_or_update(ActionAPI.to_model(instance)) for _, fixture in six.iteritems(FIXTURES['policytypes']): instance = PolicyTypeAPI(**fixture) PolicyType.add_or_update(PolicyTypeAPI.to_model(instance)) for _, fixture in six.iteritems(FIXTURES['policies']): instance = PolicyAPI(**fixture) Policy.add_or_update(PolicyAPI.to_model(instance))
def create_execution_object(liveaction, action_db=None, runnertype_db=None, publish=True): if not action_db: action_db = action_utils.get_action_by_ref(liveaction.action) if not runnertype_db: runnertype_db = RunnerType.get_by_name(action_db.runner_type['name']) attrs = { 'action': vars(ActionAPI.from_model(action_db)), 'parameters': liveaction['parameters'], 'runner': vars(RunnerTypeAPI.from_model(runnertype_db)) } attrs.update(_decompose_liveaction(liveaction)) if 'rule' in liveaction.context: rule = reference.get_model_from_ref(Rule, liveaction.context.get('rule', {})) attrs['rule'] = vars(RuleAPI.from_model(rule)) if 'trigger_instance' in liveaction.context: trigger_instance_id = liveaction.context.get('trigger_instance', {}) trigger_instance_id = trigger_instance_id.get('id', None) trigger_instance = TriggerInstance.get_by_id(trigger_instance_id) trigger = reference.get_model_by_resource_ref(db_api=Trigger, ref=trigger_instance.trigger) trigger_type = reference.get_model_by_resource_ref(db_api=TriggerType, ref=trigger.type) trigger_instance = reference.get_model_from_ref( TriggerInstance, liveaction.context.get('trigger_instance', {})) attrs['trigger_instance'] = vars(TriggerInstanceAPI.from_model(trigger_instance)) attrs['trigger'] = vars(TriggerAPI.from_model(trigger)) attrs['trigger_type'] = vars(TriggerTypeAPI.from_model(trigger_type)) parent = _get_parent_execution(liveaction) if parent: attrs['parent'] = str(parent.id) attrs['log'] = [_create_execution_log_entry(liveaction['status'])] # TODO: This object initialization takes 20-30or so ms execution = ActionExecutionDB(**attrs) # TODO: Do 100% research this is fully safe and unique in distributed setups execution.id = ObjectId() execution.web_url = _get_web_url_for_execution(str(execution.id)) # NOTE: User input data is already validate as part of the API request, # other data is set by us. Skipping validation here makes operation 10%-30% faster execution = ActionExecution.add_or_update(execution, publish=publish, validate=False) if parent and str(execution.id) not in parent.children: values = {} values['push__children'] = str(execution.id) ActionExecution.update(parent, **values) return execution
def get_all(self, **kw): """ List all RunnerType objects. Handles requests: GET /runnertypes/ """ runnertype_dbs = RunnerType.get_all(**kw) runnertype_apis = [RunnerTypeAPI.from_model(runnertype_db) for runnertype_db in runnertype_dbs] return runnertype_apis
def get_one(self, id): """ List RunnerType objects by id. Handle: GET /runnertypes/1 """ LOG.info('GET /runnertypes/ with id=%s', id) runnertype_db = RunnerTypesController.__get_by_id(id) runnertype_api = RunnerTypeAPI.from_model(runnertype_db) LOG.debug('GET /runnertypes/ with id=%s, client_result=%s', id, runnertype_api) return runnertype_api
def setUpClass(cls): super(MistralValidationTest, cls).setUpClass() for _, fixture in six.iteritems(FIXTURES["runners"]): instance = RunnerTypeAPI(**fixture) RunnerType.add_or_update(RunnerTypeAPI.to_model(instance)) for _, fixture in six.iteritems(FIXTURES["actions"]): instance = ActionAPI(**fixture) Action.add_or_update(ActionAPI.to_model(instance)) cls.validator = wf_validation_utils.get_validator()
def get_all(self, **kw): """ List all RunnerType objects. Handles requests: GET /runnertypes/ """ LOG.info('GET all /runnertypes/ with filters=%s', kw) runnertype_dbs = RunnerType.get_all(**kw) runnertype_apis = [RunnerTypeAPI.from_model(runnertype_db) for runnertype_db in runnertype_dbs] LOG.debug('GET all /runnertypes/ client_result=%s', runnertype_apis) return runnertype_apis
def setUpClass(cls): super(TestActionExecutionService, cls).setUpClass() cls.runner = RunnerTypeAPI(**RUNNER) cls.runnerdb = RunnerType.add_or_update(RunnerTypeAPI.to_model(cls.runner)) runner_api = RunnerTypeAPI(**RUNNER_ACTION_CHAIN) RunnerType.add_or_update(RunnerTypeAPI.to_model(runner_api)) cls.actions = { ACTION['name']: ActionAPI(**ACTION), ACTION_WORKFLOW['name']: ActionAPI(**ACTION_WORKFLOW), ACTION_OVR_PARAM['name']: ActionAPI(**ACTION_OVR_PARAM), ACTION_OVR_PARAM_MUTABLE['name']: ActionAPI(**ACTION_OVR_PARAM_MUTABLE), ACTION_OVR_PARAM_IMMUTABLE['name']: ActionAPI(**ACTION_OVR_PARAM_IMMUTABLE), ACTION_OVR_PARAM_BAD_ATTR['name']: ActionAPI(**ACTION_OVR_PARAM_BAD_ATTR), ACTION_OVR_PARAM_BAD_ATTR_NOOP['name']: ActionAPI(**ACTION_OVR_PARAM_BAD_ATTR_NOOP) } cls.actiondbs = {name: Action.add_or_update(ActionAPI.to_model(action)) for name, action in six.iteritems(cls.actions)} cls.container = RunnerContainer()
def create_execution_object(liveaction, publish=True): action_db = action_utils.get_action_by_ref(liveaction.action) runner = RunnerType.get_by_name(action_db.runner_type['name']) attrs = { 'action': vars(ActionAPI.from_model(action_db)), 'parameters': liveaction['parameters'], 'runner': vars(RunnerTypeAPI.from_model(runner)) } attrs.update(_decompose_liveaction(liveaction)) if 'rule' in liveaction.context: rule = reference.get_model_from_ref(Rule, liveaction.context.get('rule', {})) attrs['rule'] = vars(RuleAPI.from_model(rule)) if 'trigger_instance' in liveaction.context: trigger_instance_id = liveaction.context.get('trigger_instance', {}) trigger_instance_id = trigger_instance_id.get('id', None) trigger_instance = TriggerInstance.get_by_id(trigger_instance_id) trigger = reference.get_model_by_resource_ref(db_api=Trigger, ref=trigger_instance.trigger) trigger_type = reference.get_model_by_resource_ref(db_api=TriggerType, ref=trigger.type) trigger_instance = reference.get_model_from_ref( TriggerInstance, liveaction.context.get('trigger_instance', {})) attrs['trigger_instance'] = vars(TriggerInstanceAPI.from_model(trigger_instance)) attrs['trigger'] = vars(TriggerAPI.from_model(trigger)) attrs['trigger_type'] = vars(TriggerTypeAPI.from_model(trigger_type)) parent = _get_parent_execution(liveaction) if parent: attrs['parent'] = str(parent.id) attrs['log'] = [_create_execution_log_entry(liveaction['status'])] execution = ActionExecutionDB(**attrs) execution = ActionExecution.add_or_update(execution, publish=False) # Update the web_url field in execution. Unfortunately, we need # the execution id for constructing the URL which we only get # after the model is written to disk. execution.web_url = _get_web_url_for_execution(str(execution.id)) execution = ActionExecution.add_or_update(execution, publish=publish) if parent: if str(execution.id) not in parent.children: parent.children.append(str(execution.id)) ActionExecution.add_or_update(parent) return execution
def test_triggered_execution(self): docs = { 'trigger_type': copy.deepcopy(fixture.ARTIFACTS['trigger_type']), 'trigger': copy.deepcopy(fixture.ARTIFACTS['trigger']), 'rule': copy.deepcopy(fixture.ARTIFACTS['rule']), 'trigger_instance': copy.deepcopy(fixture.ARTIFACTS['trigger_instance'])} # Trigger an action execution. trigger_type = TriggerType.add_or_update( TriggerTypeAPI.to_model(TriggerTypeAPI(**docs['trigger_type']))) trigger = Trigger.add_or_update(TriggerAPI.to_model(TriggerAPI(**docs['trigger']))) rule = RuleAPI.to_model(RuleAPI(**docs['rule'])) rule.trigger = reference.get_str_resource_ref_from_model(trigger) rule = Rule.add_or_update(rule) trigger_instance = TriggerInstance.add_or_update( TriggerInstanceAPI.to_model(TriggerInstanceAPI(**docs['trigger_instance']))) trace_service.add_or_update_given_trace_context( trace_context={'trace_tag': 'test_triggered_execution_trace'}, trigger_instances=[str(trigger_instance.id)]) enforcer = RuleEnforcer(trigger_instance, rule) enforcer.enforce() # Wait for the action execution to complete and then confirm outcome. liveaction = LiveAction.get(context__trigger_instance__id=str(trigger_instance.id)) self.assertIsNotNone(liveaction) liveaction = self._wait_on_status(liveaction, action_constants.LIVEACTION_STATUS_FAILED) execution = self._get_action_execution( liveaction__id=str(liveaction.id), raise_exception=True ) self.assertDictEqual(execution.trigger, vars(TriggerAPI.from_model(trigger))) self.assertDictEqual(execution.trigger_type, vars(TriggerTypeAPI.from_model(trigger_type))) self.assertDictEqual(execution.trigger_instance, vars(TriggerInstanceAPI.from_model(trigger_instance))) self.assertDictEqual(execution.rule, vars(RuleAPI.from_model(rule))) action = action_utils.get_action_by_ref(liveaction.action) self.assertDictEqual(execution.action, vars(ActionAPI.from_model(action))) runner = RunnerType.get_by_name(action.runner_type['name']) self.assertDictEqual(execution.runner, vars(RunnerTypeAPI.from_model(runner))) liveaction = LiveAction.get_by_id(str(liveaction.id)) self.assertEqual(execution.start_timestamp, liveaction.start_timestamp) self.assertEqual(execution.end_timestamp, liveaction.end_timestamp) self.assertEqual(execution.result, liveaction.result) self.assertEqual(execution.status, liveaction.status) self.assertEqual(execution.context, liveaction.context) self.assertEqual(execution.liveaction['callback'], liveaction.callback) self.assertEqual(execution.liveaction['action'], liveaction.action)
def test_execution_creation_manual_action_run(self): liveaction = self.MODELS['liveactions']['liveaction1.yaml'] executions_util.create_execution_object(liveaction) execution = self._get_action_execution(liveaction__id=str(liveaction.id), raise_exception=True) self.assertDictEqual(execution.trigger, {}) self.assertDictEqual(execution.trigger_type, {}) self.assertDictEqual(execution.trigger_instance, {}) self.assertDictEqual(execution.rule, {}) action = action_utils.get_action_by_ref('core.local') self.assertDictEqual(execution.action, vars(ActionAPI.from_model(action))) runner = RunnerType.get_by_name(action.runner_type['name']) self.assertDictEqual(execution.runner, vars(RunnerTypeAPI.from_model(runner))) liveaction = LiveAction.get_by_id(str(liveaction.id)) self.assertEquals(execution.liveaction['id'], str(liveaction.id))
def setUpClass(cls): super(ActionParamsUtilsTest, cls).setUpClass() cls.runnertype_dbs = {} cls.action_dbs = {} for _, fixture in six.iteritems(FIXTURES['runners']): instance = RunnerTypeAPI(**fixture) runnertype_db = RunnerType.add_or_update(RunnerTypeAPI.to_model(instance)) cls.runnertype_dbs[runnertype_db.name] = runnertype_db for _, fixture in six.iteritems(FIXTURES['actions']): instance = ActionAPI(**fixture) action_db = Action.add_or_update(ActionAPI.to_model(instance)) cls.action_dbs[action_db.name] = action_db
def record_action_execution(self, body): try: history_id = bson.ObjectId() execution = ActionExecution.get_by_id(str(body.id)) action_ref = ResourceReference.from_string_reference(ref=execution.action) action_db, _ = action_utils.get_action_by_dict( {'name': action_ref.name, 'pack': action_ref.pack}) runner = RunnerType.get_by_name(action_db.runner_type['name']) attrs = { 'id': history_id, 'action': vars(ActionAPI.from_model(action_db)), 'runner': vars(RunnerTypeAPI.from_model(runner)), 'execution': vars(ActionExecutionAPI.from_model(execution)) } if 'rule' in execution.context: rule = reference.get_model_from_ref(Rule, execution.context.get('rule', {})) attrs['rule'] = vars(RuleAPI.from_model(rule)) if 'trigger_instance' in execution.context: trigger_instance_id = execution.context.get('trigger_instance', {}) trigger_instance_id = trigger_instance_id.get('id', None) trigger_instance = TriggerInstance.get_by_id(trigger_instance_id) trigger = reference.get_model_by_resource_ref(db_api=Trigger, ref=trigger_instance.trigger) trigger_type = reference.get_model_by_resource_ref(db_api=TriggerType, ref=trigger.type) trigger_instance = reference.get_model_from_ref( TriggerInstance, execution.context.get('trigger_instance', {})) attrs['trigger_instance'] = vars(TriggerInstanceAPI.from_model(trigger_instance)) attrs['trigger'] = vars(TriggerAPI.from_model(trigger)) attrs['trigger_type'] = vars(TriggerTypeAPI.from_model(trigger_type)) parent = ActionExecutionHistory.get(execution__id=execution.context.get('parent', '')) if parent: attrs['parent'] = str(parent.id) if str(history_id) not in parent.children: parent.children.append(str(history_id)) ActionExecutionHistory.add_or_update(parent) history = ActionExecutionHistoryDB(**attrs) history = ActionExecutionHistory.add_or_update(history) except: LOG.exception('An unexpected error occurred while creating the ' 'action execution history.') raise
def setUpClass(cls): super(PolicyTest, cls).setUpClass() for _, fixture in six.iteritems(FIXTURES['runners']): instance = RunnerTypeAPI(**fixture) RunnerType.add_or_update(RunnerTypeAPI.to_model(instance)) for _, fixture in six.iteritems(FIXTURES['actions']): instance = ActionAPI(**fixture) Action.add_or_update(ActionAPI.to_model(instance)) for _, fixture in six.iteritems(FIXTURES['policytypes']): instance = PolicyTypeAPI(**fixture) PolicyType.add_or_update(PolicyTypeAPI.to_model(instance)) for _, fixture in six.iteritems(FIXTURES['policies']): instance = PolicyAPI(**fixture) Policy.add_or_update(PolicyAPI.to_model(instance))