]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
misc: make get_mon_names() and get_first_mon() cluster-aware
authorJosh Durgin <jdurgin@redhat.com>
Wed, 16 Mar 2016 04:36:45 +0000 (21:36 -0700)
committerJosh Durgin <jdurgin@redhat.com>
Mon, 11 Apr 2016 21:36:41 +0000 (14:36 -0700)
Signed-off-by: Josh Durgin <jdurgin@redhat.com>
teuthology/misc.py
teuthology/test/test_misc.py

index 694fd7d9a63405054d8f8bd0145d5704ae396e38..f10991930489febf1f7b9fb8290bc87e0d739436 100644 (file)
@@ -1024,26 +1024,24 @@ def get_user():
     return getpass.getuser() + '@' + socket.gethostname()
 
 
-def get_mon_names(ctx):
+def get_mon_names(ctx, cluster='ceph'):
     """
     :returns: a list of monitor names
     """
-    mons = []
-    for remote, roles in ctx.cluster.remotes.items():
-        for role in roles:
-            if not role.startswith('mon.'):
-                continue
-            mons.append(role)
-    return mons
+    is_mon = is_type('mon', cluster)
+    host_mons = [[role for role in roles if is_mon(role)]
+                 for roles in ctx.cluster.remotes.values()]
+    return [mon for mons in host_mons for mon in mons]
 
 
-def get_first_mon(ctx, config):
+def get_first_mon(ctx, config, cluster='ceph'):
     """
-    return the "first" mon (alphanumerically, for lack of anything better)
+    return the "first" mon role (alphanumerically, for lack of anything better)
     """
-    firstmon = sorted(get_mon_names(ctx))[0]
-    assert firstmon
-    return firstmon
+    mons = get_mon_names(ctx, cluster)
+    if mons:
+        return sorted(mons)[0]
+    assert False, 'no mon for cluster found'
 
 
 def replace_all_with_clients(cluster, config):
index a8ae63af848777942d6deb2652feb37025f29390..80f18915d4e0991c0e3208607c853e41d06beeeb 100644 (file)
@@ -89,6 +89,38 @@ def test_get_clients_simple():
         next(g)
 
 
+def test_get_mon_names():
+    expected = [
+        ([['mon.a', 'osd.0', 'mon.c']], 'ceph', ['mon.a', 'mon.c']),
+        ([['ceph.mon.a', 'osd.0', 'ceph.mon.c']], 'ceph', ['ceph.mon.a', 'ceph.mon.c']),
+        ([['mon.a', 'osd.0', 'mon.c'], ['ceph.mon.b']], 'ceph', ['mon.a', 'mon.c', 'ceph.mon.b']),
+        ([['mon.a', 'osd.0', 'mon.c'], ['foo.mon.a']], 'ceph', ['mon.a', 'mon.c']),
+        ([['mon.a', 'osd.0', 'mon.c'], ['foo.mon.a']], 'foo', ['foo.mon.a']),
+    ]
+    for remote_roles, cluster_name, expected_mons in expected:
+        ctx = argparse.Namespace()
+        ctx.cluster = Mock()
+        ctx.cluster.remotes = {i: roles for i, roles in enumerate(remote_roles)}
+        mons = misc.get_mon_names(ctx, cluster_name)
+        assert expected_mons == mons
+
+
+def test_get_first_mon():
+    expected = [
+        ([['mon.a', 'osd.0', 'mon.c']], 'ceph', 'mon.a'),
+        ([['ceph.mon.a', 'osd.0', 'ceph.mon.c']], 'ceph', 'ceph.mon.a'),
+        ([['mon.a', 'osd.0', 'mon.c'], ['ceph.mon.b']], 'ceph', 'ceph.mon.b'),
+        ([['mon.a', 'osd.0', 'mon.c'], ['foo.mon.a']], 'ceph', 'mon.a'),
+        ([['foo.mon.b', 'osd.0', 'mon.c'], ['foo.mon.a']], 'foo', 'foo.mon.a'),
+    ]
+    for remote_roles, cluster_name, expected_mon in expected:
+        ctx = argparse.Namespace()
+        ctx.cluster = Mock()
+        ctx.cluster.remotes = {i: roles for i, roles in enumerate(remote_roles)}
+        mon = misc.get_first_mon(ctx, None, cluster_name)
+        assert expected_mon == mon
+
+
 def test_roles_of_type():
     expected = [
         (['client.0', 'osd.0', 'ceph.osd.1'], 'osd', ['0', '1']),