Пример #1
0
def jenkins_request_with_headers(jenkins_server, req):
    """
    We need to get the headers in addition to the body answer
    to get the location from them
    This function uses jenkins_request method from python-jenkins library
    with just the return call changed

    :param jenkins_server: The server to query
    :param req: The request to execute
    :return: Dict containing the response body (key body)
        and the headers coming along (headers)
    """
    try:
        response = jenkins_server.jenkins_request(req)
        response_body = response.content
        response_headers = response.headers
        if response_body is None:
            raise jenkins.EmptyResponseException(
                "Error communicating with server[%s]: "
                "empty response" % jenkins_server.server)
        return {'body': response_body.decode('utf-8'), 'headers': response_headers}
    except HTTPError as e:
        # Jenkins's funky authentication means its nigh impossible to distinguish errors.
        if e.code in [401, 403, 500]:
            raise JenkinsException(
                'Error in request. Possibly authentication failed [%s]: %s' % (e.code, e.msg)
            )
        elif e.code == 404:
            raise jenkins.NotFoundException('Requested item could not be found')
        else:
            raise
    except socket.timeout as e:
        raise jenkins.TimeoutException('Error in request: %s' % e)
    except URLError as e:
        raise JenkinsException('Error in request: %s' % e.reason)
Пример #2
0
    def create_node(self, name, numExecutors=2, nodeDescription=None,
                    remoteFS='/var/lib/jenkins', labels=None, exclusive=False,
                    launcher='hudson.slaves.JNLPLauncher', launcher_params={}):
        '''
        @param name: name of node to create
        @type  name: str
        @param numExecutors: number of executors for node
        @type  numExecutors: int
        @param nodeDescription: Description of node
        @type  nodeDescription: str
        @param remoteFS: Remote filesystem location to use
        @type  remoteFS: str
        @param labels: Labels to associate with node
        @type  labels: str
        @param exclusive: Use this node for tied jobs only
        @type  exclusive: boolean
        @param launcher: The launch method for the slave
        @type  launcher: str
        @param launcher_params: Additional parameters for the launcher
        @type  launcher_params: dict
        '''
        if self.node_exists(name):
            raise JenkinsException('node[%s] already exists' % (name))

        mode = 'NORMAL'
        if exclusive:
            mode = 'EXCLUSIVE'

        #hudson.plugins.sshslaves.SSHLauncher
        #hudson.slaves.CommandLauncher
        #hudson.os.windows.ManagedWindowsServiceLauncher
        launcher_params['stapler-class'] = launcher

        inner_params = {
            'name': name,
            'nodeDescription': nodeDescription,
            'numExecutors': numExecutors,
            'remoteFS': remoteFS,
            'labelString': labels,
            'mode': mode,
            'type': NODE_TYPE,
            'retentionStrategy': {
                'stapler-class': 'hudson.slaves.RetentionStrategy$Always'},
            'nodeProperties': {'stapler-class-bag': 'true'},
            'launcher': launcher_params
        }

        params = {
            'name': name,
            'type': NODE_TYPE,
            'json': json.dumps(inner_params)
        }

        self.jenkins_open(urlrequest.Request(
            self.server + CREATE_NODE % urlparse.urlencode(params)))

        if not self.node_exists(name):
            raise JenkinsException('create[%s] failed' % (name))
Пример #3
0
def jenkins_request_with_headers(jenkins_server, req, add_crumb=True):
    """
    We need to get the headers in addition to the body answer
    to get the location from them
    This function is just a copy of the one present in python-jenkins library
    with just the return call changed

    :param jenkins_server: The server to query
    :param req: The request to execute
    :param add_crumb: Boolean to indicate if it should add crumb to the request
    :return:
    """
    try:
        if jenkins_server.auth:
            req.add_header('Authorization', jenkins_server.auth)
        if add_crumb:
            jenkins_server.maybe_add_crumb(req)
        response = urlopen(req, timeout=jenkins_server.timeout)
        response_body = response.read()
        response_headers = response.info()
        if response_body is None:
            raise jenkins.EmptyResponseException(
                "Error communicating with server[%s]: "
                "empty response" % jenkins_server.server)
        return {
            'body': response_body.decode('utf-8'),
            'headers': response_headers
        }
    except HTTPError as e:
        # Jenkins's funky authentication means its nigh impossible to
        # distinguish errors.
        if e.code in [401, 403, 500]:
            # six.moves.urllib.error.HTTPError provides a 'reason'
            # attribute for all python version except for ver 2.6
            # Falling back to HTTPError.msg since it contains the
            # same info as reason
            raise JenkinsException('Error in request. ' +
                                   'Possibly authentication failed [%s]: %s' %
                                   (e.code, e.msg))
        elif e.code == 404:
            raise jenkins.NotFoundException(
                'Requested item could not be found')
        else:
            raise
    except socket.timeout as e:
        raise jenkins.TimeoutException('Error in request: %s' % e)
    except URLError as e:
        # python 2.6 compatibility to ensure same exception raised
        # since URLError wraps a socket timeout on python 2.6.
        if str(e.reason) == "timed out":
            raise jenkins.TimeoutException('Error in request: %s' % e.reason)
        raise JenkinsException('Error in request: %s' % e.reason)
