From 6c5cb9680f1d038503f41f99f591078a65d48e9b Mon Sep 17 00:00:00 2001 From: salimfadhley Date: Fri, 14 Jun 2013 23:30:59 +0100 Subject: [PATCH] refactor of jenkin launcher --- jenkinsapi_tests/systests/__init__.py | 64 +++++------------------------------ jenkinsapi_utils/jenkins_launcher.py | 39 ++++++++++++++++----- travis.yaml | 7 ++++ 3 files changed, 46 insertions(+), 64 deletions(-) create mode 100644 travis.yaml diff --git a/jenkinsapi_tests/systests/__init__.py b/jenkinsapi_tests/systests/__init__.py index 52e4c9f..8353531 100644 --- a/jenkinsapi_tests/systests/__init__.py +++ b/jenkinsapi_tests/systests/__init__.py @@ -1,59 +1,5 @@ import os -import time -import shutil -import logging -import tempfile -import subprocess - -log = logging.getLogger(__name__) - -class Timeout(RuntimeError): - pass - -class FailedToLaunch(RuntimeError): - pass - -class JenkinsLauncher(object): - - def __init__(self, timeout=10, update_war=False, launch=False): - self.timeout = timeout - self.directory = os.path.dirname(__file__) - if update_war: - self.update_war() - if launch: - self.launch() - - def update_war(self): - os.chdir(self.directory) - subprocess.check_call('./get-jenkins-war.sh') - - def launch(self): - ''' - Launches jenkins and waits while it's ready. - ''' - log.info("Atempting to launch Jenkins...") - self.jenkins_home = tempfile.mkdtemp(prefix='jenkins-home-') - os.environ['JENKINS_HOME'] = self.jenkins_home - jenkins_command = 'java -jar jenkins.war' - self.jenkins_process = subprocess.Popen( - jenkins_command.split(), stdin=subprocess.PIPE, - stdout=subprocess.PIPE, stderr=subprocess.PIPE) - start_time = time.time() - while time.time() - start_time < self.timeout: - line = self.jenkins_process.stderr.readline().strip() - log.info('Jenkins: %s' % line) - - if 'Winstone shutdown successfully' in line: - raise FailedToLaunch() - - if line == 'INFO: Jenkins is fully up and running': - return - raise Timeout('Timeout error occured while waiting for Jenkins start.') - - def stop(self): - shutil.rmtree(self.jenkins_home) - self.jenkins_process.terminate() - self.jenkins_process.wait() +from jenkinsapi_utils.jenkins_launcher import JenkinsLancher launcher = None @@ -61,7 +7,13 @@ launcher = None def setUpPackage(): global launcher - launcher = JenkinsLauncher(update_war=True, launch=True) + import ipdb + ipdb.set_trace() + + systests_dir, _ = os.path.split(__file__) + war_path = os.path.join(systests_dir, 'jenkins.war' ) + launcher = JenkinsLancher(war_path) + launcher.start() def tearDownPackage(): diff --git a/jenkinsapi_utils/jenkins_launcher.py b/jenkinsapi_utils/jenkins_launcher.py index be917e3..21eb231 100644 --- a/jenkinsapi_utils/jenkins_launcher.py +++ b/jenkinsapi_utils/jenkins_launcher.py @@ -1,11 +1,13 @@ import os +import io import time +import Queue import shutil import logging -import subprocess +import requests import tempfile -import Queue import threading +import subprocess log = logging.getLogger(__name__) @@ -15,6 +17,8 @@ class TimeOut(Exception): pass class StreamThread(threading.Thread): + + def __init__(self, name, q, stream, fn_log): threading.Thread.__init__(self) self.name = name @@ -22,6 +26,8 @@ class StreamThread(threading.Thread): self.stream = stream self.fn_log = fn_log + + def run(self): log.info("Starting %s", self.name) @@ -38,6 +44,8 @@ class JenkinsLancher(object): """ Launch jenkins """ + JENKINS_WAR_URL="http://mirrors.jenkins-ci.org/war/latest/jenkins.war" + def __init__(self, war_path): self.war_path = war_path self.war_directory, self.war_filename = os.path.split(self.war_path) @@ -45,6 +53,17 @@ class JenkinsLancher(object): self.jenkins_process = None self.q = Queue.Queue() + def update_war(self): + if not os.path.exists(self.war_path): + with io.open(self.war_path, 'wb') as war_file: + log.info("Downloading the Jenkins WAR file") + war_response = requests.get(self.JENKINS_WAR_URL) + war_file.write(war_response.read()) + log.info('Done!') + + os.chdir(self.war_directory) + subprocess.check_call('./get-jenkins-war.sh') + def stop(self): log.info("Shutting down jenkins.") self.jenkins_process.terminate() @@ -52,15 +71,17 @@ class JenkinsLancher(object): shutil.rmtree(self.jenkins_home) def start(self, timeout=30): + self.update_war() + os.environ['JENKINS_HOME'] = self.jenkins_home os.chdir(self.war_directory) - jenkins_command = 'java -jar %s' % self.war_filename + jenkins_command = ['java', '-jar', self.war_filename] log.info("About to start Jenkins...") - log.info("%s> %s", os.getcwd(), jenkins_command) + log.info("%s> %s", os.getcwd(), " ".join(jenkins_command)) self.jenkins_process = subprocess.Popen( - jenkins_command.split(), stdin=subprocess.PIPE, + jenkins_command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) threads = [ @@ -75,6 +96,10 @@ class JenkinsLancher(object): while True: try: streamName, line = self.q.get(block=True, timeout=timeout) + except Queue.Empty: + log.warn("Input ended unexpectedly") + break + else: if line: if 'Failed to initialize Jenkins' in line: raise FailedToStart(line) @@ -85,9 +110,7 @@ class JenkinsLancher(object): else: log.warn('Stream %s has terminated', streamName) - except Queue.Empty: - print "unexpected end!" - break + if __name__ == '__main__': logging.basicConfig() diff --git a/travis.yaml b/travis.yaml new file mode 100644 index 0000000..81f9959 --- /dev/null +++ b/travis.yaml @@ -0,0 +1,7 @@ +language: python +python: + - "2.7" +# command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors +install: python setup.py develop +# command to run tests, e.g. python setup.py test +script: python setup.py test -- 2.7.4