Fixes regression: reboot only if hostname is set
This commit is contained in:
@@ -14,6 +14,8 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
import platform
|
||||||
|
|
||||||
from oslo.config import cfg
|
from oslo.config import cfg
|
||||||
|
|
||||||
from cloudbaseinit.openstack.common import log as logging
|
from cloudbaseinit.openstack.common import log as logging
|
||||||
@@ -56,7 +58,12 @@ class SetHostNamePlugin(base.BasePlugin):
|
|||||||
else:
|
else:
|
||||||
new_host_name = metadata_host_name
|
new_host_name = metadata_host_name
|
||||||
|
|
||||||
LOG.info("Setting hostname: %s" % new_host_name)
|
if platform.node().lower() == new_host_name.lower():
|
||||||
reboot_required = osutils.set_host_name(new_host_name)
|
LOG.debug("Hostname already set to: %s" % new_host_name)
|
||||||
|
reboot_required = False
|
||||||
|
else:
|
||||||
|
LOG.info("Setting hostname: %s" % new_host_name)
|
||||||
|
osutils.set_host_name(new_host_name)
|
||||||
|
reboot_required = True
|
||||||
|
|
||||||
return (base.PLUGIN_EXECUTION_DONE, reboot_required)
|
return (base.PLUGIN_EXECUTION_DONE, reboot_required)
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import unittest
|
|||||||
|
|
||||||
from oslo.config import cfg
|
from oslo.config import cfg
|
||||||
|
|
||||||
|
from cloudbaseinit.plugins import base
|
||||||
from cloudbaseinit.plugins.windows import sethostname
|
from cloudbaseinit.plugins.windows import sethostname
|
||||||
from cloudbaseinit.tests.metadata import fake_json_response
|
from cloudbaseinit.tests.metadata import fake_json_response
|
||||||
|
|
||||||
@@ -32,41 +33,59 @@ class SetHostNamePluginPluginTests(unittest.TestCase):
|
|||||||
self.fake_data = fake_json_response.get_fake_metadata_json(
|
self.fake_data = fake_json_response.get_fake_metadata_json(
|
||||||
'2013-04-04')
|
'2013-04-04')
|
||||||
|
|
||||||
|
@mock.patch('platform.node')
|
||||||
@mock.patch('cloudbaseinit.osutils.factory.get_os_utils')
|
@mock.patch('cloudbaseinit.osutils.factory.get_os_utils')
|
||||||
def _test_execute(self, mock_get_os_utils, hostname_exists,
|
def _test_execute(self, mock_get_os_utils, mock_node, hostname_exists=True,
|
||||||
new_hostname_length=1):
|
hostname_already_set=False, new_hostname_length=1):
|
||||||
mock_service = mock.MagicMock()
|
mock_service = mock.MagicMock()
|
||||||
mock_osutils = mock.MagicMock()
|
mock_osutils = mock.MagicMock()
|
||||||
fake_shared_data = 'fake data'
|
fake_shared_data = 'fake data'
|
||||||
new_hostname = 'x' * new_hostname_length
|
new_hostname = 'x' * new_hostname_length
|
||||||
|
|
||||||
if hostname_exists:
|
if hostname_exists:
|
||||||
mock_service.get_host_name.return_value = new_hostname
|
mock_service.get_host_name.return_value = new_hostname
|
||||||
else:
|
else:
|
||||||
mock_service.get_host_name.return_value = None
|
mock_service.get_host_name.return_value = None
|
||||||
|
|
||||||
CONF.set_override('netbios_host_name_compatibility', True)
|
CONF.set_override('netbios_host_name_compatibility', True)
|
||||||
mock_get_os_utils.return_value = mock_osutils
|
mock_get_os_utils.return_value = mock_osutils
|
||||||
mock_osutils.set_host_name.return_value = False
|
|
||||||
response = self._sethostname_plugin.execute(mock_service,
|
|
||||||
fake_shared_data)
|
|
||||||
mock_service.get_host_name.assert_caled_once_with()
|
|
||||||
if hostname_exists is True:
|
if hostname_exists is True:
|
||||||
length = sethostname.NETBIOS_HOST_NAME_MAX_LEN
|
length = sethostname.NETBIOS_HOST_NAME_MAX_LEN
|
||||||
mock_get_os_utils.assert_called_once_with()
|
|
||||||
hostname = new_hostname.split('.', 1)[0]
|
hostname = new_hostname.split('.', 1)[0]
|
||||||
if len(new_hostname) > length:
|
if len(new_hostname) > length:
|
||||||
hostname = hostname[:length]
|
hostname = hostname[:length]
|
||||||
mock_osutils.set_host_name.assert_called_once_with(hostname)
|
|
||||||
|
|
||||||
self.assertEqual(response, (1, False))
|
if hostname_already_set:
|
||||||
|
mock_node.return_value = hostname
|
||||||
|
else:
|
||||||
|
mock_node.return_value = 'fake_old_value'
|
||||||
|
|
||||||
|
response = self._sethostname_plugin.execute(mock_service,
|
||||||
|
fake_shared_data)
|
||||||
|
|
||||||
|
mock_service.get_host_name.assert_called_once_with()
|
||||||
|
|
||||||
|
if hostname_exists is True:
|
||||||
|
mock_get_os_utils.assert_called_once_with()
|
||||||
|
if hostname_already_set:
|
||||||
|
self.assertFalse(mock_osutils.set_host_name.called)
|
||||||
|
else:
|
||||||
|
mock_osutils.set_host_name.assert_called_once_with(hostname)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
response, (base.PLUGIN_EXECUTION_DONE,
|
||||||
|
hostname_exists and not hostname_already_set))
|
||||||
|
|
||||||
|
def test_execute_hostname_already_set(self):
|
||||||
|
self._test_execute(hostname_already_set=True)
|
||||||
|
|
||||||
def test_execute_hostname_to_be_truncated(self):
|
def test_execute_hostname_to_be_truncated(self):
|
||||||
self._test_execute(
|
self._test_execute(
|
||||||
hostname_exists=True,
|
|
||||||
new_hostname_length=sethostname.NETBIOS_HOST_NAME_MAX_LEN + 1)
|
new_hostname_length=sethostname.NETBIOS_HOST_NAME_MAX_LEN + 1)
|
||||||
|
|
||||||
def test_execute_no_truncate_needed(self):
|
def test_execute_no_truncate_needed(self):
|
||||||
self._test_execute(
|
self._test_execute(
|
||||||
hostname_exists=True,
|
|
||||||
new_hostname_length=sethostname.NETBIOS_HOST_NAME_MAX_LEN)
|
new_hostname_length=sethostname.NETBIOS_HOST_NAME_MAX_LEN)
|
||||||
|
|
||||||
def test_execute_no_hostname(self):
|
def test_execute_no_hostname(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user