def test_execute_job_multiple_vendors(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_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_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 get_min_details(cls, job_template_id):
     job_input_json = {
         "job_template_id": job_template_id,
         "input": {
             "playbook_data": "some playbook data"
         },
         "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))
     return job_input_json, log_utils

if __name__ == "__main__":

    # parse the params passed to the job manager process and initialize
    # sandesh instance
    job_input_json = None
    try:
        job_params = parse_args()
        job_input_json = json.loads(job_params.job_input[0])
        if job_input_json is None:
            sys.exit("Job input data is not passed to job mgr. "
                     "Aborting job ...")

        job_log_utils = JobLogUtils(
            sandesh_instance_id=job_input_json['job_execution_id'],
            config_args=job_input_json['args'])
        logger = job_log_utils.config_logger
    except Exception as e:
        print >> sys.stderr, "Failed to initialize logger due "\
                             "to Exception: %s" % traceback.print_stack()
        sys.exit("Exiting due to logger initialization error: %s" % repr(e))

    # initialize _vnc_api instance
    vnc_api = None
    try:
        auth_token = job_input_json['auth_token']
        vnc_api = VncApi(auth_token=auth_token)
        logger.info("VNC api is initialized using the auth token passed.")
    except Exception as e:
        logger.error("Caught exception when initialing vnc api: "