예제 #1
0
    def test_workflow_metrics(self):
        execution_db_help.load_playbooks(
            ['multiactionError', 'multiactionWorkflowTest'])
        error_id = executiondb.execution_db.session.query(Workflow).join(
            Workflow.playbook).filter(
                and_(Workflow.name == 'multiactionErrorWorkflow',
                     Playbook.name == 'multiactionError')).first().id
        test_id = executiondb.execution_db.session.query(Workflow).join(
            Workflow.playbook).filter(
                and_(Workflow.name == 'multiactionWorkflow',
                     Playbook.name == 'multiactionWorkflowTest')).first().id

        error_key = 'multiactionErrorWorkflow'
        multiaction_key = 'multiactionWorkflow'
        server.running_context.executor.execute_workflow(error_id)
        server.running_context.executor.execute_workflow(error_id)
        server.running_context.executor.execute_workflow(test_id)

        server.running_context.executor.wait_and_reset(3)

        keys = [error_key, multiaction_key]
        orderless_list_compare(self, list(metrics.workflow_metrics.keys()),
                               keys)

        for key in keys:
            orderless_list_compare(self, metrics.workflow_metrics[key],
                                   ['count', 'avg_time'])

        self.assertEqual(metrics.workflow_metrics[error_key]['count'], 2)
        self.assertEqual(metrics.workflow_metrics[multiaction_key]['count'], 1)
예제 #2
0
    def test_error_workflow(self):
        workflow_name = construct_workflow_name_key(
            'multistepError', 'multiactionErrorWorkflow')
        step_names = ['start', '1', 'error']
        setup_subscriptions_for_step(workflow_name, step_names)
        server.running_context.controller.execute_workflow(
            'multistepError', 'multiactionErrorWorkflow')

        with server.running_context.flask_app.app_context():
            server.running_context.shutdown_threads()

        steps = executed_steps('defaultController', workflow_name, self.start,
                               datetime.utcnow())
        self.assertEqual(len(steps), 2)
        names = [step['ancestry'].split(',')[-1] for step in steps]
        orderless_list_compare(self, names, ['start', 'error'])
        name_result = {
            'start': {
                "message": "HELLO WORLD"
            },
            'error': "REPEATING: Hello World"
        }
        for step in steps:
            name = step['ancestry'].split(',')[-1]
            self.assertIn(name, name_result)
            if type(name_result[name]) == dict:
                self.assertDictEqual(step['data']['result'], name_result[name])
            else:
                self.assertEqual(step['data']['result'], name_result[name])
예제 #3
0
    def test_workflow_metrics(self):
        server.running_context.controller.load_playbook(
            resource=config.test_workflows_path + 'multistepError.playbook')
        server.running_context.controller.load_playbook(
            resource=config.test_workflows_path +
            'multiactionWorkflowTest.playbook')

        error_key = 'multiactionErrorWorkflow'
        multiaction_key = 'multiactionWorkflow'
        server.running_context.controller.execute_workflow(
            'multistepError', 'multiactionErrorWorkflow')
        server.running_context.controller.execute_workflow(
            'multistepError', 'multiactionErrorWorkflow')
        server.running_context.controller.execute_workflow(
            'multiactionWorkflowTest', 'multiactionWorkflow')

        server.running_context.controller.shutdown_pool(3)

        keys = [error_key, multiaction_key]
        orderless_list_compare(self, list(metrics.workflow_metrics.keys()),
                               keys)

        for key in keys:
            orderless_list_compare(self, metrics.workflow_metrics[key],
                                   ['count', 'avg_time'])

        self.assertEqual(metrics.workflow_metrics[error_key]['count'], 2)
        self.assertEqual(metrics.workflow_metrics[multiaction_key]['count'], 1)
예제 #4
0
 def test_list_apps_with_interfaces(self):
     expected_apps = ['HelloWorld']
     response = self.app.get('/api/apps?interfaces_only=true',
                             headers=self.headers)
     self.assertEqual(response.status_code, SUCCESS)
     response = json.loads(response.get_data(as_text=True))
     orderless_list_compare(self, response, expected_apps)
