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

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