Commit Graph

97 Commits

Author SHA1 Message Date
Tomas Janousek
b1564e94b5 Test requested URLs in test_getall
This would've caught that the info parameter is ignored in get_info and
I'm going to refactor/improve get_all_jobs so I need stricter tests
anyway.

Also, this makes test_unsafe_chars useful. Before it was a totally
meaningless test that didn't test anything at all. :-(

(Actually the get_info problem was caught by this test, but I'm
committing the fix first to not break git bisect.)

Change-Id: Ifd06bbbd9969a2739322e36ae83f521490a6eaa0
2018-10-17 18:06:56 +02:00
Zuul
afa1e05e74 Merge "Check for 'Location' header in the response" 2018-09-21 14:11:48 +00:00
Zuul
ede93173e3 Merge "Add folder credential support" 2018-09-21 13:46:39 +00:00
Aigars Mahinovs
811a6cffc8 Allow adding extra HTTP headers to Jenkins requests
In some network setups Jenkins may be hidden behind complex reverse
proxy setups that require additional custom headers to be set on
each request in order to pass them trough

Allow providing such headers using JENKINS_API_EXTRA_HEADERS environment
variable

Change-Id: If071c5c707f916ba5f4f2c371ec600b7476bf723
2018-09-12 13:42:23 +02:00
Thanh Ha
93515ae07d
Revert "detect and respect http redirects"
This reverts commit 4150a83d45.

Change-Id: I972404ff936cbd3c0fe164dcbaa186d5e530736c
2018-08-24 13:12:38 -04:00
Tomas Janousek
fc953ee505 Fix item being ignored in get_info
This, I think, would've caused get_all_jobs to loop indefinitely if the
Jenkins instance has folders. Am I really the first to have caught this?

Change-Id: I4158b0d068135de08a3337486d8a4b41d8b7cda6
2018-08-04 19:05:15 +02:00
joelee
857235b7fa Add folder credential support
implement functions to manage credential in Jenkins Folder
job with rest api provided by :
https://wiki.jenkins.io/display/JENKINS/Credentials+Plugin

Change-Id: I9bcc3943e4ec705fe8705ad1d457b6fd4ad1024a
2018-08-03 06:39:13 +03:00
Sorin Sbarnea
4150a83d45
detect and respect http redirects
Detects if API url is redirected and corrects it in order
to avoid doubling number of requests and even failing
POST ones because of having them silently converted by
requests from POST to GET.

Displays a warning to the user when redirection occurs
because almost always this means that they are using an
old endpoint, like a non secured one.

Change-Id: I7387bf150dad307342f9a6a91afbae32859bc82e
Signed-off-by: Sorin Sbarnea <ssbarnea@redhat.com>
2018-07-16 17:56:19 +01:00
Dennis Dmitriev
f420d6de01 Check for 'Location' header in the response
If the keep_alive is not available because of using reverse
proxy, the header 'Content-Lenght' may not be available,
causing the exception:

 Error communicating with server[...]: empty response

However, if 'Location' header is present, the response is valid.

Add unit tests:
- build_job must pass even if no 'content-lenght'
  in response header
- build_job must fail if no 'location' in response header

Change-Id: I4da6dd19f9d8302a76652a3686a9377f9a2503a6
Closes-Bug:#1775047
2018-06-25 12:59:38 +03:00
Nickolas Pohilets
f7fa4ce059
Adds support for executing Groovy scripts on jenkins nodes
Change-Id: I4c9d266073c3ba2a85f2c58a44afc2f041bf37f1
Signed-off-by: Sorin Sbarnea <ssbarnea@redhat.com>
2018-06-17 19:15:13 +01:00
Jan Priessnitz
44d211b90f
Allow use of unicode job names
Closes bug: #1713481
Signed-off-by: Jan Priessnitz <jan.priessnitz@gooddata.com>