예제 #5
0
 def test_delete_playbook_invalid_name(self):
     initial_playbooks = flask_server.running_context.controller.get_all_workflows(
     )
     initial_playbook_files = [
         os.path.splitext(playbook)[0]
         for playbook in helpers.locate_workflows_in_directory()
     ]
     response = self.delete_with_status_check(
         '/playbooks/junkPlaybookName',
         error='Playbook does not exist.',
         headers=self.headers,
         status_code=OBJECT_DNE_ERROR)
     self.assertDictEqual(response, {
         'error': 'Playbook does not exist.',
         'playbooks': initial_playbooks
     })
     self.assertFalse(
         flask_server.running_context.controller.is_playbook_registered(
             'junkPlaybookName'))
     final_playbook_files = [
         os.path.splitext(playbook)[0]
         for playbook in helpers.locate_workflows_in_directory()
     ]
     orderless_list_compare(self, final_playbook_files,
                            initial_playbook_files)
예제 #6
0
    def test_error_workflow(self):
        workflow_name = construct_workflow_name_key(
            'multistepError', 'multiactionErrorWorkflow')
        step_names = ['start', '1', 'error']
        setup_subscriptions_for_step(workflow_name, step_names)
        self.controller.execute_workflow('multistepError',
                                         'multiactionErrorWorkflow')

        shutdown_pool()

        steps = executed_steps('defaultController', workflow_name, self.start,
                               datetime.utcnow())
        self.assertEqual(len(steps), 2)
        names = [step['ancestry'].split(',')[-1] for step in steps]
        orderless_list_compare(self, names, ['start', 'error'])
        name_result = {
            'start': {
                'result': {
                    "message": "HELLO WORLD"
                },
                'status': 'Success'
            },
            'error': {
                'status': 'Success',
                'result': 'REPEATING: Hello World'
            }
        }
        for step in steps:
            name = step['ancestry'].split(',')[-1]
            self.assertIn(name, name_result)
            result = json.loads(step['data'])
            self.assertDictEqual(result['result'], name_result[name])
예제 #7
0
 def test_MultiActionWorkflow(self):
     workflow_name = construct_workflow_name_key('multiactionWorkflowTest',
                                                 'multiactionWorkflow')
     step_names = ['start', '1']
     setup_subscriptions_for_step(workflow_name, step_names)
     self.c.executeWorkflow('multiactionWorkflowTest',
                            'multiactionWorkflow')
     steps = executed_steps('defaultController', workflow_name, self.start,
                            datetime.utcnow())
     self.assertEqual(len(steps), 2)
     names = [step['ancestry'].split(',')[-1] for step in steps]
     orderless_list_compare(self, names, step_names)
     name_result = {
         'start': {
             "message": "HELLO WORLD"
         },
         '1': "REPEATING: Hello World"
     }
     for step in steps:
         name = step['ancestry'].split(',')[-1]
         self.assertIn(name, name_result)
         if type(name_result[name]) == dict:
             self.assertDictEqual(step['data']['result'], name_result[name])
         else:
             self.assertEqual(step['data']['result'], name_result[name])
예제 #8
0
    def test_convert_workflow_time_average(self):
        expected_json = {'workflows': [{'count': 100,
                                        'avg_time': '1 day, 0:01:40.000500',
                                        'name': 'workflow4'},
                                       {'count': 2,
                                        'avg_time': '0:00:00.001000',
                                        'name': 'workflow2'},
                                       {'count': 0,
                                        'avg_time': '0:01:40.000001',
                                        'name': 'workflow3'},
                                       {'count': 0,
                                        'avg_time': '100 days, 0:00:00.000001',
                                        'name': 'workflow1'}]}

        wf1 = WorkflowMetric(uuid.uuid4(), 'workflow1', timedelta(100, 0, 1).total_seconds())
        wf1.count = 0
        wf2 = WorkflowMetric(uuid.uuid4(), 'workflow2', timedelta(0, 0, 1000).total_seconds())
        wf2.count = 2
        wf3 = WorkflowMetric(uuid.uuid4(), 'workflow3', timedelta(0, 100, 1).total_seconds())
        wf3.count = 0
        wf4 = WorkflowMetric(uuid.uuid4(), 'workflow4', timedelta(1, 100, 500).total_seconds())
        wf4.count = 100

        current_app.running_context.execution_db.session.add_all([wf1, wf2, wf3, wf4])
        current_app.running_context.execution_db.session.commit()

        converted = _convert_workflow_time_averages()
        orderless_list_compare(self, converted.keys(), ['workflows'])
        self.assertEqual(len(converted['workflows']), len(expected_json['workflows']))
        for workflow in expected_json['workflows']:
            self.assertIn(workflow, converted['workflows'])
