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

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