]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
Make (de)canonicalize_hostname() work without user
authorZack Cerza <zack@cerza.org>
Mon, 19 May 2014 22:40:11 +0000 (17:40 -0500)
committerZack Cerza <zack.cerza@inktank.com>
Mon, 25 Aug 2014 17:14:36 +0000 (11:14 -0600)
Add tests proving they still work *with* usernames as well.

Signed-off-by: Zack Cerza <zack.cerza@inktank.com>
teuthology/lock.py
teuthology/test/test_lock.py

index 74bea13b83781e280d3f13e33d79484622d68734..5db5408a13bd8c4518b19c123ccc7f237380f5f9 100644 (file)
@@ -21,6 +21,25 @@ from teuthology.misc import get_distro_version
 
 log = logging.getLogger(__name__)
 
+hostname_expr = '(?P<user>.*@)?(?P<shortname>.*)\.front\.sepia\.ceph\.com'
+
+
+def canonicalize_hostname(hostname, user='ubuntu'):
+    match = re.match(hostname_expr, hostname)
+    if match is None:
+        user_at = user + '@' if user else ''
+        hostname = '{user_at}{short}.front.sepia.ceph.com'.format(
+            user_at=user_at,
+            short=hostname)
+    return hostname
+
+
+def decanonicalize_hostname(hostname):
+    match = re.match(hostname_expr, hostname)
+    if match:
+        hostname = match.groupdict()['shortname']
+    return hostname
+
 
 def lock_many(ctx, num, machinetype, user=None, description=None):
     machinetypes = misc.get_multi_machine_types(machinetype)
@@ -133,12 +152,6 @@ def update_lock(ctx, name, description=None, status=None, sshpubkey=None):
     return True
 
 
-def canonicalize_hostname(s):
-    if re.match('ubuntu@.*\.front\.sepia\.ceph\.com', s) is None:
-        s = 'ubuntu@' + s + '.front.sepia.ceph.com'
-    return s
-
-
 def main(ctx):
     if ctx.verbose:
         teuthology.log.setLevel(logging.DEBUG)
@@ -416,12 +429,6 @@ def do_summary(ctx):
     print "{cnt:12d}  {up:3d}".format(cnt=total_count, up=total_up)
 
 
-def decanonicalize_hostname(s):
-    if re.match('ubuntu@.*\.front\.sepia\.ceph\.com', s):
-        s = s[len('ubuntu@'): -len('.front.sepia.ceph.com')]
-    return s
-
-
 def _get_downburst_exec():
     """
     First check for downburst in the user's path.
index b7ac33ae8abd6da71f720c0d98b3de88053b1e02..153083f9c991677cf8ba7aae0f0c887f5735ce23 100644 (file)
@@ -12,3 +12,13 @@ class TestLock(object):
         host = 'ubuntu@box1.front.sepia.ceph.com'
         result = lock.decanonicalize_hostname(host)
         assert result == 'box1'
+
+    def test_canonicalize_hostname_nouser(self):
+        host_base = 'box1'
+        result = lock.canonicalize_hostname(host_base, user=None)
+        assert result == 'box1.front.sepia.ceph.com'
+
+    def test_decanonicalize_hostname_nouser(self):
+        host = 'box1.front.sepia.ceph.com'
+        result = lock.decanonicalize_hostname(host)
+        assert result == 'box1'