Пример #4
0
def jenkins_request_with_headers(jenkins_server, req):
    """
    We need to get the headers in addition to the body answer
    to get the location from them
    This function uses jenkins_request method from python-jenkins library
    with just the return call changed

    :param jenkins_server: The server to query
    :param req: The request to execute
    :return: Dict containing the response body (key body)
        and the headers coming along (headers)
    """
    try:
        response = jenkins_server.jenkins_request(req)
        response_body = response.content
        response_headers = response.headers
        if response_body is None:
            raise jenkins.EmptyResponseException(
                "Error communicating with server[%s]: "
                "empty response" % jenkins_server.server)
        return {
            'body': response_body.decode('utf-8'),
            'headers': response_headers
        }
    except HTTPError as e:
        # Jenkins's funky authentication means its nigh impossible to
        # distinguish errors.
        if e.code in [401, 403, 500]:
            # six.moves.urllib.error.HTTPError provides a 'reason'
            # attribute for all python version except for ver 2.6
            # Falling back to HTTPError.msg since it contains the
            # same info as reason
            raise JenkinsException('Error in request. ' +
                                   'Possibly authentication failed [%s]: %s' %
                                   (e.code, e.msg))
        elif e.code == 404:
            raise jenkins.NotFoundException(
                'Requested item could not be found')
        else:
            raise
    except socket.timeout as e:
        raise jenkins.TimeoutException('Error in request: %s' % e)
    except URLError as e:
        # python 2.6 compatibility to ensure same exception raised
        # since URLError wraps a socket timeout on python 2.6.
        if str(e.reason) == "timed out":
            raise jenkins.TimeoutException('Error in request: %s' % e.reason)
        raise JenkinsException('Error in request: %s' % e.reason)
Пример #5
0
 def transient_failure():
     try:
         if not tries:
             raise JenkinsException("error")
         get_whoami()
     finally:
         tries.append(True)
Пример #6
0
 def transient_failure(*args, **kwargs):
     try:
         if not tries:
             raise JenkinsException("error")
         create_node(*args, **kwargs)
     finally:
         tries.append(True)
Пример #7
0
    def test_artifact_event_jenkins_exception(self):
        eg = es_logger.plugins.artifact.ArtifactEvent()
        fields = eg.get_fields()
        nose.tools.ok_(
            fields == es_logger.interface.EventGenerator.DEFAULT_FIELDS)

        esl = unittest.mock.MagicMock()
        esl.console_log = 'log'
        esl.build_info = {
            'artifacts': [{
                'relativePath': 'es-logger-data.json'
            }]
        }
        esl.server.get_build_artifact.side_effect = JenkinsException(
            "Bad download")

        with nose.tools.assert_logs(level='DEBUG') as cm:
            events = eg.generate_events(esl)

        nose.tools.ok_(events == [])
        nose.tools.assert_equal(cm.output, [
            'WARNING:es_logger.plugins.artifact:JenkinsException when attempting to get '
            + 'es-logger-data.json artifact: Bad download',
            'INFO:es_logger.plugins.artifact:No saved event data found in artifact '
            + 'es-logger-data.json',
            'DEBUG:es_logger.plugins.artifact:Data: []'
        ])
