Handle empty NetworkConfig

In some cases the NetworkConfig config resource may come back as an
empty dict from Heat, such as when using net-config-noop.yaml. In that
case, we should not attempt to write any config.

Change-Id: I02129adf193f7a4a8b38411f3a20079d10cfa872
Related-bug: #1834094
This commit is contained in:
James Slagle 2019-07-01 16:55:20 -04:00 committed by Harald Jensås
parent cd12dc6320
commit ecd3738077
2 changed files with 30 additions and 10 deletions

View File

@ -807,3 +807,19 @@ class TestConfig(base.TestCase):
self.config = ooo_config.Config(heat)
self.assertRaises(yaml.scanner.ScannerError,
self.config.validate_config, stack_config, yaml_file)
@patch('tripleo_common.utils.config.Config.get_network_config_data')
def test_render_network_config_empty_dict(self,
mock_get_network_config_data):
heat = mock.MagicMock()
heat.stacks.get.return_value = fakes.create_tht_stack()
config_mock = mock.MagicMock()
config_mock.config = {}
heat.software_configs.get.return_value = config_mock
self.config = ooo_config.Config(heat)
stack = mock.Mock()
server_roles = dict(Controller='controller')
mock_get_network_config_data.return_value = dict(Controller='config')
config_dir = '/tmp/tht'
self.config.render_network_config(stack, config_dir, server_roles)

View File

@ -200,6 +200,19 @@ class Config(object):
"error {}".format(yaml_file, e))
raise e
def render_network_config(self, stack, config_dir, server_roles):
network_config = self.get_network_config_data(stack)
for server, config in network_config.items():
server_deployment_dir = os.path.join(
config_dir, server_roles[server], server)
self._mkdir(server_deployment_dir)
network_config_path = os.path.join(
server_deployment_dir, "NetworkConfig")
s_config = self.client.software_configs.get(config)
if getattr(s_config, 'config', ''):
with open(network_config_path, 'w') as f:
f.write(s_config.config)
def write_config(self, stack, name, config_dir, config_type=None):
# Get role data:
role_data = self.stack_outputs.get('RoleData', {})
@ -505,16 +518,7 @@ class Config(object):
f.write(template_data)
# Render NetworkConfig data
network_config = self.get_network_config_data(stack)
for server, config in network_config.items():
server_deployment_dir = os.path.join(
config_dir, server_roles[server], server)
self._mkdir(server_deployment_dir)
network_config_path = os.path.join(
server_deployment_dir, "NetworkConfig")
s_config = self.client.software_configs.get(config)
with open(network_config_path, 'w') as f:
f.write(s_config.config)
self.render_network_config(stack, config_dir, server_roles)
shutil.copyfile(
os.path.join(templates_path, 'deployments.yaml'),