Blame SOURCES/0004-result-names-are-not-translated-when-reading-input-f.patch

db8a92
From 5efeafa16a893cb6277ece4d573184bb64ee2744 Mon Sep 17 00:00:00 2001
db8a92
From: Rob Crittenden <rcritten@redhat.com>
db8a92
Date: Mon, 30 Nov 2020 15:06:03 -0500
db8a92
Subject: [PATCH] result names are not translated when reading input from json
db8a92
 file
db8a92
db8a92
The strings were being retained so when processing the results to
db8a92
determine the return code it was always a 1 because none of
db8a92
the values were being translated. It was always comparing
db8a92
the string like 'SUCCESS' to constants.SUCCESS which is 0.
db8a92
db8a92
https://bugzilla.redhat.com/show_bug.cgi?id=1866558
db8a92
---
db8a92
 src/ipahealthcheck/core/constants.py | 20 +++++++++++++++++++-
db8a92
 src/ipahealthcheck/core/plugin.py    |  4 ++--
db8a92
 tests/test_results.py                |  8 ++++++++
db8a92
 3 files changed, 29 insertions(+), 3 deletions(-)
db8a92
db8a92
diff --git a/src/ipahealthcheck/core/constants.py b/src/ipahealthcheck/core/constants.py
db8a92
index a55469c..b6ee029 100644
db8a92
--- a/src/ipahealthcheck/core/constants.py
db8a92
+++ b/src/ipahealthcheck/core/constants.py
db8a92
@@ -36,7 +36,25 @@ def getLevelName(level):
db8a92
     is passed in instead the corresponding string representation is
db8a92
     returned.
db8a92
     """
db8a92
-    return _levelToName.get(level) or _nameToLevel.get(level) or level
db8a92
+    name = _levelToName.get(level) or _nameToLevel.get(level)
db8a92
+    if name is not None:
db8a92
+        return name
db8a92
+
db8a92
+    return level
db8a92
+
db8a92
+
db8a92
+def getLevel(name):
db8a92
+    """
db8a92
+    Translate between level text and their numeric constants
db8a92
+
db8a92
+    If the level is one of the predefined levels then returns the
db8a92
+    corresponding number.
db8a92
+    """
db8a92
+    level = _nameToLevel.get(name)
db8a92
+    if level is not None:
db8a92
+        return level
db8a92
+
db8a92
+    return name
db8a92
 
db8a92
 
db8a92
 CONFIG_FILE = '/etc/ipahealthcheck/ipahealthcheck.conf'
db8a92
diff --git a/src/ipahealthcheck/core/plugin.py b/src/ipahealthcheck/core/plugin.py
db8a92
index 7ac923a..26dddd4 100644
db8a92
--- a/src/ipahealthcheck/core/plugin.py
db8a92
+++ b/src/ipahealthcheck/core/plugin.py
db8a92
@@ -6,7 +6,7 @@ import uuid
db8a92
 from datetime import datetime
db8a92
 from functools import wraps
db8a92
 
db8a92
-from ipahealthcheck.core.constants import getLevelName
db8a92
+from ipahealthcheck.core.constants import getLevelName, getLevel
db8a92
 
db8a92
 
db8a92
 def duration(f):
db8a92
@@ -204,7 +204,7 @@ def json_to_results(data):
db8a92
     results = Results()
db8a92
 
db8a92
     for line in data:
db8a92
-        result = line.pop('result')
db8a92
+        result = getLevel(line.pop('result'))
db8a92
         source = line.pop('source')
db8a92
         check = line.pop('check')
db8a92
         duration = line.pop('duration')
db8a92
diff --git a/tests/test_results.py b/tests/test_results.py
db8a92
index dd6e8fd..99c18d7 100644
db8a92
--- a/tests/test_results.py
db8a92
+++ b/tests/test_results.py
db8a92
@@ -69,3 +69,11 @@ def test_Result():
db8a92
         assert x['result'] in (constants.getLevelName(constants.SUCCESS),
db8a92
                                constants.getLevelName(constants.CRITICAL))
db8a92
         assert len(x['kw']) == 0
db8a92
+
db8a92
+
db8a92
+def test_getLevel():
db8a92
+    assert constants.getLevel('SUCCESS') == constants.SUCCESS
db8a92
+    assert constants.getLevel('WARNING') == constants.WARNING
db8a92
+    assert constants.getLevel('ERROR') == constants.ERROR
db8a92
+    assert constants.getLevel('CRITICAL') == constants.CRITICAL
db8a92
+    assert constants.getLevel('FOO') == 'FOO'
db8a92
-- 
db8a92
2.25.4
db8a92