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

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