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

View File

@ -508,13 +508,16 @@ class TestContainerImageBuild(TestPluginV1):
@mock.patch('os.fdopen', autospec=True) @mock.patch('os.fdopen', autospec=True)
@mock.patch('tempfile.mkdtemp') @mock.patch('tempfile.mkdtemp')
@mock.patch('tempfile.mkstemp') @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', @mock.patch('tripleo_common.image.builder.buildah.BuildahBuilder',
autospec=True) autospec=True)
@mock.patch('tripleo_common.image.kolla_builder.KollaImageBuilder', @mock.patch('tripleo_common.image.kolla_builder.KollaImageBuilder',
autospec=True) autospec=True)
@mock.patch('tripleoclient.utils.get_read_config')
@mock.patch('os.remove') @mock.patch('os.remove')
def test_container_image_build_with_buildah(self, mock_remove, def test_container_image_build_with_buildah(self, mock_remove,
mock_read_conf,
mock_builder, mock_buildah, mock_builder, mock_buildah,
mock_kolla_cfg, mock_mkstemp, mock_kolla_cfg, mock_mkstemp,
mock_mkdtemp, mock_fdopen): mock_mkdtemp, mock_fdopen):
@ -540,12 +543,13 @@ class TestContainerImageBuild(TestPluginV1):
cfg_files = list(parsed_args.kolla_config_files) cfg_files = list(parsed_args.kolla_config_files)
cfg_files.append('/tmp/whatever_file') cfg_files.append('/tmp/whatever_file')
mock_read_conf.assert_any_call(cfg_files)
cfg_calls = [ cfg_calls = [
mock.call(cfg_files, 'base'), mock.call(mock_read_conf.return_value, 'base'),
mock.call(cfg_files, 'type'), mock.call(mock_read_conf.return_value, 'type'),
mock.call(cfg_files, 'tag'), mock.call(mock_read_conf.return_value, 'tag'),
mock.call(cfg_files, 'namespace'), mock.call(mock_read_conf.return_value, 'namespace'),
mock.call(cfg_files, 'registry'), mock.call(mock_read_conf.return_value, 'registry'),
] ]
mock_kolla_cfg.assert_has_calls(cfg_calls) mock_kolla_cfg.assert_has_calls(cfg_calls)
mock_bb.build_all.assert_called_once() 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) raise RuntimeError(message)
def get_config_value(cfg, section, option): def get_read_config(cfg):
"""Return the value of an option in ini config file(s)""" """Return the config read from ini config file(s)"""
config = ConfigParser() config = ConfigParser()
config.read(cfg) config.read(cfg)
return config
def get_from_cfg(cfg, param, section="DEFAULT"):
"""Return a parameter from Kolla config"""
try: try:
val = config.get(section, option) val = cfg.get(section, param)
except Exception: except Exception:
raise exceptions.NotFound(_('Unable to find {section}/{option} in ' raise exceptions.NotFound(_("Unable to find {section}/{option} in "
'{config}').format(section=section, "{config}").format(section=param,
option=option, option=section,
config=cfg)) config=cfg))
return val return val

View File

@ -181,11 +181,6 @@ class BuildImage(command.Command):
) )
return parser 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): def take_action(self, parsed_args):
self.log.debug("take_action(%s)" % parsed_args) self.log.debug("take_action(%s)" % parsed_args)
@ -211,15 +206,14 @@ class BuildImage(command.Command):
if parsed_args.use_buildah: if parsed_args.use_buildah:
deps = json.loads(result) deps = json.loads(result)
cfg = kolla_config_files kolla_cfg = utils.get_read_config(kolla_config_files)
bb = buildah.BuildahBuilder(kolla_tmp_dir, deps, bb = buildah.BuildahBuilder(
BuildImage.kolla_cfg(cfg, 'base'), kolla_tmp_dir, deps,
BuildImage.kolla_cfg(cfg, 'type'), utils.get_from_cfg(kolla_cfg, "base"),
BuildImage.kolla_cfg(cfg, 'tag'), utils.get_from_cfg(kolla_cfg, "type"),
BuildImage.kolla_cfg(cfg, utils.get_from_cfg(kolla_cfg, "tag"),
'namespace'), utils.get_from_cfg(kolla_cfg, "namespace"),
BuildImage.kolla_cfg(cfg, utils.get_from_cfg(kolla_cfg, "registry"))
'registry'))
bb.build_all() bb.build_all()
elif parsed_args.list_dependencies: elif parsed_args.list_dependencies:
deps = json.loads(result) deps = json.loads(result)