]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
Makes (de)canonicalize_hostname ready to empty domains 1559/head
authorRoman Grigorev <rgrigorev@suse.de>
Wed, 16 Sep 2020 05:34:50 +0000 (07:34 +0200)
committerRoman Grigorev <rgrigorev@suse.de>
Mon, 26 Oct 2020 21:16:16 +0000 (22:16 +0100)
teuthology/misc.py : addied support of empty domains to
 misc.canonicalize_hostname and misc.decanonicalize_hostname.

teuthology/test/test_misc.py: added test cases for new code

Signed-off-by: Roman Grigorev <rgrigorev@suse.de>
teuthology/misc.py
teuthology/test/test_misc.py

index 547709fd642c5121cd4112bf5421406e629a08e2..23e09069c2e658b5262eaa1bdd485caaacfb73ec 100644 (file)
@@ -37,7 +37,7 @@ stamp = datetime.datetime.now().strftime("%y%m%d%H%M")
 is_arm = lambda x: x.startswith('tala') or x.startswith(
     'ubuntu@tala') or x.startswith('saya') or x.startswith('ubuntu@saya')
 
-hostname_expr_templ = '(?P<user>.*@)?(?P<shortname>.*)\.{lab_domain}'
+hostname_expr_templ = '(?P<user>.*@)?(?P<shortname>.*){lab_domain}'
 
 def host_shortname(hostname):
     if _is_ipv4(hostname) or _is_ipv6(hostname):
@@ -63,18 +63,22 @@ def canonicalize_hostname(hostname, user='ubuntu'):
         user_ = user
 
     user_at = user_.strip('@') + '@' if user_ else ''
-
-    ret = '{user_at}{short}.{lab_domain}'.format(
+    domain = config.lab_domain
+    if domain and not shortname.endswith('.'):
+        domain = '.' + domain
+    ret = '{user_at}{short}{domain}'.format(
         user_at=user_at,
         short=shortname,
-        lab_domain=config.lab_domain,
+        domain=domain,
     )
     return ret
 
 
 def decanonicalize_hostname(hostname):
-    hostname_expr = hostname_expr_templ.format(
-        lab_domain=config.lab_domain.replace('.', '\.'))
+    lab_domain = ''
+    if config.lab_domain:
+        lab_domain='\.' + config.lab_domain.replace('.', '\.')
+    hostname_expr = hostname_expr_templ.format(lab_domain=lab_domain)
     match = re.match(hostname_expr, hostname)
     if match:
         hostname = match.groupdict()['shortname']
index 8756a3df08283567fd61af5cdf58a84011408cc1..9b8fe905353cfb2f6cd72c98fb195581ff574674 100644 (file)
@@ -292,6 +292,29 @@ class TestHostnames(object):
         result = misc.decanonicalize_hostname(host)
         assert result == 'box1'
 
+    def test_canonicalize_hostname_nodomain(self):
+        config.lab_domain = ''
+        host = 'box2'
+        result = misc.canonicalize_hostname(host)
+        assert result == 'ubuntu@' + host
+
+    def test_decanonicalize_hostname_nodomain(self):
+        config.lab_domain = ''
+        host = 'ubuntu@box2'
+        result = misc.decanonicalize_hostname(host)
+        assert result == 'box2'
+
+    def test_canonicalize_hostname_full_other_user(self):
+        config.lab_domain = 'example.com'
+        host = 'user1@box1.example.come'
+        result = misc.canonicalize_hostname(host)
+        assert result == 'user1@box1.example.com'
+
+    def test_decanonicalize_hostname_full_other_user(self):
+        config.lab_domain = 'example.com'
+        host = 'user1@box1.example.come'
+        result = misc.decanonicalize_hostname(host)
+        assert result == 'box1'
 
 class TestMergeConfigs(object):
     """ Tests merge_config and deep_merge in teuthology.misc """