def test_execute_job_polling_loop(self):
        jenkins_mock = mock.Mock(spec=jenkins.Jenkins, auth='secret')
        jenkins_mock.get_job_info.return_value = {'nextBuildNumber': '1'}
        jenkins_mock.get_build_info.side_effect = \
            [{'result': None},
             {'result': 'SUCCESS',
              'url': 'http://aaa.fake-url.com/congratulation/its-a-job'}]
        jenkins_mock.build_job_url.return_value = \
            'http://www.jenkins.url/somewhere/in/the/universe'

        hook_mock = mock.Mock(spec=JenkinsHook)
        hook_mock.get_jenkins_server.return_value = jenkins_mock

        the_parameters = {'a_param': 'blip', 'another_param': '42'}

        with mock.patch.object(JenkinsJobTriggerOperator, "get_hook") as get_hook_mocked,\
            mock.patch('airflow.contrib.operators.jenkins_job_trigger_operator'
                       '.jenkins_request_with_headers') as mock_make_request:
            mock_make_request.side_effect = \
                [{'body': '', 'headers': {'Location': 'http://what-a-strange.url/18'}},
                 {'body': '{"executable":{"number":"1"}}', 'headers': {}}]
            get_hook_mocked.return_value = hook_mock
            operator = JenkinsJobTriggerOperator(
                dag=None,
                task_id="operator_test",
                job_name="a_job_on_jenkins",
                jenkins_connection_id="fake_jenkins_connection",
                # The hook is mocked, this connection won't be used
                parameters=the_parameters,
                sleep_time=1)

            operator.execute(None)
            self.assertEqual(jenkins_mock.get_build_info.call_count, 2)
    def test_execute_job_polling_loop(self):
        jenkins_mock = mock.Mock(spec=jenkins.Jenkins, auth='secret')
        jenkins_mock.get_job_info.return_value = {'nextBuildNumber': '1'}
        jenkins_mock.get_build_info.side_effect = \
            [{'result': None},
             {'result': 'SUCCESS',
              'url': 'http://aaa.fake-url.com/congratulation/its-a-job'}]
        jenkins_mock.build_job_url.return_value = \
            'http://www.jenkins.url/somewhere/in/the/universe'

        hook_mock = mock.Mock(spec=JenkinsHook)
        hook_mock.get_jenkins_server.return_value = jenkins_mock

        the_parameters = {'a_param': 'blip', 'another_param': '42'}

        with mock.patch.object(JenkinsJobTriggerOperator, "get_hook") as get_hook_mocked,\
            mock.patch('airflow.contrib.operators.jenkins_job_trigger_operator'
                       '.jenkins_request_with_headers') as mock_make_request:
            mock_make_request.side_effect = \
                [{'body': '', 'headers': {'Location': 'http://what-a-strange.url/18'}},
                 {'body': '{"executable":{"number":"1"}}', 'headers': {}}]
            get_hook_mocked.return_value = hook_mock
            operator = JenkinsJobTriggerOperator(
                dag=None,
                task_id="operator_test",
                job_name="a_job_on_jenkins",
                jenkins_connection_id="fake_jenkins_connection",
                # The hook is mocked, this connection won't be used
                parameters=the_parameters,
                sleep_time=1)

            operator.execute(None)
            self.assertEqual(jenkins_mock.get_build_info.call_count, 2)
    def test_build_job_request_settings(self):
        jenkins_mock = mock.Mock(spec=jenkins.Jenkins, auth='secret', timeout=2)
        jenkins_mock.build_job_url.return_value = 'http://apache.org'

        with mock.patch('airflow.contrib.operators.jenkins_job_trigger_operator'
                        '.jenkins_request_with_headers') as mock_make_request:
            operator = JenkinsJobTriggerOperator(
                dag=None,
                task_id="build_job_test",
                job_name="a_job_on_jenkins",
                jenkins_connection_id="fake_jenkins_connection")
            operator.build_job(jenkins_mock)
            mock_request = mock_make_request.call_args_list[0][0][1]

        self.assertEqual(mock_request.method, 'POST')
        self.assertEqual(mock_request.url, 'http://apache.org')
Example #4
0
    "start_date": datetime_start_date,
    "retries": 1,
    "retry_delay": timedelta(minutes=5),
    "depends_on_past": False,
    "concurrency": 8,
    "max_active_runs": 8
}

with DAG("test_jenkins", default_args=default_args,
         schedule_interval=None) as dag:
    job_trigger = JenkinsJobTriggerOperator(
        task_id="trigger_job",
        job_name="generate-merlin-config",
        parameters={
            "first_parameter": "a_value",
            "second_parameter": "18"
        },
        # parameters="resources/paremeter.json", You can also pass a path to a json file containing your param
        jenkins_connection_id=
        "your_jenkins_connection"  # T he connection must be configured first
    )

    def grab_artifact_from_jenkins(**context):
        """
        Grab an artifact from the previous job
        The python-jenkins library doesn't expose a method for that
        But it's totally possible to build manually the request for that
        """
        hook = JenkinsHook("your_jenkins_connection")
        jenkins_server = hook.get_jenkins_server()
        url = context['task_instance'].xcom_pull(task_ids='trigger_job')