diff --git a/src/krkn_lib/tests/test_utils.py b/src/krkn_lib/tests/test_utils.py index 3fe04511..7e3a4782 100644 --- a/src/krkn_lib/tests/test_utils.py +++ b/src/krkn_lib/tests/test_utils.py @@ -17,6 +17,7 @@ get_random_string, get_yaml_item_value, is_host_reachable, + get_junit_test_case, ) @@ -323,3 +324,66 @@ def test_is_host_reachable(self): self.assertFalse( is_host_reachable(f"{get_random_string(10)}.com", 12345) ) + + def test_get_junit_test_case(self): + test_suite_name = "krkn-test" + test_case_description_success = ( + "success [test-case] krkn-lib test case" + ) + test_case_description_failure = ( + "failure [test-case] krkn-lib test case" + ) + test_stdout = "KRKN STDOUT" + test_version = "OCP 4.16" + time = 10 + success_output = ( + f'' # NOQA + f'' + f'' # NOQA + f"" + ) + + success_output_not_test_version = ( + f'' # NOQA + f'' # NOQA + f"" + ) + + failure_output = ( + f'' # NOQA + f'' + f'' + f'{test_stdout}' + f"" + f"" + ) + success_test = get_junit_test_case( + True, + time, + test_suite_name, + test_case_description_success, + test_stdout, + test_version, + ) + success_test_not_test_version = get_junit_test_case( + True, + time, + test_suite_name, + test_case_description_success, + test_stdout, + ) + + failure_test = get_junit_test_case( + False, + time, + test_suite_name, + test_case_description_failure, + test_stdout, + test_version, + ) + + self.assertEqual(success_output, success_test) + self.assertEqual( + success_output_not_test_version, success_test_not_test_version + ) + self.assertEqual(failure_output, failure_test) diff --git a/src/krkn_lib/utils/functions.py b/src/krkn_lib/utils/functions.py index 7033c0c7..66d3c222 100644 --- a/src/krkn_lib/utils/functions.py +++ b/src/krkn_lib/utils/functions.py @@ -13,6 +13,7 @@ from base64io import Base64IO from dateutil import parser from dateutil.parser import ParserError +import xml.etree.cElementTree as ET def decode_base64_file(source_filename: str, destination_filename: str): @@ -380,3 +381,48 @@ def is_host_reachable(host: str, port: int, timeout: int = 2) -> bool: return True except OSError: return False + + +def get_junit_test_case( + success: bool, + time: int, + test_suite_name: str, + test_case_description: str, + test_stdout: str = "", + test_version: str = None, +) -> str: + """ + Creates an XML compatible with sippy to track regressions + on OCP tests. + + :param success: if true will print a success test case, + otherwise a failure test case + :param time: sets the duration in seconds of the testsuite + :param test_suite_name: sets the name of the test suite + :param test_case_description: sets the description of + the testcase, it has to contain tags that map the test case + to the monitored component on sippy like [sig-etcd] + or others + :param test_stdout: if a test failes the stdout of the krkn-run + is attached to the testcase element in the XML + :param test_version: sets an optional property to the testsuite + containing the version on the test + :return: the XML string to be written in the junit xml file + """ + + root = ET.Element("testsuite") + root.attrib["name"] = test_suite_name + root.attrib["tests"] = "1" + root.attrib["skipped"] = "0" + root.attrib["failures"] = "0" if success else "1" + root.attrib["time"] = f"{time}" + if test_version: + ET.SubElement(root, "property", name="TestVersion", value=test_version) + + test_case = ET.SubElement( + root, "testcase", name=test_case_description, time=f"{time}" + ) + if not success: + ET.SubElement(test_case, "failure", message="").text = test_stdout + + return ET.tostring(root, encoding="utf-8").decode("UTF-8")