-
Notifications
You must be signed in to change notification settings - Fork 111
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
7042f7e
commit 5639bc4
Showing
4 changed files
with
155 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
95 changes: 95 additions & 0 deletions
95
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,95 @@ | ||
#!/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'?> | ||
<testsuites> | ||
<testsuite name="validations" failures="0" skipped="0" tests="2" errors="1" time="7.090"> | ||
<testcase name="test-1" classname="validations.test-1" time="2.545" /> | ||
<testcase name="test-2" classname="validations.test-2" time="4.545"> | ||
<error message="error message" /> | ||
</testcase> | ||
</testsuite> | ||
</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): | ||
|
||
root_elm = ET.Element("testsuites") | ||
tree = ET.ElementTree(element=root_elm) | ||
total_time = sum( | ||
[data["time"] for data in test_results.values() if "time" in data] | ||
) | ||
ts_elm = ET.SubElement( | ||
root_elm, | ||
"testsuite", | ||
attrib={ | ||
"name": "validations", | ||
"failures": str( | ||
len([elem for elem in test_results.values() if "error" in elem]) | ||
), | ||
"skipped": "0", | ||
"tests": str(len(test_results)), | ||
"errors": "0", | ||
"time": cls.__float_conversion(total_time), | ||
}, | ||
) | ||
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: | ||
ET.SubElement(tc_elm, "failure", attrib={"message": data["error"]}) | ||
ET.indent(tree, " ") | ||
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 | ||
} | ||
} | ||
) | ||
}} |