Change-Id: I8a1ff4bf0f7711ab9c9b76f538411a854244283c
2018-06-17 15:28:46 +01:00
Zuul
1c4870c20b Merge "Update URLS that require depth or tree filters" 2018-06-16 21:29:43 +00:00
Thanh Ha
7ffb27c7d5
Fix run_scripts() API
Resolve regression caused by I5369a0d35be4bf8b3b197a51e60aba21b5742cc7
preventing run_scripts() from successfully running against the remote
Jenkins instance.

Switching data to a dictionary and passing the script string without
quote() appears to allow the script to successfully run.

Add a magic string ')]}.' as an indicator of if the groovy script
successfully passed or failed. In theory if the groovy script failed
perhaps due to syntax error or some other reason the magic string
will not be printed and we can assume failure.

Change-Id: Ibaffb768ea82c76b44ec5a6cccde8563afe1783f
Signed-off-by: Thanh Ha <zxiiro@linux.com>
2018-06-15 11:37:46 -04:00
Darragh Bailey
f56e719ec9 Selectively mock requests_kerberos import
To ensure consistent test behaviour need to mock the import of
requests_kerberos to be None only for the test classes based
off of the default JenkinsTestBase

Change-Id: I79903a6b3e4a1677fa1a1980af9f73c84b385828
Depends-On: I56c9580be9a90f5cda7349a148d467e6ff4e9270
2018-06-01 15:07:12 +01:00
Sorin Sbarnea
e391e92bad Revert "Do not require requests-kerberos for tests"
This reverts commit 1946ed303b.

Change-Id: If4b1db131af467eb9a0910584c0fdc08dcee10c6
2018-05-30 16:51:44 +00:00
JP Sullivan
f1f97ab4bf Update URLS that require depth or tree filters
Some API endpoints require the use of a filter or they will respond
with HTTP error code 418 I'm a teapot.

This was seen on CloudBees Jenkins Enterprise 2.107.2.1-rolling.

Adding a depth filter will ensure the API calls will succeed.

Change-Id: Ib4d6a251bf3a024a76081b2fc83baa7839ad4015
2018-05-28 10:17:26 +01:00
Zuul
cd440ab4ac Merge "Do not require requests-kerberos for tests" 2018-05-23 10:02:36 +00:00
Zuul
892cd41f94 Merge "Update to _response_handler" 2018-05-19 21:42:19 +00:00
Jan Hruban
1946ed303b Do not require requests-kerberos for tests
The test for kerberos was dropped in
34cca0c4d9, as it was not needed for the
simpler implementation with requests. Let's drop the requests-kerberos
from test dependencies as well, as it has only caused troubles in other
tests - at least for me.

One had to set jenkins.requests_kerberos to None, as was done in the
tests.base.JenkinsTestBase. In places where it was not set to None, the
jenkins.Jenkins.jenkins_open tried the kerberos authentication in
addition to anonymous authentication on the /api/json endpoint.

This was the case for
  tests.test_jenkins_sockets.JenkinsRequestTimeoutTests.test_jenkins_open_timeout
where it has resulted in a different exception than was expected:

  Traceback (most recent call last):
    File "tests/test_jenkins_sockets.py", line 31, in test_jenkins_open_timeout
      j.jenkins_open(request, add_crumb=False)
    File "jenkins/__init__.py", line 533, in jenkins_open
      return self.jenkins_request(req, add_crumb, resolve_auth).text
    File "jenkins/__init__.py", line 547, in jenkins_request
      self._maybe_add_auth()
    File "jenkins/__init__.py", line 384, in _maybe_add_auth
      % '\n'.join(failures))
  jenkins.JenkinsException: Unable to authenticate with any scheme:
  auth(kerberos) Error in request: HTTPConnectionPool(host='127.0.0.1', port=33921): Read timed out. (read timeout=0.1)
  auth(anonymous) Error in request: HTTPConnectionPool(host='127.0.0.1', port=33921): Read timed out. (read timeout=0.1)

