From: Salim Fadhley Date: Sun, 30 Jun 2013 01:03:44 +0000 (+0100) Subject: moved the scm test out of systests into unittests X-Git-Tag: v0.2.23~117^2~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=90f6eebc62e6c246dc7e0acdef47cb4a8c8ea933;p=tools%2Fpython-jenkinsapi.git moved the scm test out of systests into unittests --- diff --git a/jenkinsapi_tests/systests/__init__.py b/jenkinsapi_tests/systests/__init__.py index 12a581d..3d9eb34 100644 --- a/jenkinsapi_tests/systests/__init__.py +++ b/jenkinsapi_tests/systests/__init__.py @@ -3,11 +3,14 @@ from jenkinsapi_utils.jenkins_launcher import JenkinsLancher state={} +# Extra plugins required by the systests +PLUGIN_DEPENDENCIES = ["http://updates.jenkins-ci.org/latest/git.hpi", + "http://updates.jenkins-ci.org/latest/git-client.hpi"] + def setUpPackage(): systests_dir, _ = os.path.split(__file__) war_path = os.path.join(systests_dir, 'jenkins.war' ) - state['launcher'] = JenkinsLancher(war_path) - + state['launcher'] = JenkinsLancher(war_path, PLUGIN_DEPENDENCIES) state['launcher'].start() def tearDownPackage(): diff --git a/jenkinsapi_tests/systests/job_configs.py b/jenkinsapi_tests/systests/job_configs.py index 2dd75db..9d2d547 100644 --- a/jenkinsapi_tests/systests/job_configs.py +++ b/jenkinsapi_tests/systests/job_configs.py @@ -74,8 +74,9 @@ SCM_GIT_JOB = """ + false - + 2 @@ -108,7 +109,7 @@ SCM_GIT_JOB = """ - false + true @@ -116,15 +117,12 @@ SCM_GIT_JOB = """ false false false - - - ping -c 10 localhost - - + + false + - -""" +""".strip() JOB_WITH_ARTIFACTS = """ diff --git a/jenkinsapi_tests/systests/test_scm.py b/jenkinsapi_tests/systests/test_scm.py index 00ea8ef..b12ef1f 100644 --- a/jenkinsapi_tests/systests/test_scm.py +++ b/jenkinsapi_tests/systests/test_scm.py @@ -1,25 +1,24 @@ # ''' -System tests for `jenkinsapi.jenkins` module. -''' -import unittest -from jenkinsapi_tests.systests import state -from jenkinsapi_tests.systests.base import BaseSystemTest -from jenkinsapi_tests.test_utils.random_strings import random_string -from jenkinsapi_tests.systests.job_configs import SCM_GIT_JOB +# System tests for `jenkinsapi.jenkins` module. +# ''' +# import unittest +# from jenkinsapi_tests.systests.base import BaseSystemTest +# from jenkinsapi_tests.test_utils.random_strings import random_string +# from jenkinsapi_tests.systests.job_configs import SCM_GIT_JOB -# Maybe have a base class for all SCM test activites? -class TestSCMGit(BaseSystemTest): - # Maybe it makes sense to move plugin dependencies outside the code. - # Have a config to dependencies mapping from the launcher can use to install plugins. - def test_get_revision(self): - job_name = 'create_%s' % random_string() - job = self.jenkins.create_job(job_name, SCM_GIT_JOB) - ii = job.invoke() - ii.block(until='completed') - self.assertFalse(ii.is_running()) - b = ii.get_build() - self.assertIsInstance(b.get_revision(), basestring) +# # Maybe have a base class for all SCM test activites? +# class TestSCMGit(BaseSystemTest): +# # Maybe it makes sense to move plugin dependencies outside the code. +# # Have a config to dependencies mapping from the launcher can use to install plugins. +# def test_get_revision(self): +# job_name = 'git_%s' % random_string() +# job = self.jenkins.create_job(job_name, SCM_GIT_JOB) +# ii = job.invoke() +# ii.block(until='completed') +# self.assertFalse(ii.is_running()) +# b = ii.get_build() +# self.assertIsInstance(b.get_revision(), basestring) -if __name__ == '__main__': - unittest.main() +# if __name__ == '__main__': +# unittest.main() diff --git a/jenkinsapi_tests/unittests/test_build_scm_git.py b/jenkinsapi_tests/unittests/test_build_scm_git.py new file mode 100644 index 0000000..b7f8db6 --- /dev/null +++ b/jenkinsapi_tests/unittests/test_build_scm_git.py @@ -0,0 +1,74 @@ +import mock +import unittest +from jenkinsapi.build import Build + +class test_build(unittest.TestCase): + + DATA = {'actions': [{'causes': [{'shortDescription': 'Started by an SCM change'}]}, + {}, + {'buildsByBranchName': {'origin/HEAD': {'buildNumber': 2, + 'buildResult': None, + 'revision': {'SHA1': 'd2a5d435fa2df3bff572bd06e43c86544749c5d2', + 'branch': [{'SHA1': 'd2a5d435fa2df3bff572bd06e43c86544749c5d2', + 'name': 'origin/HEAD'}, + {'SHA1': 'd2a5d435fa2df3bff572bd06e43c86544749c5d2', + 'name': 'origin/master'}]}}, + 'origin/master': {'buildNumber': 2, + 'buildResult': None, + 'revision': {'SHA1': 'd2a5d435fa2df3bff572bd06e43c86544749c5d2', + 'branch': [{'SHA1': 'd2a5d435fa2df3bff572bd06e43c86544749c5d2', + 'name': 'origin/HEAD'}, + {'SHA1': 'd2a5d435fa2df3bff572bd06e43c86544749c5d2', + 'name': 'origin/master'}]}}, + 'origin/python_3_compatibility': {'buildNumber': 1, + 'buildResult': None, + 'revision': {'SHA1': 'c9d1c96bc926ff63a5209c51b3ed537e62ea50e6', + 'branch': [{'SHA1': 'c9d1c96bc926ff63a5209c51b3ed537e62ea50e6', + 'name': 'origin/python_3_compatibility'}]}}, + 'origin/unstable': {'buildNumber': 3, + 'buildResult': None, + 'revision': {'SHA1': '7def9ed6e92580f37d00e4980c36c4d36e68f702', + 'branch': [{'SHA1': '7def9ed6e92580f37d00e4980c36c4d36e68f702', + 'name': 'origin/unstable'}]}}}, + 'lastBuiltRevision': {'SHA1': '7def9ed6e92580f37d00e4980c36c4d36e68f702', + 'branch': [{'SHA1': '7def9ed6e92580f37d00e4980c36c4d36e68f702', + 'name': 'origin/unstable'}]}, + 'remoteUrls': ['https://github.com/salimfadhley/jenkinsapi.git'], + 'scmName': ''}, + {}, + {}], + 'artifacts': [], + 'building': False, + 'builtOn': '', + 'changeSet': {'items': [], 'kind': 'git'}, + 'culprits': [], + 'description': None, + 'duration': 1051, + 'estimatedDuration': 2260, + 'executor': None, + 'fullDisplayName': 'git_yssrtigfds #3', + 'id': '2013-06-30_01-54-35', + 'keepLog': False, + 'number': 3, + 'result': 'SUCCESS', + 'timestamp': 1372553675652, + 'url': 'http://localhost:8080/job/git_yssrtigfds/3/'} + + + @mock.patch.object(Build, '_poll') + def setUp(self, _poll): + _poll.return_value = self.DATA + self.j = mock.MagicMock() # Job + self.j.name = 'FooJob' + + self.b = Build('http://', 97, self.j) + + def test_git_scm(self): + """ + Can we extract git build revision data from a build object? + """ + self.assertIsInstance(self.b.get_revision(), basestring) + self.assertEquals(self.b.get_revision(), '7def9ed6e92580f37d00e4980c36c4d36e68f702') + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/jenkinsapi_utils/jenkins_launcher.py b/jenkinsapi_utils/jenkins_launcher.py index fb8f9fe..2b1c243 100644 --- a/jenkinsapi_utils/jenkins_launcher.py +++ b/jenkinsapi_utils/jenkins_launcher.py @@ -53,12 +53,13 @@ class JenkinsLancher(object): """ JENKINS_WAR_URL = "http://mirrors.jenkins-ci.org/war/latest/jenkins.war" - def __init__(self, war_path): + def __init__(self, war_path, plugin_urls=None): self.war_path = war_path self.war_directory, self.war_filename = os.path.split(self.war_path) self.jenkins_home = tempfile.mkdtemp(prefix='jenkins-home-') self.jenkins_process = None self.q = Queue.Queue() + self.plugin_urls = plugin_urls or [] def update_war(self): os.chdir(self.war_directory) @@ -74,24 +75,23 @@ class JenkinsLancher(object): config_source = pkg_resources.resource_string('jenkinsapi_tests.systests', 'config.xml') config_dest_file.write(config_source.encode('UTF-8')) - def install_plugin(self, hpi_url): + def install_plugins(self): + for i, url in enumerate(self.plugin_urls): + self.install_plugin(url, i) + + def install_plugin(self, hpi_url, i): plugin_dir = os.path.join(self.jenkins_home, 'plugins') if not os.path.exists(plugin_dir): os.mkdir(plugin_dir) - hpi_url = [hpi_url] if isinstance(hpi_url, basestring) else hpi_url - for i,url in enumerate(hpi_url): - log.info("Downloading %s", url) - log.info("Plugins will be installed in '%s'" % plugin_dir) - # FIXME: This is kinda ugly but works - filename = "plugin_%s.hpi" % i - plugin_path = os.path.join(plugin_dir, filename) - with open(plugin_path, 'wb') as h: - request = requests.get(url) - h.write(request.content) - log.info("Restarting Jenkins after installing the plugins") - self.jenkins_process.terminate() - self.jenkins_process.wait() - self.start() + + log.info("Downloading %s", hpi_url) + log.info("Plugins will be installed in '%s'" % plugin_dir) + # FIXME: This is kinda ugly but works + filename = "plugin_%s.hpi" % i + plugin_path = os.path.join(plugin_dir, filename) + with open(plugin_path, 'wb') as h: + request = requests.get(hpi_url) + h.write(request.content) def stop(self): log.info("Shutting down jenkins.") @@ -116,6 +116,7 @@ class JenkinsLancher(object): def start(self, timeout=60): self.update_war() self.update_config() + self.install_plugins() os.environ['JENKINS_HOME'] = self.jenkins_home os.chdir(self.war_directory) @@ -167,7 +168,8 @@ if __name__ == '__main__': log.info("Hello!") jl = JenkinsLancher( - '/home/sal/workspace/jenkinsapi/src/jenkinsapi_tests/systests/jenkins.war') + '/home/sal/workspace/jenkinsapi/src/jenkinsapi_tests/systests/jenkins.war' + ) jl.start() log.info("Jenkins was launched...")