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

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