예제 #9
0
    def test_convert_workflow_time_average(self):
        expected_json = {'workflows': [{'count': 100,
                                        'avg_time': '1 day, 0:01:40.000500',
                                        'name': 'workflow4'},
                                       {'count': 2,
                                        'avg_time': '0:00:00.001000',
                                        'name': 'workflow2'},
                                       {'count': 0,
                                        'avg_time': '0:01:40.000001',
                                        'name': 'workflow3'},
                                       {'count': 0,
                                        'avg_time': '100 days, 0:00:00.000001',
                                        'name': 'workflow1'}]}

        wf1 = WorkflowMetric(uuid.uuid4(), 'workflow1', timedelta(100, 0, 1).total_seconds())
        wf1.count = 0
        wf2 = WorkflowMetric(uuid.uuid4(), 'workflow2', timedelta(0, 0, 1000).total_seconds())
        wf2.count = 2
        wf3 = WorkflowMetric(uuid.uuid4(), 'workflow3', timedelta(0, 100, 1).total_seconds())
        wf3.count = 0
        wf4 = WorkflowMetric(uuid.uuid4(), 'workflow4', timedelta(1, 100, 500).total_seconds())
        wf4.count = 100

        current_app.running_context.execution_db.session.add_all([wf1, wf2, wf3, wf4])
        current_app.running_context.execution_db.session.commit()

        converted = _convert_workflow_time_averages()
        orderless_list_compare(self, converted.keys(), ['workflows'])
        self.assertEqual(len(converted['workflows']), len(expected_json['workflows']))
        for workflow in expected_json['workflows']:
            self.assertIn(workflow, converted['workflows'])
예제 #10
0
    def test_simple_tiered_workflow(self):
        workflow_name1 = construct_workflow_name_key('tieredWorkflow', 'parentWorkflow')
        workflow_name2 = construct_workflow_name_key('tieredWorkflow', 'childWorkflow')
        step_names = ['start', '1']
        setup_subscriptions_for_step([workflow_name1, workflow_name2], step_names)
        self.controller.execute_workflow('tieredWorkflow', 'parentWorkflow')

        shutdown_pool()

        steps = executed_steps('defaultController', workflow_name1, self.start, datetime.utcnow())
        steps.extend(executed_steps('defaultController', workflow_name2, self.start, datetime.utcnow()))
        ancestries = [step['ancestry'].split(',') for step in steps]
        name_ids = [(ancestry[-2], ancestry[-1]) for ancestry in ancestries]
        expected_ids = [(workflow_name1, 'start'), (workflow_name1, '1'), (workflow_name2, 'start')]
        orderless_list_compare(self, name_ids, expected_ids)

        name_result = {(workflow_name1, 'start'):  {'status': 'Success', 'result': 'REPEATING: Parent Step One'},
                       (workflow_name2, 'start'):  {'status': 'Success', 'result': 'REPEATING: Child Step One'},
                       (workflow_name1, '1'): {'status': 'Success', 'result': 'REPEATING: Parent Step Two'}}

        for step in steps:
            ancestry = step['ancestry'].split(',')
            name_id = (ancestry[-2], ancestry[-1])
            self.assertIn(name_id, name_result)
            result = json.loads(step['data'])
            if type(name_result[name_id]) == dict:
                self.assertDictEqual(result['result'], name_result[name_id])
            else:
                self.assertEqual(result['result'], name_result[name_id])
예제 #11
0
    def test_TemplatedWorkflow(self):
        self.c.loadWorkflowsFromFile(path=config.testWorkflowsPath +
                                     'templatedWorkflowTest.workflow')
        workflow_name = construct_workflow_name_key('templatedWorkflowTest',
                                                    'templatedWorkflow')
        step_names = ['start', '1']
        setup_subscriptions_for_step(workflow_name, step_names)
        self.c.executeWorkflow('templatedWorkflowTest', 'templatedWorkflow')
        steps = executed_steps('defaultController', workflow_name, self.start,
                               datetime.utcnow())

        self.assertEqual(
            len(steps), 2, 'Unexpected number of steps executed. '
            'Expected {0}, got {1}'.format(2, len(steps)))
        names = [step['ancestry'].split(',')[-1] for step in steps]
        orderless_list_compare(self, names, step_names)
        name_result = {
            'start': {
                "message": "HELLO WORLD"
            },
            '1': "REPEATING: {'message': 'HELLO WORLD'}"
        }

        for step in steps:
            name = step['ancestry'].split(',')[-1]
            self.assertIn(name, name_result)
            if type(name_result[name]) == dict:
                self.assertDictEqual(step['data']['result'], name_result[name])
            else:
                self.assertEqual(step['data']['result'], name_result[name])
