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

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