def __init__(self): self.playbook1 = Playbook( 'play1', workflows=[Workflow('work1'), Workflow('work2')]) self.playbook2 = Playbook( 'play2', workflows=[Workflow('work1'), Workflow('work3')])
def test_load_playbooks_mixed_files_in_directory(self): workflows = [Workflow('something'), Workflow('something2')] playbook = Playbook('test', workflows=workflows) filepath = os.path.join(test_data_path, 'test.playbook') with open(filepath, 'w') as file_out: file_out.write(json.dumps(playbook.read())) workflows = [Workflow('something'), Workflow('something2')] playbook = Playbook('test2', workflows=workflows) filepath = os.path.join(test_data_path, 'test2.some_extension') with open(filepath, 'w') as file_out: file_out.write(json.dumps(playbook.read())) loaded = Loader.load_playbooks(test_data_path) self.assertEqual(len(loaded), 1) self.assertEqual(loaded[0].name, 'test')
def test_load_multiple_workflows(self): workflows = [Workflow('something'), Workflow('something2')] playbook = Playbook('test', workflows=workflows) filepath = os.path.join(test_data_path, 'test.playbook') with open(filepath, 'w') as file_out: file_out.write(json.dumps(playbook.read())) workflows = [Workflow('something'), Workflow('something2')] playbook = Playbook('test2', workflows=workflows) filepath = os.path.join(test_data_path, 'test2.playbook') with open(filepath, 'w') as file_out: file_out.write(json.dumps(playbook.read())) loaded = Loader.load_playbooks(test_data_path) self.assertEqual(len(loaded), 2) self.assertSetEqual({playbook.name for playbook in loaded}, {'test', 'test2'})
def copy_workflow(self, old_playbook_name, new_playbook_name, old_workflow_name, new_workflow_name): """Duplicates a workflow into its current playbook, or a different playbook. Args: old_playbook_name (str): Playbook name under which the workflow is located. new_playbook_name (str): The new playbook name for the duplicated workflow. old_workflow_name (str): The name of the workflow to be copied. new_workflow_name (str): The new name of the duplicated workflow. """ workflow = self.get_workflow(old_playbook_name, old_workflow_name) if workflow is not None: workflow.strip_events() workflow_copy = deepcopy(workflow) workflow_copy.name = new_workflow_name workflow_copy.regenerate_uids() workflow_copy.reset_event() if new_playbook_name in self.playbooks: self.playbooks[new_playbook_name].add_workflow(workflow_copy) else: self.playbooks[new_playbook_name] = Playbook( new_playbook_name, [workflow_copy]) logger.info('Workflow copied from {0}-{1} to {2}-{3}'.format( old_playbook_name, old_workflow_name, new_playbook_name, new_workflow_name))
def load_workflow(self, resource, workflow_name, loader=JsonPlaybookLoader): """Loads a workflow from a file. Args: resource (str): Path to the workflow. workflow_name (str): Name of the workflow to load. loader (cls): Class used to load the workflow Returns: True on success, False otherwise. """ loaded = loader.load_workflow(resource, workflow_name) if loaded is None: return None else: playbook_name, workflow = loaded if playbook_name not in self.playbooks: logger.debug( 'Playbook name {0} not found while loading workflow {1}. Adding to storage.' .format(playbook_name, workflow.uid)) self.playbooks[playbook_name] = Playbook(playbook_name, [workflow]) else: self.playbooks[playbook_name].add_workflow(workflow) logger.info('Loaded workflow {} into storage'.format(workflow.uid))
def test_init_with_workflows(self): workflows = [MockWorkflow(i, i + 1) for i in range(3)] playbook = Playbook('test', workflows) self.assertEqual(playbook.name, 'test') self.assertEqual(playbook.walkoff_version, walkoff.__version__) orderless_list_compare(self, list(playbook.workflows.keys()), [workflow.name for workflow in workflows])
def test_get_all_workflows_as_limited_json(self): workflows = [MockWorkflow(i, i + 1) for i in range(3)] playbook = Playbook('test', workflows) workflow_jsons = playbook.get_all_workflows_as_limited_json() expected = [{'name': i + 1, 'uid': i} for i in range(3)] for workflow_json in workflow_jsons: self.assertIn(workflow_json, expected)
def test_rename_workflow_not_found(self): workflows = [MockWorkflow(i, i + 1) for i in range(3)] playbook = Playbook('test', workflows) playbook.rename_workflow('invalid', 'new_name') self.assertTrue(all( playbook.has_workflow_uid(uid) for uid in range(3))) self.assertFalse(playbook.has_workflow_name('invalid'))
def test_load_playbook(self): workflows = [Workflow('something'), Workflow('something2')] playbook = Playbook('test', workflows=workflows) filepath = os.path.join(test_data_path, 'test.json') with open(filepath, 'w') as file_out: file_out.write(json.dumps(playbook.read())) loaded = Loader.load_playbook(filepath) self.assertIsInstance(loaded, Playbook) self.assertEqual(loaded.name, 'test')
def create_playbook(self, playbook_name, workflows=None): """Creates a playbook from a playbook template. Args: playbook_name (str): The name of the new playbook. workflows (list[Workflow], optional): The list of workflows to be associated with the playbook. Defaults to None """ workflows = workflows if workflows is not None else [] self.add_playbook(Playbook(playbook_name, workflows))
def test_get_all_workflows_as_json(self): workflows = [MockWorkflow(i, i + 1) for i in range(3)] playbook = Playbook('test', workflows) workflow_jsons = playbook.get_all_workflow_representations() expected = [{ 'name': i + 1, 'uid': i, 'other': 'other' } for i in range(3)] for workflow_json in workflow_jsons: self.assertIn(workflow_json, expected)
def add_workflow(self, playbook_name, workflow): """ Adds a workflow to the store Args: playbook_name (str): Playbook to add the workflow to workflow (Workflow): Workflow to add """ if playbook_name in self.playbooks: self.playbooks[playbook_name].add_workflow(workflow) else: logger.warning( 'Playbook wih name {} does not exist storage. Creating.'. format(playbook_name)) self.playbooks[playbook_name] = Playbook(playbook_name, workflows=[workflow]) logger.info('Loaded workflow {0} into playbook {1} in storage'.format( workflow.name, playbook_name))
def test_has_workflow_name(self): workflow = MockWorkflow('uid', 'wf_name') playbook = Playbook('test', [workflow]) self.assertTrue(playbook.has_workflow_name('wf_name'))
def test_add_workflow_name_already_exists(self): workflow = MockWorkflow('uid', 'wf_name') playbook = Playbook('test', [workflow]) playbook.add_workflow(MockWorkflow('uid2', 'wf_name')) self.assertEqual(playbook.workflows['wf_name'].uid, 'uid2')
def test_add_workflow(self): workflow = MockWorkflow('uid', 'wf_name') playbook = Playbook('test', [workflow]) playbook.add_workflow(MockWorkflow('uid2', 'test2')) orderless_list_compare(self, list(playbook.workflows.keys()), ['wf_name', 'test2'])
def test_init_with_walkoff_version_number(self): playbook = Playbook('test', walkoff_version='0.4.0') self.assertEqual(playbook.name, 'test') self.assertDictEqual(playbook.workflows, {}) self.assertEqual(playbook.walkoff_version, '0.4.0')
def test_init_with_uid(self): playbook = Playbook('test', uid='uuu') self.assertEqual(playbook.name, 'test') self.assertEqual(playbook.uid, 'uuu') self.assertDictEqual(playbook.workflows, {}) self.assertEqual(playbook.walkoff_version, walkoff.__version__)
def test_remove_workflow_by_name(self): workflows = [MockWorkflow(i, i + 1) for i in range(3)] playbook = Playbook('test', workflows) playbook.remove_workflow_by_name(2) self.assertEqual(len(playbook.workflows), 2) self.assertFalse(playbook.has_workflow_name(2))
def test_get_workflow_by_name_no_name(self): workflow = MockWorkflow('uid', 'wf_name') playbook = Playbook('test', [workflow]) self.assertIsNone(playbook.get_workflow_by_name('invalid'))
def test_has_workflow_uid_no_workflows(self): playbook = Playbook('test', []) self.assertFalse(playbook.has_workflow_uid('anything'))
def test_get_workflow_by_uid_no_workflows(self): playbook = Playbook('test', []) self.assertIsNone(playbook.get_workflow_by_uid('anything'))
def test_get_all_workflows_as_json_no_workflows(self): playbook = Playbook('test', []) self.assertListEqual(playbook.get_all_workflow_representations(), [])
def test_rename_workflow_no_workflows(self): playbook = Playbook('test', []) playbook.rename_workflow('anything', 'renamed') self.assertDictEqual(playbook.workflows, {})
def test_get_all_workflows_as_limited_json_no_workflows(self): playbook = Playbook('test', []) self.assertListEqual(playbook.get_all_workflows_as_limited_json(), [])
def test_has_workflow_uid_no_uid(self): workflow = MockWorkflow('uid', 'wf_name') playbook = Playbook('test', [workflow]) self.assertFalse(playbook.has_workflow_uid('invalid'))
def test_remove_workflow_by_name_no_workflows(self): playbook = Playbook('test', []) playbook.remove_workflow_by_name('something') self.assertDictEqual(playbook.workflows, {})
def test_init(self): playbook = Playbook('test') self.assertEqual(playbook.name, 'test') self.assertIsNotNone(playbook.uid) self.assertDictEqual(playbook.workflows, {}) self.assertEqual(playbook.walkoff_version, walkoff.__version__)
def test_remove_workflow_by_name_workflow_not_found(self): workflows = [MockWorkflow(i, i + 1) for i in range(3)] playbook = Playbook('test', workflows) playbook.remove_workflow_by_name('invalid') self.assertEqual(len(playbook.workflows), 3)
def test_get_workflow_by_uid(self): workflow = MockWorkflow('uid', 'wf_name') playbook = Playbook('test', [workflow]) self.assertEqual(playbook.get_workflow_by_uid('uid'), workflow)
def test_rename_workflow(self): workflows = [MockWorkflow(i, i + 1) for i in range(3)] playbook = Playbook('test', workflows) playbook.rename_workflow(3, 'new_name') self.assertTrue(playbook.has_workflow_name('new_name')) self.assertFalse(playbook.has_workflow_name(3))