from ceph_volume.tests.conftest import Factory
-class TestCheckID(object):
+class TestOSDIDAvailable(object):
def test_false_if_id_is_none(self):
- assert not prepare.check_id(None)
+ assert not prepare.osd_id_available(None)
def test_returncode_is_not_zero(self, monkeypatch):
monkeypatch.setattr('ceph_volume.process.call', lambda *a, **kw: ('', '', 1))
with pytest.raises(RuntimeError):
- prepare.check_id(1)
+ prepare.osd_id_available(1)
- def test_id_does_exist(self, monkeypatch):
+ def test_id_does_exist_but_not_available(self, monkeypatch):
stdout = dict(nodes=[
- dict(id=0),
+ dict(id=0, status="up"),
])
stdout = ['', json.dumps(stdout)]
monkeypatch.setattr('ceph_volume.process.call', lambda *a, **kw: (stdout, '', 0))
- result = prepare.check_id(0)
- assert result
+ result = prepare.osd_id_available(0)
+ assert not result
def test_id_does_not_exist(self, monkeypatch):
stdout = dict(nodes=[
])
stdout = ['', json.dumps(stdout)]
monkeypatch.setattr('ceph_volume.process.call', lambda *a, **kw: (stdout, '', 0))
- result = prepare.check_id(1)
+ result = prepare.osd_id_available(1)
assert not result
def test_invalid_osd_id(self, monkeypatch):
])
stdout = ['', json.dumps(stdout)]
monkeypatch.setattr('ceph_volume.process.call', lambda *a, **kw: (stdout, '', 0))
- result = prepare.check_id("foo")
+ result = prepare.osd_id_available("foo")
assert not result
+ def test_returns_true_when_id_is_destroyed(self, monkeypatch):
+ stdout = dict(nodes=[
+ dict(id=0, status="destroyed"),
+ ])
+ stdout = ['', json.dumps(stdout)]
+ monkeypatch.setattr('ceph_volume.process.call', lambda *a, **kw: (stdout, '', 0))
+ result = prepare.osd_id_available(0)
+ assert result
+
class TestFormatDevice(object):
'-i', '-',
'osd', 'new', fsid
]
- if osd_id is not None and not osd_id_exists(osd_id):
- cmd.append(osd_id)
+ if osd_id is not None:
+ if osd_id_available(osd_id):
+ cmd.append(osd_id)
+ else:
+ raise RuntimeError("The osd ID {} is already in use or does not exist.".format(osd_id))
stdout, stderr, returncode = process.call(
cmd,
stdin=json_secrets,
return ' '.join(stdout).strip()
-def osd_id_exists(osd_id):
+def osd_id_available(osd_id):
"""
- Checks to see if an osd ID exists or not. Returns True
- if it does exist, False if it doesn't.
+ Checks to see if an osd ID exists and if it's available for
+ reuse. Returns True if it is, False if it isn't.
:param osd_id: The osd ID to check
"""
output = json.loads(''.join(stdout).strip())
osds = output['nodes']
- return any([str(osd['id']) == str(osd_id) for osd in osds])
+ osd = [osd for osd in osds if str(osd['id']) == str(osd_id)]
+ if osd and osd[0].get('status') == "destroyed":
+ return True
+ return False
def mount_tmpfs(path):