def _register_action(self, pack, action): with open(action, 'r') as fd: try: content = json.load(fd) except ValueError: LOG.exception('Failed loading action json from %s.', action) raise try: model = Action.get_by_name(str(content['name'])) except ValueError: model = ActionDB() model.name = content['name'] model.description = content['description'] model.enabled = content['enabled'] model.pack = pack model.entry_point = content['entry_point'] model.parameters = content.get('parameters', {}) runner_type = str(content['runner_type']) valid_runner_type, runner_type_db = self._has_valid_runner_type(runner_type) if valid_runner_type: model.runner_type = {'name': runner_type_db.name} else: LOG.exception('Runner type %s doesn\'t exist.') raise try: model = Action.add_or_update(model) LOG.audit('Action created. Action %s from %s.', model, action) except Exception: LOG.exception('Failed to write action to db %s.', model.name) raise
def _register_action(self, pack, action): content = self._meta_loader.load(action) try: model = Action.get_by_name(str(content['name'])) except ValueError: model = ActionDB() model.name = content['name'] model.description = content['description'] model.enabled = content['enabled'] model.pack = pack model.entry_point = content['entry_point'] model.parameters = content.get('parameters', {}) runner_type = str(content['runner_type']) valid_runner_type, runner_type_db = self._has_valid_runner_type( runner_type) if valid_runner_type: model.runner_type = {'name': runner_type_db.name} else: LOG.exception('Runner type %s doesn\'t exist.', runner_type) raise try: model = Action.add_or_update(model) LOG.audit('Action created. Action %s from %s.', model, action) except Exception: LOG.exception('Failed to write action to db %s.', model.name) raise
def test_pack_name_missing(self): registrar = actions_registrar.ActionsRegistrar() action_file = os.path.join(tests_base.get_fixtures_path(), 'wolfpack/actions/action_3_pack_missing.json') registrar._register_action('dummy', action_file) action_name = None with open(action_file, 'r') as fd: content = json.load(fd) action_name = str(content['name']) action_db = Action.get_by_name(action_name) self.assertEqual(action_db.pack, 'dummy', 'Content pack must be ' + 'set to dummy') Action.delete(action_db)
def test_pack_name_missing(self): registrar = actions_registrar.ActionsRegistrar() loader = fixtures_loader.FixturesLoader() action_file = loader.get_fixture_file_path_abs( 'generic', 'actions', 'action_3_pack_missing.yaml') registrar._register_action('dummy', action_file) action_name = None with open(action_file, 'r') as fd: content = yaml.safe_load(fd) action_name = str(content['name']) action_db = Action.get_by_name(action_name) self.assertEqual(action_db.pack, 'dummy', 'Content pack must be ' + 'set to dummy') Action.delete(action_db)
def test_pack_name_missing(self): registrar = actions_registrar.ActionsRegistrar() loader = fixtures_loader.FixturesLoader() action_file = loader.get_fixture_file_path_abs( 'generic', 'actions', 'action_3_pack_missing.json') registrar._register_action('dummy', action_file) action_name = None with open(action_file, 'r') as fd: content = json.load(fd) action_name = str(content['name']) action_db = Action.get_by_name(action_name) self.assertEqual(action_db.pack, 'dummy', 'Content pack must be ' + 'set to dummy') Action.delete(action_db)
def test_pack_name_missing(self): registrar = actions_registrar.ActionsRegistrar() loader = fixtures_loader.FixturesLoader() action_file = loader.get_fixture_file_path_abs( "generic", "actions", "action_3_pack_missing.yaml") registrar._register_action("dummy", action_file) action_name = None with open(action_file, "r") as fd: content = yaml.safe_load(fd) action_name = str(content["name"]) action_db = Action.get_by_name(action_name) expected_msg = "Content pack must be set to dummy" self.assertEqual(action_db.pack, "dummy", expected_msg) Action.delete(action_db)
def test_action_update(self): registrar = actions_registrar.ActionsRegistrar() loader = fixtures_loader.FixturesLoader() action_file = loader.get_fixture_file_path_abs( 'generic', 'actions', 'action1.yaml') registrar._register_action('wolfpack', action_file) # try registering again. this should not throw errors. registrar._register_action('wolfpack', action_file) action_name = None with open(action_file, 'r') as fd: content = yaml.safe_load(fd) action_name = str(content['name']) action_db = Action.get_by_name(action_name) self.assertEqual(action_db.pack, 'wolfpack', 'Content pack must be ' + 'set to wolfpack') Action.delete(action_db)
def test_action_update(self): registrar = actions_registrar.ActionsRegistrar() loader = fixtures_loader.FixturesLoader() action_file = loader.get_fixture_file_path_abs( 'generic', 'actions', 'action1.json') registrar._register_action('wolfpack', action_file) # try registering again. this should not throw errors. registrar._register_action('wolfpack', action_file) action_name = None with open(action_file, 'r') as fd: content = json.load(fd) action_name = str(content['name']) action_db = Action.get_by_name(action_name) self.assertEqual(action_db.pack, 'wolfpack', 'Content pack must be ' + 'set to wolfpack') Action.delete(action_db)
def test_action_update(self): registrar = actions_registrar.ActionsRegistrar() loader = fixtures_loader.FixturesLoader() action_file = loader.get_fixture_file_path_abs("generic", "actions", "action1.yaml") registrar._register_action("wolfpack", action_file) # try registering again. this should not throw errors. registrar._register_action("wolfpack", action_file) action_name = None with open(action_file, "r") as fd: content = yaml.safe_load(fd) action_name = str(content["name"]) action_db = Action.get_by_name(action_name) expected_msg = "Content pack must be set to wolfpack" self.assertEqual(action_db.pack, "wolfpack", expected_msg) Action.delete(action_db)
def test_post_duplicate(self): action_ids = [] post_resp = self.__do_post(ACTION_1) self.assertEqual(post_resp.status_int, 201) action_in_db = Action.get_by_name(ACTION_1.get('name')) self.assertTrue(action_in_db is not None, 'Action must be in db.') action_ids.append(self.__get_action_id(post_resp)) post_resp = self.__do_post(ACTION_1, expect_errors=True) # Verify name conflict self.assertEqual(post_resp.status_int, 409) post_resp = self.__do_post(ACTION_10) action_ids.append(self.__get_action_id(post_resp)) # Verify action with same name but different pack is written. self.assertEqual(post_resp.status_int, 201) for i in action_ids: self.__do_delete(i)