You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ipa-healthcheck/SOURCES/0002-Handle-files-that-don-...

64 lines
2.4 KiB

From 7db48931c9b3045406e465abb4d2a21beaadfcc4 Mon Sep 17 00:00:00 2001
From: Rob Crittenden <rcritten@redhat.com>
Date: Mon, 14 Jun 2021 08:44:39 -0400
Subject: [PATCH] Handle files that don't exist in FileCheck
A raw os.stat() was called which could raise an exception if the file
doesn't exist. Instead call os.path.exists() and if the result is False
then raise a SUCCESS with a message that the file doesn't exist.
https://github.com/freeipa/freeipa-healthcheck/issues/213
Signed-off-by: Rob Crittenden <rcritten@redhat.com>
---
src/ipahealthcheck/core/files.py | 7 +++++++
tests/test_core_files.py | 17 +++++++++++++++++
2 files changed, 24 insertions(+)
diff --git a/src/ipahealthcheck/core/files.py b/src/ipahealthcheck/core/files.py
index a63c06b..0a7641e 100644
--- a/src/ipahealthcheck/core/files.py
+++ b/src/ipahealthcheck/core/files.py
@@ -33,6 +33,13 @@ class FileCheck:
owner = tuple((owner,))
if not isinstance(group, tuple):
group = tuple((group,))
+ if not os.path.exists(path):
+ for type in ('mode', 'owner', 'group'):
+ key = '%s_%s' % (path.replace('/', '_'), type)
+ yield Result(self, constants.SUCCESS, key=key,
+ type=type, path=path,
+ msg='File does not exist')
+ continue
stat = os.stat(path)
fmode = str(oct(stat.st_mode)[-4:])
key = '%s_mode' % path.replace('/', '_')
diff --git a/tests/test_core_files.py b/tests/test_core_files.py
index 3b71469..10824ab 100644
--- a/tests/test_core_files.py
+++ b/tests/test_core_files.py
@@ -144,3 +144,20 @@ def test_files_mode(mock_stat):
my_results = get_results(results, 'mode')
assert my_results.results[0].result == constants.WARNING
assert my_results.results[1].result == constants.WARNING
+
+
+@patch('os.path.exists')
+def test_files_not_found(mock_exists):
+ mock_exists.return_value = False
+
+ f = FileCheck()
+ f.files = files
+
+ results = capture_results(f)
+
+ for type in ('mode', 'group', 'owner'):
+ my_results = get_results(results, type)
+ assert len(my_results.results) == 4
+ for result in my_results.results:
+ assert result.result == constants.SUCCESS
+ assert result.kw.get('msg') == 'File does not exist'
--
2.26.3