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

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