Skip to content

Commit 5639bc4

Browse files
drosenfeopenshift-merge-bot[bot]
authored andcommitted
Add create xml results file to validations role
1 parent 7042f7e commit 5639bc4

File tree

4 files changed

+155
-2
lines changed

4 files changed

+155
-2
lines changed

roles/validations/defaults/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ cifmw_validations_namespace: "openstack"
4545
cifmw_validations_hotfixed_edpm_nova_compute_image: quay.io/podified-antelope-centos9/openstack-nova-compute:current-podified
4646
cifmw_validations_custom_nova_service: "nova-custom-ceph"
4747

48+
cifmw_validations_xml_status_file_dir: "{{ cifmw_validations_basedir }}/tests/validations"
49+
4850
# variables needed for scaledown
4951
cifmw_validations_edpm_scale_down_hostname: compute-2.ctlplane.example.com
5052
cifmw_validations_edpm_scale_down_nodename: edpm-compute-2
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/python3
2+
3+
__metaclass__ = type
4+
5+
DOCUMENTATION = """
6+
name: cifmw_validations_xml_filter
7+
short_description: Maps the internal results structure to a JUnit XML string.
8+
description:
9+
- Maps the internal results structure to a JUnit XML string.
10+
options:
11+
_input:
12+
description:
13+
- The internal role test results.
14+
type: dict
15+
required: true
16+
"""
17+
18+
EXAMPLES = """
19+
- name: Define data to work on in the examples below
20+
vars:
21+
_internal_results:
22+
test-1:
23+
time: 2.54512
24+
test-case-2:
25+
time: 4.5450345
26+
error: "error message"
27+
ansible.builtin.set_fact:
28+
_xml_string: >-
29+
{{
30+
_internal_results | cifmw_validations_xml_filter
31+
}}
32+
"""
33+
34+
RETURN = """
35+
_value:
36+
description: The translated JUnit XML string.
37+
type: string
38+
sample: >-
39+
<?xml version='1.0' encoding='utf-8'?>
40+
<testsuites>
41+
<testsuite name="validations" failures="0" skipped="0" tests="2" errors="1" time="7.090">
42+
<testcase name="test-1" classname="validations.test-1" time="2.545" />
43+
<testcase name="test-2" classname="validations.test-2" time="4.545">
44+
<error message="error message" />
45+
</testcase>
46+
</testsuite>
47+
</testsuites>
48+
"""
49+
50+
51+
import xml.etree.ElementTree as ET
52+
53+
54+
class FilterModule:
55+
56+
@staticmethod
57+
def __float_conversion(x: float) -> str:
58+
return "{0:0.3f}".format(round(x, 3))
59+
60+
@classmethod
61+
def __map_xml_results(cls, test_results):
62+
63+
root_elm = ET.Element("testsuites")
64+
tree = ET.ElementTree(element=root_elm)
65+
total_time = sum(
66+
[data["time"] for data in test_results.values() if "time" in data]
67+
)
68+
ts_elm = ET.SubElement(
69+
root_elm,
70+
"testsuite",
71+
attrib={
72+
"name": "validations",
73+
"failures": str(
74+
len([elem for elem in test_results.values() if "error" in elem])
75+
),
76+
"skipped": "0",
77+
"tests": str(len(test_results)),
78+
"errors": "0",
79+
"time": cls.__float_conversion(total_time),
80+
},
81+
)
82+
for name, data in test_results.items():
83+
attributes = {"name": name, "classname": f"validations.{name}"}
84+
if "time" in data:
85+
attributes["time"] = cls.__float_conversion(data["time"])
86+
tc_elm = ET.SubElement(ts_elm, "testcase", attrib=attributes)
87+
if "error" in data:
88+
ET.SubElement(tc_elm, "failure", attrib={"message": data["error"]})
89+
ET.indent(tree, " ")
90+
return ET.tostring(root_elm, encoding="utf-8", xml_declaration=True)
91+
92+
def filters(self):
93+
return {
94+
"cifmw_validations_xml_filter": self.__map_xml_results,
95+
}

roles/validations/tasks/main.yml

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
- artifacts
2626
- logs
2727

28+
- name: Initialize variables needed for generating polarion xml file
29+
ansible.builtin.set_fact:
30+
_cifmw_validations_results: {}
31+
2832
# We can execute all defined validations when cifmw_validation_run_all is defined.
2933
# Else, we will skip this and run only the explicitly defined validations from
3034
# cifmw_validations_list
@@ -42,7 +46,7 @@
4246

4347
- name: Run all found validations
4448
ansible.builtin.include_tasks:
45-
file: "{{ item.path }}"
49+
file: run_validation.yml
4650
loop: "{{ found_validations.files }}"
4751

4852
- name: Run selected validations
@@ -56,5 +60,25 @@
5660
failed_when: not validation_exists.stat.exists
5761

5862
- name: Run validations
59-
ansible.builtin.include_tasks: "{{ item }}"
63+
ansible.builtin.include_tasks:
64+
file: run_validation.yml
6065
loop: "{{ cifmw_validations_list }}"
66+
67+
- name: Create validations directory
68+
ansible.builtin.file:
69+
path: "{{ cifmw_validations_xml_status_file_dir }}"
70+
state: directory
71+
mode: "0755"
72+
73+
- name: Create the XML file
74+
ansible.builtin.copy:
75+
content: "{{ _cifmw_validations_results | cifmw_validations_xml_filter }}"
76+
dest: "{{ cifmw_validations_xml_status_file_dir }}/validations_results.xml"
77+
mode: "0644"
78+
79+
- name: Fail job when validations fail
80+
ansible.builtin.assert:
81+
that:
82+
- _cifmw_validations_results.values() |
83+
selectattr('error', 'defined') | length == 0
84+
msg: "One or more validations failed"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
- name: Get validation start time
3+
ansible.builtin.set_fact:
4+
_cifmw_validations_run_start_time: "{{ now(fmt='%s.%f') }}"
5+
_cifmw_validations_status: ""
6+
7+
- name: Run validation and catch errors
8+
environment:
9+
KUBECONFIG: "{{ cifmw_openshift_kubeconfig }}"
10+
PATH: "{{ cifmw_path }}"
11+
block:
12+
- name: Run a validation
13+
ansible.builtin.include_tasks: "{{ item }}"
14+
rescue:
15+
- name: Flag the validation as failed
16+
ansible.builtin.set_fact:
17+
_cifmw_validations_status: "Validator failed task: {{ ansible_failed_task.name }}, Validator failed reason: {{ ansible_failed_result.msg}}"
18+
19+
- name: Add testcase name and time to generate xml script
20+
ansible.builtin.set_fact:
21+
_cifmw_validations_results: >-
22+
{{
23+
_cifmw_validations_results |
24+
combine(
25+
{
26+
(item | basename): {
27+
'time': (now(fmt='%s.%f')| float - _cifmw_validations_run_start_time | float),
28+
'error': _cifmw_validations_status if 'failed' in _cifmw_validations_status else omit
29+
}
30+
}
31+
)
32+
}}

0 commit comments

Comments
 (0)