Skip to content

Commit 7193ada

Browse files
committed
ceph-volume: Refactor is_ceph_device to simplify error handling
Replace the try-except block with a direct get() call on lv.tags to check for ceph.osd_id. This avoids catching AttributeError unnecessarily and makes the logic more concise. Additionally, the warning message is now logged when osd_id is None or 'null', ensuring consistency in error handling. Also, this fixes the unit test `api/test_lvm.py::TestVolume::test_is_not_ceph_device()` as `api.lvm.is_ceph_device()` expects a `Volume` object. Signed-off-by: Guillaume Abrioux <[email protected]>
1 parent fc08540 commit 7193ada

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

src/ceph-volume/ceph_volume/api/lvm.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -322,16 +322,13 @@ def dmsetup_splitname(dev: str) -> Dict[str, Any]:
322322

323323

324324
def is_ceph_device(lv: "Volume") -> bool:
325-
try:
326-
lv.tags['ceph.osd_id']
327-
except (KeyError, AttributeError):
328-
logger.warning('device is not part of ceph: %s', lv)
329-
return False
325+
osd_id = lv.tags.get('ceph.osd_id', 'null')
330326

331-
if lv.tags['ceph.osd_id'] == 'null':
332-
return False
333-
else:
334-
return True
327+
if osd_id == 'null':
328+
logger.warning('device is not part of ceph: %s', lv)
329+
return False
330+
331+
return True
335332

336333
class Lvm:
337334
def __init__(self, name_key: str, tags_key: str, **kw: Any) -> None:
@@ -907,6 +904,7 @@ def __init__(self, **kw: Any) -> None:
907904
self.lv_uuid: str = ''
908905
self.vg_name: str = ''
909906
self.lv_size: str = ''
907+
self.tags: Dict[str, Any] = {}
910908
self.lv_tags: Dict[str, Any] = {}
911909
super().__init__('lv_name', 'lv_tags', **kw)
912910
self.lv_api = kw

src/ceph-volume/ceph_volume/tests/api/test_lvm.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,20 @@ def test_multiple_csv_expands_in_dict(self):
3131
assert result['ceph.fsid'] == '0000'
3232

3333

34-
class TestVolume(object):
35-
34+
class TestVolume:
3635
def test_is_ceph_device(self):
3736
lv_tags = "ceph.type=data,ceph.osd_id=0"
3837
osd = api.Volume(lv_name='osd/volume', lv_tags=lv_tags)
3938
assert api.is_ceph_device(osd)
4039

41-
@pytest.mark.parametrize('dev',[
42-
'/dev/sdb',
43-
api.VolumeGroup(vg_name='foo'),
44-
api.Volume(lv_name='vg/no_osd', lv_tags='', lv_path='lv/path'),
45-
api.Volume(lv_name='vg/no_osd', lv_tags='ceph.osd_id=null', lv_path='lv/path'),
46-
None,
47-
])
40+
@pytest.mark.parametrize('dev',
41+
[api.VolumeGroup(vg_name='foo'),
42+
api.Volume(lv_name='vg/no_osd',
43+
lv_tags='',
44+
lv_path='lv/path'),
45+
api.Volume(lv_name='vg/no_osd',
46+
lv_tags='ceph.osd_id=null',
47+
lv_path='lv/path')])
4848
def test_is_not_ceph_device(self, dev):
4949
assert not api.is_ceph_device(dev)
5050

src/ceph-volume/ceph_volume/tests/util/test_device.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_lvm_size_rounds_down(self, fake_call, device_info):
5050

5151
def test_is_lv(self, fake_call, device_info, monkeypatch):
5252
monkeypatch.setattr('ceph_volume.util.device.Device.is_lv', lambda: True)
53-
data = {"lv_path": "vg/lv", "vg_name": "vg", "name": "lv"}
53+
data = {"lv_path": "vg/lv", "vg_name": "vg", "name": "lv", "tags": {}}
5454
lsblk = {"TYPE": "lvm", "NAME": "vg-lv"}
5555
device_info(lv=data,lsblk=lsblk)
5656
disk = device.Device("vg/lv")
@@ -322,7 +322,7 @@ def test_reject_lv_symlink_to_device(self,
322322
fake_call):
323323
m_os_path_islink.return_value = True
324324
m_os_path_realpath.return_value = '/dev/mapper/vg-lv'
325-
lv = {"lv_path": "/dev/vg/lv", "vg_name": "vg", "name": "lv"}
325+
lv = {"lv_path": "/dev/vg/lv", "vg_name": "vg", "name": "lv", "tags": {}}
326326
lsblk = {"TYPE": "lvm", "NAME": "vg-lv"}
327327
device_info(lv=lv,lsblk=lsblk)
328328
disk = device.Device("/dev/vg/lv")

0 commit comments

Comments
 (0)