From b41e0c1e54c8c75a505b9d970ed4842aeff551de Mon Sep 17 00:00:00 2001 From: Salim Fadhley Date: Mon, 17 Jun 2013 00:15:21 +0100 Subject: [PATCH] queues have been almost entirely refactored, still need docs & examples --- jenkinsapi/jenkins.py | 4 +- jenkinsapi/queue.py | 2 +- jenkinsapi_tests/systests/test_jenkins.py | 2 +- jenkinsapi_tests/systests/test_queue.py | 59 ++++++++++++++++ jenkinsapi_tests/unittests/test_jenkins.py | 17 +---- jenkinsapi_tests/unittests/test_queue.py | 107 +++++++++++++++++++++++++++++ 6 files changed, 172 insertions(+), 19 deletions(-) create mode 100644 jenkinsapi_tests/systests/test_queue.py create mode 100644 jenkinsapi_tests/unittests/test_queue.py diff --git a/jenkinsapi/jenkins.py b/jenkinsapi/jenkins.py index 745f828..54b2186 100644 --- a/jenkinsapi/jenkins.py +++ b/jenkinsapi/jenkins.py @@ -37,7 +37,7 @@ class Jenkins(JenkinsBase): def _clone(self): return Jenkins(self.baseurl, username=self.username, password=self.password, requester=self.requester) - def get_base_server_url(self): + def base_server_url(self): if config.JENKINS_API in self.baseurl: return self.baseurl[:-(len(config.JENKINS_API))] else: @@ -258,7 +258,7 @@ class Jenkins(JenkinsBase): return url def get_queue_url(self): - url = "%(baseurl)s/queue/" % {'baseurl': self.get_base_server_url()} + url = "%(baseurl)s/queue/" % {'baseurl': self.baseurl} return url def get_queue(self): diff --git a/jenkinsapi/queue.py b/jenkinsapi/queue.py index baca7e5..08c9806 100644 --- a/jenkinsapi/queue.py +++ b/jenkinsapi/queue.py @@ -73,7 +73,7 @@ class QueueItem(object): self.jenkins = jenkins self.__dict__.update(kwargs) - def getJob(self): + def get_job(self): """ Return the job associated with this queue item """ diff --git a/jenkinsapi_tests/systests/test_jenkins.py b/jenkinsapi_tests/systests/test_jenkins.py index f0eb69e..4e936e6 100644 --- a/jenkinsapi_tests/systests/test_jenkins.py +++ b/jenkinsapi_tests/systests/test_jenkins.py @@ -75,4 +75,4 @@ class JobTests(BaseSystemTest): self.assertJobIsPresent(copied_job_name) if __name__ == '__main__': - unittest.main() + unittest.main() \ No newline at end of file diff --git a/jenkinsapi_tests/systests/test_queue.py b/jenkinsapi_tests/systests/test_queue.py new file mode 100644 index 0000000..c44538a --- /dev/null +++ b/jenkinsapi_tests/systests/test_queue.py @@ -0,0 +1,59 @@ +''' +System tests for `jenkinsapi.jenkins` module. +''' +import time +import logging +import unittest +from jenkinsapi.queue import Queue +from jenkinsapi_tests.systests.base import BaseSystemTest +from jenkinsapi_tests.test_utils.random_strings import random_string + +log = logging.getLogger(__name__) + +JOB_XML = """ + + + + + false + + + true + false + false + false + + false + + + ping -c 100 localhost + + + + +""".strip() + + +class TestQueue(BaseSystemTest): + + def test_get_queue(self): + q = self.jenkins.get_queue() + self.assertIsInstance(q, Queue) + + def test_invoke_job_parameterized(self): + job_names = [random_string() for i in range(5)] + jobs = [] + + for job_name in job_names: + j = self.jenkins.create_job(job_name, JOB_XML) + jobs.append(j) + j.invoke() + + queue = self.jenkins.get_queue() + reprString = repr(queue) + self.assertIn(queue.baseurl, reprString) + + +if __name__ == '__main__': + logging.basicConfig() + unittest.main() diff --git a/jenkinsapi_tests/unittests/test_jenkins.py b/jenkinsapi_tests/unittests/test_jenkins.py index dedfcfe..07193f3 100644 --- a/jenkinsapi_tests/unittests/test_jenkins.py +++ b/jenkinsapi_tests/unittests/test_jenkins.py @@ -1,12 +1,9 @@ import mock import unittest -import datetime -import urllib2 -from jenkinsapi.jenkins import Jenkins, JenkinsBase, View, Job +from jenkinsapi.jenkins import Jenkins, JenkinsBase, Job from jenkinsapi.utils.requester import Requester -from jenkinsapi.exceptions import UnknownJob, NotAuthorized, JenkinsAPIException - +from jenkinsapi.exceptions import JenkinsAPIException class TestJenkins(unittest.TestCase): @@ -305,15 +302,5 @@ class TestJenkinsURLs(unittest.TestCase): self.assertEquals( J.get_create_url(), 'http://localhost:8080/createItem') - @mock.patch.object(Jenkins, '_poll') - def test_get_base_server_url(self, _poll): - _poll.return_value = {} - J = Jenkins('http://localhost:8080/', - username='foouser', password='foopassword') - self.assertEquals(J.baseurl, 'http://localhost:8080') - self.assertEquals( - J.get_base_server_url(), 'http://localhost:8080') - - if __name__ == '__main__': unittest.main() diff --git a/jenkinsapi_tests/unittests/test_queue.py b/jenkinsapi_tests/unittests/test_queue.py new file mode 100644 index 0000000..aebd4b9 --- /dev/null +++ b/jenkinsapi_tests/unittests/test_queue.py @@ -0,0 +1,107 @@ +import mock +import unittest + +from collections import defaultdict +from jenkinsapi.jenkins import Jenkins +from jenkinsapi.queue import Queue, QueueItem +from jenkinsapi.jenkinsbase import JenkinsBase +from jenkinsapi.job import Job + +class FourOhFourError(Exception): + """ + Missing fake data + """ + +class TestQueue(unittest.TestCase): + + @classmethod + def mockGetData(self, url): + try: + return TestQueue.URL_DATA[url] + except KeyError: + raise FourOhFourError(url) + + URL_DATA = {} + + URL_DATA['http://localhost:8080/api/python/'] = \ + {'jobs':[ + {'name':'utmebvpxrw', + 'url':'http://localhost/job/utmebvpxrw'} + ] + } + + URL_DATA['http://localhost/job/utmebvpxrw/api/python/'] = \ + {} + + + URL_DATA['http://localhost:8080/queue/api/python/'] = \ + {'items': [{'actions': [{'causes': [{'shortDescription': 'Started by user anonymous', + 'userId': None, + 'userName': 'anonymous'}]}], + 'blocked': False, + 'buildable': True, + 'buildableStartMilliseconds': 1371419916747, + 'id': 42, + 'inQueueSince': 1371419909428, + 'params': '', + 'stuck': False, + 'task': {'color': 'grey', + 'name': 'klscuimkqo', + 'url': 'http://localhost:8080/job/klscuimkqo/'}, + 'why': 'Waiting for next available executor'}, + {'actions': [{'causes': [{'shortDescription': 'Started by user anonymous', + 'userId': None, + 'userName': 'anonymous'}]}], + 'blocked': False, + 'buildable': True, + 'buildableStartMilliseconds': 1371419911747, + 'id': 41, + 'inQueueSince': 1371419906327, + 'params': '', + 'stuck': False, + 'task': {'color': 'grey', + 'name': 'vluyhzzepl', + 'url': 'http://localhost:8080/job/vluyhzzepl/'}, + 'why': 'Waiting for next available executor'}, + {'actions': [{'causes': [{'shortDescription': 'Started by user anonymous', + 'userId': None, + 'userName': 'anonymous'}]}], + 'blocked': False, + 'buildable': True, + 'buildableStartMilliseconds': 1371419911747, + 'id': 40, + 'inQueueSince': 1371419903212, + 'params': '', + 'stuck': False, + 'task': {'color': 'grey', + 'name': 'utmebvpxrw', + 'url': 'http://localhost:8080/job/utmebvpxrw/'}, + 'why': 'Waiting for next available executor'}]} + + + @mock.patch.object(JenkinsBase, 'get_data', mockGetData) + def setUp(self): + self.J = Jenkins('http://localhost:8080') # Jenkins + self.q = Queue('http://localhost:8080/queue', self.J) + + def testRepr(self): + self.assertTrue(repr(self.q)) + + def test_length(self): + self.assertEquals(len(self.q), 3) + + def test_list_items(self): + self.assertEquals(set(self.q.keys()), set([40,41,42])) + + def test_getitem(self): + item40 = self.q[40] + self.assertIsInstance(item40, QueueItem) + + @mock.patch.object(JenkinsBase, 'get_data', mockGetData) + def test_get_job_for_queue_item(self): + item40 = self.q[40] + j = item40.get_job() + self.assertIsInstance(j, Job) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file -- 2.7.4