
This commit intentionally introduces a number of important API breakages. Specifically, the jenkins_jobs.cmd module has been pared down to some of its most difficult-to-refactor elements. * Create jenkins_jobs.cli.entry.JenkinsJobs class to organize command line parsing and execution. * Remove references to ConfigParser object in test code, hidden as an implementation detail of JenkinsJobs command line parsing. This will be necessary in the next stage of JJB 2.0 code which will be to create a JJBConfig object that handles logic and presentation of configuration from various sources--defaults, command line arguments, configuration file, and maybe environment variables in the future. * Remove references to Namespace object produced by argparse module. Required rewrite of multipath & recursive path tests with a new MatchesDir testtools Matcher class that validates the expected output for a run of JJB against a given set of yamldirs with the specified command line arguments. * Use stevedore to dynamically load subcommand parsers. * Move configuration loading/testing to its own test file. Also fix the global vs home directory JJB config file test. Change-Id: If62280418ba7319c313033ab387af4284237747e
74 lines
2.5 KiB
Python
74 lines
2.5 KiB
Python
import io
|
|
import os
|
|
|
|
from jenkins_jobs.cli import entry
|
|
from mock import patch
|
|
from tests.base import mock
|
|
from tests.cmd.test_cmd import CmdTestsBase
|
|
|
|
|
|
@mock.patch('jenkins_jobs.builder.Jenkins.get_plugins_info', mock.MagicMock)
|
|
class TestConfigs(CmdTestsBase):
|
|
|
|
global_conf = '/etc/jenkins_jobs/jenkins_jobs.ini'
|
|
user_conf = os.path.join(os.path.expanduser('~'), '.config',
|
|
'jenkins_jobs', 'jenkins_jobs.ini')
|
|
|
|
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.user_conf:
|
|
return False
|
|
if path == self.global_conf:
|
|
return True
|
|
|
|
m_isfile.side_effect = side_effect
|
|
|
|
with patch('io.open', return_value=conffp) as m_open:
|
|
entry.JenkinsJobs(args)
|
|
m_open.assert_called_with(self.global_conf, 'r',
|
|
encoding='utf-8')
|
|
|
|
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
|
|
|
|
m_isfile.side_effect = side_effect
|
|
with patch('io.open', return_value=conffp) as m_open:
|
|
entry.JenkinsJobs(args)
|
|
m_open.assert_called_with(self.user_conf, 'r',
|
|
encoding='utf-8')
|
|
|
|
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_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)
|