Change-Id: I45cbb3a666ee6a9ecbe628ca8b7f0b07dcfcb0eb
2018-05-11 22:24:36 +02:00
JP Sullivan
827f28a223 Add functions to retrieve more data from Jenkins
If tests results are added to builds, or environment variables are injected
into a job, there are separate API calls to retrieve that data.  Add functions
to do so.

Change-Id: Ie0a51839d527db6d440719eeb44cd1cc89644d06
2018-04-25 13:30:29 +01:00
JP Sullivan
5033405db7 Update to _response_handler
Account for situations where no Content-Length or Transfer-Encoding
headers are present in a response.

The length of the response.content could still indicate a response was
given.  The earlier raise_for_status() should catch bad HTTP responses
that return content.

Closes-bug: #1766660
Change-Id: I6739e26a039a898982f5cdc96d19d6992ed37737
2018-04-25 12:36:20 +01:00
Ken Dreyer
284c3d37b7 add get_queue_item() method
Pass in a queue ID number to discover a job's status, and possibly a job
URL.

Change-Id: I20541ec49cc30e5c74a6c596e02b3f42b2567fa5
Closes-Bug: #1724932
2018-04-17 12:27:32 -06:00
Ken Dreyer
91ab3d9057 build_job: return queue item identifier
Since Jenkins version 1.519 (released 2013/06/17), build_job() would
always return an empty string (the empty HTTP body from the POST
request).

Read the HTTP response's Location header and return the queue ID number.

Clients can use this number to query server for the the upcoming job's
status.

Change-Id: I2a1ef3abb7e675e0ad37dd8eb4a377af22f27a03
Closes-Bug: #1724932
2018-04-17 12:27:32 -06:00
Zuul
abe6df6d1d Merge "Adds support for multi-select build parameters" 2018-04-15 09:44:55 +00:00
Akshat Tandon
96f0a7fa5f Adds support for multi-select build parameters
This feature will allow build_job to accept build parameters as a list
of two membered tuples - [('parameter key', 'parameter value', ), ...].
Currently, build_job expects to receive a dictionary of parameters but
this limits the build_job_url function (which is called from
build_job()) to build jenkins url for jobs with multi-select
parameter(s) because dictionaries cannot have duplicate keys (in case,
when there are several values to one parameter key). urlencode is
capable of converting lists with two membered tuples to url as well.

Change-Id: I134a5f8b69ba91acd89afa791cb1cf0b5e63f5f5
Signed-off-by: Sorin Sbarnea <ssbarnea@redhat.com>
2018-04-14 07:34:21 +01:00
Teemu Patja
03ed39ba01 Adds support for deleting builds
Support for deleting individual builds for a Jenkins job.

Change-Id: I35c98855b0883986a85cbe7212ec52e6f67e5638
Signed-off-by: Sorin Sbarnea <ssbarnea@redhat.com>
2018-04-13 18:23:47 +01:00
Amadeusz Kryze
e539017eef Added folder support in views keeping backward compatibility and added tests for it.
Change-Id: I48c231cd1b9a038ede526e7e7a50e65af8aaf13b
2018-04-05 22:57:34 +02:00
Zuul
7166e97eb2 Merge "Migration to using requests" 2018-04-04 20:40:17 +00:00
Zuul
b8d2f6d444 Merge "Pass form data as body instead of in the url" 2018-04-04 20:37:21 +00:00
Ayoub Elhamdani
b7f0f717fc This Allow to include folders in jobs list when calling get_all_jobs()
method.
Closes-Bug: #1731903

Change-Id: Ib55d58c96f2bc4543db1b85336c04e4c6cd81021
2018-02-28 10:21:22 +00:00
Darragh Bailey
34cca0c4d9 Migration to using requests
Convert to the requests library to allow for more sophisticated response
handling to be added.

