Modify handle_api_result so it can be used in more places

Some results have errors and no status, others have status and errors.
Update the function to return the final rc to exit with, and a bool
indicating whether or not to continue processing the other fields.

Add a bunch of tests for the new function to make sure I have the logic
correct.

(cherry picked from commit 35fa067219)
This commit is contained in:
Brian C. Lane 2018-07-25 17:30:50 -07:00
parent ac6f6f6a80
commit 85b2807a15
2 changed files with 64 additions and 7 deletions

View File

@ -58,17 +58,28 @@ def handle_api_result(result, show_json=False):
:param result: JSON result from the http query
:type result: dict
:rtype: tuple
:returns: (rc, errors)
Return the correct rc for the program (0 or 1), and whether or
not to continue processing the results.
"""
if show_json:
print(json.dumps(result, indent=4))
for err in result.get("errors", []):
log.error(err)
if result["status"] == True:
return 0
else:
return 1
for err in result.get("errors", []):
log.error(err)
# What's the rc? If status is present, use that
# If not, use length of errors
if "status" in result:
rc = bool(not result["status"])
else:
rc = bool(len(result.get("errors", [])) > 0)
# Caller should return if showing json, or status was present and False
exit_now = show_json or ("status" in result and rc)
return (rc, exit_now)
def packageNEVRA(pkg):
"""Return the package info as a NEVRA

View File

@ -17,6 +17,7 @@
import unittest
from composer.cli.utilities import argify, toml_filename, frozen_toml_filename, packageNEVRA
from composer.cli.utilities import handle_api_result
class CliUtilitiesTest(unittest.TestCase):
def test_argify(self):
@ -45,3 +46,48 @@ class CliUtilitiesTest(unittest.TestCase):
"version": "10.0"}
self.assertEqual(packageNEVRA(epoch_0), "basesystem-10.0-7.el7.noarch")
self.assertEqual(packageNEVRA(epoch_3), "basesystem-3:10.0-7.el7.noarch")
def test_api_result_1(self):
"""Test a result with no status and no error fields"""
result = {"foo": "bar"}
self.assertEqual(handle_api_result(result, show_json=False), (0, False))
def test_api_result_2(self):
"""Test a result with errors=["some error"], and no status field"""
result = {"foo": "bar", "errors": ["some error"]}
self.assertEqual(handle_api_result(result, show_json=False), (1, False))
def test_api_result_3(self):
"""Test a result with status=True, and errors=[]"""
result = {"status": True, "errors": []}
self.assertEqual(handle_api_result(result, show_json=False), (0, False))
def test_api_result_4(self):
"""Test a result with status=False, and errors=[]"""
result = {"status": False, "errors": []}
self.assertEqual(handle_api_result(result, show_json=False), (1, True))
def test_api_result_5(self):
"""Test a result with status=False, and errors=["some error"]"""
result = {"status": False, "errors": ["some error"]}
self.assertEqual(handle_api_result(result, show_json=False), (1, True))
def test_api_result_6(self):
"""Test a result with show_json=True, and no status or errors fields"""
result = {"foo": "bar"}
self.assertEqual(handle_api_result(result, show_json=True), (0, True))
def test_api_result_7(self):
"""Test a result with show_json=True, status=False, and errors=["some error"]"""
result = {"status": False, "errors": ["some error"]}
self.assertEqual(handle_api_result(result, show_json=True), (1, True))
def test_api_result_8(self):
"""Test a result with show_json=True, errors=["some error"], and no status field"""
result = {"foo": "bar", "errors": ["some error"]}
self.assertEqual(handle_api_result(result, show_json=True), (1, True))
def test_api_result_9(self):
"""Test a result with show_json=True, errors=[], and no status field"""
result = {"foo": "bar", "errors": []}
self.assertEqual(handle_api_result(result, show_json=True), (0, True))