From cced7e5701beb6bf5f842ef08222f124cfaa980e Mon Sep 17 00:00:00 2001 From: Guillaume Abrioux Date: Wed, 6 May 2026 10:49:49 +0200 Subject: [PATCH] ceph-volume: fallback to default for empty get_file_contents values With this change, get_file_contents() returns the default when a file exists but reads as empty. This avoids repeating fallback checks in callers. Fixes: https://tracker.ceph.com/issues/76431 Signed-off-by: Guillaume Abrioux --- src/ceph-volume/ceph_volume/tests/util/test_system.py | 10 ++++++++++ src/ceph-volume/ceph_volume/util/system.py | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/ceph-volume/ceph_volume/tests/util/test_system.py b/src/ceph-volume/ceph_volume/tests/util/test_system.py index ec5c086b5820..d5080b09deaf 100644 --- a/src/ceph-volume/ceph_volume/tests/util/test_system.py +++ b/src/ceph-volume/ceph_volume/tests/util/test_system.py @@ -208,6 +208,16 @@ class TestGetFileContents(object): result = system.get_file_contents(interesting_file.path) assert result == "0\n1" + def test_path_empty_returns_default(self, fake_filesystem): + interesting_file = fake_filesystem.create_file('/tmp/fake-file', contents="") + result = system.get_file_contents(interesting_file.path, 'default') + assert result == 'default' + + def test_path_whitespace_returns_default(self, fake_filesystem): + interesting_file = fake_filesystem.create_file('/tmp/fake-file', contents=" \n\t") + result = system.get_file_contents(interesting_file.path, 'default') + assert result == 'default' + def test_exception_returns_default(self): with patch('builtins.open') as mocked_open: mocked_open.side_effect = Exception() diff --git a/src/ceph-volume/ceph_volume/util/system.py b/src/ceph-volume/ceph_volume/util/system.py index 4b44d31336cc..30d67b743ac7 100644 --- a/src/ceph-volume/ceph_volume/util/system.py +++ b/src/ceph-volume/ceph_volume/util/system.py @@ -122,7 +122,7 @@ def get_file_contents(path, default=''): return contents try: with open(path, 'r') as open_file: - contents = open_file.read().strip() + contents = open_file.read().strip() or default except Exception: logger.exception('Failed to read contents from: %s' % path) -- 2.47.3