Want to allow for applications to override the response handling in
certain cases where the default is incorrect. Handling of urlopen
responses results in version specific handling to ensure correct
behaviour across multiple versions of python.

Changing to use the requests package, provides a higher level interface
and removes some of the version specific handling for exceptions.

Change-Id: I5369a0d35be4bf8b3b197a51e60aba21b5742cc7
Depends-On: Iabd70aa457ceb4dbc147d7cbaeec913148cb3b56
2018-02-12 11:16:54 +00:00
Darragh Bailey
cd237fbcad Pass form data as body instead of in the url
Switch create node to pass form data via request body, removing the
need for special handling in the _build_url method. Consequently remove
the string placeholder in the endpoint format string.

Remove unnecessary parameters of name and type from inclusion in the
json form data item, confirming they are ignored and required to be set
in the main form data.

Tested against jenkins 2.19.4.

Change-Id: I5f8f870a2ee3539505e984abccb0c87c632c1b9a
2018-01-05 16:36:30 +00:00
Andrew Mellen
16007e0185 Sanitize url in get_info() function
Change-Id: I540f6d636e277d0ad230f844d2db6220edfb672f
Closes-Bug: #1698815
2017-09-15 18:49:20 -04:00
Ken Dreyer
04c153cc0b create_node: avoid double-encoding
Prior to this change, create_node() would double-encode certain url
params: once when quote()'ing in _get_encoded_params(), and again in
urlencode().

This would cause HTTP 400 errors when creating a node with a name that
contains a url-encodable character, like "my+test+node".

Reviewed-by: Alfredo Deza <adeza@redhat.com>

Change-Id: I237e2988e168af81e623d3f065753f2e9b617696
2017-05-01 10:30:35 -06:00
grahamlyons
106048dd87 Fix error thrown in presence of placeholder tasks
When there are placeholder tasks in an executor for a node then there is no
build number yet assigned and attempting to access that key in the dictionary
will fail.

This fix filters out any tasks which are instances of that class before
proceeding.

Change-Id: Ie5b237c3e6023c1822f91e5982ec84f1a363130c
Closes-Bug: #1659787
2017-02-09 16:30:52 +00:00
Guido Günther
0f6739103b Allow to wait for jenkins to enter normal operation
During Jenkins deployment via e.g. puppet jenkins might be started but
not yet serving requests. Allow to wait for jenkins to enter normal
operation mode and fail if it doesn't show up after N seconds.

Allow for 0 seconds to just test and return success or failure without
any waiting.

This will allow us to avoid open coding "is jenkins ready?" for
different provisioners like puppet, chef, salt, ansible, ... when this
gets jused by jjb.

Change-Id: I7fd8aed43f571528f27dac681cc1e1f77a0e0ad7
Co-Authored-By: Darragh Bailey <dbailey@hp.com>
2017-01-21 06:41:45 -08:00
mhuin
cf4f3d17ed url-proof scripts passed to run_script
Groovy scripts may contain characters that need to be escaped prior
to being passed to Jenkins' REST API; for example "&&" in condition
testing. Not doing so will result in a script execution failure on
the server.

Change-Id: Ied6c2a48392cadbadb84865dad47ceed99e694a1
2016-12-16 16:39:45 +00:00
Ardar Martian
0d7b59f5d2 get_running_builds failed when server has a path
When the server has an URL path such as http://example.org/ci/, the
get_running_builds() would fail to extract the job name from the
executor informations.  It most probably fails when using CloudBees
folder as well.

Relax the regex used to detect the job name by changing match() with
search().

Update JenkinsListRunningBuildsTest so it craft the URLs based on the
test scenario (make_url) instead of hardcoding them.  That would have
caught the bug. See 2aa1a5f1 which introduced the scenarios.

