Skip to content

Commit e619aee

Browse files
fernflowerDmitry Shibut
authored andcommitted
Handle possible failures of xfs_info command
In case the xfs_info command fails for any reason, xfsinfoscanner would crash. By no reason this should cause leapp to exit with a traceback. An additional vetting if the directory is an actual mount point has been added together with proper exception handling. Unit tests also added. OAMG-8147 BZ#2155661 - leapp xfs_info_scanner fails when fstab xfs entry not mounted
1 parent c18549c commit e619aee

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

repos/system_upgrade/common/actors/xfsinfoscanner/libraries/xfsinfoscanner.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from leapp.libraries.stdlib import api, run, CalledProcessError
1+
import os
2+
3+
from leapp.libraries.stdlib import api, CalledProcessError, run
24
from leapp.models import StorageInfo, XFSPresence
35

46

@@ -21,18 +23,21 @@ def scan_xfs_mount(data):
2123

2224

2325
def is_xfs_without_ftype(mp):
24-
try:
25-
for l in run(['/usr/sbin/xfs_info', '{}'.format(mp)], split=True)['stdout']:
26-
if 'ftype=0' in l:
27-
return True
26+
if not os.path.ismount(mp):
27+
# Check if mp is actually a mountpoint
28+
api.current_logger().warning('{} is not mounted'.format(mp))
2829
return False
29-
# xfs_info can sometimes throw errors like the following if fed a CageFS mountpoint.
30-
# xfs_info: /usr/share/cagefs-skeleton/var/www/cgi-bin\040(deleted) is not a mounted XFS filesystem
30+
try:
31+
xfs_info = run(['/usr/sbin/xfs_info', '{}'.format(mp)], split=True)
3132
except CalledProcessError as err:
32-
if "cagefs" in mp:
33-
api.current_logger().info("CageFS XFS mountpoint {} ignored in scanner".format(mp))
34-
return False
35-
raise err
33+
api.current_logger().warning('Error during command execution: {}'.format(err))
34+
return False
35+
36+
for l in xfs_info['stdout']:
37+
if 'ftype=0' in l:
38+
return True
39+
40+
return False
3641

3742

3843
def scan_xfs():

repos/system_upgrade/common/actors/xfsinfoscanner/tests/unit_test_xfsinfoscanner.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import os
2+
13
from leapp.libraries.actor import xfsinfoscanner
24
from leapp.libraries.common.testutils import produce_mocked
3-
from leapp.libraries.stdlib import api
4-
from leapp.models import StorageInfo, FstabEntry, MountEntry, SystemdMountEntry, XFSPresence
5+
from leapp.libraries.stdlib import api, CalledProcessError
6+
from leapp.models import FstabEntry, MountEntry, StorageInfo, SystemdMountEntry, XFSPresence
57

68

79
class run_mocked(object):
@@ -87,6 +89,7 @@ def test_scan_xfs_mount(monkeypatch):
8789

8890
def test_is_xfs_without_ftype(monkeypatch):
8991
monkeypatch.setattr(xfsinfoscanner, "run", run_mocked())
92+
monkeypatch.setattr(os.path, "ismount", lambda _: True)
9093

9194
assert xfsinfoscanner.is_xfs_without_ftype("/var")
9295
assert ' '.join(xfsinfoscanner.run.args) == "/usr/sbin/xfs_info /var"
@@ -95,8 +98,22 @@ def test_is_xfs_without_ftype(monkeypatch):
9598
assert ' '.join(xfsinfoscanner.run.args) == "/usr/sbin/xfs_info /boot"
9699

97100

101+
def test_is_xfs_command_failed(monkeypatch):
102+
def _run_mocked_exception(*args, **kwargs):
103+
raise CalledProcessError(message="No such file or directory", command=["xfs_info", "/nosuchmountpoint"],
104+
result=1)
105+
# not a mountpoint
106+
monkeypatch.setattr(os.path, "ismount", lambda _: False)
107+
monkeypatch.setattr(xfsinfoscanner, "run", _run_mocked_exception)
108+
assert not xfsinfoscanner.is_xfs_without_ftype("/nosuchmountpoint")
109+
# a real mountpoint but something else caused command to fail
110+
monkeypatch.setattr(os.path, "ismount", lambda _: True)
111+
assert not xfsinfoscanner.is_xfs_without_ftype("/nosuchmountpoint")
112+
113+
98114
def test_scan_xfs(monkeypatch):
99115
monkeypatch.setattr(xfsinfoscanner, "run", run_mocked())
116+
monkeypatch.setattr(os.path, "ismount", lambda _: True)
100117

101118
def consume_no_xfs_message_mocked(*models):
102119
yield StorageInfo()

0 commit comments

Comments
 (0)