From 121fd02671c55872218baba74a137a74684a46f6 Mon Sep 17 00:00:00 2001 From: Zack Cerza Date: Mon, 19 May 2014 17:40:11 -0500 Subject: [PATCH] Make (de)canonicalize_hostname() work without user Add tests proving they still work *with* usernames as well. Signed-off-by: Zack Cerza --- teuthology/lock.py | 31 +++++++++++++++++++------------ teuthology/test/test_lock.py | 10 ++++++++++ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/teuthology/lock.py b/teuthology/lock.py index 74bea13b83..5db5408a13 100644 --- a/teuthology/lock.py +++ b/teuthology/lock.py @@ -21,6 +21,25 @@ from teuthology.misc import get_distro_version log = logging.getLogger(__name__) +hostname_expr = '(?P.*@)?(?P.*)\.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. diff --git a/teuthology/test/test_lock.py b/teuthology/test/test_lock.py index b7ac33ae8a..153083f9c9 100644 --- a/teuthology/test/test_lock.py +++ b/teuthology/test/test_lock.py @@ -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' -- 2.39.5