]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-volume: fallback to default for empty get_file_contents values 68765/head
authorGuillaume Abrioux <gabrioux@ibm.com>
Wed, 6 May 2026 08:49:49 +0000 (10:49 +0200)
committerGuillaume Abrioux <gabrioux@ibm.com>
Wed, 6 May 2026 09:00:11 +0000 (11:00 +0200)
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 <gabrioux@ibm.com>
src/ceph-volume/ceph_volume/tests/util/test_system.py
src/ceph-volume/ceph_volume/util/system.py

index ec5c086b58206b580909c578a2012bccecbd6ce8..d5080b09deafa8bee6446f19bcac4ab034abe00a 100644 (file)
@@ -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()
index 4b44d31336cc567b3017487c80e69645f6270f7f..30d67b743ac7acf2e455c10f1d971aaed996312a 100644 (file)
@@ -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)