From 28f5f7af97d76935e4b9070fb92779de753c076e Mon Sep 17 00:00:00 2001 From: sahanaspc Date: Fri, 22 Jun 2018 22:47:08 +0000 Subject: [PATCH] [DM] Changes to the job manager test cases Modified job manager test cases to support playbook chaining. Closes-Bug: #1778301 Change-Id: If461e8e0853e1dfb4e8ef8e022a848b52bef05d6 (cherry picked from commit bfaf30ebb341ecd326a3f7a368546e5deecd8f1b) --- SConscript | 1 + .../fabric-ansible/tests/test_job_manager.py | 234 +++-- .../tests/test_job_manager_err_cases.py | 829 ++++++++++-------- 3 files changed, 608 insertions(+), 456 deletions(-) diff --git a/SConscript b/SConscript index fd778273873..56a8b26cc36 100644 --- a/SConscript +++ b/SConscript @@ -21,6 +21,7 @@ env.Alias('controller/test', [ 'controller/src/xmpp:test', 'src/contrail-api-client/api-lib:test', 'controller/src/config/api-server:test', + 'controller/src/config/fabric-ansible:tests', 'controller/src/config/schema-transformer:test', 'controller/src/ksync:test', 'src/contrail-common/database/gendb:test', diff --git a/src/config/fabric-ansible/tests/test_job_manager.py b/src/config/fabric-ansible/tests/test_job_manager.py index 0276e5f67f6..417d9588a31 100644 --- a/src/config/fabric-ansible/tests/test_job_manager.py +++ b/src/config/fabric-ansible/tests/test_job_manager.py @@ -1,4 +1,4 @@ -# +#!/usr/bin/python # Copyright (c) 2018 Juniper Networks, Inc. All rights reserved. # import gevent @@ -6,10 +6,10 @@ gevent.monkey.patch_all(thread=False) import sys import os +import io import logging from flexmock import flexmock import subprocess32 - from vnc_api.vnc_api import PlaybookInfoType from vnc_api.vnc_api import PlaybookInfoListType from vnc_api.vnc_api import JobTemplate @@ -17,13 +17,14 @@ sys.path.append('../common/tests') import test_case -from job_manager.job_mgr import JobManager +from job_manager.job_mgr import WFManager from job_manager.job_utils import JobStatus from test_job_manager_utils import TestJobManagerUtils logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) +loop_var = 0 class TestJobManager(test_case.JobTestCase): @@ -33,17 +34,20 @@ def setUpClass(cls, *args, **kwargs): cls.console_handler.setLevel(logging.DEBUG) logger.addHandler(cls.console_handler) super(TestJobManager, cls).setUpClass(*args, **kwargs) + # end setUpClass @classmethod def tearDownClass(cls, *args, **kwargs): logger.removeHandler(cls.console_handler) super(TestJobManager, cls).tearDownClass(*args, **kwargs) + # end tearDownClass + #Test for a single playbook in the workflow template def test_execute_job_success(self): # create job template - play_info = PlaybookInfoType(playbook_uri='job_manager_test.yaml', + play_info = PlaybookInfoType(playbook_uri='job_manager_test.yml', vendor='Juniper', device_family='MX') playbooks_list = PlaybookInfoListType(playbook_info=[play_info]) @@ -54,21 +58,61 @@ def test_execute_job_success(self): name='Test_template') job_template_uuid = self._vnc_lib.job_template_create(job_template) + # mocking creation of a process + self.mock_play_book_execution() + # mocking Sandesh + TestJobManagerUtils.mock_sandesh_check() + # getting details required for job manager execution + job_input_json, log_utils = TestJobManagerUtils.get_min_details( + job_template_uuid) + + wm = WFManager(log_utils.get_config_logger(), self._vnc_lib, + job_input_json, log_utils) + wm.start_job() + self.assertEqual(wm.result_handler.job_result_status, + JobStatus.SUCCESS) + + #Test for job success with multiple playbooks in the workflow template + def test_execute_job_success_multiple_templates(self): + # create job template + play_info = PlaybookInfoType(playbook_uri='job_manager_test.yml', + vendor='Juniper', + device_family='MX') + play_info1 = PlaybookInfoType( + playbook_uri='job_manager_test_multiple.yml', + vendor='Juniper', + device_family='QFX') + playbooks_list = PlaybookInfoListType(playbook_info=[play_info, + play_info1]) + job_template = JobTemplate(job_template_type='device', + job_template_job_runtime='ansible', + job_template_multi_device_job=False, + job_template_playbooks=playbooks_list, + name='Test_template1') + job_template_uuid = self._vnc_lib.job_template_create(job_template) + + # mocking creation of a process self.mock_play_book_execution() + # mocking Sandesh TestJobManagerUtils.mock_sandesh_check() + # getting details required for job manager execution job_input_json, log_utils = TestJobManagerUtils.get_min_details( - job_template_uuid) - jm = JobManager(log_utils.get_config_logger(), - self._vnc_lib, job_input_json, log_utils) - jm.start_job() - self.assertEqual(jm.result_handler.job_result_status, + job_template_uuid) + + wm = WFManager(log_utils.get_config_logger(), self._vnc_lib, + job_input_json, log_utils) + wm.start_job() + self.assertEqual(wm.result_handler.job_result_status, JobStatus.SUCCESS) - # to test the case when only device vendor is passed in job_template_input + #to test the case when only device vendor is passed in job_template_input def test_execute_job_with_vendor_only(self): play_info = PlaybookInfoType(playbook_uri='job_manager_test.yaml', vendor='Juniper') - playbooks_list = PlaybookInfoListType(playbook_info=[play_info]) + play_info1 = PlaybookInfoType(playbook_uri='job_manager_test_multiple.yml', + vendor='Juniper') + + playbooks_list = PlaybookInfoListType(playbook_info=[play_info,play_info1]) job_template = JobTemplate(job_template_type='device', job_template_job_runtime='ansible', job_template_multi_device_job=True, @@ -81,22 +125,25 @@ def test_execute_job_with_vendor_only(self): TestJobManagerUtils.mock_sandesh_check() job_input_json, log_utils = self.get_details(job_template_uuid) - jm = JobManager(log_utils.get_config_logger(), self._vnc_lib, - job_input_json, log_utils) - jm.start_job() - self.assertEqual(jm.result_handler.job_result_status, + wm = WFManager(log_utils.get_config_logger(), self._vnc_lib, + job_input_json, log_utils) + + wm.start_job() + self.assertEqual(wm.result_handler.job_result_status, JobStatus.SUCCESS) - # to test the case when device vendor and multiple device families are - # passed in job_template_input + #to test the case when device vendor and multiple device families are + #passed in job_template_input def test_execute_job_multiple_device_families(self): - play_info_mx = PlaybookInfoType(playbook_uri='job_manager_test.yaml', - vendor='Juniper', device_family='MX') - play_info_qfx = PlaybookInfoType(playbook_uri='job_manager_test.yaml', - vendor='Juniper', device_family='QFX') + play_info_mx = PlaybookInfoType( + playbook_uri='job_manager_test.yaml', + vendor='Juniper', device_family='MX') + play_info_qfx = PlaybookInfoType( + playbook_uri='job_manager_test.yaml', + vendor='Juniper', device_family='QFX') playbooks_list = PlaybookInfoListType( - playbook_info=[play_info_qfx, play_info_mx]) + playbook_info=[play_info_qfx, play_info_mx]) job_template = JobTemplate(job_template_type='device', job_template_job_runtime='ansible', job_template_multi_device_job=True, @@ -111,24 +158,63 @@ def test_execute_job_multiple_device_families(self): job_input_json, log_utils = self.get_details(job_template_uuid) - jm = JobManager(log_utils.get_config_logger(), self._vnc_lib, - job_input_json, log_utils) - jm.start_job() - self.assertEqual(jm.result_handler.job_result_status, + wm = WFManager(log_utils.get_config_logger(), self._vnc_lib, + job_input_json, log_utils) + + wm.start_job() + self.assertEqual(wm.result_handler.job_result_status, + JobStatus.SUCCESS) + + #to test the case when device vendor and multiple device families are + #passed in workflow_template_input along with multiple playbooks + #TODO - The task weightage array from be gotten from the workflow_template. Currently the test case fails beacuse the task weightage array is hard coded. + def test_execute_job_multiple_device_families_multiple_playbooks(self): + play_info_mx = PlaybookInfoType( + playbook_uri='job_manager_test.yaml', + vendor='Juniper', device_family='MX') + play_info_qfx = PlaybookInfoType( + playbook_uri='job_manager_test.yaml', + vendor='Juniper', device_family='QFX') + play_info_2 = PlaybookInfoType( + playbook_uri='job_manager_test2.yaml', + vendor='Juniper') + + playbooks_list = PlaybookInfoListType( + playbook_info=[play_info_qfx, play_info_mx, play_info_2]) + job_template = JobTemplate(job_template_type='device', + job_template_job_runtime='ansible', + job_template_multi_device_job=True, + job_template_playbooks=playbooks_list, + name='Test_template_multi_devfamilies') + job_template_uuid = self._vnc_lib.job_template_create(job_template) + + # mock the play book executor call + self.mock_play_book_execution() + + TestJobManagerUtils.mock_sandesh_check() + + job_input_json, log_utils = self.get_details(job_template_uuid) + + wm = WFManager(log_utils.get_config_logger(), self._vnc_lib, + job_input_json, log_utils) + + wm.start_job() + self.assertEqual(wm.result_handler.job_result_status, JobStatus.SUCCESS) - # to test the case when multiple device vendors are - # passed in job_template_input - def test_execute_job_multiple_vendors(self): + #to test the case when multiple device vendors and multiple_playbooks are + #passed in job_template_input + #TODO - Test case fails for the same reason as above.the hardcoded array only considers chaining of two playbooks. + def test_execute_job_multiple_vendors_multiple_playbooks(self): play_info_juniper_mx = PlaybookInfoType( - playbook_uri='job_manager_test.yaml', - vendor='Juniper', device_family='MX') + playbook_uri='job_manager_test.yaml', + vendor='Juniper', device_family='MX') play_info_juniper_qfx = PlaybookInfoType( - playbook_uri='job_manager_test.yaml', - vendor='Juniper', device_family='QFX') + playbook_uri='job_manager_test.yaml', + vendor='Juniper', device_family='QFX') play_info_arista_df = PlaybookInfoType( - playbook_uri='job_manager_test.yaml', - vendor='Arista', device_family='df') + playbook_uri='job_manager_test2.yaml', + vendor='Arista', device_family='df') playbooks_list = PlaybookInfoListType( playbook_info=[play_info_arista_df, play_info_juniper_qfx, @@ -144,22 +230,24 @@ def test_execute_job_multiple_vendors(self): self.mock_play_book_execution() TestJobManagerUtils.mock_sandesh_check() - job_input_json, log_utils = self.get_details(job_template_uuid) - jm = JobManager(log_utils.get_config_logger(), self._vnc_lib, - job_input_json, log_utils) - jm.start_job() - self.assertEqual(jm.result_handler.job_result_status, + wm = WFManager(log_utils.get_config_logger(), self._vnc_lib, + job_input_json, log_utils) + + wm.start_job() + self.assertEqual(wm.result_handler.job_result_status, JobStatus.SUCCESS) - # to test the case when no vendor is passed in job_template_input + #to test the case when no vendor is passed in workflow_template_input for + #second playbook_info - Depending on the device vendor, the right + # playbooks has to be picked. def test_execute_job_no_vendor(self): play_info_juniper_qfx = PlaybookInfoType( - playbook_uri='job_manager_test.yaml', - vendor='Juniper', device_family='QFX') + playbook_uri='job_manager_test.yaml', + vendor='Juniper', device_family='QFX') play_info_vendor_agnostic = PlaybookInfoType( - playbook_uri='job_manager_test.yaml') + playbook_uri='job_manager_test2.yaml') playbooks_list = PlaybookInfoListType( playbook_info=[play_info_juniper_qfx, play_info_vendor_agnostic]) @@ -179,38 +267,52 @@ def test_execute_job_no_vendor(self): job_input_json, log_utils = self.get_details(job_template_uuid) - jm = JobManager(log_utils.get_config_logger(), self._vnc_lib, - job_input_json, log_utils) - jm.start_job() - self.assertEqual(jm.result_handler.job_result_status, - JobStatus.SUCCESS) + wm = WFManager(log_utils.get_config_logger(), self._vnc_lib, + job_input_json, log_utils) - def get_details(self, job_template_uuid): + wm.start_job() + self.assertEqual(wm.result_handler.job_result_status, + JobStatus.SUCCESS) + def get_details(self, job_template_uuid): job_input_json, log_utils = TestJobManagerUtils.get_min_details( - job_template_uuid) + job_template_uuid) job_input_json.update({ - "params": {"device_list": - ["aad74e24-a00b-4eb3-8412-f8b9412925c3"]}, - "device_json": { - "aad74e24-a00b-4eb3-8412-f8b9412925c3": - {'device_vendor': 'Juniper', - 'device_family': 'MX', - 'device_username': 'username', - 'device_password': 'pswd'}} - }) + "params": {"device_list": + ["aad74e24-a00b-4eb3-8412-f8b9412925c3"]}, + "device_json": { + "aad74e24-a00b-4eb3-8412-f8b9412925c3": + {'device_vendor': 'Juniper', + 'device_family': 'MX', + 'device_username': 'username', + 'device_password': 'pswd'}} + }) return job_input_json, log_utils def mock_play_book_execution(self): - # mock the call to invoke the playbook - fake_process = flexmock(returncode=0, pid=123) + #mocking the loop with the stdout.readline function. + loop_var = 0 + def mock_readline(): + global loop_var + loop_var += 1 + if loop_var == 1: + with open("tests/test.txt",'r') as f: + line = f.readline() + return line + if loop_var == 2: + fake_process.should_receive("poll").and_return(123) + loop_var = 0 + return "" + + stdout = flexmock(readline=mock_readline) + mock_subprocess32 = flexmock(subprocess32) + fake_process = flexmock(returncode=0, pid=123, + stdout=stdout) fake_process.should_receive('wait') - flexmock(subprocess32).should_receive('Popen').and_return(fake_process) - + mock_subprocess32.should_receive('Popen').and_return( + fake_process) # mock the call to invoke the playbook process flexmock(os.path).should_receive('exists').and_return(True) - # mock sys exit call flexmock(sys).should_receive('exit') - diff --git a/src/config/fabric-ansible/tests/test_job_manager_err_cases.py b/src/config/fabric-ansible/tests/test_job_manager_err_cases.py index d5edcf838d2..de3fba30c50 100644 --- a/src/config/fabric-ansible/tests/test_job_manager_err_cases.py +++ b/src/config/fabric-ansible/tests/test_job_manager_err_cases.py @@ -19,7 +19,7 @@ import test_case from job_manager.job_result_handler import JobResultHandler from job_manager.job_handler import JobHandler -from job_manager.job_mgr import JobManager +from job_manager.job_mgr import WFManager from job_manager.job_log_utils import JobLogUtils from job_manager.job_utils import JobUtils, JobStatus from job_manager.job_exception import JobException @@ -36,6 +36,7 @@ logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) +loop_var = 0 class TestJobManagerEC(test_case.JobTestCase): @@ -53,31 +54,32 @@ def tearDownClass(cls, *args, **kwargs): super(TestJobManagerEC, cls).tearDownClass(*args, **kwargs) # end tearDownClass - # to test the case when sandesh initialization times out + #to test the case when sandesh initialization times out def test_sandesh_timeout(self): + import pdb; pdb.set_trace() mocked_sandesh_utils = flexmock() flexmock(SandeshUtils, __new__=mocked_sandesh_utils) mocked_sandesh_utils.should_receive('__init__') - mocked_sandesh_utils.should_receive('wait_for_connection_establish')\ - .and_raise(JobException()) + mocked_sandesh_utils.should_receive('wait_for_connection_establish') \ + .and_raise(JobException()) args = {"collectors": ['127.0.0.1:8086']} exc_msg = self.assertRaises( JobException, JobLogUtils, "rdm_exc_id", json.dumps(args)) + print str(exc_msg) self.assertEqual(str(exc_msg), "JobException in execution" + " (None): " + MsgBundle.getMessage( - MsgBundle.SANDESH_INITIALIZATION_TIMEOUT_ERROR)) + MsgBundle.SANDESH_INITIALIZATION_TIMEOUT_ERROR)) # to test the case when job_template_id is missing while initializing the - # job_manager + # workflow_manager def test_execute_job_no_template_id(self): - job_input_json = {"no_template_id": "Missing template_id"} exc_msg = self.assertRaises( Exception, - JobManager, + WFManager, None, None, job_input_json, @@ -85,14 +87,13 @@ def test_execute_job_no_template_id(self): self.assertEqual(str(exc_msg), MsgBundle.getMessage( MsgBundle.JOB_TEMPLATE_MISSING)) - # to test the case when job_execution_id is missing while initializing the - # job_manager + #to test the case when job_execution_id is missing while initializing the + #workflow_manager def test_execute_job_no_execution_id(self): - - job_input_json = {"job_template_id": "random_template_id"} + job_input_json = {"job_template_id": "template_id"} exc_msg = self.assertRaises( Exception, - JobManager, + WFManager, None, None, job_input_json, @@ -100,9 +101,8 @@ def test_execute_job_no_execution_id(self): self.assertEqual(str(exc_msg), MsgBundle.getMessage( MsgBundle.JOB_EXECUTION_ID_MISSING)) - # to test the case when there is no job_template + #to test the case when there is no job_template def test_execute_job_read_job_template(self): - job_template_id = str(uuid.uuid4()) TestJobManagerUtils.mock_sandesh_check() @@ -113,12 +113,12 @@ def test_execute_job_read_job_template(self): mock_vnc = flexmock() flexmock(VncApi).new_instances(mock_vnc) - flexmock(mock_vnc).should_receive('job_template_read')\ - .with_args(id=job_template_id)\ - .and_raise(NoIdError('No such job Template id')) - jmgr = JobManager(log_utils.get_config_logger(), - mock_vnc, job_input_json, log_utils) - sys_exit_msg = self.assertRaises(SystemExit, jmgr.start_job) + flexmock(mock_vnc).should_receive('job_template_read') \ + .with_args(id=job_template_id) \ + .and_raise(Exception('No such job Template id')) + wm = WFManager(log_utils.get_config_logger(), + mock_vnc, job_input_json, log_utils) + sys_exit_msg = self.assertRaises(SystemExit, wm.start_job) self.assertEqual( sys_exit_msg.code, MsgBundle.getMessage(MsgBundle.JOB_EXC_REC_HDR) + @@ -126,7 +126,7 @@ def test_execute_job_read_job_template(self): job_template_id=job_template_id) + " ") - # to test the generic exception in handle_job + #to test the generic exception in handle_job for single device def test_execute_job_generic_exception_handle_job(self): # create job template job_template = JobTemplate(job_template_type='device', @@ -145,85 +145,90 @@ def test_execute_job_generic_exception_handle_job(self): job_input_json, log_utils = TestJobManagerUtils.get_min_details( job_template_uuid) - jmgr = JobManager(log_utils.get_config_logger(), - self._vnc_lib, job_input_json, log_utils) + wm = WFManager(log_utils.get_config_logger(), + self._vnc_lib, job_input_json, log_utils) # mock the job_handler to raise a general exception mock_job_handler = flexmock() flexmock(JobHandler).new_instances(mock_job_handler) flexmock(mock_job_handler).should_receive('handle_job') - flexmock(mock_job_handler).should_receive('get_playbook_info')\ - .and_raise( - Exception("Job Handler " - "Generic Exception")) - + flexmock(mock_job_handler).should_receive('get_playbook_info') \ + .and_raise( + Exception("Job Handler " + "Generic Exception")) mock_result_handler = flexmock(job_result_status=JobStatus.FAILURE, job_summary_message=exc_msg) flexmock(JobResultHandler).new_instances(mock_result_handler) flexmock(mock_result_handler).should_receive( - 'create_job_summary_message') .and_return(exc_msg) - flexmock(mock_result_handler).should_receive('create_job_summary_log') - flexmock(mock_result_handler).should_receive('update_job_status')\ - .with_args(JobStatus.FAILURE, exc_msg) - - sys_exit_msg = self.assertRaises(SystemExit, jmgr.start_job) + 'create_job_summary_message').and_return(exc_msg) + flexmock(mock_result_handler).should_receive( + 'create_job_summary_log') + flexmock(mock_result_handler).should_receive('update_job_status') \ + .with_args(JobStatus.FAILURE, exc_msg) + flexmock(mock_result_handler).should_receive( + 'get_device_data') + sys_exit_msg = self.assertRaises(SystemExit, wm.start_job) self.assertEqual(sys_exit_msg.code, exc_msg) - # to test generic exception from get_playbook_info - def test_execute_job_handler_gen_exc(self): - # create job template - fake_job_template = flexmock( - job_template_type='device', - job_template_job_runtime='ansible', - job_template_multi_device_job=False, - job_template_playbooks=TestJobManagerUtils. - playbooks_list, - name='single_device_temp_jh_gen_exc', - fq_name=["default-global-system-config", - "single_device_temp_jh_gen_exc"], - uuid='random_uuid') - - mock_vnc = flexmock() - flexmock(VncApi).new_instances(mock_vnc) - - flexmock(mock_vnc).should_receive('job_template_read')\ - .with_args(id="random_uuid")\ - .and_return(fake_job_template) - - TestJobManagerUtils.mock_sandesh_check() - job_input_json, log_utils = TestJobManagerUtils.get_min_details( - "random_uuid") - - jmgr = JobManager(log_utils.get_config_logger(), - mock_vnc, job_input_json, log_utils) - - fake_job_template.should_receive( - 'get_job_template_input_schema').and_return(None) - fake_job_template.should_receive( - 'get_job_template_multi_device_job').and_return(False) - fake_job_template.should_receive('get_uuid').and_return('random_uuid') - # mock the job_handler to raise a general exception - fake_job_template.should_receive('get_job_template_playbooks')\ - .and_raise( - Exception("Mock " - "Generic Exception in job_handler")) - - exc = Exception('Mock Generic Exception in job_handler') - - sys_exit_msg = self.assertRaises(SystemExit, jmgr.start_job) - self.assertEqual(sys_exit_msg.code, MsgBundle. - getMessage(MsgBundle.JOB_SUMMARY_MESSAGE_HDR) + - MsgBundle. - getMessage(MsgBundle. - JOB_SINGLE_DEVICE_FAILED_MESSAGE_HDR) + - MsgBundle. - getMessage(MsgBundle.GET_PLAYBOOK_INFO_ERROR, - job_template_id="random_uuid", - exc_msg=repr(exc))) - - # to test single_device_job when playbook is not present in the path + #to test generic exception from get_playbook_info + #TODO - THIS test case it not longer necessary. GET_PLAYBOOK_INFO_ERROR + # def test_execute_job_handler_gen_exc(self): + # # create job template + # fake_job_template = flexmock( + # job_template_type='device', + # job_template_job_runtime='ansible', + # job_template_multi_device_job=False, + # job_template_playbooks=TestJobManagerUtils. + # playbooks_list, + # name='single_device_temp_jh_gen_exc', + # fq_name=["default-global-system-config", + # "single_device_temp_jh_gen_exc"], + # uuid='random_uuid') + # + # mock_vnc = flexmock() + # flexmock(VncApi).new_instances(mock_vnc) + # + # flexmock(mock_vnc).should_receive('job_template_read') \ + # .with_args(id="random_uuid") \ + # .and_return(fake_job_template) + # + # TestJobManagerUtils.mock_sandesh_check() + # + # job_input_json, log_utils = TestJobManagerUtils.get_min_details( + # "random_uuid") + # + # wm = WFManager(log_utils.get_config_logger(), + # mock_vnc, job_input_json, log_utils) + # + # fake_job_template.should_receive( + # 'get_job_template_input_schema').and_return(None) + # + # fake_job_template.should_receive('get_uuid').and_return( + # 'random_uuid') + # + # # mock the job_handler to raise a general exception + # fake_job_template.should_receive('get_job_template_playbooks') \ + # .and_raise( + # Exception("Mock " + # "Generic Exception in job_handler")) + # + # exc_msg = repr(Exception('Mock Generic Exception in job_handler')) + # + # sys_exit_msg = self.assertRaises(SystemExit, wm.start_job) + # self.assertEqual(sys_exit_msg.code, MsgBundle. + # getMessage(MsgBundle.JOB_SUMMARY_MESSAGE_HDR) + + # MsgBundle. + # getMessage(MsgBundle. + # JOB_SINGLE_DEVICE_FAILED_MESSAGE_HDR) + + # MsgBundle. + # getMessage(MsgBundle.GET_PLAYBOOK_INFO_ERROR, + # job_template_id="random_uuid", + # exc_msg = exc_msg + " ")) + # + + # #to test single_device_job when playbook is not present in the path def test_execute_job_no_pb_file_on_path(self): # create job template play_info = PlaybookInfoType(playbook_uri='job_manager_test.yaml') @@ -243,13 +248,13 @@ def test_execute_job_no_pb_file_on_path(self): job_input_json, log_utils = TestJobManagerUtils.get_min_details( job_template_uuid) - jmgr = JobManager(log_utils.get_config_logger(), - self._vnc_lib, job_input_json, log_utils) + wm = WFManager(log_utils.get_config_logger(), + self._vnc_lib, job_input_json, log_utils) # mock the call to invoke the playbook process flexmock(os.path).should_receive('exists').and_return(False) - sys_exit_msg = self.assertRaises(SystemExit, jmgr.start_job) + sys_exit_msg = self.assertRaises(SystemExit, wm.start_job) self.assertEqual(sys_exit_msg.code, MsgBundle. getMessage(MsgBundle. JOB_SUMMARY_MESSAGE_HDR) + @@ -260,9 +265,7 @@ def test_execute_job_no_pb_file_on_path(self): getMessage(MsgBundle.PLAYBOOK_NOT_FOUND, playbook_uri=playbook_info['uri'])) - # to test single_device_job when there is a generic exception - # from job_handler in job_manager - + #To test generic job exception in handle_job def test_execute_job_generic_exception(self): # create job template job_template = JobTemplate(job_template_type='device', @@ -278,57 +281,59 @@ def test_execute_job_generic_exception(self): job_input_json, log_utils = TestJobManagerUtils.get_min_details( job_template_uuid) - jmgr = JobManager(log_utils.get_config_logger(), - self._vnc_lib, job_input_json, log_utils) + wm = WFManager(log_utils.get_config_logger(), + self._vnc_lib, job_input_json, log_utils) # mock the job_handler to raise a general exception mock_job_handler = flexmock() flexmock(JobHandler).new_instances(mock_job_handler) exc = Exception("Mock " - "Generic Exception") + "Generic Exception") - flexmock(mock_job_handler).should_receive('handle_job')\ - .and_raise(exc) + flexmock(mock_job_handler).should_receive('handle_job') \ + .and_raise(exc) exc_msg = repr(exc) - sys_exit_msg = self.assertRaises(SystemExit, jmgr.start_job) + sys_exit_msg = self.assertRaises(SystemExit, wm.start_job) self.assertEqual(sys_exit_msg.code, MsgBundle.getMessage( MsgBundle.EXC_JOB_ERR_HDR) + exc_msg + " ") + + #TODO this test case can be removed becuase id device id is omitted, # to test generic exception in job_manager # Simulate this by omitting (device_id in) job_params - - def test_execute_job_generic_exception_multi_device(self): - # create job template - job_template = JobTemplate(job_template_type='device', - job_template_job_runtime='ansible', - job_template_multi_device_job=True, - job_template_playbooks=TestJobManagerUtils. - playbooks_list, - name='multi_device_template_gen_exc') - job_template_uuid = self._vnc_lib.job_template_create(job_template) - - TestJobManagerUtils.mock_sandesh_check() - - job_input_json, log_utils = TestJobManagerUtils.get_min_details( - job_template_uuid) - - jmgr = JobManager(log_utils.get_config_logger(), - self._vnc_lib, job_input_json, log_utils) - - exc_msg = repr(TypeError( - "'NoneType' object has no attribute '__getitem__'")) - sys_exit_msg = self.assertRaises(SystemExit, jmgr.start_job) - self.assertEqual(sys_exit_msg.code, - MsgBundle.getMessage( - MsgBundle.EXC_JOB_ERR_HDR) + - exc_msg + " ") - - # to test the generic exception in handle_device_job + # def test_execute_job_generic_exception_multi_device(self): + # # create job template + # job_template = JobTemplate(job_template_type='device', + # job_template_job_runtime='ansible', + # job_template_multi_device_job=True, + # job_template_playbooks=TestJobManagerUtils. + # playbooks_list, + # name='multi_device_template_gen_exc') + # job_template_uuid = self._vnc_lib.job_template_create(job_template) + # + # TestJobManagerUtils.mock_sandesh_check() + # + # job_input_json, log_utils = TestJobManagerUtils.get_min_details( + # job_template_uuid) + # + # wm = WFManager(log_utils.get_config_logger(), + # self._vnc_lib, job_input_json, log_utils) + # + # exc_msg = repr(TypeError( + # "'NoneType' object has no attribute '__getitem__'")) + # sys_exit_msg = self.assertRaises(SystemExit, wm.start_job) + # self.assertEqual(sys_exit_msg.code, + # MsgBundle.getMessage( + # MsgBundle.EXC_JOB_ERR_HDR) + + # exc_msg + " ") + + #to test the generic exception in handle_device_job when executing for + #multiple devices. def test_execute_job_generic_exception_handle_device_job(self): # create job template job_template = JobTemplate(job_template_type='device', @@ -346,39 +351,43 @@ def test_execute_job_generic_exception_handle_device_job(self): job_input_json, log_utils = TestJobManagerUtils.get_min_details( job_template_uuid) job_input_json["params"] = {"device_list": - ["aad74e24-a00b-4eb3-8412-f8b9412925c3"]} + [ + "aad74e24-a00b-4eb3-8412-f8b9412925c3"]} - jmgr = JobManager(log_utils.get_config_logger(), - self._vnc_lib, job_input_json, log_utils) + wm = WFManager(log_utils.get_config_logger(), + self._vnc_lib, job_input_json, log_utils) # mock the job_handler to raise a general exception mock_job_handler = flexmock() flexmock(JobHandler).new_instances(mock_job_handler) flexmock(mock_job_handler).should_receive('handle_job') - flexmock(mock_job_handler).should_receive('get_playbook_info')\ - .and_raise( - Exception("Job Device Handler " - "Generic Exception")) + flexmock(mock_job_handler).should_receive('get_playbook_info') \ + .and_raise( + Exception("Job Device Handler " + "Generic Exception")) mock_result_handler = flexmock(job_result_status=JobStatus.FAILURE, job_summary_message=exc_msg) flexmock(JobResultHandler).new_instances(mock_result_handler) flexmock(mock_result_handler).should_receive( - 'create_job_summary_message') .and_return(exc_msg) - flexmock(mock_result_handler).should_receive('create_job_summary_log') - flexmock(mock_result_handler).should_receive('update_job_status')\ - .with_args(JobStatus.FAILURE, exc_msg) + 'create_job_summary_message').and_return(exc_msg) + flexmock(mock_result_handler).should_receive( + 'create_job_summary_log') + flexmock(mock_result_handler).should_receive('get_device_data') + flexmock(mock_result_handler).should_receive('update_job_status') \ + .with_args(JobStatus.FAILURE, exc_msg) - sys_exit_msg = self.assertRaises(SystemExit, jmgr.start_job) + sys_exit_msg = self.assertRaises(SystemExit, wm.start_job) self.assertEqual(sys_exit_msg.code, exc_msg) - # to test multi_device_job when device_vendor is not found + + #to test multi_device_job when device_vendor is not found. + #get_playbook_info fails. def test_execute_job_no_device_vendor(self): # create job template job_template = JobTemplate(job_template_type='device', job_template_job_runtime='ansible', - job_template_multi_device_job=True, job_template_playbooks=TestJobManagerUtils. playbooks_list, name='multi_device_no_device_vendor') @@ -392,70 +401,74 @@ def test_execute_job_no_device_vendor(self): job_template_uuid) job_input_json.update({ "params": {"device_list": - [device_id]}, - "device_json": { - device_id: - {"device_family": "MX"}}}) + [device_id]}, + "device_json": { + device_id: + {"device_family": "MX"}}}) - jmgr = JobManager(log_utils.get_config_logger(), - self._vnc_lib, job_input_json, log_utils) + wm = WFManager(log_utils.get_config_logger(), + self._vnc_lib, job_input_json, log_utils) - sys_exit_msg = self.assertRaises(SystemExit, jmgr.start_job) + sys_exit_msg = self.assertRaises(SystemExit, wm.start_job) self.assertEqual(sys_exit_msg.code, MsgBundle.getMessage( MsgBundle.JOB_SUMMARY_MESSAGE_HDR) + MsgBundle.getMessage( - MsgBundle.JOB_MULTI_DEVICE_FAILED_MESSAGE_HDR) + + MsgBundle.JOB_MULTI_DEVICE_FAILED_MESSAGE_HDR) + device_id + ",\n" + MsgBundle.getMessage(MsgBundle. PLAYBOOK_RESULTS_MESSAGE) + device_id + ":" + MsgBundle.getMessage(MsgBundle. DEVICE_VENDOR_FAMILY_MISSING, - device_id=device_id) + " \n" + device_id=device_id) + " \n" ) - # to test multi_device_job when there is explicit mismatch - def test_execute_job_explicit_mismatch(self): - # create job template - job_template = JobTemplate(job_template_type='device', - job_template_job_runtime='ansible', - job_template_multi_device_job=True, - job_template_playbooks=TestJobManagerUtils. - playbooks_list, - name='multi_device_explicit_mismatch') - job_template_uuid = self._vnc_lib.job_template_create(job_template) - - TestJobManagerUtils.mock_sandesh_check() - device_id = "aad74e24-a00b-4eb3-8412-f8b9412925c3" - job_input_json, log_utils = TestJobManagerUtils.get_min_details( - job_template_uuid) - job_input_json.update({"params": {"device_list": - [device_id]}, - "device_json": { - device_id: - {"device_family": "MX", - "device_vendor": "Arista", - "device_username": "username", - "device_password": "password"}}}) - jmgr = JobManager(log_utils.get_config_logger(), - self._vnc_lib, job_input_json, log_utils) - - sys_exit_msg = self.assertRaises(SystemExit, jmgr.start_job) - self.assertEqual( - sys_exit_msg.code, - MsgBundle.getMessage(MsgBundle.JOB_SUMMARY_MESSAGE_HDR) + - MsgBundle.getMessage(MsgBundle. - JOB_MULTI_DEVICE_FAILED_MESSAGE_HDR) + - device_id + ",\n" + - MsgBundle.getMessage(MsgBundle.PLAYBOOK_RESULTS_MESSAGE) + - device_id + ":" + - MsgBundle.getMessage(MsgBundle.PLAYBOOK_INFO_DEVICE_MISMATCH, - device_vendor="Arista", - device_family="MX") + " \n") - - # to test multi_device_job when credentials are not provided + # # to test multi_device_job when there is explicit mismatch, between + # device vendor and playbok vendor + # #TODO this is not necessary because depending on the device vendor family, the right playbook will be chosen + # def test_execute_job_explicit_mismatch(self): + # # create job template + # job_template = JobTemplate(job_template_type='device', + # job_template_job_runtime='ansible', + # job_template_playbooks=TestJobManagerUtils. + # playbooks_list, + # name='multi_device_explicit_mismatch') + # job_template_uuid = self._vnc_lib.job_template_create(job_template) + # + # TestJobManagerUtils.mock_sandesh_check() + # + # device_id = "aad74e24-a00b-4eb3-8412-f8b9412925c3" + # job_input_json, log_utils = TestJobManagerUtils.get_min_details( + # job_template_uuid) + # job_input_json.update({"params": {"device_list": + # [device_id]}, + # "device_json": { + # device_id: + # {"device_family": "MX", + # "device_vendor": "Arista", + # "device_username": "username", + # "device_password": "password"}}}) + # + # wm = WFManager(log_utils.get_config_logger(), + # self._vnc_lib, job_input_json, log_utils) + # + # sys_exit_msg = self.assertRaises(SystemExit, wm.start_job) + # self.assertEqual( + # sys_exit_msg.code, + # MsgBundle.getMessage(MsgBundle.JOB_SUMMARY_MESSAGE_HDR) + + # MsgBundle.getMessage(MsgBundle. + # JOB_MULTI_DEVICE_FAILED_MESSAGE_HDR) + + # device_id + ",\n" + + # MsgBundle.getMessage(MsgBundle.PLAYBOOK_RESULTS_MESSAGE) + + # device_id + ":" + + # MsgBundle.getMessage(MsgBundle.PLAYBOOK_INFO_DEVICE_MISMATCH, + # device_vendor="Arista", + # device_family="MX") + " \n") + + + #to test multi_device_job when credentials are not provided def test_execute_job_no_credentials(self): # create job template job_template = JobTemplate(job_template_type='device', @@ -471,18 +484,18 @@ def test_execute_job_no_credentials(self): TestJobManagerUtils.mock_sandesh_check() job_input_json, log_utils = TestJobManagerUtils.get_min_details( - job_template_uuid) + job_template_uuid) job_input_json.update({"params": {"device_list": - [device_id]}, + [device_id]}, "device_json": { device_id: {"device_family": "MX", "device_vendor": "Juniper"}}}) - jmgr = JobManager(log_utils.get_config_logger(), - self._vnc_lib, job_input_json, log_utils) + wm = WFManager(log_utils.get_config_logger(), + self._vnc_lib, job_input_json, log_utils) - sys_exit_msg = self.assertRaises(SystemExit, jmgr.start_job) + sys_exit_msg = self.assertRaises(SystemExit, wm.start_job) self.assertEqual( sys_exit_msg.code, MsgBundle.getMessage(MsgBundle.JOB_SUMMARY_MESSAGE_HDR) + @@ -494,97 +507,103 @@ def test_execute_job_no_credentials(self): device_id + ":" + MsgBundle.getMessage( - MsgBundle. - NO_CREDENTIALS_FOUND, - device_id=device_id) + + MsgBundle. + NO_CREDENTIALS_FOUND, + device_id=device_id) + " \n") - # to test multi_device_job when device details for given device - # cannot be found - def test_execute_job_no_device_data(self): - # create job template - job_template = JobTemplate(job_template_type='device', - job_template_job_runtime='ansible', - job_template_multi_device_job=True, - job_template_playbooks=TestJobManagerUtils. - playbooks_list, - name='multi_device_no_data') - job_template_uuid = self._vnc_lib.job_template_create(job_template) - - device_id = "aad74e24-a00b-4eb3-8412-f8b9412925c3" - - TestJobManagerUtils.mock_sandesh_check() - - job_input_json, log_utils = TestJobManagerUtils.get_min_details( - job_template_uuid) - job_input_json.update({"params": {"device_list": - [device_id]}, - "device_json": { - "some_random_id": - {"device_family": "MX", - "device_vendor": "Juniper"} - }}) - - jmgr = JobManager(log_utils.get_config_logger(), - self._vnc_lib, job_input_json, log_utils) - sys_exit_msg = self.assertRaises(SystemExit, jmgr.start_job) - self.assertEqual( - sys_exit_msg.code, - MsgBundle.getMessage(MsgBundle.JOB_SUMMARY_MESSAGE_HDR) + - MsgBundle.getMessage(MsgBundle. - JOB_MULTI_DEVICE_FAILED_MESSAGE_HDR) + - device_id + - ",\n" + - MsgBundle.getMessage(MsgBundle.PLAYBOOK_RESULTS_MESSAGE) + - device_id + - ":" + - MsgBundle.getMessage(MsgBundle. - NO_DEVICE_DATA_FOUND, - device_id=device_id) - + " \n") - - # to test multi_device_job when no device json - # can be found - - def test_execute_job_no_device_json(self): - # create job template - job_template = JobTemplate(job_template_type='device', - job_template_job_runtime='ansible', - job_template_multi_device_job=True, - job_template_playbooks=TestJobManagerUtils. - playbooks_list, - name='multi_device_no_json') - job_template_uuid = self._vnc_lib.job_template_create(job_template) - - device_id = "aad74e24-a00b-4eb3-8412-f8b9412925c3" - - TestJobManagerUtils.mock_sandesh_check() - - job_input_json, log_utils = TestJobManagerUtils.get_min_details( - job_template_uuid) - job_input_json.update({ - "params": {"device_list": - [device_id]}, - }) - - jmgr = JobManager(log_utils.get_config_logger(), - self._vnc_lib, job_input_json, log_utils) - sys_exit_msg = self.assertRaises(SystemExit, jmgr.start_job) - self.assertEqual( - sys_exit_msg.code, - MsgBundle.getMessage(MsgBundle.JOB_SUMMARY_MESSAGE_HDR) + - MsgBundle.getMessage(MsgBundle. - JOB_MULTI_DEVICE_FAILED_MESSAGE_HDR) + - device_id + - ",\n" + - MsgBundle.getMessage(MsgBundle.PLAYBOOK_RESULTS_MESSAGE) + - device_id + - ":" + - MsgBundle.getMessage(MsgBundle.DEVICE_JSON_NOT_FOUND) + - " \n") - - # to test run_playbook generic exception + # # to test multi_device_job when device details for given device + # # cannot be found + # #TODO - not required since we always check if the device id is present. + # def test_execute_job_no_device_data(self): + # # create job template + # job_template = JobTemplate(job_template_type='device', + # job_template_job_runtime='ansible', + # job_template_multi_device_job=True, + # job_template_playbooks=TestJobManagerUtils. + # playbooks_list, + # name='multi_device_no_data') + # job_template_uuid = self._vnc_lib.job_template_create(job_template) + # + # device_id = "aad74e24-a00b-4eb3-8412-f8b9412925c3" + # + # TestJobManagerUtils.mock_sandesh_check() + # + # job_input_json, log_utils = TestJobManagerUtils.get_min_details( + # job_template_uuid) + # job_input_json.update({"params": {"device_list": + # [device_id]}, + # "device_json": { + # "some_random_id": + # {"device_family": "MX", + # "device_vendor": "Juniper"} + # }}) + # + # wm = WFManager(log_utils.get_config_logger(), + # self._vnc_lib, job_input_json, log_utils) + # sys_exit_msg = self.assertRaises(SystemExit, wm.start_job) + # self.assertEqual( + # sys_exit_msg.code, + # MsgBundle.getMessage(MsgBundle.JOB_SUMMARY_MESSAGE_HDR) + + # MsgBundle.getMessage(MsgBundle. + # JOB_MULTI_DEVICE_FAILED_MESSAGE_HDR) + + # device_id + + # ",\n" + + # MsgBundle.getMessage(MsgBundle.PLAYBOOK_RESULTS_MESSAGE) + + # device_id + + # ":" + + # MsgBundle.getMessage(MsgBundle. + # NO_DEVICE_DATA_FOUND, + # device_id=device_id) + # + " \n") + + # to test multi_device_job when no device json does not contain any details. + #TODO device_json cannot be empty for multi job, so not necessary. + # def test_execute_job_no_device_json(self): + # # create job template + # job_template = JobTemplate(job_template_type='device', + # job_template_job_runtime='ansible', + # job_template_multi_device_job=True, + # job_template_playbooks=TestJobManagerUtils. + # playbooks_list, + # name='multi_device_no_json') + # job_template_uuid = self._vnc_lib.job_template_create(job_template) + # + # device_id = "aad74e24-a00b-4eb3-8412-f8b9412925c3" + # + # TestJobManagerUtils.mock_sandesh_check() + # + # job_input_json, log_utils = TestJobManagerUtils.get_min_details( + # job_template_uuid) + # # job_input_json.update({ + # # "params": {"device_list": + # # [device_id]}, + # # }) + # job_input_json.update({"params": {"device_list": + # [device_id]}, + # "device_json": { + # + # }}) + # + # wm = WFManager(log_utils.get_config_logger(), + # self._vnc_lib, job_input_json, log_utils) + # sys_exit_msg = self.assertRaises(SystemExit, wm.start_job) + # self.assertEqual( + # sys_exit_msg.code, + # MsgBundle.getMessage(MsgBundle.JOB_SUMMARY_MESSAGE_HDR) + + # MsgBundle.getMessage(MsgBundle. + # JOB_MULTI_DEVICE_FAILED_MESSAGE_HDR) + + # device_id + + # ",\n" + + # MsgBundle.getMessage(MsgBundle.PLAYBOOK_RESULTS_MESSAGE) + + # device_id + + # ":" + + # MsgBundle.getMessage(MsgBundle.DEVICE_JSON_NOT_FOUND) + + # " \n") + # + + #to test run_playbook generic exception- fail json. def test_execute_job_run_pb_gen_exc(self): # create job template job_template = JobTemplate(job_template_type='device', @@ -598,17 +617,17 @@ def test_execute_job_run_pb_gen_exc(self): TestJobManagerUtils.mock_sandesh_check() job_input_json, log_utils = TestJobManagerUtils.get_min_details( - job_template_uuid) + job_template_uuid) - jmgr = JobManager(log_utils.get_config_logger(), - self._vnc_lib, job_input_json, log_utils) + wm = WFManager(log_utils.get_config_logger(), + self._vnc_lib, job_input_json, log_utils) exc = Exception('some gen exc') # mock the call to raise exception - flexmock(json).should_receive('dumps')\ + flexmock(json).should_receive('dumps') \ .and_raise(exc) - sys_exit_msg = self.assertRaises(SystemExit, jmgr.start_job) + sys_exit_msg = self.assertRaises(SystemExit, wm.start_job) self.assertEqual(sys_exit_msg.code, MsgBundle.getMessage(MsgBundle. @@ -622,8 +641,8 @@ def test_execute_job_run_pb_gen_exc(self): playbook_uri, exc_msg=repr(exc))) - # to handle run playbook process rc =1 exception + #to handle run playbook process rc =1 exception def test_execute_job_run_pb_process_rc1(self): # create job template job_template = JobTemplate(job_template_type='device', @@ -637,35 +656,51 @@ def test_execute_job_run_pb_process_rc1(self): TestJobManagerUtils.mock_sandesh_check() job_input_json, log_utils = TestJobManagerUtils.get_min_details( - job_template_uuid) - - jmgr = JobManager(log_utils.get_config_logger(), - self._vnc_lib, job_input_json, log_utils) + job_template_uuid) + wm = WFManager(log_utils.get_config_logger(), + self._vnc_lib, job_input_json, log_utils) + loop_var = 0 + + def mock_readline(): + global loop_var + + loop_var += 1 + if loop_var == 1: + with open("tests/test.txt", 'r') as f: + line = f.readline() + return line + if loop_var == 2: + fake_process.should_receive("poll").and_return(123) + loop_var = 0 + return "" + + stdout = flexmock(readline=mock_readline) + flexmock(json).should_receive("loads") flexmock(os.path).should_receive('exists').and_return(True) # mock the call to raise exception - fake_process = flexmock(returncode=1) + fake_process = flexmock(returncode=1,stdout=stdout) fake_process.should_receive('wait') # flexmock(subprocess).should_receive('TimeoutExpired') - flexmock(subprocess32).should_receive('Popen').and_return(fake_process) - sys_exit_msg = self.assertRaises(SystemExit, jmgr.start_job) + flexmock(subprocess32).should_receive('Popen').and_return( + fake_process) + sys_exit_msg = self.assertRaises(SystemExit, wm.start_job) self.assertEqual(sys_exit_msg.code, MsgBundle.getMessage(MsgBundle. JOB_SUMMARY_MESSAGE_HDR) + MsgBundle.getMessage( - MsgBundle. - JOB_SINGLE_DEVICE_FAILED_MESSAGE_HDR) + + MsgBundle. + JOB_SINGLE_DEVICE_FAILED_MESSAGE_HDR) + MsgBundle.getMessage( - MsgBundle.PLAYBOOK_EXIT_WITH_ERROR, - playbook_uri=TestJobManagerUtils. - play_info. - playbook_uri,) + MsgBundle.PLAYBOOK_EXIT_WITH_ERROR, + playbook_uri=TestJobManagerUtils. + play_info. + playbook_uri, ) ) - # to handle run playbook process generic exception - + #to handle run playbook process generic exception def test_execute_job_run_pb_process_gen_exc(self): # create job template job_template = JobTemplate(job_template_type='device', @@ -679,22 +714,20 @@ def test_execute_job_run_pb_process_gen_exc(self): TestJobManagerUtils.mock_sandesh_check() job_input_json, log_utils = TestJobManagerUtils.get_min_details( - job_template_uuid) + job_template_uuid) - jmgr = JobManager(log_utils.get_config_logger(), - self._vnc_lib, job_input_json, log_utils) + wm = WFManager(log_utils.get_config_logger(), + self._vnc_lib, job_input_json, log_utils) flexmock(os.path).should_receive('exists').and_return(True) # mock the call to raise exception fake_process = flexmock(returncode=None) fake_process.should_receive('wait') - # flexmock(subprocess).should_receive('TimeoutExpired') - exc = Exception('mock gen exception in run_playbook_process') flexmock(subprocess32).should_receive('Popen').and_raise(exc) - sys_exit_msg = self.assertRaises(SystemExit, jmgr.start_job) + sys_exit_msg = self.assertRaises(SystemExit, wm.start_job) self.assertEqual( sys_exit_msg.code, @@ -706,35 +739,91 @@ def test_execute_job_run_pb_process_gen_exc(self): playbook_uri, exc_msg=repr(exc))) - # to test job_input_schema_validations + #Test run_playbook_process for TimeoutExpired + def test_execute_job_run_pb_process_timeout_expired(self): + # create job template + job_template = JobTemplate( + job_template_type='device', + job_template_job_runtime='ansible', + job_template_multi_device_job=False, + job_template_playbooks=TestJobManagerUtils. + playbooks_list, + name='run_pb_prc_rc_timeout') + job_template_uuid = self._vnc_lib.job_template_create(job_template) + + TestJobManagerUtils.mock_sandesh_check() + + job_input_json, log_utils = TestJobManagerUtils.get_min_details( + job_template_uuid) + + wm = WFManager(log_utils.get_config_logger(), + self._vnc_lib, job_input_json, log_utils) + + loop_var = 0 + def mock_readline(): + global loop_var + + loop_var += 1 + if loop_var == 1: + with open("tests/test.txt", 'r') as f: + line = f.readline() + return line + if loop_var == 2: + fake_process.should_receive("poll").and_return(123) + loop_var = 0 + return "" + + stdout = flexmock(readline=mock_readline) + flexmock(json).should_receive("loads") + flexmock(os.path).should_receive('exists').and_return(True) + flexmock(os).should_receive('kill') + # mock the call to raise exception, using return code = 1 + fake_process = flexmock(returncode=1, pid=1234, stdout=stdout) + exc = subprocess32.TimeoutExpired(cmd='Mock timeout exc cmd', + timeout=3600) + fake_process.should_receive('wait').and_raise(exc) + flexmock(subprocess32).should_receive('Popen').and_return( + fake_process) + sys_exit_msg = self.assertRaises(SystemExit, wm.start_job) + self.assertEqual( + sys_exit_msg.code, + MsgBundle.getMessage(MsgBundle.JOB_SUMMARY_MESSAGE_HDR) + + MsgBundle.getMessage(MsgBundle. + JOB_SINGLE_DEVICE_FAILED_MESSAGE_HDR) + + MsgBundle.getMessage(MsgBundle.RUN_PLAYBOOK_PROCESS_TIMEOUT, + playbook_uri=TestJobManagerUtils.play_info. + playbook_uri, + exc_msg=repr(exc))) + + #to test job_input_schema_validation def test_execute_job_input_schema(self): # create job template fake_job_template = flexmock( - job_template_type='device', - job_template_job_runtime='ansible', - job_template_multi_device_job=False, - job_template_playbooks=TestJobManagerUtils. - playbooks_list, - name='input_schema_template', - fq_name=["default-global-system-config", - "input_schema_template"], - uuid='random_uuid') + job_template_type='device', + job_template_job_runtime='ansible', + job_template_multi_device_job=False, + job_template_playbooks=TestJobManagerUtils. + playbooks_list, + name='input_schema_template', + fq_name=["default-global-system-config", + "input_schema_template"], + uuid='random_uuid') TestJobManagerUtils.mock_sandesh_check() job_input_json, log_utils = TestJobManagerUtils.get_min_details( - "random_uuid") + "random_uuid") mock_vnc = flexmock() flexmock(VncApi).new_instances(mock_vnc) flexmock(mock_vnc).should_receive( - 'job_template_read').with_args( - id="random_uuid").and_return(fake_job_template) + 'job_template_read').with_args( + id="random_uuid").and_return(fake_job_template) - jmgr = JobManager(log_utils.get_config_logger(), - mock_vnc, job_input_json, log_utils) + wm = WFManager(log_utils.get_config_logger(), + mock_vnc, job_input_json, log_utils) exc = Exception("'name' is a required property") @@ -742,13 +831,12 @@ def test_execute_job_input_schema(self): fake_job_template.should_receive( 'get_job_template_input_schema').and_return(fake_schema) - fake_job_template.should_receive( - 'get_job_template_multi_device_job').and_return(False) - fake_job_template.should_receive('get_uuid').and_return('random_uuid') + fake_job_template.should_receive('get_uuid').and_return( + 'random_uuid') # mock the job_handler to raise an exception fake_job_template.should_receive('get_job_template_playbooks') - sys_exit_msg = self.assertRaises(SystemExit, jmgr.start_job) + sys_exit_msg = self.assertRaises(SystemExit, wm.start_job) self.assertEqual(sys_exit_msg.code, MsgBundle.getMessage(MsgBundle.JOB_EXC_REC_HDR) + MsgBundle.getMessage(MsgBundle.INVALID_SCHEMA, @@ -756,15 +844,16 @@ def test_execute_job_input_schema(self): exc_obj=exc) + " ") + #Test when input is not found when input schema is specified. def test_execute_job_input_schema_ip_not_found(self): # create job template fake_job_template = flexmock( - job_template_type='device', - job_template_job_runtime='ansible', - job_template_multi_device_job=False, - job_template_playbooks=TestJobManagerUtils. - playbooks_list, - name='input_schema_template_ip', + job_template_type='device', + job_template_job_runtime='ansible', + job_template_multi_device_job=False, + job_template_playbooks=TestJobManagerUtils. + playbooks_list, + name='input_schema_template_ip', fq_name=["default-global-system-config", "input_schema_template_ip"], uuid='random_uuid') @@ -772,8 +861,8 @@ def test_execute_job_input_schema_ip_not_found(self): mock_vnc = flexmock() flexmock(VncApi).new_instances(mock_vnc) - flexmock(mock_vnc).should_receive('job_template_read')\ - .with_args(id="random_uuid")\ + flexmock(mock_vnc).should_receive('job_template_read') \ + .with_args(id="random_uuid") \ .and_return(fake_job_template) TestJobManagerUtils.mock_sandesh_check() @@ -784,68 +873,28 @@ def test_execute_job_input_schema_ip_not_found(self): "fabric_fq_name": "Global-system-config:fabric:1", "auth_token": "6e7d7f87faa54fac96a2a28ec752336a", "args": TestJobManagerUtils.args - } + } log_utils = JobLogUtils( sandesh_instance_id=TestJobManagerUtils.execution_id, config_args=json.dumps(TestJobManagerUtils.args)) - jmgr = JobManager(log_utils.get_config_logger(), - mock_vnc, job_input_json, log_utils) + wm = WFManager(log_utils.get_config_logger(), + mock_vnc, job_input_json, log_utils) fake_schema = TestJobManagerUtils.fake_schema - fake_job_template.should_receive('get_job_template_input_schema')\ - .and_return(fake_schema) + fake_job_template.should_receive('get_job_template_input_schema') \ + .and_return(fake_schema) fake_job_template.should_receive( - 'get_job_template_multi_device_job')\ - .and_return(False) + 'get_job_template_multi_device_job') \ + .and_return(False) fake_job_template.should_receive('get_uuid').and_return('random_uuid') # mock the job_handler to raise an exception fake_job_template.should_receive('get_job_template_playbooks') - sys_exit_msg = self.assertRaises(SystemExit, jmgr.start_job) + sys_exit_msg = self.assertRaises(SystemExit, wm.start_job) self.assertEqual(sys_exit_msg.code, - MsgBundle.getMessage(MsgBundle.JOB_EXC_REC_HDR) + - MsgBundle.getMessage( - MsgBundle.INPUT_SCHEMA_INPUT_NOT_FOUND) + - " ") - - def test_execute_job_run_pb_process_timeout_expired(self): - # create job template - job_template = JobTemplate( - job_template_type='device', - job_template_job_runtime='ansible', - job_template_multi_device_job=False, - job_template_playbooks=TestJobManagerUtils. - playbooks_list, - name='run_pb_prc_rc_timeout') - job_template_uuid = self._vnc_lib.job_template_create(job_template) - - TestJobManagerUtils.mock_sandesh_check() - - job_input_json, log_utils = TestJobManagerUtils.get_min_details( - job_template_uuid) - - jmgr = JobManager(log_utils.get_config_logger(), - self._vnc_lib, job_input_json, log_utils) - - flexmock(os.path).should_receive('exists').and_return(True) - flexmock(os).should_receive('kill') - # mock the call to raise exception - - fake_process = flexmock(returncode=1, pid=1234) - exc = subprocess32.TimeoutExpired(cmd='Mock timeout exc cmd', - timeout=3600) - fake_process.should_receive('wait').and_raise(exc) - flexmock(subprocess32).should_receive('Popen').and_return(fake_process) - sys_exit_msg = self.assertRaises(SystemExit, jmgr.start_job) - self.assertEqual( - sys_exit_msg.code, - MsgBundle.getMessage(MsgBundle.JOB_SUMMARY_MESSAGE_HDR) + - MsgBundle.getMessage(MsgBundle. - JOB_SINGLE_DEVICE_FAILED_MESSAGE_HDR) + - MsgBundle.getMessage(MsgBundle.RUN_PLAYBOOK_PROCESS_TIMEOUT, - playbook_uri=TestJobManagerUtils.play_info. - playbook_uri, - exc_msg=repr(exc))) - + MsgBundle.getMessage(MsgBundle.JOB_EXC_REC_HDR) + + MsgBundle.getMessage( + MsgBundle.INPUT_SCHEMA_INPUT_NOT_FOUND) + + " ")