From 31f108d91012049d5a1a3e316873d648676000d2 Mon Sep 17 00:00:00 2001 From: James Oakley Date: Sun, 15 Jun 2025 12:04:35 -0300 Subject: [PATCH] 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) --- src/cephadm/cephadm.py | 4 +++- src/cephadm/tests/test_cephadm.py | 10 ++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/cephadm/cephadm.py b/src/cephadm/cephadm.py index 5a6be98bcbd8d..f75aaa86dac68 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 8af15c2e883be..ddb98c4205767 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): -- 2.39.5