If the hostname ends with '-', replace the '-' with '0'

This commit is contained in:
Adrian Vladu 2014-06-04 17:45:48 -07:00
parent 73727b8ae9
commit 3a517452a0
2 changed files with 14 additions and 2 deletions

View File

@ -15,6 +15,7 @@
# under the License.
import platform
import re
from oslo.config import cfg
@ -58,6 +59,7 @@ class SetHostNamePlugin(base.BasePlugin):
else:
new_host_name = metadata_host_name
new_host_name = re.sub(r'-$', '0', new_host_name)
if platform.node().lower() == new_host_name.lower():
LOG.debug("Hostname already set to: %s" % new_host_name)
reboot_required = False

View File

@ -36,12 +36,16 @@ class SetHostNamePluginPluginTests(unittest.TestCase):
@mock.patch('platform.node')
@mock.patch('cloudbaseinit.osutils.factory.get_os_utils')
def _test_execute(self, mock_get_os_utils, mock_node, hostname_exists=True,
hostname_already_set=False, new_hostname_length=1):
hostname_already_set=False, new_hostname_length=1,
hostname_truncate_to_zero=False):
mock_service = mock.MagicMock()
mock_osutils = mock.MagicMock()
fake_shared_data = 'fake data'
new_hostname = 'x' * new_hostname_length
if hostname_truncate_to_zero:
new_hostname = ('%s-') % new_hostname[:-1]
if hostname_exists:
mock_service.get_host_name.return_value = new_hostname
else:
@ -55,7 +59,8 @@ class SetHostNamePluginPluginTests(unittest.TestCase):
hostname = new_hostname.split('.', 1)[0]
if len(new_hostname) > length:
hostname = hostname[:length]
if hostname_truncate_to_zero:
hostname = ('%s0') % hostname[:-1]
if hostname_already_set:
mock_node.return_value = hostname
else:
@ -88,5 +93,10 @@ class SetHostNamePluginPluginTests(unittest.TestCase):
self._test_execute(
new_hostname_length=sethostname.NETBIOS_HOST_NAME_MAX_LEN)
def test_execute_truncate_to_zero(self):
self._test_execute(
new_hostname_length=sethostname.NETBIOS_HOST_NAME_MAX_LEN,
hostname_truncate_to_zero=True)
def test_execute_no_hostname(self):
self._test_execute(hostname_exists=False)