예제 #12
0
    def test_workflow_with_dataflow(self):
        workflow_name = construct_workflow_name_key('dataflowTest',
                                                    'dataflowWorkflow')
        step_names = ['start', '1', '2']
        setup_subscriptions_for_step(workflow_name, step_names)
        self.controller.execute_workflow('dataflowTest', 'dataflowWorkflow')

        shutdown_pool()

        steps = executed_steps('defaultController', workflow_name, self.start,
                               datetime.utcnow())
        self.assertEqual(len(steps), 3)
        names = [step['ancestry'].split(',')[-1] for step in steps]
        orderless_list_compare(self, names, ['start', '1', '2'])
        name_result = {
            'start': {
                'result': 6,
                'status': 'Success'
            },
            '1': {
                'result': 6,
                'status': 'Success'
            },
            '2': {
                'result': 15,
                'status': 'Success'
            }
        }
        for step in steps:
            name = step['ancestry'].split(',')[-1]
            self.assertIn(name, name_result)
            result = json.loads(step['data'])
            self.assertDictEqual(result['result'], name_result[name])
예제 #13
0
 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])
예제 #14
0
 def test_read_all_app_apis(self):
     response = self.test_client.get('/api/apps/apis', headers=self.headers)
     self.assertEqual(response.status_code, SUCCESS)
     response = json.loads(response.get_data(as_text=True))
     orderless_list_compare(
         self, [app['name'] for app in response],
         ['HelloWorldBounded', 'DailyQuote', 'HelloWorld'])
예제 #15
0
    def test_delete_playbook_no_file(self):
        initial_playbook_files = [
            os.path.splitext(playbook)[0]
            for playbook in helpers.locate_playbooks_in_directory()
        ]
        data = {"name": "test_playbook"}
        self.app.put('/api/playbooks',
                     headers=self.headers,
                     content_type="application/json",
                     data=json.dumps(data))
        self.delete_with_status_check('/api/playbooks/test_playbook',
                                      headers=self.headers)

        self.assertTrue(
            flask_server.running_context.controller.is_playbook_registered(
                'test'))
        self.assertFalse(
            flask_server.running_context.controller.is_playbook_registered(
                'test_playbook'))

        final_playbook_files = [
            os.path.splitext(playbook)[0]
            for playbook in helpers.locate_playbooks_in_directory()
        ]
        orderless_list_compare(self, final_playbook_files,
                               initial_playbook_files)
예제 #16
0
 def test_convert_workflow_time_average(self):
     test1 = {'worfklow1': {'count': 0,
                            'avg_time': timedelta(100, 0, 1)},
              'worfklow2': {'count': 2,
                            'avg_time': timedelta(0, 0, 1000)},
              'worfklow3': {'count': 0,
                            'avg_time': timedelta(0, 100, 1)},
              'worfklow4': {'count': 100,
                            'avg_time': timedelta(1, 100, 500)}}
     expected_json = {'workflows': [{'count': 100,
                                     'avg_time': '1 day, 0:01:40.000500',
                                     'name': 'worfklow4'},
                                    {'count': 2,
                                     'avg_time': '0:00:00.001000',
                                     'name': 'worfklow2'},
                                    {'count': 0,
                                     'avg_time': '0:01:40.000001',
                                     'name': 'worfklow3'},
                                    {'count': 0,
                                     'avg_time': '100 days, 0:00:00.000001',
                                     'name': 'worfklow1'}]}
     metrics.workflow_metrics = test1
     converted = _convert_workflow_time_averages()
     orderless_list_compare(self, converted.keys(), ['workflows'])
     self.assertEqual(len(converted['workflows']), len(expected_json['workflows']))
     for workflow in expected_json['workflows']:
         self.assertIn(workflow, converted['workflows'])
