From 3a517452a0f9277a048e9e430a72fc6b5de9bdbc Mon Sep 17 00:00:00 2001 From: Adrian Vladu Date: Wed, 4 Jun 2014 17:45:48 -0700 Subject: [PATCH] If the hostname ends with '-', replace the '-' with '0' --- cloudbaseinit/plugins/windows/sethostname.py | 2 ++ .../tests/plugins/windows/test_sethostname.py | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/cloudbaseinit/plugins/windows/sethostname.py b/cloudbaseinit/plugins/windows/sethostname.py index 11fade57..59a8941e 100644 --- a/cloudbaseinit/plugins/windows/sethostname.py +++ b/cloudbaseinit/plugins/windows/sethostname.py @@ -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 diff --git a/cloudbaseinit/tests/plugins/windows/test_sethostname.py b/cloudbaseinit/tests/plugins/windows/test_sethostname.py index b02e2d60..8b2d92fc 100644 --- a/cloudbaseinit/tests/plugins/windows/test_sethostname.py +++ b/cloudbaseinit/tests/plugins/windows/test_sethostname.py @@ -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)