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='workflow', 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) wm = WFManager(log_utils.get_config_logger(), self._vnc_lib, job_input_json, log_utils, self.fake_zk_client) wm.start_job() self.assertEqual(wm.result_handler.job_result_status, JobStatus.SUCCESS)
def test_execute_job_with_vendor_only(self): play_info = PlaybookInfoType(playbook_uri='job_manager_test.yaml', vendor='Juniper') 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='workflow', job_template_multi_device_job=True, job_template_playbooks=playbooks_list, name='Test_template_multidevice') 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, self.fake_zk_client) wm.start_job() self.assertEqual(wm.result_handler.job_result_status, JobStatus.SUCCESS)
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') play_info_juniper_qfx = PlaybookInfoType( playbook_uri='job_manager_test.yaml', vendor='Juniper', device_family='QFX') play_info_arista_df = PlaybookInfoType( playbook_uri='job_manager_test2.yaml', vendor='Arista', device_family='df') playbooks_list = PlaybookInfoListType( playbook_info=[play_info_arista_df, play_info_juniper_qfx, play_info_juniper_mx]) job_template = JobTemplate(job_template_type='workflow', job_template_multi_device_job=True, job_template_playbooks=playbooks_list, name='Test_template_multivendors') 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)
def test_execute_job_no_vendor(self): play_info_juniper_qfx = PlaybookInfoType( playbook_uri='job_manager_test.yaml', vendor='Juniper', device_family='QFX') play_info_vendor_agnostic = PlaybookInfoType( playbook_uri='job_manager_test2.yaml') playbooks_list = PlaybookInfoListType( playbook_info=[play_info_juniper_qfx, play_info_vendor_agnostic]) job_template = JobTemplate(job_template_type='workflow', job_template_multi_device_job=True, job_template_playbooks=playbooks_list, name='Test_template_no_vendor') job_template_uuid = self._vnc_lib.job_template_create(job_template) # mock the play book executor call # check if the playbook is getting executed after job_manager # validation 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, self.fake_zk_client) wm.start_job() self.assertEqual(wm.result_handler.job_result_status, JobStatus.SUCCESS)
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='workflow', 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) wm = WFManager(log_utils.get_config_logger(), self._vnc_lib, job_input_json, log_utils, self.fake_zk_client) wm.start_job() self.assertEqual(wm.result_handler.job_result_status, JobStatus.SUCCESS)
def test_execute_job_generic_exception(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='single_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) # 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") flexmock(mock_job_handler).should_receive('handle_job') \ .and_raise(exc) exc_msg = repr(exc) sys_exit_msg = self.assertRaises(SystemExit, wm.start_job) self.assertEqual(sys_exit_msg.code, MsgBundle.getMessage( MsgBundle.EXC_JOB_ERR_HDR) + exc_msg + " ")
def test_execute_job_no_pb_file_on_path(self): # create job template play_info = PlaybookInfoType(playbook_uri='job_manager_test.yaml') playbooks_list = PlaybookInfoListType(playbook_info=[play_info]) 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='single_device_template') job_template_uuid = self._vnc_lib.job_template_create(job_template) playbook_info = {"uri": "job_manager_test.yaml"} 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) # mock the call to invoke the playbook process flexmock(os.path).should_receive('exists').and_return(False) 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.PLAYBOOK_NOT_FOUND, playbook_uri=playbook_info['uri']))
def test_execute_job_run_pb_process_rc1(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_1') 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) # mock the call to raise exception fake_resp = flexmock(status_code=123) fake_request = flexmock(requests).should_receive( 'post').and_return(fake_resp) fake_process = flexmock(returncode=1, stdout=stdout) fake_process.should_receive('wait') fake_process.should_receive('poll').and_return(123) # flexmock(subprocess).should_receive('TimeoutExpired') 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.PLAYBOOK_EXIT_WITH_ERROR, playbook_uri=TestJobManagerUtils. play_info.playbook_uri) )
def test_execute_job_with_recovery(self): # Create a job template play_info_1 = PlaybookInfoType( playbook_uri='job_manager_test_error.yml', vendor='Juniper', device_family='MX', sequence_no=0) play_info_2 = PlaybookInfoType( playbook_uri='job_manager_test_recovery.yml', vendor='Juniper', device_family='QFX', sequence_no=1, recovery_playbook=True) playbooks_list = PlaybookInfoListType([play_info_1, play_info_2]) job_template = JobTemplate(job_template_type='workflow', job_template_multi_device_job=False, job_template_playbooks=playbooks_list, name='Test_template_with_recovery') job_template_uuid = self._vnc_lib.job_template_create(job_template) # Mock creation of a process self.mock_play_book_execution(rc=42) # Mock 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, self.fake_zk_client) wm.start_job() self.assertEqual(wm.result_handler.job_result_status, JobStatus.FAILURE) expected_job_result_message = "Finished cleaning up after the error" self.assertTrue(wm.result_handler.job_result_message, expected_job_result_message)
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', fq_name=["default-global-system-config", "input_schema_template_ip"], 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 = { "job_template_id": "random_uuid", "job_execution_id": TestJobManagerUtils.execution_id, "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)) 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_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, 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_generic_exception_handle_device_job(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='handle_dev_job_gen_exc') job_template_uuid = self._vnc_lib.job_template_create(job_template) exc_msg = 'Job Device Handler Generic Exception' TestJobManagerUtils.mock_sandesh_check() job_input_json, log_utils = TestJobManagerUtils.get_min_details( job_template_uuid) job_input_json["params"] = {"device_list": [ "aad74e24-a00b-4eb3-8412-f8b9412925c3"]} 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")) 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('get_device_data') flexmock(mock_result_handler).should_receive('update_job_status') \ .with_args(JobStatus.FAILURE, exc_msg) sys_exit_msg = self.assertRaises(SystemExit, wm.start_job) self.assertEqual(sys_exit_msg.code, exc_msg)
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') TestJobManagerUtils.mock_sandesh_check() job_input_json, log_utils = TestJobManagerUtils.get_min_details( "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) wm = WFManager(log_utils.get_config_logger(), mock_vnc, job_input_json, log_utils) exc = Exception("'name' is a required property") 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_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, wm.start_job) self.assertEqual(sys_exit_msg.code, MsgBundle.getMessage(MsgBundle.JOB_EXC_REC_HDR) + MsgBundle.getMessage(MsgBundle.INVALID_SCHEMA, job_template_id="random_uuid", exc_obj=exc) + " ")
def test_execute_job_no_credentials(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_credentials') 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": { device_id: {"device_family": "MX", "device_vendor": "Juniper", "device_product": "Some random product", "device_fqname": [ "default-global-system-config", "random_fqname"]}}}) 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_CREDENTIALS_FOUND, device_id=device_id) + " \n")
def test_execute_job_read_job_template(self): job_template_id = str(uuid.uuid4()) TestJobManagerUtils.mock_sandesh_check() job_input_json, log_utils = TestJobManagerUtils.get_min_details( job_template_id) 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(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) + MsgBundle.getMessage(MsgBundle.READ_JOB_TEMPLATE_ERROR, job_template_id=job_template_id) + " ")
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_playbooks=TestJobManagerUtils. playbooks_list, name='multi_device_no_device_vendor') 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": { device_id: {"device_family": "MX"}}}) 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_VENDOR_FAMILY_MISSING, device_id=device_id) + " \n" )
def test_execute_job_run_pb_process_gen_exc(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_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) 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') exc = Exception('mock gen exception in run_playbook_process') flexmock(subprocess32).should_receive('Popen').and_raise(exc) 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_ERROR, playbook_uri=TestJobManagerUtils.play_info. playbook_uri, exc_msg=repr(exc)))
def test_execute_job_run_pb_gen_exc(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_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 = Exception('some gen exc') # mock the call to raise exception flexmock(json).should_receive('dumps') \ .and_raise(exc) 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_ERROR, playbook_uri=TestJobManagerUtils. play_info. playbook_uri, exc_msg=repr(exc)))