Exemplo n.º 1
0
    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_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)

        jmgr = JobManager(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, 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_ERROR,
                playbook_uri=TestJobManagerUtils.play_info.playbook_uri,
                exc_msg=repr(exc)))
    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")
    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)

        jmgr = 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)

        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, jmgr.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)

        jmgr = 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, 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.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,
            "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)

        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, jmgr.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")

        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)))
    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)

        jmgr = JobManager(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_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)
        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_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"]
        }

        jmgr = 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, jmgr.start_job)
        self.assertEqual(sys_exit_msg.code, exc_msg)
    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_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)

        jmgr = 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')

        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)

        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_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'))
        jmgr = JobManager(log_utils.get_config_logger(), mock_vnc,
                          job_input_json, log_utils)
        sys_exit_msg = self.assertRaises(SystemExit, jmgr.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_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 + " ")