From b6e9080a8922e1f290c1ae760397dabf2cb0bbff Mon Sep 17 00:00:00 2001 From: Wayne Warren Date: Sun, 27 Dec 2015 15:56:50 -0800 Subject: [PATCH] Finish removing code from jenkins_jobs.cmd * Move behavior of jenkins_jobs.cmd.execute() into jenkins_jobs.cli.entry.JJBConfig.execute() * Delete jenkins_jobs/cmd.py * Fix up unit tests to deal with rearranged code. Change-Id: Ia0d3a062de16c4be10863372c753f4ba8480e620 --- jenkins_jobs/cli/entry.py | 45 +++++++++- jenkins_jobs/cmd.py | 93 --------------------- jenkins_jobs/utils.py | 5 ++ tests/cmd/subcommands/test_delete.py | 4 +- tests/cmd/subcommands/test_update.py | 10 ++- tests/moduleregistry/test_moduleregistry.py | 10 +-- 6 files changed, 61 insertions(+), 106 deletions(-) delete mode 100755 jenkins_jobs/cmd.py diff --git a/jenkins_jobs/cli/entry.py b/jenkins_jobs/cli/entry.py index 7c083c4d5..f28ad9e52 100644 --- a/jenkins_jobs/cli/entry.py +++ b/jenkins_jobs/cli/entry.py @@ -21,8 +21,8 @@ import sys import yaml +from jenkins_jobs.builder import Builder from jenkins_jobs.cli.parser import create_parser -from jenkins_jobs import cmd from jenkins_jobs.config import JJBConfig from jenkins_jobs import utils from jenkins_jobs import version @@ -118,7 +118,48 @@ class JenkinsJobs(object): self.options.path = paths def execute(self): - cmd.execute(self.options, self.jjb_config) + config = self.jjb_config.config_parser + options = self.options + + builder = Builder(config.get('jenkins', 'url'), + self.jjb_config.user, + self.jjb_config.password, + self.jjb_config.config_parser, + jenkins_timeout=self.jjb_config.timeout, + ignore_cache=self.jjb_config.ignore_cache, + flush_cache=options.flush_cache, + plugins_list=self.jjb_config.plugins_info) + + if options.command == 'delete': + for job in options.name: + builder.delete_job(job, options.path) + elif options.command == 'delete-all': + if not utils.confirm( + 'Sure you want to delete *ALL* jobs from Jenkins ' + 'server?\n(including those not managed by Jenkins ' + 'Job Builder)'): + sys.exit('Aborted') + + logger.info("Deleting all jobs") + builder.delete_all_jobs() + elif options.command == 'update': + if options.n_workers < 0: + self.parser.error( + 'Number of workers must be equal or greater than 0') + + logger.info("Updating jobs in {0} ({1})".format( + options.path, options.names)) + jobs, num_updated_jobs = builder.update_jobs( + options.path, options.names, + n_workers=options.n_workers) + logger.info("Number of jobs updated: %d", num_updated_jobs) + if options.delete_old: + num_deleted_jobs = builder.delete_old_managed() + logger.info("Number of jobs deleted: %d", num_deleted_jobs) + elif options.command == 'test': + builder.update_jobs(options.path, options.name, + output=options.output_dir, + n_workers=1) def main(): diff --git a/jenkins_jobs/cmd.py b/jenkins_jobs/cmd.py deleted file mode 100755 index afdbc8ec5..000000000 --- a/jenkins_jobs/cmd.py +++ /dev/null @@ -1,93 +0,0 @@ -#!/usr/bin/env python -# Copyright (C) 2012 OpenStack Foundation -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import logging -import sys - -from six.moves import input - -from jenkins_jobs.builder import Builder -from jenkins_jobs.errors import JenkinsJobsException - - -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger() - -DEFAULT_CONF = """ -[job_builder] -keep_descriptions=False -ignore_cache=False -recursive=False -exclude=.* -allow_duplicates=False -allow_empty_variables=False - -[jenkins] -url=http://localhost:8080/ -query_plugins_info=True - -[hipchat] -authtoken=dummy -send-as=Jenkins - -[__future__] -param_order_from_yaml=False -""" - - -def confirm(question): - answer = input('%s (Y/N): ' % question).upper().strip() - if not answer == 'Y': - sys.exit('Aborted') - - -def execute(options, jjb_config): - config = jjb_config.config_parser - - builder = Builder(config.get('jenkins', 'url'), - jjb_config.user, - jjb_config.password, - jjb_config.config_parser, - jenkins_timeout=jjb_config.timeout, - ignore_cache=jjb_config.ignore_cache, - flush_cache=options.flush_cache, - plugins_list=jjb_config.plugins_info) - - if options.command == 'delete': - for job in options.name: - builder.delete_job(job, options.path) - elif options.command == 'delete-all': - confirm('Sure you want to delete *ALL* jobs from Jenkins server?\n' - '(including those not managed by Jenkins Job Builder)') - logger.info("Deleting all jobs") - builder.delete_all_jobs() - elif options.command == 'update': - if options.n_workers < 0: - raise JenkinsJobsException( - 'Number of workers must be equal or greater than 0') - - logger.info("Updating jobs in {0} ({1})".format( - options.path, options.names)) - jobs, num_updated_jobs = builder.update_jobs( - options.path, options.names, - n_workers=options.n_workers) - logger.info("Number of jobs updated: %d", num_updated_jobs) - if options.delete_old: - num_deleted_jobs = builder.delete_old_managed() - logger.info("Number of jobs deleted: %d", num_deleted_jobs) - elif options.command == 'test': - builder.update_jobs(options.path, options.name, - output=options.output_dir, - n_workers=1) diff --git a/jenkins_jobs/utils.py b/jenkins_jobs/utils.py index 612966634..7b4a08861 100644 --- a/jenkins_jobs/utils.py +++ b/jenkins_jobs/utils.py @@ -62,3 +62,8 @@ def recurse_path(root, excludes=None): pathlist.extend([os.path.join(root, path) for path in dirs]) return pathlist + + +def confirm(question): + answer = input('%s (Y/N): ' % question).upper().strip() + return not answer == 'Y' diff --git a/tests/cmd/subcommands/test_delete.py b/tests/cmd/subcommands/test_delete.py index 45b128a79..084e4804c 100644 --- a/tests/cmd/subcommands/test_delete.py +++ b/tests/cmd/subcommands/test_delete.py @@ -7,7 +7,7 @@ from tests.cmd.test_cmd import CmdTestsBase @mock.patch('jenkins_jobs.builder.Jenkins.get_plugins_info', mock.MagicMock) class DeleteTests(CmdTestsBase): - @mock.patch('jenkins_jobs.cmd.Builder.delete_job') + @mock.patch('jenkins_jobs.cli.entry.Builder.delete_job') def test_delete_single_job(self, delete_job_mock): """ Test handling the deletion of a single Jenkins job. @@ -16,7 +16,7 @@ class DeleteTests(CmdTestsBase): args = ['--conf', self.default_config_file, 'delete', 'test_job'] self.execute_jenkins_jobs_with_args(args) - @mock.patch('jenkins_jobs.cmd.Builder.delete_job') + @mock.patch('jenkins_jobs.cli.entry.Builder.delete_job') def test_delete_multiple_jobs(self, delete_job_mock): """ Test handling the deletion of multiple Jenkins jobs. diff --git a/tests/cmd/subcommands/test_update.py b/tests/cmd/subcommands/test_update.py index 6a7f432a3..9508ccfb5 100644 --- a/tests/cmd/subcommands/test_update.py +++ b/tests/cmd/subcommands/test_update.py @@ -25,7 +25,7 @@ from tests.cmd.test_cmd import CmdTestsBase @mock.patch('jenkins_jobs.builder.Jenkins.get_plugins_info', mock.MagicMock) class UpdateTests(CmdTestsBase): - @mock.patch('jenkins_jobs.cmd.Builder.update_jobs') + @mock.patch('jenkins_jobs.cli.entry.Builder.update_jobs') def test_update_jobs(self, update_jobs_mock): """ Test update_job is called @@ -62,7 +62,7 @@ class UpdateTests(CmdTestsBase): @mock.patch('jenkins_jobs.builder.Jenkins.is_job', return_value=True) @mock.patch('jenkins_jobs.builder.Jenkins.get_jobs') @mock.patch('jenkins_jobs.builder.Builder.delete_job') - @mock.patch('jenkins_jobs.cmd.Builder') + @mock.patch('jenkins_jobs.cli.entry.Builder') def test_update_jobs_and_delete_old(self, builder_mock, delete_job_mock, get_jobs_mock, is_job_mock): """ @@ -132,7 +132,8 @@ class UpdateTests(CmdTestsBase): path = os.path.join(self.fixtures_path, 'cmd-002.yaml') args = ['--conf', self.default_config_file, 'update', path] - with mock.patch('jenkins_jobs.cmd.Builder.update_job') as update_mock: + with mock.patch( + 'jenkins_jobs.cli.entry.Builder.update_job') as update_mock: update_mock.return_value = ([], 0) self.execute_jenkins_jobs_with_args(args) # unless the timeout is set, should only call with 3 arguments @@ -152,7 +153,8 @@ class UpdateTests(CmdTestsBase): 'non-default-timeout.ini') args = ['--conf', config_file, 'update', path] - with mock.patch('jenkins_jobs.cmd.Builder.update_job') as update_mock: + with mock.patch( + 'jenkins_jobs.cli.entry.Builder.update_job') as update_mock: update_mock.return_value = ([], 0) self.execute_jenkins_jobs_with_args(args) # when timeout is set, the fourth argument to the Jenkins api init diff --git a/tests/moduleregistry/test_moduleregistry.py b/tests/moduleregistry/test_moduleregistry.py index 389323d77..8479db9f6 100644 --- a/tests/moduleregistry/test_moduleregistry.py +++ b/tests/moduleregistry/test_moduleregistry.py @@ -1,12 +1,11 @@ import pkg_resources -from six.moves import configparser -from six.moves import StringIO from testscenarios.testcase import TestWithScenarios + import testtools as tt from testtools.content import text_content -from jenkins_jobs import cmd +from jenkins_jobs.config import JJBConfig from jenkins_jobs.registry import ModuleRegistry from tests.base import LoggingFixture @@ -33,8 +32,9 @@ class ModuleRegistryPluginInfoTestsWithScenarios(TestWithScenarios, def setUp(self): super(ModuleRegistryPluginInfoTestsWithScenarios, self).setUp() - config = configparser.ConfigParser() - config.readfp(StringIO(cmd.DEFAULT_CONF)) + jjb_config = JJBConfig() + jjb_config.validate() + config = jjb_config.config_parser plugin_info = [{'shortName': "HerpDerpPlugin", 'longName': "Blah Blah Blah Plugin"