From 327615028d17dba8b168d0e96d274d8581c0f1f8 Mon Sep 17 00:00:00 2001 From: Josh Durgin Date: Tue, 15 Mar 2016 14:40:05 -0700 Subject: [PATCH] misc: add helpers to get components of a role Signed-off-by: Josh Durgin --- teuthology/misc.py | 20 ++++++++++++++++++++ teuthology/test/test_misc.py | 12 ++++++++++++ 2 files changed, 32 insertions(+) diff --git a/teuthology/misc.py b/teuthology/misc.py index 2df4b1fd3f..59077f00ff 100644 --- a/teuthology/misc.py +++ b/teuthology/misc.py @@ -346,6 +346,26 @@ def skeleton_config(ctx, roles, ips): return conf +def ceph_role(role): + """ + Return the ceph name for the role, without any cluster prefix, e.g. osd.0. + """ + _, type_, id_ = split_role(role) + return type_ + '.' + id_ + + +def split_role(role): + """ + Return a tuple of cluster, type, and id + If no cluster is included in the role, the default cluster, 'ceph', is used + """ + cluster = 'ceph' + if role.count('.') > 1: + cluster, role = role.split('.', 1) + type_, id_ = role.split('.', 1) + return cluster, type_, id_ + + def roles_of_type(roles_for_host, type_): """ Generator of roles. diff --git a/teuthology/test/test_misc.py b/teuthology/test/test_misc.py index 6b392bfbe2..9476af4df0 100644 --- a/teuthology/test/test_misc.py +++ b/teuthology/test/test_misc.py @@ -149,6 +149,18 @@ def test_get_mons(): 'ceph.mon.c': ips[2] + ':6789'} +def test_split_role(): + expected = { + 'client.0': ('ceph', 'client', '0'), + 'foo.client.0': ('foo', 'client', '0'), + 'bar.baz.x.y.z': ('bar', 'baz', 'x.y.z'), + 'mds.a-s-b': ('ceph', 'mds', 'a-s-b'), + } + + for role, expected_split in expected.items(): + actual_split = misc.split_role(role) + assert actual_split == expected_split + class TestHostnames(object): def setup(self): config._conf = dict()