예제 #17
0
 def test_display_workflow_element(self):
     workflow_filename = os.path.join(".", "tests", "testWorkflows",
                                      'multiactionWorkflowTest.workflow')
     flask_server.running_context.controller.load_workflows_from_file(
         path=workflow_filename)
     workflow = flask_server.running_context.controller.get_workflow(
         'multiactionWorkflowTest', 'multiactionWorkflow')
     ancestries_to_json = [
         ([], {
             "steps": ['start', '1']
         }),
         (['start'], workflow.steps['start'].as_json(with_children=False)),
         (['1'], workflow.steps['1'].as_json(with_children=False)),
         (['start', '1'], workflow.steps['start'].conditionals[0].as_json(
             with_children=False)),
         (['start', '1', 'regMatch'],
          workflow.steps['start'].conditionals[0].flags[0].as_json(
              with_children=False)),
         (['start', '1', 'regMatch', 'length'], workflow.steps['start'].
          conditionals[0].flags[0].filters[0].as_json())
     ]
     for ancestry, expected_output in ancestries_to_json:
         data = {"ancestry": ancestry}
         response = self.get_with_status_check(
             '/playbooks/multiactionWorkflowTest/workflows/multiactionWorkflow',
             data=json.dumps(data),
             content_type='application/json',
             headers=self.headers)
         self.assertIn('element', response)
         orderless_list_compare(self, response['element'], expected_output)
예제 #18
0
 def test_add_workflow(self):
     workflow = Workflow('wf_name', 0)
     playbook = Playbook('test', [workflow])
     playbook.add_workflow(Workflow('test2', 0))
     orderless_list_compare(
         self, [workflow.name for workflow in playbook.workflows],
         ['wf_name', 'test2'])
예제 #19
0
    def test_workflow_result_recording(self):
        flaskserver.running_context.controller.execute_workflow(
            'basicWorkflowTest', 'helloWorldWorkflow')
        flaskserver.running_context.controller.execute_workflow(
            'loopWorkflow', 'loopWorkflow')
        flaskserver.running_context.controller.execute_workflow(
            'testExecutionWorkflow', 'helloWorldWorkflow')
        self.assertEqual(len(server.workflowresults.results), 3)

        def __get_record(playbook, workflow):
            name = construct_workflow_name_key(playbook, workflow)
            return next((record for record in server.workflowresults.results
                         if record['name'] == name), None)

        input_ouput = {
            ('basicWorkflowTest', 'helloWorldWorkflow'):
            "REPEATING: Hello World",
            ('loopWorkflow', 'loopWorkflow'):
            'REPEATING: 5',
            ('testExecutionWorkflow', 'helloWorldWorkflow'):
            "REPEATING: Hello World"
        }
        for (playbook, workflow), expected_result in input_ouput.items():
            record = __get_record(playbook, workflow)
            self.assertIsNotNone(record)
            orderless_list_compare(self, list(record.keys()),
                                   ['name', 'timestamp', 'result'])
            self.assertIsInstance(record['timestamp'], str)
            self.assertEqual(record['result'], expected_result)
예제 #20
0
    def test_delete_playbook_no_file(self):
        initial_playbooks = flask_server.running_context.controller.get_all_workflows(
        )
        initial_playbook_files = [
            os.path.splitext(playbook)[0]
            for playbook in helpers.locate_workflows_in_directory()
        ]
        self.app.put('/playbooks/test_playbook', headers=self.headers)
        response = self.delete_with_status_check('/playbooks/test_playbook',
                                                 headers=self.headers)
        self.assertDictEqual(response, {'playbooks': initial_playbooks})

        self.assertTrue(
            flask_server.running_context.controller.is_playbook_registered(
                'test'))
        self.assertFalse(
            flask_server.running_context.controller.is_playbook_registered(
                'test_playbook'))

        final_playbook_files = [
            os.path.splitext(playbook)[0]
            for playbook in helpers.locate_workflows_in_directory()
        ]
        orderless_list_compare(self, final_playbook_files,
                               initial_playbook_files)
예제 #21
0
 def test_read_all_app_apis_with_device(self):
     response = self.app.get('/api/apps/apis?field_name=device_apis', headers=self.headers)
     self.assertEqual(response.status_code, SUCCESS)
     response = json.loads(response.get_data(as_text=True))
     orderless_list_compare(self, [app['name'] for app in response],
                            ['HelloWorldBounded', 'DailyQuote', 'HelloWorld'])
     for app in response:
         self.assertSetEqual(set(app.keys()), {'name', 'device_apis'})
예제 #22
0
 def test_read_all_app_apis_with_device(self):
     response = self.test_client.get('/api/apps/apis?field_name=device_apis', headers=self.headers)
     self.assertEqual(response.status_code, SUCCESS)
     response = json.loads(response.get_data(as_text=True))
     orderless_list_compare(self, [app['name'] for app in response],
                            ['HelloWorldBounded', 'DailyQuote', 'HelloWorld'])
     for app in response:
         self.assertSetEqual(set(app.keys()), {'name', 'device_apis'})
