diff options
author | Eli Qiao <liyong.qiao@intel.com> | 2015-09-17 13:57:24 +0800 |
---|---|---|
committer | He Jie Xu <hejie.xu@intel.com> | 2015-09-23 17:02:30 +0800 |
commit | bc6f30de953303604625e84ad2345cfb595170d2 (patch) | |
tree | 2e7f45893914caf44be5b0aeb2acaf235b51216c /nova/tests/unit/test_utils.py | |
parent | 806dc025dbc9a102f7bace78be9c56f8e9af0929 (diff) | |
download | nova-bc6f30de953303604625e84ad2345cfb595170d2.tar.gz |
Give instance default hostname if hostname is empty
Instance hostname is generated by displayname, currently, displayname
can be '----', and other Non-ASCII character such as Chinese characters,
sanitize_hostname will return empty in such cases.
This patch adds a helper function _default_host_name to give an instance
default name if sanitize_hostname returns empty hostname. And,
sanitize_hostname will truncated to 63 if the host name is too long.
Also Window, Linux, and Dnsmasq have different limitation:
Windows: 255 (net_bios limit to 15, but window will truncate it)
Linux: 64
Dnsmasq: 63
Due to nova-network will leverage dnsmasq to set hostname, so we chose
63.
Besides, added more test cases to cover sanitize_hostname.
DocImpact
Change-Id: I443b51e0576cf657d96e498d6700bfc34d987301
Closes-Bug: #1495388
Co-authored-by: Alex Xu <hejie.xu@intel.com>
Diffstat (limited to 'nova/tests/unit/test_utils.py')
-rw-r--r-- | nova/tests/unit/test_utils.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/nova/tests/unit/test_utils.py b/nova/tests/unit/test_utils.py index f4482b19a2..ca68a3e01e 100644 --- a/nova/tests/unit/test_utils.py +++ b/nova/tests/unit/test_utils.py @@ -94,6 +94,40 @@ class GenericUtilsTestCase(test.NoDBTestCase): hostname = "<}\x1fh\x10e\x08l\x02l\x05o\x12!{>" self.assertEqual("hello", utils.sanitize_hostname(hostname)) + def test_hostname_has_default(self): + hostname = u"\u7684hello" + defaultname = "Server-1" + self.assertEqual("hello", utils.sanitize_hostname(hostname, + defaultname)) + + def test_hostname_empty_has_default(self): + hostname = u"\u7684" + defaultname = "Server-1" + self.assertEqual(defaultname, utils.sanitize_hostname(hostname, + defaultname)) + + def test_hostname_empty_has_default_too_long(self): + hostname = u"\u7684" + defaultname = "a" * 64 + self.assertEqual("a" * 63, utils.sanitize_hostname(hostname, + defaultname)) + + def test_hostname_empty_no_default(self): + hostname = u"\u7684" + self.assertEqual("", utils.sanitize_hostname(hostname)) + + def test_hostname_empty_minus_period(self): + hostname = "---..." + self.assertEqual("", utils.sanitize_hostname(hostname)) + + def test_hostname_with_space(self): + hostname = " a b c " + self.assertEqual("a-b-c", utils.sanitize_hostname(hostname)) + + def test_hostname_too_long(self): + hostname = "a" * 64 + self.assertEqual(63, len(utils.sanitize_hostname(hostname))) + def test_generate_password(self): password = utils.generate_password() self.assertTrue([c for c in password if c in '0123456789']) |