Blame SOURCES/0012-Handle-files-that-don-t-exist-in-FileCheck.patch

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