Allow JJB to set connection timeout to jenkins server
Change If84778231b provided an option to set a connection timeout in the python-jenkins library. This change allows JJB to override that timeout value. Add tests to ensure that the timeout is only set when specified via the config options. Change-Id: I3dfe9139469dd0e8549eeedb1833c55ac79ea8b5
This commit is contained in:
parent
6a8223e05b
commit
55e0fdc86c
|
@ -71,6 +71,10 @@ jenkins section
|
|||
**url**
|
||||
The base URL for your Jenkins installation.
|
||||
|
||||
**timeout**
|
||||
(Optional) The connection timeout (in seconds) to the Jenkins server.
|
||||
By default this is set to the system configured socket timeout.
|
||||
|
||||
**query_plugins_info**
|
||||
Whether to query the Jenkins instance for plugin info when a configuration
|
||||
file is provided. If a configuration file is not provided `jenkins-jobs` will
|
||||
|
|
|
@ -32,6 +32,8 @@ from jenkins_jobs.parser import YamlParser
|
|||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_DEFAULT_TIMEOUT = object()
|
||||
|
||||
|
||||
class CacheStorage(object):
|
||||
# ensure each instance of the class has a reference to the required
|
||||
|
@ -100,8 +102,11 @@ class CacheStorage(object):
|
|||
|
||||
|
||||
class Jenkins(object):
|
||||
def __init__(self, url, user, password):
|
||||
self.jenkins = jenkins.Jenkins(url, user, password)
|
||||
def __init__(self, url, user, password, timeout=_DEFAULT_TIMEOUT):
|
||||
if timeout != _DEFAULT_TIMEOUT:
|
||||
self.jenkins = jenkins.Jenkins(url, user, password, timeout)
|
||||
else:
|
||||
self.jenkins = jenkins.Jenkins(url, user, password)
|
||||
self._jobs = None
|
||||
self._job_list = None
|
||||
|
||||
|
@ -190,9 +195,10 @@ class Jenkins(object):
|
|||
|
||||
class Builder(object):
|
||||
def __init__(self, jenkins_url, jenkins_user, jenkins_password,
|
||||
config=None, ignore_cache=False, flush_cache=False,
|
||||
plugins_list=None):
|
||||
self.jenkins = Jenkins(jenkins_url, jenkins_user, jenkins_password)
|
||||
config=None, jenkins_timeout=_DEFAULT_TIMEOUT,
|
||||
ignore_cache=False, flush_cache=False, plugins_list=None):
|
||||
self.jenkins = Jenkins(jenkins_url, jenkins_user, jenkins_password,
|
||||
jenkins_timeout)
|
||||
self.cache = CacheStorage(jenkins_url, flush=flush_cache)
|
||||
self.global_config = config
|
||||
self.ignore_cache = ignore_cache
|
||||
|
|
|
@ -246,6 +246,21 @@ def execute(options, config):
|
|||
"Password provided, please check your configuration."
|
||||
)
|
||||
|
||||
# None -- no timeout, blocking mode; same as setblocking(True)
|
||||
# 0.0 -- non-blocking mode; same as setblocking(False) <--- default
|
||||
# > 0 -- timeout mode; operations time out after timeout seconds
|
||||
# < 0 -- illegal; raises an exception
|
||||
# to retain the default must use
|
||||
# "timeout=jenkins_jobs.builder._DEFAULT_TIMEOUT" or not set timeout at
|
||||
# all.
|
||||
timeout = jenkins_jobs.builder._DEFAULT_TIMEOUT
|
||||
try:
|
||||
timeout = config.getfloat('jenkins', 'timeout')
|
||||
except (ValueError):
|
||||
raise JenkinsJobsException("Jenkins timeout config is invalid")
|
||||
except (TypeError, configparser.NoOptionError):
|
||||
pass
|
||||
|
||||
plugins_info = None
|
||||
|
||||
if getattr(options, 'plugins_info_path', None) is not None:
|
||||
|
@ -269,6 +284,7 @@ def execute(options, config):
|
|||
user,
|
||||
password,
|
||||
config,
|
||||
jenkins_timeout=timeout,
|
||||
ignore_cache=ignore_cache,
|
||||
flush_cache=options.flush_cache,
|
||||
plugins_list=plugins_info)
|
||||
|
|
|
@ -99,3 +99,40 @@ class UpdateTests(CmdTestsBase):
|
|||
"Called with: %s" % (2, delete_job_mock.call_count,
|
||||
delete_job_mock.mock_calls))
|
||||
delete_job_mock.assert_has_calls(calls, any_order=True)
|
||||
|
||||
@mock.patch('jenkins_jobs.builder.jenkins.Jenkins')
|
||||
def test_update_timeout_not_set(self, jenkins_mock):
|
||||
"""Check that timeout is left unset
|
||||
|
||||
Test that the Jenkins object has the timeout set on it only when
|
||||
provided via the config option.
|
||||
"""
|
||||
|
||||
path = os.path.join(self.fixtures_path, 'cmd-002.yaml')
|
||||
args = self.parser.parse_args(['update', path])
|
||||
|
||||
with mock.patch('jenkins_jobs.cmd.Builder.update_job') as update_mock:
|
||||
update_mock.return_value = ([], 0)
|
||||
cmd.execute(args, self.config)
|
||||
# unless the timeout is set, should only call with 3 arguments
|
||||
# (url, user, password)
|
||||
self.assertEquals(len(jenkins_mock.call_args[0]), 3)
|
||||
|
||||
@mock.patch('jenkins_jobs.builder.jenkins.Jenkins')
|
||||
def test_update_timeout_set(self, jenkins_mock):
|
||||
"""Check that timeout is set correctly
|
||||
|
||||
Test that the Jenkins object has the timeout set on it only when
|
||||
provided via the config option.
|
||||
"""
|
||||
|
||||
path = os.path.join(self.fixtures_path, 'cmd-002.yaml')
|
||||
args = self.parser.parse_args(['update', path])
|
||||
self.config.set('jenkins', 'timeout', '0.2')
|
||||
|
||||
with mock.patch('jenkins_jobs.cmd.Builder.update_job') as update_mock:
|
||||
update_mock.return_value = ([], 0)
|
||||
cmd.execute(args, self.config)
|
||||
# when timeout is set, the fourth argument to the Jenkins api init
|
||||
# should be the value specified from the config
|
||||
self.assertEquals(jenkins_mock.call_args[0][3], 0.2)
|
||||
|
|
Loading…
Reference in New Issue