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

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