Permalink
Browse files

Merge pull request #13 from rdodev/master

Add Ability to ignore SSL Certificate Errors (redux)
  • Loading branch information...
2 parents e7f268b + bf220ad commit d64eee37b28ab0c46794192464f9ee45809f76b2 @txels committed Nov 2, 2012
Showing with 28 additions and 16 deletions.
  1. +15 −9 autojenkins/jobs.py
  2. +10 −7 autojenkins/tests/test_unit_jobs.py
  3. +3 −0 docs/intro.rst
View
@@ -50,9 +50,10 @@ def _validate(response):
class Jenkins(object):
"""Main class to interact with a Jenkins server."""
- def __init__(self, base_url, auth=None):
+ def __init__(self, base_url, auth=None, verify_ssl_cert=True):
self.ROOT = base_url
self.auth = auth
+ self.verify_ssl_cert = verify_ssl_cert
def _build_url(self, command, *args):
"""
@@ -68,12 +69,12 @@ def _other_url(self, root, command, *args):
def _get(self, url_pattern, *args):
response = requests.get(self._build_url(url_pattern, *args),
- auth=self.auth)
+ auth=self.auth, verify=self.verify_ssl_cert)
return _validate(response)
def _post(self, url_pattern, *args):
response = requests.post(self._build_url(url_pattern, *args),
- auth=self.auth)
+ auth=self.auth, verify=self.verify_ssl_cert)
return _validate(response)
def all_jobs(self):
@@ -131,7 +132,7 @@ def last_result(self, jobname):
Obtain results from last execution.
"""
last_result_url = self.job_info(jobname)['lastBuild']['url']
- response = requests.get(last_result_url + API, auth=self.auth)
+ response = requests.get(last_result_url + API, auth=self.auth, verify=self.verify_ssl_cert)
return eval(response.content)
def last_success(self, jobname):
@@ -155,7 +156,8 @@ def set_config_xml(self, jobname, config):
return requests.post(self._build_url(CONFIG, jobname),
data=config,
headers={'Content-Type': 'application/xml'},
- auth=self.auth)
+ auth=self.auth,
+ verify=self.verify_ssl_cert)
def create(self, jobname, config_file, **context):
"""
@@ -172,7 +174,8 @@ def create(self, jobname, config_file, **context):
data=content,
params=params,
headers={'Content-Type': 'application/xml'},
- auth=self.auth)
+ auth=self.auth,
+ verify=self.verify_ssl_cert)
def create_copy(self, jobname, template_job, enable=True, **context):
"""
@@ -194,7 +197,8 @@ def create_copy(self, jobname, template_job, enable=True, **context):
data=config,
params={'name': jobname},
headers={'Content-Type': 'application/xml'},
- auth=self.auth)
+ auth=self.auth,
+ verify=self.verify_ssl_cert)
def transfer(self, jobname, to_server):
"""
@@ -205,15 +209,17 @@ def transfer(self, jobname, to_server):
data=config,
params={'name': jobname},
headers={'Content-Type': 'application/xml'},
- auth=self.auth)
+ auth=self.auth,
+ verify=self.verify_ssl_cert)
def copy(self, jobname, copy_from='template'):
"""
Copy a job from another one (by default from one called ``template``).
"""
params = {'name': jobname, 'mode': 'copy', 'from': copy_from}
return requests.post(self._build_url(NEWJOB), params=params,
- auth=self.auth)
+ auth=self.auth,
+ verify=self.verify_ssl_cert)
def build(self, jobname, wait=False, grace=10):
"""
@@ -41,6 +41,7 @@ def test_all_jobs(self, requests):
requests.get.return_value = mock_response(response)
jobs = self.jenkins.all_jobs()
requests.get.assert_called_once_with('http://jenkins/api/python',
+ verify=True,
auth=None)
self.assertEqual(jobs, [('job1', 'blue')])
@@ -58,7 +59,7 @@ def test_last_result(self, requests, *args):
self.assertEqual(23, response['result'])
self.assertEqual(
(('https://builds.apache.org/job/Solr-Trunk/1783/api/python',),
- {'auth': None}),
+ {'auth': None, 'verify': True}),
requests.get.call_args_list[1]
)
@@ -75,6 +76,7 @@ def test_get_methods_with_jobname(self, case, requests):
response = getattr(self.jenkins, method)('name')
requests.get.assert_called_once_with(
'http://jenkins/' + url.format('name'),
+ verify=True,
auth=None)
getattr(self, 'checks_{0}'.format(method))(response)
@@ -84,6 +86,7 @@ def test_build_info(self, requests):
self.jenkins.build_info('name', 3)
requests.get.assert_called_once_with(
'http://jenkins/' + url,
+ verify=True,
auth=None)
def check_result(self, response, route, value):
@@ -142,7 +145,7 @@ def test_create(self, requests):
auth=None,
headers={'Content-Type': 'application/xml'},
params={'name': 'job'},
- data=CFG)
+ data=CFG, verify=True)
def test_create_copy(self, requests):
requests.get.return_value = mock_response('create_copy.txt')
@@ -154,7 +157,7 @@ def test_create_copy(self, requests):
auth=None,
headers={'Content-Type': 'application/xml'},
params={'name': 'job'},
- data=CFG)
+ data=CFG, verify= True)
def test_transfer(self, requests):
requests.get.return_value = mock_response('transfer.txt')
@@ -166,7 +169,7 @@ def test_transfer(self, requests):
auth=None,
headers={'Content-Type': 'application/xml'},
params={'name': 'job'},
- data=CFG)
+ data=CFG, verify=True)
@data(
('build', 'job/{0}/build'),
@@ -182,7 +185,7 @@ def test_post_methods_with_jobname_no_data(self, case, requests):
self.assertEqual(302, response.status_code)
requests.post.assert_called_once_with(
'http://jenkins/' + url.format('name'),
- auth=None)
+ auth=None, verify=True)
def test_set_config_xml(self, requests):
requests.post.return_value = Mock()
@@ -194,7 +197,7 @@ def test_set_config_xml(self, requests):
'http://jenkins/job/name/config.xml',
headers={'Content-Type': 'application/xml'},
data=CFG,
- auth=None)
+ auth=None, verify=True)
@patch('autojenkins.jobs.time')
@patch('autojenkins.jobs.Jenkins.last_result')
@@ -208,7 +211,7 @@ def test_build_with_wait(self, wait_for_build, last_result, time,
self.assertEqual({'result': 'HELLO'}, result)
requests.post.assert_called_once_with(
'http://jenkins/job/name/build',
- auth=None)
+ auth=None, verify=True)
last_result.assert_called_once_with('name')
time.sleep.assert_called_once_with(10)
View
@@ -25,6 +25,9 @@ Sample use:
from autojenkins import Jenkins
j = Jenkins('http://jenkins.pe.local')
+ #You can now add verify_ssl_cert=False argument to the instantiation
+ #so that all requests will ignore SSL certificate errros, should you need that
+ #i.e. j = Jenkins('https://my.secure.jenkins', verify_ssl_cert=False)
# trigger a manual build and check results
j.build('warehouse-screens-us544_login')

0 comments on commit d64eee3

Please sign in to comment.