def test_execute_job(self): # create job template play_info = PlaybookInfoType(playbook_uri='job_manager_test.yaml', vendor='Juniper', device_family='MX') 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=playbooks_list, name='Test_template') job_template_uuid = self._vnc_lib.job_template_create(job_template) # mock the play book executor call self.mock_play_book_executor() execution_id = uuid.uuid4() # Hardcoding a sample auth token since its a madatory value passed # by the api server while invoking the job manager. Here since we # are directly invoking the job manager, we are passing a dummy # auth token for testing. This value is not used internally since # the calls that use the auth token are mocked sample_auth_token = "6e7d7f87faa54fac96a2a28ec752336a" job_input_json = { "job_template_id": job_template_uuid, "input": { "playbook_data": "some playbook data" }, "job_execution_id": str(execution_id), "auth_token": sample_auth_token } logger = JobLogger() jm = JobManager(logger, self._vnc_lib, job_input_json) jm.start_job()
def test_execute_job(self): # create job template play_info = PlaybookInfoType(playbook_uri='job_manager_test.yaml', vendor='Juniper', device_family='MX') 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=playbooks_list, name='Test_template') job_template_uuid = self._vnc_lib.job_template_create(job_template) # mock the play book executor call self.mock_play_book_executor() execution_id = uuid.uuid4() job_input_json = { "job_template_id": job_template_uuid, "input": { "playbook_data": "some playbook data" }, "execution_id": str(execution_id) } sandesh_logger = initialize_sandesh_logger() jm = JobManager(sandesh_logger, self._vnc_lib, job_input_json) jm.start_job()
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') playbooks_list = PlaybookInfoListType( 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, job_template_playbooks=playbooks_list, name='Test_template_multidevfamilies') 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) 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)
def test_execute_job_04(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_test.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='device', job_template_job_runtime='ansible', 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_executor() execution_id = uuid.uuid4() # Hardcoding a sample auth token since its a madatory value passed # by the api server while invoking the job manager. Here since we # are directly invoking the job manager, we are passing a dummy # auth token for testing. This value is not used internally since # the calls that use the auth token are mocked sample_auth_token = "6e7d7f87faa54fac96a2a28ec752336a" job_input_json = { "job_template_id": job_template_uuid, "input": { "playbook_data": "some playbook data" }, "job_execution_id": str(execution_id), "auth_token": sample_auth_token, "params": { "device_list": ["aad74e24-a00b-4eb3-8412-f8b9412925c3"] }, "device_json": { "aad74e24-a00b-4eb3-8412-f8b9412925c3": { 'device_vendor': 'Juniper', 'device_family': 'MX' } } } logger = JobLogger() jm = JobManager(logger, self._vnc_lib, job_input_json) resp = jm.start_job()
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_test.yaml') playbooks_list = PlaybookInfoListType( playbook_info=[play_info_juniper_qfx, play_info_vendor_agnostic]) 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_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() self.mock_sandesh_check() execution_id = uuid.uuid4() # Hardcoding a sample auth token since its a madatory value passed # by the api server while invoking the job manager. Here since we # are directly invoking the job manager, we are passing a dummy # auth token for testing. This value is not used internally since # the calls that use the auth token are mocked sample_auth_token = "6e7d7f87faa54fac96a2a28ec752336a" job_input_json = {"job_template_id": job_template_uuid, "input": {"playbook_data": "some playbook data"}, "job_execution_id": str(execution_id), "auth_token": sample_auth_token, "params": { "device_list": ["aad74e24-a00b-4eb3-8412-f8b9412925c3"]}, "device_json": { "aad74e24-a00b-4eb3-8412-f8b9412925c3": {'device_vendor': 'Juniper', 'device_family': 'MX' } }, "args": {"collectors": ['127.0.0.1:8086']}} args = {"collectors": ['127.0.0.1:8086']} log_utils = JobLogUtils(sandesh_instance_id=str(execution_id), config_args=json.dumps(args)) 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)
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) jm = 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, jm.start_job) self.assertEqual(sys_exit_msg.code, MsgBundle.getMessage( MsgBundle.EXC_JOB_ERR_HDR) + exc_msg + " ")
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) jm = JobManager(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) e = Exception("Mock " "Generic Exception") flexmock(mock_job_handler).should_receive('handle_job')\ .and_raise(e) exc_msg = repr(e) sys_exit_msg = self.assertRaises(SystemExit, jm.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) jm = JobManager(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, jm.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_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, "auth_token": "6e7d7f87faa54fac96a2a28ec752336a", "args": TestJobManagerUtils.args } log_utils = JobLogUtils( sandesh_instance_id=TestJobManagerUtils.execution_id, config_args=json.dumps(TestJobManagerUtils.args)) jm = JobManager(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, jm.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_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") jm = 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")) e = Exception('Mock Generic Exception in job_handler') sys_exit_msg = self.assertRaises(SystemExit, jm.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(e)))
def test_execute_job_success(self): # create job template play_info = PlaybookInfoType(playbook_uri='job_manager_test.yaml', vendor='Juniper', device_family='MX') 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=playbooks_list, name='Test_template') job_template_uuid = self._vnc_lib.job_template_create(job_template) self.mock_play_book_execution() self.mock_sandesh_check() # Hardcoding a sample auth token since its a madatory value passed # by the api server while invoking the job manager. Here since we # are directly invoking the job manager, we are passing a dummy # auth token for testing. This value is not used internally since # the calls that use the auth token are mocked sample_auth_token = "6e7d7f87faa54fac96a2a28ec752336a" execution_id = uuid.uuid4() job_input_json = {"job_template_id": job_template_uuid, "input": {"playbook_data": "some playbook data"}, "job_execution_id": str(execution_id), "auth_token": sample_auth_token, "args": {"collectors": ['127.0.0.1:8086']}} args = {"collectors": ['127.0.0.1:8086']} log_utils = JobLogUtils(sandesh_instance_id=str(execution_id), config_args=json.dumps(args)) 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)
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) jm = JobManager(log_utils.get_config_logger(), mock_vnc, job_input_json, log_utils) e = 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_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, jm.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=e) + " ")
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"]} jm = JobManager(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('update_job_status')\ .with_args(JobStatus.FAILURE, exc_msg) sys_exit_msg = self.assertRaises(SystemExit, jm.start_job) self.assertEqual(sys_exit_msg.code, exc_msg)
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"}}}) jm = JobManager(log_utils.get_config_logger(), self._vnc_lib, job_input_json, log_utils) sys_exit_msg = self.assertRaises(SystemExit, jm.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_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) jm = JobManager(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') e = Exception('mock gen exception in run_playbook_process') flexmock(subprocess32).should_receive('Popen').and_raise(e) sys_exit_msg = self.assertRaises(SystemExit, jm.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(e)))
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": "******", "device_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")
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) jm = JobManager(log_utils.get_config_logger(), self._vnc_lib, job_input_json, log_utils) e = Exception('some gen exc') # mock the call to raise exception flexmock(json).should_receive('dumps')\ .and_raise(e) sys_exit_msg = self.assertRaises(SystemExit, jm.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(e)))
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(NoIdError('No such job Template id')) jm = JobManager(log_utils.get_config_logger(), mock_vnc, job_input_json, log_utils) sys_exit_msg = self.assertRaises(SystemExit, jm.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) + " ")