示例#1
0
def main():

    # NOTE: trailing slash is significant
    jenkins_url = os.environ["HUDSON_URL"]
    api = Jenkins(
        jenkins_url,
        os.environ["HUDSON_USER"],
        os.environ["HUDSON_PASSWORD"]
    )

    view_of_jobs_to_rename = sys.argv[1]
    from_regex = sys.argv[2]
    to_substr = sys.argv[3]

    count = 0

    for job_name in api.get_view( view_of_jobs_to_rename ).get_job_dict():
        resp = None
        if not re.search(from_regex, job_name):
            print("Job name not matched", job_name)
            continue
        new_job_name = re.sub(from_regex, to_substr, job_name )
        try:
            resp = rename(
                jenkins_url,
                os.environ["HUDSON_USER"],
                os.environ["HUDSON_PASSWORD"],
                job_name,
                new_job_name
            )
        except urllib2.HTTPError as e:
            print( "FAILURE %s -> %s" % ( job_name, new_job_name) )
            print(e)
            sys.exit(1)

        count = count +1
        print count

    api = api._clone()
示例#2
0
class TestJenkins(unittest.TestCase):

    DATA = {}

    @mock.patch.object(Jenkins, '_poll')
    def setUp(self, _poll):
        _poll.return_value = self.DATA
        self.J = Jenkins('http://*****:*****@mock.patch.object(Jenkins, '_poll')
    def test_clone(self, _poll):
        _poll.return_value = self.DATA
        JJ = self.J._clone()
        self.assertNotEquals(id(JJ), id(self.J))
        self.assertEquals(JJ, self.J)

    def test_stored_passwords(self):
        self.assertEquals(self.J.requester.password, 'foopassword')
        self.assertEquals(self.J.requester.username, 'foouser')

    @mock.patch.object(Jenkins, '_poll')
    def test_reload(self, _poll):
        mock_requester = Requester(username='******', password='******')
        mock_requester.get_url = mock.MagicMock(return_value='')
        J = Jenkins('http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_jobs(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {
                    'name': 'job_one',
                    'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_lazy_loading(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {
                    'name': 'job_one',
                    'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_jobs_info(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {
                    'name': 'job_one',
                    'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_jobs_list(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {
                    'name': 'job_one',
                    'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_job(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {
                    'name': 'job_one',
                    'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_job_that_does_not_exist(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {
                    'name': 'job_one',
                    'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_has_job(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {
                    'name': 'job_one',
                    'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_has_no_job(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {
                    'name': 'job_one',
                    'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_create_dup_job(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {
                    'name': 'job_one',
                    'url': 'http://*****:*****@mock.patch.object(Jenkins, '_poll', side_effect=second_call_poll)
    @mock.patch.object(Job, '_poll', side_effect=job_second_call_poll)
    def test_create_new_job(self, _poll, _job_poll):
        _job_poll.return_value = {}

        mock_requester = Requester(username='******', password='******')
        mock_requester.post_xml_and_confirm_status = mock.MagicMock(
            return_value='')

        J = Jenkins('http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_create_new_job_fail(self, _base_poll, _poll, _job_poll):
        _job_poll.return_value = {}
        _poll.return_value = {
            'jobs': [
                {
                    'name': 'job_one',
                    'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_jenkins_obj_from_url(self, _base_poll, _poll, _job_poll):
        _job_poll.return_value = {}
        _poll.return_value = {
            'jobs': [
                {
                    'name': 'job_one',
                    'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_jenkins_obj(self, _base_poll, _poll, _job_poll):
        _job_poll.return_value = {}
        _poll.return_value = {
            'jobs': [
                {
                    'name': 'job_one',
                    'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    def test_get_version(self, _base_poll, _poll):
        class MockResponse(object):
            def __init__(self):
                self.headers = {}
                self.headers['X-Jenkins'] = '1.542'

        mock_requester = Requester(username='******', password='******')
        mock_requester.get_and_confirm_status = mock.MagicMock(
            return_value=MockResponse())
        J = Jenkins('http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    def test_get_version_nonexistent(self, _base_poll, _poll):
        class MockResponse(object):
            def __init__(self):
                self.headers = {}

        base_url = 'http://*****:*****@mock.patch.object(JenkinsBase, 'get_data')
    def test_get_master_data(self, _base_poll):
        base_url = 'http://localhost:808'
        _base_poll.return_value = {"busyExecutors": 59, "totalExecutors": 75}
        j = Jenkins(base_url, username='******', password='******')
        data = j.get_master_data()
        self.assertEquals(data['busyExecutors'], 59)
        self.assertEquals(data['totalExecutors'], 75)
示例#3
0
class TestJenkins(unittest.TestCase):

    DATA = {}

    @mock.patch.object(Jenkins, '_poll')
    def setUp(self, _poll):
        _poll.return_value = self.DATA
        self.J = Jenkins('http://*****:*****@mock.patch.object(Jenkins, '_poll')
    def test_clone(self, _poll):
        _poll.return_value = self.DATA
        JJ = self.J._clone()
        self.assertNotEquals(id(JJ), id(self.J))
        self.assertEquals(JJ, self.J)

    def test_stored_passwords(self):
        self.assertEquals(self.J.requester.password, 'foopassword')
        self.assertEquals(self.J.requester.username, 'foouser')

    @mock.patch.object(Jenkins, '_poll')
    def test_reload(self, _poll):
        mock_requester = Requester(username='******', password='******')
        mock_requester.get_url = mock.MagicMock(return_value='')
        J = Jenkins('http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_jobs(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one',
                 'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_lazy_loading(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one',
                 'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_jobs_info(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one',
                 'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_jobs_list(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one',
                 'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_job(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one',
                 'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_job_that_does_not_exist(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one',
                 'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_has_job(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one',
                 'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_has_no_job(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one',
                 'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_create_dup_job(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one',
                 'url': 'http://*****:*****@mock.patch.object(Jenkins, '_poll', side_effect=second_call_poll)
    @mock.patch.object(Job, '_poll', side_effect=job_second_call_poll)
    def test_create_new_job(self, _poll, _job_poll):
        _job_poll.return_value = {}

        mock_requester = Requester(username='******', password='******')
        mock_requester.post_xml_and_confirm_status = mock.MagicMock(
            return_value='')

        J = Jenkins('http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_create_new_job_fail(self, _base_poll, _poll, _job_poll):
        _job_poll.return_value = {}
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one',
                 'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_jenkins_obj_from_url(self, _base_poll, _poll, _job_poll):
        _job_poll.return_value = {}
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one',
                 'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_jenkins_obj(self, _base_poll, _poll, _job_poll):
        _job_poll.return_value = {}
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one',
                 'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    def test_get_version(self, _base_poll, _poll):
        class MockResponse(object):

            def __init__(self):
                self.headers = {}
                self.headers['X-Jenkins'] = '1.542'
        mock_requester = Requester(username='******', password='******')
        mock_requester.get_and_confirm_status = mock.MagicMock(
            return_value=MockResponse())
        J = Jenkins('http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    def test_get_version_nonexistent(self, _base_poll, _poll):
        class MockResponse(object):

            def __init__(self):
                self.headers = {}
        base_url = 'http://*****:*****@mock.patch.object(JenkinsBase, 'get_data')
    def test_get_master_data(self, _base_poll):
        base_url = 'http://localhost:808'
        _base_poll.return_value = {
            "busyExecutors": 59,
            "totalExecutors": 75
        }
        j = Jenkins(base_url,
                    username='******', password='******')
        data = j.get_master_data()
        self.assertEquals(data['busyExecutors'], 59)
        self.assertEquals(data['totalExecutors'], 75)
示例#4
0
class TestJenkins(unittest.TestCase):

    DATA = {}

    @mock.patch.object(Jenkins, '_poll')
    def setUp(self, _poll):
        _poll.return_value = self.DATA
        self.J = Jenkins('http://*****:*****@mock.patch.object(Jenkins, '_poll')
    def test_clone(self, _poll):
        _poll.return_value = self.DATA
        JJ = self.J._clone()
        self.assertNotEquals(id(JJ), id(self.J))
        self.assertEquals(JJ, self.J)

    def test_stored_passwords(self):
        self.assertEquals(self.J.requester.password, 'foopassword')
        self.assertEquals(self.J.requester.username, 'foouser')

    @mock.patch.object(Jenkins, '_poll')
    def test_reload(self, _poll):
        mock_requester = Requester(username='******', password='******')
        mock_requester.get_url = mock.MagicMock(return_value='')
        J = Jenkins('http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_jobs(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one', 'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_jobs_info(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one', 'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_jobs_list(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one', 'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_job(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one', 'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_job_that_does_not_exist(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one', 'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_has_job(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one', 'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_has_no_job(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one', 'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_create_dup_job(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one', 'url': 'http://*****:*****@mock.patch.object(Jenkins, '_poll', side_effect=second_call_poll)
    @mock.patch.object(Job, '_poll')
    def test_create_new_job(self, _poll, _job_poll):
        _job_poll.return_value = {}

        mock_requester = Requester(username='******', password='******')
        mock_requester.post_xml_and_confirm_status = mock.MagicMock(return_value='')

        J = Jenkins('http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_create_new_job_fail(self, _base_poll, _poll, _job_poll):
        _job_poll.return_value = {}
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one', 'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_jenkins_obj_from_url(self, _base_poll, _poll, _job_poll):
        _job_poll.return_value = {}
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one', 'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_jenkins_obj(self, _base_poll, _poll, _job_poll):
        _job_poll.return_value = {}
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one', 'url': 'http://*****:*****@mock.patch.object(Jenkins, '_poll')
    def test_poll_cache(self, _poll):
        # only gets called once in interval
        J = Jenkins('http://localhost:8080/', username='******',
                    password='******', poll_cache_timeout=1)
        for i in range(2):
            J.poll()
        self.assertEquals(_poll.call_count, 1)
        J.poll(True)  # test force poll
        self.assertEquals(_poll.call_count, 2)

        # ensure it gets called again after cache timeout
        _poll.reset_mock()
        time.sleep(1.1)
        J.poll()
        self.assertTrue(_poll.called)

        # ensure it is disabled by default
        _poll.reset_mock()
        for i in range(2):
            self.J.poll()
        self.assertEquals(_poll.call_count, 2)
        self.assertIsNone(self.J.poll_cache_expires)
示例#5
0
class TestJenkins(unittest.TestCase):

    DATA = {}

    @mock.patch.object(Jenkins, '_poll')
    def setUp(self, _poll):
        _poll.return_value = self.DATA
        self.J = Jenkins('http://*****:*****@mock.patch.object(Jenkins, '_poll')
    def test_clone(self, _poll):
        _poll.return_value = self.DATA
        JJ = self.J._clone()
        self.assertNotEquals(id(JJ), id(self.J))
        self.assertEquals(JJ, self.J)

    def test_stored_passwords(self):
        self.assertEquals(self.J.requester.password, 'foopassword')
        self.assertEquals(self.J.requester.username, 'foouser')

    @mock.patch.object(Jenkins, '_poll')
    def test_reload(self, _poll):
        mock_requester = Requester(username='******', password='******')
        mock_requester.get_url = mock.MagicMock(return_value='')
        J = Jenkins('http://*****:*****@mock.patch.object(Jenkins, '_poll')
# def test_unauthorised_reload(self, _poll):
#     def fail_get_url(url):
#         raise urllib2.HTTPError(url=url, code=403,
#             msg='You are not authorized to reload this server',
#             hdrs=None, fp=None)

#     _poll.return_value = {}
#     mock_requester = Requester(username='******', password='******')
#     mock_requester.get_url = mock.MagicMock(return_value='',
#             side_effect=fail_get_url)
#     J = Jenkins('http://*****:*****@mock.patch.object(Jenkins, '_poll')
# def test_httperror_reload(self, _poll):
#     def fail_get_url(url):
#         raise urllib2.HTTPError(url=url, code=500,
#                 msg='Because I said so!', hdrs=None, fp=None)

#     _poll.return_value = {}
#     mock_requester = Requester(username='******', password='******')
#     mock_requester.get_url = mock.MagicMock(return_value='',
#             side_effect=fail_get_url)
#     J = Jenkins('http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_jobs(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {
                    'name': 'job_one',
                    'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_jobs_info(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {
                    'name': 'job_one',
                    'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_jobs_list(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {
                    'name': 'job_one',
                    'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_job(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {
                    'name': 'job_one',
                    'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_has_job(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {
                    'name': 'job_one',
                    'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_has_no_job(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {
                    'name': 'job_one',
                    'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_create_dup_job(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {
                    'name': 'job_one',
                    'url': 'http://*****:*****@mock.patch.object(Jenkins, '_poll', side_effect=second_call_poll)
    @mock.patch.object(Job, '_poll')
    def test_create_new_job(self, _poll, _job_poll):
        _job_poll.return_value = {}

        mock_requester = Requester(username='******', password='******')
        mock_requester.post_xml_and_confirm_status = mock.MagicMock(
            return_value='')

        J = Jenkins('http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_create_new_job_fail(self, _base_poll, _poll, _job_poll):
        _job_poll.return_value = {}
        _poll.return_value = {
            'jobs': [
                {
                    'name': 'job_one',
                    'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_jenkins_obj_from_url(self, _base_poll, _poll, _job_poll):
        _job_poll.return_value = {}
        _poll.return_value = {
            'jobs': [
                {
                    'name': 'job_one',
                    'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_jenkins_obj(self, _base_poll, _poll, _job_poll):
        _job_poll.return_value = {}
        _poll.return_value = {
            'jobs': [
                {
                    'name': 'job_one',
                    'url': 'http://localhost:8080/job_one'
                },
                {
                    'name': 'job_one',
                    'url': 'http://localhost:8080/job_one'
                },
            ]
        }
        _base_poll.return_value = _poll.return_value

        mock_requester = Requester(username='******', password='******')
        mock_requester.post_xml_and_confirm_status = mock.MagicMock(
            return_value='')

        J = Jenkins('http://localhost:8080/',
                    username='******',
                    password='******',
                    requester=mock_requester)

        new_jenkins = J.get_jenkins_obj()
        self.assertEquals(new_jenkins, J)
示例#6
0
class TestJenkins(unittest.TestCase):

    DATA = {}

    @mock.patch.object(Jenkins, '_poll')
    def setUp(self, _poll):
        _poll.return_value = self.DATA
        self.J = Jenkins('http://*****:*****@mock.patch.object(Jenkins, '_poll')
    def test_clone(self, _poll):
        _poll.return_value = self.DATA
        JJ = self.J._clone()
        self.assertNotEquals(id(JJ), id(self.J))
        self.assertEquals(JJ, self.J)

    def test_stored_passwords(self):
        self.assertEquals(self.J.requester.password, 'foopassword')
        self.assertEquals(self.J.requester.username, 'foouser')

    @mock.patch.object(Jenkins, '_poll')
    def test_reload(self, _poll):
        mock_requester = Requester(username='******', password='******')
        mock_requester.get_url = mock.MagicMock(return_value='')
        J = Jenkins('http://*****:*****@mock.patch.object(Jenkins, '_poll')
    # def test_unauthorised_reload(self, _poll):
    #     def fail_get_url(url):
    #         raise urllib2.HTTPError(url=url, code=403,
    #             msg='You are not authorized to reload this server',
    #             hdrs=None, fp=None)

    #     _poll.return_value = {}
    #     mock_requester = Requester(username='******', password='******')
    #     mock_requester.get_url = mock.MagicMock(return_value='',
    #             side_effect=fail_get_url)
    #     J = Jenkins('http://*****:*****@mock.patch.object(Jenkins, '_poll')
    # def test_httperror_reload(self, _poll):
    #     def fail_get_url(url):
    #         raise urllib2.HTTPError(url=url, code=500,
    #                 msg='Because I said so!', hdrs=None, fp=None)

    #     _poll.return_value = {}
    #     mock_requester = Requester(username='******', password='******')
    #     mock_requester.get_url = mock.MagicMock(return_value='',
    #             side_effect=fail_get_url)
    #     J = Jenkins('http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_jobs(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {'jobs': [
                        {'name': 'job_one',
                         'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_jobs_info(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {'jobs': [
                        {'name': 'job_one',
                         'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_jobs_list(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {'jobs': [
                        {'name': 'job_one',
                         'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_get_job(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {'jobs': [
                        {'name': 'job_one',
                         'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_has_job(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {'jobs': [
                        {'name': 'job_one',
                         'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_has_no_job(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {'jobs': [
                        {'name': 'job_one',
                         'url': 'http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_create_dup_job(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {'jobs': [
                        {'name': 'job_one',
                         'url': 'http://*****:*****@mock.patch.object(Jenkins, '_poll', side_effect=second_call_poll)
    @mock.patch.object(Job, '_poll')
    def test_create_new_job(self, _poll, _job_poll):
        _job_poll.return_value = {}

        mock_requester = Requester(username='******', password='******')
        mock_requester.post_xml_and_confirm_status = mock.MagicMock(
                return_value='')

        J = Jenkins('http://*****:*****@mock.patch.object(JenkinsBase, '_poll')
    @mock.patch.object(Jenkins, '_poll')
    @mock.patch.object(Job, '_poll')
    def test_create_new_job_fail(self, _base_poll, _poll, _job_poll):
        _job_poll.return_value = {}
        _poll.return_value = {'jobs': [
                        {'name': 'job_one',
                         'url': 'http://localhost:8080/job_one'},
                        {'name': 'job_one',
                         'url': 'http://localhost:8080/job_one'},
                        ]}
        _base_poll.return_value = _poll.return_value

        mock_requester = Requester(username='******', password='******')
        mock_requester.post_xml_and_confirm_status = mock.MagicMock(
                return_value='')

        J = Jenkins('http://localhost:8080/',
                    username='******', password='******',
                    requester=mock_requester)

        with self.assertRaises(JenkinsAPIException) as ar:
            J.create_job('job_new', None)

        self.assertEquals(ar.exception.message, 'Cannot create job job_new')