From: James Oakley Date: Sun, 15 Jun 2025 15:04:35 +0000 (-0300) Subject: cephadm: Fix get_cluster_count when data_dir is missing X-Git-Tag: v20.1.0~49^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=refs%2Fpull%2F64510%2Fhead;p=ceph.git cephadm: Fix get_cluster_count when data_dir is missing It is possible for cephadm, if it fails to create a cluster, to direct the user to delete the cluster even though the data_dir has not yet been created. Running that command would fail during the cluster count check. Signed-off-by: James Oakley (cherry picked from commit 8e39f8911c4a485975d6adb04b04893e43128e67) --- diff --git a/src/cephadm/cephadm.py b/src/cephadm/cephadm.py index 5a6be98bcbd8..f75aaa86dac6 100755 --- a/src/cephadm/cephadm.py +++ b/src/cephadm/cephadm.py @@ -3958,7 +3958,9 @@ def command_zap_osds(ctx: CephadmContext) -> None: def get_ceph_cluster_count(ctx: CephadmContext) -> int: - return len([c for c in os.listdir(ctx.data_dir) if is_fsid(c)]) + if os.path.isdir(ctx.data_dir): + return len([c for c in os.listdir(ctx.data_dir) if is_fsid(c)]) + return 0 def command_rm_cluster(ctx: CephadmContext) -> None: diff --git a/src/cephadm/tests/test_cephadm.py b/src/cephadm/tests/test_cephadm.py index 8af15c2e883b..ddb98c420576 100644 --- a/src/cephadm/tests/test_cephadm.py +++ b/src/cephadm/tests/test_cephadm.py @@ -1501,8 +1501,14 @@ ff792c06d8544b983.scope not found.: OCI runtime error""" ]) def test_get_ceph_cluster_count(self, test_input, expected): ctx = _cephadm.CephadmContext() - with mock.patch('os.listdir', return_value=test_input): - assert _cephadm.get_ceph_cluster_count(ctx) == expected + with mock.patch('os.path.isdir', return_value=True): + with mock.patch('os.listdir', return_value=test_input): + assert _cephadm.get_ceph_cluster_count(ctx) == expected + + def test_get_ceph_cluster_count_missing_datadir(self): + ctx = _cephadm.CephadmContext() + with mock.patch('os.path.isdir', return_value=False): + assert _cephadm.get_ceph_cluster_count(ctx) == 0 def test_set_image_minimize_config(self): def throw_cmd(cmd):