From ac33e4914f872267cc403c26d5418c8cc454ed56 Mon Sep 17 00:00:00 2001 From: Sateesh Kumar Date: Fri, 21 Mar 2014 22:57:33 +0530 Subject: [PATCH] Adapt fingerprint to catch HTTPError from requests Remove reference to urllib2 exceptions as the HTTP fetches uses 'requests' module --- jenkinsapi/fingerprint.py | 14 ++++-- .../unittests/test_fingerprint.py | 49 +++++++++++++++++++ 2 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 jenkinsapi_tests/unittests/test_fingerprint.py diff --git a/jenkinsapi/fingerprint.py b/jenkinsapi/fingerprint.py index 9a98914..0fa279c 100644 --- a/jenkinsapi/fingerprint.py +++ b/jenkinsapi/fingerprint.py @@ -5,8 +5,8 @@ Module for jenkinsapi Fingerprint from jenkinsapi.jenkinsbase import JenkinsBase from jenkinsapi.custom_exceptions import ArtifactBroken -import urllib2 import re +import requests import logging @@ -45,16 +45,20 @@ class Fingerprint(JenkinsBase): try: self.poll() self.unknown = False - except urllib2.HTTPError as err: + except requests.exceptions.HTTPError as err: # We can't really say anything about the validity of # fingerprints not found -- but the artifact can still # exist, so it is not possible to definitely say they are # valid or not. - if err.code == 404: + # The response object is of type: requests.models.Response + # extract the status code from it + response_obj = err.response + if response_obj.status_code == 404: self.unknown = True return True + else: + return False - return False return True def validate_for_build(self, filename, job, build): @@ -86,7 +90,7 @@ class Fingerprint(JenkinsBase): assert self.valid() except AssertionError: raise ArtifactBroken("Artifact %s seems to be broken, check %s" % (self.id_, self.baseurl)) - except urllib2.HTTPError: + except requests.exceptions.HTTPError: raise ArtifactBroken("Unable to validate artifact id %s using %s" % (self.id_, self.baseurl)) return True diff --git a/jenkinsapi_tests/unittests/test_fingerprint.py b/jenkinsapi_tests/unittests/test_fingerprint.py new file mode 100644 index 0000000..f34e5aa --- /dev/null +++ b/jenkinsapi_tests/unittests/test_fingerprint.py @@ -0,0 +1,49 @@ +import mock +import unittest +import hashlib +import requests + +from jenkinsapi.jenkins import Jenkins +from jenkinsapi.jenkinsbase import JenkinsBase +from jenkinsapi.fingerprint import Fingerprint +from jenkinsapi.utils.requester import Requester + +class TestFingerprint(unittest.TestCase): + + def setUp(self): + self.baseurl = 'http://localhost:8080' + m = hashlib.md5() + m.update("some dummy string") + self.dummy_md5 = m.hexdigest() + + @mock.patch.object(Jenkins, '_poll') + @mock.patch.object(JenkinsBase, '_poll') + def test_object_creation(self, _poll, _basepoll): + J = Jenkins(self.baseurl, username='foouser', password='foopassword') + self.fp_instance = Fingerprint(self.baseurl, self.dummy_md5, J) + self.assertTrue(isinstance(self.fp_instance, Fingerprint)) + self.assertEquals(str(self.fp_instance), self.dummy_md5) + self.assertTrue(self.fp_instance.valid()) + + @mock.patch.object(Jenkins, '_poll') + @mock.patch.object(JenkinsBase, '_poll') + def test_valid_with_requests_HTTPError_404(self,_poll,_basepoll): + resp_obj = requests.models.Response() + resp_obj.status_code = 404 + _poll.side_effect = requests.exceptions.HTTPError(response=resp_obj) + J = Jenkins(self.baseurl, username='foouser', password='foopassword') + fp = Fingerprint(self.baseurl, self.dummy_md5, J) + self.assertTrue(fp.valid()) + + @mock.patch.object(Jenkins, '_poll') + @mock.patch.object(JenkinsBase, '_poll') + def test_valid_with_requests_HTTPError_Not404(self,_poll,_basepoll): + resp_obj = requests.models.Response() + resp_obj.status_code = 401 + _poll.side_effect = requests.exceptions.HTTPError(response=resp_obj) + J = Jenkins(self.baseurl, username='foouser', password='foopassword') + fp = Fingerprint(self.baseurl, self.dummy_md5, J) + self.assertFalse(fp.valid()) + +if __name__ == '__main__': + unittest.main() -- 2.34.1