From c797654d3bb833d5bae4d84bc96669c1ce59fedb Mon Sep 17 00:00:00 2001 From: Roman Grigorev Date: Wed, 16 Sep 2020 07:34:50 +0200 Subject: [PATCH] Makes (de)canonicalize_hostname ready to empty domains 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 --- teuthology/misc.py | 16 ++++++++++------ teuthology/test/test_misc.py | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/teuthology/misc.py b/teuthology/misc.py index 547709fd64..23e09069c2 100644 --- a/teuthology/misc.py +++ b/teuthology/misc.py @@ -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.*@)?(?P.*)\.{lab_domain}' +hostname_expr_templ = '(?P.*@)?(?P.*){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'] diff --git a/teuthology/test/test_misc.py b/teuthology/test/test_misc.py index 8756a3df08..9b8fe90535 100644 --- a/teuthology/test/test_misc.py +++ b/teuthology/test/test_misc.py @@ -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 """ -- 2.39.5