Merge "Refactor reading kolla config in image builder"

This commit is contained in:
Zuul 2019-03-18 19:49:22 +00:00 committed by Gerrit Code Review
commit 8684f64075
4 changed files with 36 additions and 35 deletions

View File

@ -1378,6 +1378,7 @@ class TestCheckEnvForProxy(TestCase):
class TestConfigParser(TestCase):
def setUp(self):
self.tmp_dir = tempfile.mkdtemp()
@ -1387,14 +1388,10 @@ class TestConfigParser(TestCase):
self.tmp_dir = None
def test_get_config_value(self):
cfile, cfile_name = tempfile.mkstemp(dir=self.tmp_dir)
cfp = open(cfile_name, 'w')
cfg = ConfigParser()
cfg.add_section('foo')
cfg.set('foo', 'bar', 'baz')
cfg.write(cfp)
cfp.close()
config = utils.get_config_value(cfile_name, 'foo', 'bar')
config = utils.get_from_cfg(cfg, 'bar', 'foo')
self.assertEqual(config, 'baz')
def test_get_config_value_multiple_files(self):
@ -1409,13 +1406,14 @@ class TestConfigParser(TestCase):
cfg.set('foo', 'bar', 'boop')
with open(cfile2_name, 'w') as fp:
cfg.write(fp)
config = utils.get_config_value(cfiles, 'foo', 'bar')
cfgs = utils.get_read_config(cfiles)
config = utils.get_from_cfg(cfgs, 'bar', 'foo')
self.assertEqual(config, 'boop')
def test_get_config_value_bad_file(self):
self.assertRaises(exceptions.NotFound,
utils.get_config_value,
'does-not-exist', 'foo', 'bar')
utils.get_from_cfg,
'does-not-exist', 'bar', 'foo')
class TestGetLocalTimezone(TestCase):

View File

@ -508,13 +508,16 @@ class TestContainerImageBuild(TestPluginV1):
@mock.patch('os.fdopen', autospec=True)
@mock.patch('tempfile.mkdtemp')
@mock.patch('tempfile.mkstemp')
@mock.patch('tripleoclient.v1.container_image.BuildImage.kolla_cfg')
@mock.patch(
'tripleoclient.utils.get_from_cfg')
@mock.patch('tripleo_common.image.builder.buildah.BuildahBuilder',
autospec=True)
@mock.patch('tripleo_common.image.kolla_builder.KollaImageBuilder',
autospec=True)
@mock.patch('tripleoclient.utils.get_read_config')
@mock.patch('os.remove')
def test_container_image_build_with_buildah(self, mock_remove,
mock_read_conf,
mock_builder, mock_buildah,
mock_kolla_cfg, mock_mkstemp,
mock_mkdtemp, mock_fdopen):
@ -540,12 +543,13 @@ class TestContainerImageBuild(TestPluginV1):
cfg_files = list(parsed_args.kolla_config_files)
cfg_files.append('/tmp/whatever_file')
mock_read_conf.assert_any_call(cfg_files)
cfg_calls = [
mock.call(cfg_files, 'base'),
mock.call(cfg_files, 'type'),
mock.call(cfg_files, 'tag'),
mock.call(cfg_files, 'namespace'),
mock.call(cfg_files, 'registry'),
mock.call(mock_read_conf.return_value, 'base'),
mock.call(mock_read_conf.return_value, 'type'),
mock.call(mock_read_conf.return_value, 'tag'),
mock.call(mock_read_conf.return_value, 'namespace'),
mock.call(mock_read_conf.return_value, 'registry'),
]
mock_kolla_cfg.assert_has_calls(cfg_calls)
mock_bb.build_all.assert_called_once()

View File

@ -1541,17 +1541,22 @@ def check_env_for_proxy(no_proxy_hosts=None):
raise RuntimeError(message)
def get_config_value(cfg, section, option):
"""Return the value of an option in ini config file(s)"""
def get_read_config(cfg):
"""Return the config read from ini config file(s)"""
config = ConfigParser()
config.read(cfg)
return config
def get_from_cfg(cfg, param, section="DEFAULT"):
"""Return a parameter from Kolla config"""
try:
val = config.get(section, option)
val = cfg.get(section, param)
except Exception:
raise exceptions.NotFound(_('Unable to find {section}/{option} in '
'{config}').format(section=section,
option=option,
config=cfg))
raise exceptions.NotFound(_("Unable to find {section}/{option} in "
"{config}").format(section=param,
option=section,
config=cfg))
return val

View File

@ -181,11 +181,6 @@ class BuildImage(command.Command):
)
return parser
@staticmethod
def kolla_cfg(cfg, param):
"""Return a parameter from Kolla config"""
return utils.get_config_value(cfg, 'DEFAULT', param)
def take_action(self, parsed_args):
self.log.debug("take_action(%s)" % parsed_args)
@ -211,15 +206,14 @@ class BuildImage(command.Command):
if parsed_args.use_buildah:
deps = json.loads(result)
cfg = kolla_config_files
bb = buildah.BuildahBuilder(kolla_tmp_dir, deps,
BuildImage.kolla_cfg(cfg, 'base'),
BuildImage.kolla_cfg(cfg, 'type'),
BuildImage.kolla_cfg(cfg, 'tag'),
BuildImage.kolla_cfg(cfg,
'namespace'),
BuildImage.kolla_cfg(cfg,
'registry'))
kolla_cfg = utils.get_read_config(kolla_config_files)
bb = buildah.BuildahBuilder(
kolla_tmp_dir, deps,
utils.get_from_cfg(kolla_cfg, "base"),
utils.get_from_cfg(kolla_cfg, "type"),
utils.get_from_cfg(kolla_cfg, "tag"),
utils.get_from_cfg(kolla_cfg, "namespace"),
utils.get_from_cfg(kolla_cfg, "registry"))
bb.build_all()
elif parsed_args.list_dependencies:
deps = json.loads(result)