From f68831bd2e587c0897c4a9b87018659ef5a4ebd4 Mon Sep 17 00:00:00 2001 From: Darragh Bailey Date: Tue, 24 Feb 2015 19:19:13 +0000 Subject: [PATCH] Only query jenkins plugins if config provided Limit querying for plugin information from Jenkins to where an explicit configuration file has been provided. Where no file is provided, should avoid any requests that could result in attempting connections. Add a config options to control this behaviour explicitly so that if a config file is provided, it may explicitly disable querying jenkins for additional information from plugins. The `test` command can use this behaviour to ensure that a basic run is possible without providing any configuration files, and can be explicitly configured to disable querying should a config file be used for testing. Ensure the default sample configuration skips attempting to query so that users may use it as a quick test for a working installation without any network or valid jenkins url dependencies. Change-Id: I641a3188013debf3765aaff109c4f5fa82a4cedb --- doc/source/installation.rst | 6 +++++ etc/jenkins_jobs.ini-sample | 1 + jenkins_jobs/cmd.py | 6 +++++ tests/cmd/fixtures/disable-query-plugins.conf | 3 +++ tests/cmd/subcommands/test_test.py | 27 +++++++++++++++++++ 5 files changed, 43 insertions(+) create mode 100644 tests/cmd/fixtures/disable-query-plugins.conf diff --git a/doc/source/installation.rst b/doc/source/installation.rst index 7dd3d9a10..fd8f8b3e1 100644 --- a/doc/source/installation.rst +++ b/doc/source/installation.rst @@ -114,6 +114,12 @@ jenkins section **url** The base URL for your Jenkins installation. +**query_plugins_info** + Whether to query the Jenkins instance for plugin info when a configuration + file is provided. If a configuration file is not provided `jenkins-jobs` will + ignore this setting and skip querying for plugin information. True by + default. + Running ------- diff --git a/etc/jenkins_jobs.ini-sample b/etc/jenkins_jobs.ini-sample index 58b670680..bb1744742 100644 --- a/etc/jenkins_jobs.ini-sample +++ b/etc/jenkins_jobs.ini-sample @@ -10,6 +10,7 @@ allow_duplicates=False user=jenkins password=1234567890abcdef1234567890abcdef url=https://jenkins.example.com +query_plugins_info=False ##### This is deprecated, use job_builder section instead #ignore_cache=True diff --git a/jenkins_jobs/cmd.py b/jenkins_jobs/cmd.py index 8c1105994..f992add12 100755 --- a/jenkins_jobs/cmd.py +++ b/jenkins_jobs/cmd.py @@ -42,6 +42,7 @@ allow_empty_variables=False url=http://localhost:8080/ user= password= +query_plugins_info=True [hipchat] authtoken=dummy @@ -238,6 +239,11 @@ def execute(options, config): if not isinstance(plugins_info, list): raise JenkinsJobsException("{0} must contain a Yaml list!" .format(options.plugins_info_path)) + elif (not options.conf or not + config.getboolean("jenkins", "query_plugins_info")): + logger.debug("Skipping plugin info retrieval") + plugins_info = {} + if options.allow_empty_variables is not None: config.set('job_builder', 'allow_empty_variables', diff --git a/tests/cmd/fixtures/disable-query-plugins.conf b/tests/cmd/fixtures/disable-query-plugins.conf new file mode 100644 index 000000000..02f5a9391 --- /dev/null +++ b/tests/cmd/fixtures/disable-query-plugins.conf @@ -0,0 +1,3 @@ +[jenkins] +url=http://test-jenkins.with.non.default.url:8080/ +query_plugins_info=False diff --git a/tests/cmd/subcommands/test_test.py b/tests/cmd/subcommands/test_test.py index 037c29539..a1697fe47 100644 --- a/tests/cmd/subcommands/test_test.py +++ b/tests/cmd/subcommands/test_test.py @@ -264,3 +264,30 @@ class TestJenkinsGetPluginInfoError(CmdTestsBase): self.fail("jenkins.JenkinsException propagated to main") except: pass # only care about jenkins.JenkinsException for now + + @mock.patch('jenkins.Jenkins.get_plugins_info') + def test_skip_plugin_retrieval_if_no_config_provided( + self, get_plugins_info_mock): + """ + Verify that retrieval of information from Jenkins instance about its + plugins will be skipped when run if no config file provided. + """ + with mock.patch('sys.stdout'): + cmd.main(['test', os.path.join(self.fixtures_path, + 'cmd-001.yaml')]) + self.assertFalse(get_plugins_info_mock.called) + + @mock.patch('jenkins.Jenkins.get_plugins_info') + def test_skip_plugin_retrieval_if_disabled(self, get_plugins_info_mock): + """ + Verify that retrieval of information from Jenkins instance about its + plugins will be skipped when run if a config file provided and disables + querying through a config option. + """ + with mock.patch('sys.stdout'): + cmd.main(['--conf', + os.path.join(self.fixtures_path, + 'disable-query-plugins.conf'), + 'test', + os.path.join(self.fixtures_path, 'cmd-001.yaml')]) + self.assertFalse(get_plugins_info_mock.called)