"""
Utilities to control systemd units
"""
+import logging
+
from ceph_volume import process
+logger = logging.getLogger(__name__)
def start(unit):
process.run(['systemctl', 'start', unit])
'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_):
+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 == []