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