]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-volume: make systemctl.get_running_osds resilient to garbage output 29631/head
authorAndrew Schoen <aschoen@redhat.com>
Fri, 8 Mar 2019 15:53:08 +0000 (09:53 -0600)
committerJan Fajerski <jfajerski@suse.com>
Tue, 13 Aug 2019 07:23:27 +0000 (09:23 +0200)
Makes systemd.systemctl.get_running_osds more resilient to output
that doesn't match what we're expecting to parse. This also handles
the case of a nonzero exit status by retuning an empty list.

Signed-off-by: Andrew Schoen <aschoen@redhat.com>
(cherry picked from commit f3707f1ad43513c2a6df17e9dce756533c3b4a1d)

src/ceph-volume/ceph_volume/systemd/systemctl.py
src/ceph-volume/ceph_volume/tests/systemd/test_systemctl.py

index 280ddada7e9983d05212e8f699dd2240d4e365f3..778ad14791720a3c19a043885faeb7312608b154 100644 (file)
@@ -1,8 +1,11 @@
 """
 Utilities to control systemd units
 """
+import logging
+
 from ceph_volume import process
 
+logger = logging.getLogger(__name__)
 
 def start(unit):
     process.run(['systemctl', 'start', unit])
@@ -44,11 +47,15 @@ def get_running_osd_ids():
         'ceph-osd@*',
     ])
     osd_ids = []
-    for line in out:
-        if line:
-            # example line looks like: Id=ceph-osd@1.service
-            osd_id = line.split("@")[1].split(".service")[0]
-            osd_ids.append(osd_id)
+    if rc == 0:
+        for line in out:
+            if line:
+                # example line looks like: Id=ceph-osd@1.service
+                try:
+                    osd_id = line.split("@")[1].split(".service")[0]
+                    osd_ids.append(osd_id)
+                except (IndexError, TypeError):
+                    logger.warning("Failed to parse output from systemctl: %s", line)
     return osd_ids
 
 def start_osd(id_):
index 76c8618a5bf4b15aa1c8877c3c5f6a2422f85b2a..8eec4a3d44160f78c51a1b744fdcb0c7e435ab50 100644 (file)
@@ -1,9 +1,21 @@
+import pytest
 from ceph_volume.systemd import systemctl
 
 class TestSystemctl(object):
 
-    def test_get_running_osd_ids(self, stub_call):
-        stdout = ['Id=ceph-osd@1.service', '', 'Id=ceph-osd@2.service']
+    @pytest.mark.parametrize("stdout,expected", [
+        (['Id=ceph-osd@1.service', '', 'Id=ceph-osd@2.service'], ['1','2']),
+        (['Id=ceph-osd1.service',], []),
+        (['Id=ceph-osd@1'], ['1']),
+        ([], []),
+    ])
+    def test_get_running_osd_ids(self, stub_call, stdout, expected):
         stub_call((stdout, [], 0))
         osd_ids = systemctl.get_running_osd_ids()
-        assert osd_ids == ['1', '2']
+        assert osd_ids == expected
+
+    def test_returns_empty_list_on_nonzero_return_code(self, stub_call):
+        stdout = ['Id=ceph-osd@1.service', '', 'Id=ceph-osd@2.service']
+        stub_call((stdout, [], 1))
+        osd_ids = systemctl.get_running_osd_ids()
+        assert osd_ids == []