def test_non_existing_config_file(self): """ Run test mode and pass a non-existing configuration file """ args = ["--conf", self.default_config_file, "test", "non-existing.yaml"] jenkins_jobs = entry.JenkinsJobs(args) self.assertRaises(IOError, jenkins_jobs.execute)
def test_non_existing_config_dir(self): """ Run test mode and pass a non-existing configuration directory """ args = ['--conf', self.default_config_file, 'test', 'foo'] jenkins_jobs = entry.JenkinsJobs(args) self.assertRaises(IOError, jenkins_jobs.execute)
def test_config_old_plugin_format_warning(self): """ Run test mode and check that old plugin settings result in a warning, while ensuring that missing sections do not trigger the same warning if a default value is provided. """ args = [ '--conf', os.path.join(self.fixtures_path, 'plugin_warning.ini'), 'test', 'foo' ] jenkins_jobs = entry.JenkinsJobs(args) jenkins_jobs.jjb_config.get_plugin_config('old_plugin', 'setting', True) jenkins_jobs.jjb_config.get_plugin_config('old_plugin_no_conf', 'setting', True) jenkins_jobs.jjb_config.get_plugin_config('new_plugin', 'setting') self.assertIn( 'using a [old_plugin] section in your config file is deprecated', self.logger.output) self.assertNotIn( 'using a [old_plugin_no_conf] secton in your config file is ' 'deprecated', self.logger.output) self.assertNotIn( 'using a [new_plugin] section in your config file is deprecated', self.logger.output)
def test_skip_plugin_retrieval_if_no_config_provided( self, get_plugins_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', new_callable=io.BytesIO): args = ['--conf', self.default_config_file, 'test', os.path.join(self.fixtures_path, 'cmd-001.yaml')] entry.JenkinsJobs(args) self.assertFalse(get_plugins_mock.called)
def test_admin_user_differs_from_user(self): """ Run test mode and check config settings from conf file retained when non of the global CLI options are set. """ config_file = os.path.join(self.fixtures_path, 'admin_user.conf') args = ['--conf', config_file, 'test', 'dummy.yaml'] jenkins_jobs = entry.JenkinsJobs(args) jjb_config = jenkins_jobs.jjb_config self.assertEqual(jjb_config.jenkins['user'], "myuser") self.assertEqual(jjb_config.jenkins['password'], "mytoken") self.assertEqual(jjb_config.jenkins['admin_user'], "myadminuser") self.assertEqual(jjb_config.jenkins['admin_password'], "myadmintoken")
def test_skip_plugin_retrieval_if_disabled(self, get_plugins_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', new_callable=io.BytesIO): args = ['--conf', os.path.join(self.fixtures_path, 'disable-query-plugins.conf'), 'test', os.path.join(self.fixtures_path, 'cmd-001.yaml')] entry.JenkinsJobs(args) self.assertFalse(get_plugins_mock.called)
def test_config_options_not_replaced_by_cli_defaults(self): """ Run test mode and check config settings from conf file retained when none of the global CLI options are set. """ config_file = os.path.join(self.fixtures_path, 'settings_from_config.ini') args = ['--conf', config_file, 'test', 'dummy.yaml'] jenkins_jobs = entry.JenkinsJobs(args) jjb_config = jenkins_jobs.jjb_config self.assertEqual(jjb_config.jenkins['user'], "jenkins_user") self.assertEqual(jjb_config.jenkins['password'], "jenkins_password") self.assertEqual(jjb_config.builder['ignore_cache'], True) self.assertEqual(jjb_config.builder['flush_cache'], True) self.assertEqual(jjb_config.yamlparser['allow_empty_variables'], True)
def test_config_options_not_replaced_by_cli_defaults(self): """ Run test mode and check config settings from conf file retained when none of the global CLI options are set. """ config_file = os.path.join(self.fixtures_path, "settings_from_config.ini") args = ["--conf", config_file, "test", "dummy.yaml"] jenkins_jobs = entry.JenkinsJobs(args) jjb_config = jenkins_jobs.jjb_config self.assertEqual(jjb_config.jenkins["user"], "jenkins_user") self.assertEqual(jjb_config.jenkins["password"], "jenkins_password") self.assertEqual(jjb_config.builder["ignore_cache"], True) self.assertEqual(jjb_config.builder["flush_cache"], True) self.assertEqual(jjb_config.builder["update"], "all") self.assertEqual(jjb_config.yamlparser["allow_empty_variables"], True)
def test_config_options_overriden_by_cli(self): """ Run test mode and check config settings from conf file retained when none of the global CLI options are set. """ args = [ '--user', 'myuser', '--password', 'mypassword', '--ignore-cache', '--flush-cache', '--allow-empty-variables', 'test', 'dummy.yaml' ] jenkins_jobs = entry.JenkinsJobs(args) jjb_config = jenkins_jobs.jjb_config self.assertEqual(jjb_config.jenkins['user'], "myuser") self.assertEqual(jjb_config.jenkins['password'], "mypassword") self.assertEqual(jjb_config.builder['ignore_cache'], True) self.assertEqual(jjb_config.builder['flush_cache'], True) self.assertEqual(jjb_config.yamlparser['allow_empty_variables'], True)
def test_stream_output_ascii_encoding_invalid_char(self): """ Run test mode simulating using pipes for input and output using ascii encoding for output with include containing a character that cannot be converted. """ console_out = io.BytesIO() console_out.encoding = "ascii" input_file = os.path.join(self.fixtures_path, "unicode001.yaml") with io.open(input_file, "r", encoding="utf-8") as f: with mock.patch("sys.stdout", console_out): with mock.patch("sys.stdin", f): args = ["--conf", self.default_config_file, "test"] jenkins_jobs = entry.JenkinsJobs(args) e = self.assertRaises(UnicodeError, jenkins_jobs.execute) self.assertIn("'ascii' codec can't encode character", str(e))
def test_use_config_in_user_home(self): """ Verify that JJB uses config file in user home folder """ args = ['test', 'foo'] conffp = io.open(self.default_config_file, 'r', encoding='utf-8') with patch('os.path.isfile', return_value=True) as m_isfile: def side_effect(path): if path == self.user_conf: return True return False m_isfile.side_effect = side_effect with patch('io.open', return_value=conffp) as m_open: entry.JenkinsJobs(args, config_file_required=True) m_open.assert_called_with(self.user_conf, 'r', encoding='utf-8')
def test_use_global_config(self): """ Verify that JJB uses the global config file by default """ args = ["test", "foo"] conffp = io.open(self.default_config_file, "r", encoding="utf-8") with patch("os.path.isfile", return_value=True) as m_isfile: def side_effect(path): if path == self.global_conf: return True return False m_isfile.side_effect = side_effect with patch("io.open", return_value=conffp) as m_open: entry.JenkinsJobs(args, config_file_required=True) m_open.assert_called_with(self.global_conf, "r", encoding="utf-8")
def test_config_options_overriden_by_cli(self): """ Run test mode and check config settings from conf file retained when none of the global CLI options are set. """ args = [ "--user", "myuser", "--password", "mypassword", "--ignore-cache", "--flush-cache", "--allow-empty-variables", "test", "dummy.yaml", ] jenkins_jobs = entry.JenkinsJobs(args) jjb_config = jenkins_jobs.jjb_config self.assertEqual(jjb_config.jenkins["user"], "myuser") self.assertEqual(jjb_config.jenkins["password"], "mypassword") self.assertEqual(jjb_config.builder["ignore_cache"], True) self.assertEqual(jjb_config.builder["flush_cache"], True) self.assertEqual(jjb_config.yamlparser["allow_empty_variables"], True)
def execute_jenkins_jobs_with_args(self, args): jenkins_jobs = entry.JenkinsJobs(args) jenkins_jobs.execute()