-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add create xml results file to validations role
- Loading branch information
1 parent
484b633
commit 86794c3
Showing
4 changed files
with
152 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
roles/validations/filter_plugins/cifmw_validations_xml_filter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#!/usr/bin/python3 | ||
|
||
__metaclass__ = type | ||
|
||
DOCUMENTATION = """ | ||
name: cifmw_validations_xml_filter | ||
short_description: Maps the internal results structure to a JUnit XML string. | ||
description: | ||
- Maps the internal results structure to a JUnit XML string. | ||
options: | ||
_input: | ||
description: | ||
- The internal role test results. | ||
type: dict | ||
required: true | ||
""" | ||
|
||
EXAMPLES = """ | ||
- name: Define data to work on in the examples below | ||
vars: | ||
_internal_results: | ||
test-1: | ||
time: 2.54512 | ||
test-case-2: | ||
time: 4.5450345 | ||
error: "error message" | ||
ansible.builtin.set_fact: | ||
_xml_string: >- | ||
{{ | ||
_internal_results | cifmw_validations_xml_filter | ||
}} | ||
""" | ||
|
||
RETURN = """ | ||
_value: | ||
description: The translated JUnit XML string. | ||
type: string | ||
sample: >- | ||
<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<testsuites time="7.090" failures="0" | ||
skipped="0" tests="2" errors="1">\n <testsuite name="validations" time="7.090">\n | ||
<testcase name="test-1" classname="validations.test-1" time="2.545" />\n | ||
<testcase name="test-2" classname="validations.test-2" time="4.545">\n | ||
<error>error message</error>\n </testcase>\n </testsuite>\n</testsuites> | ||
""" | ||
|
||
|
||
import xml.etree.ElementTree as ET | ||
|
||
|
||
class FilterModule: | ||
|
||
@staticmethod | ||
def __float_conversion(x: float) -> str: | ||
return "{0:0.3f}".format(round(x, 3)) | ||
|
||
@classmethod | ||
def __map_xml_results(cls, test_results): | ||
|
||
total_time = sum( | ||
[data["time"] for data in test_results.values() if "time" in data] | ||
) | ||
total_time_str = cls.__float_conversion(total_time) | ||
|
||
root_attributes = { | ||
"time": total_time_str, | ||
"failures": "0", | ||
"skipped": "0", | ||
"tests": str(len(test_results)), | ||
"errors": str(len([elem for elem in test_results.values() if "error" in elem])), | ||
} | ||
root_elm = ET.Element("testsuites", attrib=root_attributes) | ||
ts_elm = ET.SubElement( | ||
root_elm, | ||
"testsuite", | ||
attrib={"name": "validations", "time": total_time_str}, | ||
) | ||
for name, data in test_results.items(): | ||
attributes = {"name": name, "classname": f"validations.{name}"} | ||
if "time" in data: | ||
attributes["time"] = cls.__float_conversion(data["time"]) | ||
tc_elm = ET.SubElement(ts_elm, "testcase", attrib=attributes) | ||
if "error" in data: | ||
error_elm = ET.SubElement(tc_elm, "error") | ||
error_elm.text = str(data["error"]) | ||
|
||
return ET.tostring(root_elm, encoding="utf-8", xml_declaration=True) | ||
|
||
def filters(self): | ||
return { | ||
"cifmw_validations_xml_filter": self.__map_xml_results, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
- name: Get validation start time | ||
ansible.builtin.set_fact: | ||
_cifmw_validations_run_start_time: "{{ now(fmt='%s.%f') }}" | ||
_cifmw_validations_status: "" | ||
|
||
- name: Run validation and catch errors | ||
environment: | ||
KUBECONFIG: "{{ cifmw_openshift_kubeconfig }}" | ||
PATH: "{{ cifmw_path }}" | ||
block: | ||
- name: Run a validation | ||
ansible.builtin.include_tasks: "{{ item }}" | ||
rescue: | ||
- name: Flag the validation as failed | ||
ansible.builtin.set_fact: | ||
_cifmw_validations_status: "Validator failed task: {{ ansible_failed_task.name }}, Validator failed reason: {{ ansible_failed_result.msg}}" | ||
|
||
- name: Add testcase name and time to generate xml script | ||
ansible.builtin.set_fact: | ||
_cifmw_validations_results: >- | ||
{{ | ||
_cifmw_validations_results | | ||
combine( | ||
{ | ||
(item | basename): { | ||
'time': (now(fmt='%s.%f')| float - _cifmw_validations_run_start_time | float), | ||
'error': _cifmw_validations_status if 'failed' in _cifmw_validations_status else omit | ||
} | ||
} | ||
) | ||
}} |