예제 #23
0
 def test_get_all_list_actions(self):
     expected_json = {"HelloWorld": ['helloWorld', 'repeatBackToMe', 'returnPlusOne', 'pause']}
     response = self.app.get('/apps/actions', headers=self.headers)
     self.assertEqual(response.status_code, 200)
     response = json.loads(response.get_data(as_text=True))
     orderless_list_compare(self, response.keys(), expected_json.keys())
     for app, functions in response.items():
         orderless_list_compare(self, functions, expected_json[app])
예제 #24
0
 def test_list_functions(self):
     expected_actions = [
         'helloWorld', 'repeatBackToMe', 'returnPlusOne', 'pause'
     ]
     response = self.get_with_status_check('/apps/HelloWorld/actions',
                                           headers=self.headers)
     self.assertIn('actions', response)
     orderless_list_compare(self, response['actions'], expected_actions)
예제 #25
0
 def test_get_all_devices_for_existing_app_with_devices(self):
     devices = ['dev1', 'dev2', '', 'dev3']
     flaskserver.running_context.db.session.add(
         flaskserver.running_context.App(app='App1', devices=devices))
     flaskserver.running_context.db.session.commit()
     orderless_list_compare(
         self,
         [device.name
          for device in App.get_all_devices_for_app('App1')], devices)
예제 #26
0
 def test_delete_playbook_invalid_name(self):
     initial_playbooks = flask_server.running_context.controller.get_all_workflows()
     initial_playbook_files = [os.path.splitext(playbook)[0] for playbook in helpers.locate_workflows_in_directory()]
     response = self.app.post('/playbook/junkPlaybookName/delete', headers=self.headers)
     self.assertEqual(response.status_code, 200)
     response = json.loads(response.get_data(as_text=True))
     self.assertDictEqual(response, {'status': 'success', 'playbooks': initial_playbooks})
     self.assertFalse(flask_server.running_context.controller.is_playbook_registerd('junkPlaybookName'))
     final_playbook_files = [os.path.splitext(playbook)[0] for playbook in helpers.locate_workflows_in_directory()]
     orderless_list_compare(self, final_playbook_files, initial_playbook_files)
예제 #27
0
 def test_list_functions(self):
     expected_actions = [
         'pause', 'Add Three', 'repeatBackToMe', 'Buggy', 'returnPlusOne',
         'helloWorld', 'Hello World', 'Json Sample', 'Sample Event',
         'global1', 'global2'
     ]
     response = self.get_with_status_check('/api/apps/HelloWorld/actions',
                                           headers=self.headers)
     self.assertIn('actions', response)
     orderless_list_compare(self, response['actions'], expected_actions)
예제 #28
0
 def test_delete_playbook_invalid_name(self):
     initial_playbook_files = [os.path.splitext(playbook)[0] for playbook in
                               helpers.locate_playbooks_in_directory()]
     self.delete_with_status_check('/api/playbooks/junkPlaybookName', error='Playbook does not exist',
                                   headers=self.headers,
                                   status_code=OBJECT_DNE_ERROR)
     self.assertFalse(flask_server.running_context.controller.is_playbook_registered('junkPlaybookName'))
     final_playbook_files = [os.path.splitext(playbook)[0] for playbook in
                             helpers.locate_playbooks_in_directory()]
     orderless_list_compare(self, final_playbook_files, initial_playbook_files)
 def test_removeWorkflow(self):
     initial_workflows = list(self.c.workflows.keys())
     self.c.create_workflow_from_template('emptyWorkflow', 'emptyWorkflow')
     self.assertEqual(len(self.c.workflows), 3)
     success = self.c.removeWorkflow('emptyWorkflow', 'emptyWorkflow')
     self.assertTrue(success)
     self.assertEqual(len(self.c.workflows), 2)
     key = _WorkflowKey('emptyWorkflow', 'emptyWorkflow')
     self.assertNotIn(key, self.c.workflows)
     orderless_list_compare(self, list(self.c.workflows.keys()),
                            initial_workflows)
예제 #30
0
    def test_workflow_with_dataflow_step_not_executed(self):
        workflow_name = construct_workflow_name_key('dataflowTest', 'dataflowWorkflow')
        step_names = ['start', '1']
        setup_subscriptions_for_step(workflow_name, step_names)
        self.controller.execute_workflow('dataflowTest', 'dataflowWorkflow')

        shutdown_pool()

        steps = executed_steps('defaultController', workflow_name, self.start, datetime.utcnow())
        self.assertEqual(len(steps), 2)
        names = [step['ancestry'].split(',')[-1] for step in steps]
        orderless_list_compare(self, names, ['start', '1'])
