From bcfff3dd530994d3ae22f945f429df3afc954cb0 Mon Sep 17 00:00:00 2001 From: Brian Lamar Date: Wed, 9 Nov 2011 18:16:24 -0500 Subject: Follow hostname RFCs Updated hostname sanitization method to more closely follow RFC-952 and RFC-1123. Also moved it to nova.utils, where it seems to fit better. Fixes bug 885374 (Patch Set 1) Updated hostname sanitization with more efficient and [opinion] more readable implementation. Change-Id: I60d7ee89867c05950bec1fd53b072a1c6247ebea --- nova/tests/api/openstack/test_servers.py | 5 ++++- nova/tests/test_compute.py | 24 ++++++++++++++++++++++++ nova/tests/test_utils.py | 24 ++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index aa9c5c02c..7f7e079db 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -1187,7 +1187,10 @@ class ServersControllerTest(test.TestCase): body = dict(server=inst_dict) def server_update(context, id, params): - filtered_dict = dict(display_name='server_test') + filtered_dict = { + 'display_name': 'server_test', + 'hostname': 'server-test', + } self.assertEqual(params, filtered_dict) return filtered_dict diff --git a/nova/tests/test_compute.py b/nova/tests/test_compute.py index 1dfc781dd..6b74a14f0 100644 --- a/nova/tests/test_compute.py +++ b/nova/tests/test_compute.py @@ -1237,6 +1237,30 @@ class ComputeAPITestCase(BaseTestCase): db.instance_destroy(self.context, instance_id) + def test_hostname_create(self): + """Ensure instance hostname is set during creation.""" + inst_type = instance_types.get_instance_type_by_name('m1.tiny') + (instances, _) = self.compute_api.create(self.context, + inst_type, + None, + display_name='test host') + + self.assertEqual('test-host', instances[0]['hostname']) + + def test_hostname_update(self): + """Ensure instance hostname is set during an update.""" + instance_id = self._create_instance({"display_name": "test host"}) + instance = db.instance_get(self.context, instance_id) + + expected_hostname = 'test-host' + actual = self.compute_api.update(self.context, + instance_id, + **dict(instance)) + + self.assertEqual(expected_hostname, actual['hostname']) + + db.instance_destroy(self.context, instance_id) + def test_set_admin_password(self): """Ensure instance can have its admin password set""" instance_id = self._create_instance() diff --git a/nova/tests/test_utils.py b/nova/tests/test_utils.py index 19a15332d..2c4c02b82 100644 --- a/nova/tests/test_utils.py +++ b/nova/tests/test_utils.py @@ -282,6 +282,30 @@ class GenericUtilsTestCase(test.TestCase): result = utils.parse_server_string('www.exa:mple.com:8443') self.assertEqual(('', ''), result) + def test_hostname_unicode_sanitization(self): + hostname = u"\u7684.test.example.com" + self.assertEqual("test.example.com", + utils.sanitize_hostname(hostname)) + + def test_hostname_sanitize_periods(self): + hostname = "....test.example.com..." + self.assertEqual("test.example.com", + utils.sanitize_hostname(hostname)) + + def test_hostname_sanitize_dashes(self): + hostname = "----test.example.com---" + self.assertEqual("test.example.com", + utils.sanitize_hostname(hostname)) + + def test_hostname_sanitize_characters(self): + hostname = "(#@&$!(@*--#&91)(__=+--test-host.example!!.com-0+" + self.assertEqual("91----test-host.example.com-0", + utils.sanitize_hostname(hostname)) + + def test_hostname_translate(self): + hostname = "<}\x1fh\x10e\x08l\x02l\x05o\x12!{>" + self.assertEqual("hello", utils.sanitize_hostname(hostname)) + def test_bool_from_str(self): self.assertTrue(utils.bool_from_str('1')) self.assertTrue(utils.bool_from_str('2')) -- cgit