Merge "Cleanup UndercloudHostsEntries" into stable/ussuri

This commit is contained in:
Zuul 2020-08-15 14:55:19 +00:00 committed by Gerrit Code Review
commit 374f9acaa6
2 changed files with 30 additions and 8 deletions

View File

@ -1708,12 +1708,20 @@ class TestDeployOvercloud(fakes.TestDeployOvercloud):
@mock.patch('subprocess.Popen', autospec=True)
def test__get_undercloud_host_entry(self, mock_popen):
mock_process = mock.Mock()
mock_process.communicate.return_value = (
'fd12::1 uc.ctlplane.localdomain uc.ctlplane', '')
mock_process.returncode = 0
mock_popen.return_value = mock_process
expected = ('fd12::1 uc.ctlplane.localdomain uc.ctlplane')
self.assertEqual(expected, self.cmd._get_undercloud_host_entry())
mock_hosts = {
'fd12::1 uc.ctlplane.localdomain uc.ctlplane':
'fd12::1 uc.ctlplane.localdomain uc.ctlplane',
'fd12::1 uc.ctlplane.localdomain uc.ctlplane\n'
'fd12::1 uc.ctlplane.localdomain uc.ctlplane':
'fd12::1 uc.ctlplane.localdomain uc.ctlplane',
'1.2.3.4 uc.ctlplane foo uc.ctlplane bar uc.ctlplane':
'1.2.3.4 uc.ctlplane foo bar'
}
for value, expected in mock_hosts.items():
mock_process.communicate.return_value = (value, '')
mock_process.returncode = 0
mock_popen.return_value = mock_process
self.assertEqual(expected, self.cmd._get_undercloud_host_entry())
def test_check_limit_warning(self):
mock_warning = mock.MagicMock()

View File

@ -15,6 +15,7 @@
from __future__ import print_function
import argparse
from collections import OrderedDict
import logging
import os
import os.path
@ -102,6 +103,20 @@ class DeployOvercloud(command.Command):
return parameters
def _cleanup_host_entry(self, entry):
# remove any tab or space excess
entry_stripped = re.sub('[ \t]+', ' ', str(entry).rstrip())
# removes any duplicate identical lines
unique_lines = list(set(entry_stripped.splitlines()))
ret = ''
for line in unique_lines:
# remove any duplicate word
hosts_unique = (' '.join(
OrderedDict((w, w) for w in line.split()).keys()))
if hosts_unique != '':
ret += hosts_unique + '\n'
return ret.rstrip('\n')
def _get_undercloud_host_entry(self):
"""Get hosts entry for undercloud ctlplane network
@ -115,8 +130,7 @@ class DeployOvercloud(command.Command):
if process.returncode != 0:
raise exceptions.DeploymentError('No entry for %s in /etc/hosts'
% ctlplane_hostname)
return re.sub(' +', ' ', str(out).rstrip())
return self._cleanup_host_entry(out)
def _create_breakpoint_cleanup_env(self, tht_root, container_name):
bp_env = {}