Signed-off-by: Antoine Musso <hashar@free.fr>
Change-Id: I1b69adf121ee74e3336ac9bd1d4d55797fb72de5
2016-11-08 10:47:04 +01:00
Jenkins
4d495afe74 Merge "Fetch all builds from a job" 2016-05-19 05:26:40 +00:00
Jenkins
e39c4dcbd1 Merge "Make the kerberos support python3 ready" 2016-04-29 19:06:12 +00:00
Jan Hruban
d2cfca68d0 Make the kerberos support python3 ready
urllib.request.Request.get_host() is removed in python >=3.4 in favor of
urllib.request.Request.host. It works in python 2.7 too.

Update the test to use Mock with spec where applicable, to ensure that
only available attributes are accessed.

Change-Id: I0e6a4e1539f9db329b6f113207d52f171cc600a7
2016-04-29 19:30:24 +02:00
Chris St. Pierre
0881b20cbf Fetch all builds from a job
Jenkins only provides 100 builds of a job with the job info. This
makes python-jenkins fetch all builds when there are more than 100.

Change-Id: I326efa11c975e036b65a159414062966239c9749
2016-04-29 16:22:34 +00:00
Ken Dreyer
fd8c8151b9 add get_whoami()
This function is useful for simply testing that a user's authentication
credentials are correct.

Change-Id: I07841eccd9cd2015ac432c9c22fb8e451a638c0c
2016-04-21 07:45:19 -06:00
Aliaksandr Buhayeu
f6f26ec481 Adds possibility to retrieve all jobs from the view specified
This patch adds the new `view_name` argument
to the `get_jobs` method that limits the list of jobs
returned to only those configured in the view specified.

Change-Id: I78266ed13cd6c183a2f4858458817455b79d9a23
2016-02-22 18:03:48 +03:00
Jenkins
6728d36463 Merge "Add optional kerberos support" 2016-01-27 23:38:09 +00:00
Lukas Vacek
4152a0138b Add optional kerberos support
Check if "kerberos" package can be imported at runtime
(in __init__.py).  If not, everything works as before.
If "kerberos" package can be imported register a new
urllib handler (defined in urllib_kerb.py) which adds
support for kerberos auth.

This urllib handler (urllib_kerb.py) is only triggered
on 401 (Unauthorized) response, and we try to auth
with kerberos. If unsuccessful, next 401 handler (if any)
is triggered.

Change-Id: I1a47e455aa14535a124df950994718a11d7e4f57
2016-01-27 23:08:04 +01:00
Guido Günther
0c503b724c Add support for handling promotions
Promotions can be attached to jobs via the REST API described at:

https://issues.jenkins-ci.org/browse/JENKINS-8963

Co-Authored-By: Guido Günther <agx@sigxcpu.org>
Co-Authored-By: Joao Vale <jpvale@gmail.com>

Change-Id: I756a35f53f96ba6e46e71f36ea99f62d32b604d2
2016-01-13 18:14:39 +01:00
Dong Ma
302eb5dad6 Fixed create_node() Error
Fixed create_node() raises HTTP Error 400 Bad Request

This issue caused by:
    self.jenkins_open(Request(
        self._build_url(CREATE_NODE, params), b''))
The "params" dictionary of options isn't parsed correctly
during _build_url funciton that Jenkins is expecting

Fixed it use urlencode to parse the params and construct
the URL

Change-Id: I85fa43bc0b4ff1530133b862734e94aede9b44a5
Closes-Bug: #1500898
2015-12-12 18:35:03 +08:00
Jenkins
4c0536b428 Merge "Use TestWithScenarios to add additional url tests" 2015-12-02 19:50:08 +00:00
Darragh Bailey
2aa1a5f1b8 Use TestWithScenarios to add additional url tests
It's important to check that we correctly handle the server url for all
API calls, so ensure that the test code uses test scenarios to cover.

Fix get_info() api call creation of url.

Change-Id: Idb0becc4c0c56df81bc5545919e530b7a77dadc9
2015-12-02 11:35:59 -08:00