Пример #8
0
    def test_get_build_data_config_error(self):
        # Recreate the esl to validate parameters aren't set
        with unittest.mock.patch.dict(
                'os.environ', {'JENKINS_URL': 'jenkins_url', 'JENKINS_USER': '******',
                               'JENKINS_PASSWORD': '******', 'ES_JOB_NAME': 'es_job_name',
                               'ES_BUILD_NUMBER': '2', 'GATHER_BUILD_DATA': 'dummy'}):
            self.esl = es_logger.EsLogger(1000, ['dummy'])
        self.esl.es_build_number = '2'
        with unittest.mock.patch('stevedore.driver.DriverManager') as mock_driver_mgr, \
                unittest.mock.patch('jenkins.Jenkins.get_build_info') as mock_build_info, \
                unittest.mock.patch('jenkins.Jenkins.get_build_env_vars') as mock_env_vars, \
                unittest.mock.patch('jenkins.Jenkins.get_build_console_output') as mock_console, \
                unittest.mock.patch('jenkins.Jenkins.get_job_config') as mock_config:
            mock_env_vars.return_value = {'envMap': {'BUILD_NUMBER': '1',
                                                     'JOB_NAME': 'job_name',
                                                     'BUILD_URL': 'url',
                                                     'dummy': 'dummy'}}
            mock_build_info.return_value = {
                'description': 'description',
                'number': '1',
                'url': 'url',
                'actions': [{'_class': 'hudson.model.ParametersAction',
                             'parameters': [{'name': 'param', 'value': 'value'}]},
                            {'_class': 'hudson.plugins.git.util.BuildData',
                             'buildsByBranchName': {'b1': {'buildNumber': '1'},
                                                    'b2': {'buildNumber': '2'}},
                             'remoteUrls': ["repoURL"]}]}
            mock_console.return_value = 'log'
            mock_config.side_effect = JenkinsException("Error from Jenkins Api")

            self.esl.get_build_data()
            mock_driver_mgr.assert_called_once()

            # Job Config recorded
            expetected_error_msg = "Unable to retrieve config.xml."
            nose.tools.ok_(self.esl.es_info['job_config_info'] == expetected_error_msg,
                           "'job_config_info' value {} not '{}'".format(
                               self.esl.es_info['job_config_info'],
                               expetected_error_msg))

            # Console log recorded
            nose.tools.ok_(self.esl.es_info['console_log'] == 'log',
                           "console_log not 'log': {}".format(self.esl.es_info))
            # Parameters pulled out
            nose.tools.ok_(self.esl.es_info['parameters'].get('param') == 'value',
                           "Parameter 'param' not 'value': {}".format(self.esl.es_info))

            # Prevent ES field explosion through rewrite of builds by branch name
            nose.tools.ok_(
                self.esl.es_info['build_info']['actions'][1]['buildsByBranchName'] ==
                'Removed by es-logger',
                "buildsByBranchName not removed by es-logger: {}".format(self.esl.es_info))
            # Make sure the gather of the gather_build_data plugins was called
            nose.tools.ok_('dummy' in self.esl.es_info['build_data'].keys(),
                           "dummy not in build_data keys: {}".format(
                               self.esl.es_info))
            # SCM correctly processed
            nose.tools.ok_('repoURL' in self.esl.es_info['build_data'].keys(),
                           "repoURL not in build_data keys: {}".format(
                               self.esl.es_info['build_data']))
Пример #9
0
 def auth_failure():
     try:
         if not tries:
             raise JenkinsException("[401]")
         get_whoami()
     finally:
         tries.append(True)
Пример #10
0
 def get_info(self):
     if self.down:
         raise JenkinsException("Jenkins is down")
     d = {
         u'assignedLabels': [{}],
         u'description':
         None,
         u'jobs': [{
             u'color': u'red',
             u'name': u'test-job',
             u'url': u'https://jenkins.example.com/job/test-job/'
         }],
         u'mode':
         u'NORMAL',
         u'nodeDescription':
         u'the master Jenkins node',
         u'nodeName':
         u'',
         u'numExecutors':
         1,
         u'overallLoad': {},
         u'primaryView': {
             u'name': u'Overview',
             u'url': u'https://jenkins.example.com/'
         },
         u'quietingDown':
         self.quiet,
         u'slaveAgentPort':
         8090,
         u'unlabeledLoad': {},
         u'useCrumbs':
         False,
         u'useSecurity':
         True,
         u'views': [{
             u'name': u'test-view',
             u'url': u'https://jenkins.example.com/view/test-view/'
         }]
     }
     return d
Пример #11
0
 def failure(*args, **kwargs):
     raise JenkinsException("error")