예제 #31
0
    def test_locate_workflows(self):
        expected_workflows = [
            'basicWorkflowTest.workflow', 'DailyQuote.workflow',
            'loopWorkflow.workflow', 'multiactionWorkflowTest.workflow',
            'pauseWorkflowTest.workflow', 'multistepError.workflow',
            'simpleDataManipulationWorkflow.workflow',
            'templatedWorkflowTest.workflow', 'testExecutionWorkflow.workflow',
            'testScheduler.workflow', 'tieredWorkflow.workflow'
        ]
        received_workflows = locate_workflows_in_directory(test_workflows_path)
        orderless_list_compare(self, received_workflows, expected_workflows)

        self.assertListEqual(locate_workflows_in_directory('.'), [])
예제 #32
0
    def test_save_workflow(self):
        workflow_name = list(flask_server.running_context.controller.workflows.keys())[0].workflow
        initial_workflow = flask_server.running_context.controller.get_workflow('test', workflow_name)
        initial_steps = dict(initial_workflow.steps)
        initial_workflow_cytoscape = list(initial_workflow.get_cytoscape_data())
        added_step_cytoscape = {'data': {'id': 'new_id',
                                         'parameters': {'errors': [],
                                                        'name': 'new_id',
                                                        'app': 'new_app',
                                                        'next': [],
                                                        'device': 'new_device',
                                                        'action': 'new_action',
                                                        'input': {}}},
                                'group': 'nodes'}
        initial_workflow_cytoscape.insert(0, added_step_cytoscape)
        data = {"cytoscape": json.dumps(initial_workflow_cytoscape)}
        response = self.app.post('/playbook/test/{0}/save'.format(workflow_name),
                                 data=json.dumps(data),
                                 headers=self.headers,
                                 content_type='application/json')
        self.assertEqual(response.status_code, 200)
        response = json.loads(response.get_data(as_text=True))
        self.assertEqual(response['status'], 'success')

        resulting_workflow = flask_server.running_context.controller.get_workflow('test', workflow_name)

        # compare the steps in initial and final workflow
        self.assertEqual(len(resulting_workflow.steps.keys()), len(list(initial_steps.keys())) + 1)
        for step_name, initial_step in initial_steps.items():
            self.assertIn(step_name, resulting_workflow.steps.keys())
            self.assertDictEqual(initial_step.as_json(), resulting_workflow.steps[step_name].as_json())

        # assert that the file has been saved to a file
        workflows = [path.splitext(workflow)[0]
                     for workflow in os.listdir(coreWorkflows) if workflow.endswith('.workflow')]
        matching_workflows = [workflow for workflow in workflows if workflow == 'test']
        self.assertEqual(len(matching_workflows), 1)

        # assert that the file loads properly after being saved
        flask_server.running_context.controller.workflows = {}
        flask_server.running_context.controller.loadWorkflowsFromFile(os.path.join(coreWorkflows, 'test.workflow'))
        orderless_list_compare(self,
                               [key.workflow for key in flask_server.running_context.controller.workflows.keys()],
                               ['helloWorldWorkflow'])
        loaded_workflow = flask_server.running_context.controller.get_workflow('test', workflow_name)

        # compare the steps in loaded and expected workflow
        self.assertEqual(len(loaded_workflow.steps.keys()), len(list(resulting_workflow.steps.keys())))
        for step_name, loaded_step in loaded_workflow.steps.items():
            self.assertIn(step_name, resulting_workflow.steps.keys())
            self.assertDictEqual(loaded_step.as_json(), resulting_workflow.steps[step_name].as_json())
