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
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
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
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>
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
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>
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
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
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
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
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
Pass in a queue ID number to discover a job's status, and possibly a job
URL.
Change-Id: I20541ec49cc30e5c74a6c596e02b3f42b2567fa5
Closes-Bug: #1724932
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
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>
Support for deleting individual builds for a Jenkins job.
Change-Id: I35c98855b0883986a85cbe7212ec52e6f67e5638
Signed-off-by: Sorin Sbarnea <ssbarnea@redhat.com>
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
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
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
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
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>
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
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
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
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
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
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
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
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
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