예제 #33
0
    def test_convert_action_time_average(self):
        expected_json = {'apps': [{'count': 100,
                                   'name': 'app2',
                                   'actions': [{'error_metrics': {'count': 100,
                                                                  'avg_time': '1 day, 0:01:40.000500'},
                                                'success_metrics': {'count': 0,
                                                                    'avg_time': '0:01:40.000001'},
                                                'name': 'action1'}]},
                                  {'count': 2,
                                   'name': 'app1',
                                   'actions': [{'success_metrics': {'count': 0,
                                                                    'avg_time': '100 days, 0:00:00.000001'},
                                                'name': 'action1'},
                                               {'error_metrics': {'count': 2,
                                                                  'avg_time': '0:00:00.001000'},
                                                'name': 'action2'}]}]}

        action_status_one = ActionStatusMetric("success", timedelta(100, 0, 1).total_seconds())
        action_status_one.count = 0
        action_status_two = ActionStatusMetric("error", timedelta(0, 0, 1000).total_seconds())
        action_status_two.count = 2
        app_one = AppMetric("app1", actions=[
            ActionMetric(uuid.uuid4(), "action1", [action_status_one]),
            ActionMetric(uuid.uuid4(), "action2", [action_status_two])])
        app_one.count = 2

        as_one = ActionStatusMetric("success", timedelta(0, 100, 1).total_seconds())
        as_one.count = 0
        as_two = ActionStatusMetric("error", timedelta(1, 100, 500).total_seconds())
        as_two.count = 100
        app_two = AppMetric("app2", actions=[
            ActionMetric(uuid.uuid4(), "action1", [as_one, as_two])])
        app_two.count = 100

        current_app.running_context.execution_db.session.add(app_one)
        current_app.running_context.execution_db.session.add(app_two)
        current_app.running_context.execution_db.session.commit()

        converted = _convert_action_time_averages()
        orderless_list_compare(self, converted.keys(), ['apps'])
        self.assertEqual(len(converted['apps']), len(expected_json['apps']))
        orderless_list_compare(self, [x['name'] for x in converted['apps']], ['app1', 'app2'])

        app1_metrics = [x for x in converted['apps'] if x['name'] == 'app1'][0]
        expected_app1_metrics = [x for x in expected_json['apps'] if x['name'] == 'app1'][0]
        orderless_list_compare(self, app1_metrics.keys(), ['count', 'name', 'actions'])
        self.assertEqual(app1_metrics['count'], expected_app1_metrics['count'])
        self.assertTrue(len(app1_metrics['actions']), len(expected_app1_metrics['actions']))
        for action_metric in expected_app1_metrics['actions']:
            self.assertIn(action_metric, app1_metrics['actions'])

        app2_metrics = [x for x in converted['apps'] if x['name'] == 'app2'][0]
        expected_app2_metrics = [x for x in expected_json['apps'] if x['name'] == 'app2'][0]
        orderless_list_compare(self, app2_metrics.keys(), ['count', 'name', 'actions'])
        self.assertEqual(app2_metrics['count'], expected_app2_metrics['count'])
        self.assertTrue(len(app2_metrics['actions']), len(expected_app2_metrics['actions']))
        for action_metric in expected_app2_metrics['actions']:
            self.assertIn(action_metric, app2_metrics['actions'])
예제 #34
0
 def test_add_workflow(self):
     workflow = Workflow('wf_name', 0)
     playbook = Playbook('test', [workflow])
     playbook.add_workflow(Workflow('test2', 0))
     orderless_list_compare(self, [workflow.name for workflow in playbook.workflows], ['wf_name', 'test2'])
예제 #35
0
 def test_get_all_workflow_names(self):
     workflows = [Workflow(str(i), 0) for i in range(3)]
     playbook = Playbook('test', workflows)
     orderless_list_compare(self, playbook.get_all_workflow_names(), ['0', '1', '2'])
예제 #36
0
 def test_read_all_app_apis(self):
     response = self.test_client.get('/api/apps/apis', headers=self.headers)
     self.assertEqual(response.status_code, SUCCESS)
     response = json.loads(response.get_data(as_text=True))
     orderless_list_compare(self, [app['name'] for app in response],
                            ['HelloWorldBounded', 'DailyQuote', 'HelloWorld'])
예제 #37
0
 def test_read_all_apps(self):
     expected_apps = ['HelloWorldBounded', 'DailyQuote', 'HelloWorld']
     response = self.test_client.get('/api/apps', headers=self.headers)
     self.assertEqual(response.status_code, SUCCESS)
     response = json.loads(response.get_data(as_text=True))
     orderless_list_compare(self, response, expected_apps)
예제 #38
0
 def test_list_apps(self):
     expected_apps = ['HelloWorld', 'DailyQuote', 'HelloWorldBounded']
     orderless_list_compare(self, expected_apps, list_apps(walkoff.config.Config.APPS_PATH))
예제 #39
0
 def test_get_all_workflow_ids(self):
     workflows = [Workflow(str(i), 0) for i in range(3)]
     playbook = Playbook('test', workflows)
     orderless_list_compare(self, playbook.get_all_workflow_ids(), list(workflow.id for workflow in workflows))