Skip to content

Commit

Permalink
TELCOV10N-452 ADD role junit2json
Browse files Browse the repository at this point in the history
- the role converts reports into json, optionally merging multiple fragments into 1 report in loadable json format
- add initial tests
- add author to `galaxy.yml`
- upd collection `README.md`
    - added previously merged filter line `junit2obj`
    - added the role `junit2json`

Signed-off-by: Maxim Kovgan <makovgan@redhat.com>
  • Loading branch information
mvk committed Mar 3, 2025
1 parent 3d8b7d9 commit df3a0a0
Show file tree
Hide file tree
Showing 14 changed files with 573 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Name | Description
[redhatci.ocp.installer](https://github.com/redhatci/ansible-collection-redhatci-ocp/blob/main/roles/installer/README.md) | [IPI installer](https://github.com/openshift-kni/baremetal-deploy)
[redhatci.ocp.install_operator_gitops](https://github.com/redhatci/ansible-collection-redhatci-ocp/blob/main/roles/install_operator_gitops/README.md) | Installs and configures the openshift-gitops-operator so it can be used for ZTP deployments. Installation is optional and may be skipped by setting the variable ```ioc_configure_only: true``
[redhatci.ocp.jenkins_job_launcher](https://github.com/redhatci/ansible-collection-redhatci-ocp/blob/main/roles/jenkins_job_launcher/README.md) | Launch Jenkins jobs
[redhatci.ocp.junit2json](https://github.com/redhatci/ansible-collection-redhatci-ocp/blob/main/roles/junit2json/README.md) | Convert JUnit XML files into JSON for reporting/observability role
[redhatci.ocp.k8s_best_practices_certsuite](https://github.com/redhatci/ansible-collection-redhatci-ocp/blob/main/roles/k8s_best_practices_certsuite/README.md) | Executes the [Red Hat Best Practices Test Suite for Kubernetes](https://github.com/redhat-best-practices-for-k8s/certsuite) tool.
[redhatci.ocp.kvirt_vm](https://github.com/redhatci/ansible-collection-redhatci-ocp/blob/main/roles/kvirt_vm/README.md) | Deployment of Kubevirt virtual machines.
[redhatci.ocp.label_nodes](https://github.com/redhatci/ansible-collection-redhatci-ocp/blob/main/roles/label_nodes/README.md) | Applies labels defined at inventory level to the OCP cluster nodes.
Expand Down Expand Up @@ -161,6 +162,7 @@ Name | Description
Name | Type | Description
--- | --- | ---
[redhatci.ocp.junit2dict]() | Filter | Transforms a JUnit into a dictionary
[redhatci.ocp.junit2obj]() | Filter | Transforms a JUnit into a corresponding JSON text (will replace `redhatci.ocp.junit2dict`)
[redhatci.ocp.ocp_compatibility]() | Filter | Parse the deprecated and to-be-deprecated API after the workload installation
[redhatci.ocp.regex_diff]() | Filter | Obtain differences between two lists
[redhatci.ocp.get_compatible_rhocp_repo]() | Module | A module to find the latest available version of the RHOCP repository
Expand Down
1 change: 1 addition & 0 deletions galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ authors:
- "Keith Schincke <keith.schincke+github@gmail.com>"
- "Manuel Rodriguez <manuel.rodriguez@redhat.com>"
- "Marek Kochanowski <mkochanowski@redhat.com>"
- "Max Kovgan <makovgan@redhat.com>"
- "mbpavan <pbheeman@redhat.com>"
- "Michele Costa <micosta@redhat.com>"
- "Murali Krishnasamy <70236227+mukrishn@users.noreply.gihub.com>"
Expand Down
218 changes: 218 additions & 0 deletions roles/junit2json/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
<!-- DOCSIBLE START -->

# 📃 Role overview

## junit2json



Description: Converts XML junit reports passed or in passed directory into single or fragmented JSON report file(s)







<details>
<summary><b>🧩 Argument Specifications in meta/argument_specs</b></summary>

#### Key: main
**Description**: The resulting JSON file(s) are of the same structure for all the teams' and CI systems and used later to be sent to the data collection system.
This is the main entrypoint for the role `redhatci.ocp.junit2json`. Scans all the XML reports passed as `junit2json_input_reports`, folders scanned for files matching `global_reports_path_patterns`
The variable `junit2json_do_merge` is `true` by default and then the reports are merged into `junit2json_input_merged_report` file under `junit2json_output_dir`.
If `junit2json_do_merge` is `false`, then each file is converted into single `JSON` report file under `junit2json_output_dir`
if `junit_do_merge` is `true`, the output is written into `junit2json_output_merged_report` file under `junit2json_output_dir`, renaming `.xml` to `.json` or just appending `.json`



- **junit2json_input_reports**
- **Required**: True
- **Type**: list
- **Default**: none
- **Description**: Mixed List files or directories containing the XML report(s) to convert





- **junit2json_output_dir**
- **Required**: True
- **Type**: str
- **Default**: none
- **Description**: Output directory for resulting report JSON file path(s)





- **junit2json_do_merge**
- **Required**: False
- **Type**: bool
- **Default**: True
- **Description**: Should we merge data of converted reports into 1 file or not.
When `false`, each report `XML` file is converted to a corresponding json file appended `.json` extension
Otherwise, resulting merged report is named as the directory, with `.report.json`` extension.
in both cases, the result is stored under `junit2json_output_dir`.





- **junit2json_input_merged_report**
- **Required**: False
- **Type**: str
- **Default**: none
- **Description**: Relative file name for the Merged XML report (relevant only when `junit2json_do_merge` is `true`),
it is generated under `junit2json_output_dir`





- **junit2json_output_merged_report**
- **Required**: False
- **Type**: str
- **Default**: none
- **Description**: Relative file name for the JSON report (relevant only when `junit2json_do_merge` is `true`),
it is generated under `junit2json_output_dir`







</details>


### Defaults

**These are static variables with lower priority**

#### File: defaults/main.yml

| Var | Type | Value |Required | Title |
|--------------|--------------|-------------|-------------|-------------|
| [junit2json_hash](defaults/main.yml#L9) | str | `sha256` | n/a | n/a |
| [junit2json_supported_archives](defaults/main.yml#L10) | list | `['zip']` | n/a | n/a |
| [junit2json_do_merge](defaults/main.yml#L13) | bool | `True` | n/a | n/a |
| [junit2json_debug](defaults/main.yml#L17) | bool | `False` | n/a | n/a |





### Tasks


#### File: tasks/convert.yml

| Name | Module | Has Conditions |
| ---- | ------ | --------- |
| Read file content | ansible.builtin.set_fact | False |
| Generate content hash from the content | ansible.builtin.set_fact | False |
| Print curr XML report filename | ansible.builtin.debug | False |
| Obtain info on previous content checksum file | ansible.builtin.stat | False |
| Update junit2json_result_data [junit2json_do_merge: {{ junit2json_do_merge }}] | ansible.builtin.set_fact | True |
| Convert junit XML to JSON and save in junit2json_result_data | ansible.builtin.set_fact | True |
| Setup JSoN report file name (with extension .xml) | ansible.builtin.set_fact | True |
| Setup JSoN report file name (with extension .xml) | ansible.builtin.set_fact | True |
| Update junit2json_output_report_path [junit2json_do_merge: {{ junit2json_do_merge }}] | ansible.builtin.set_fact | True |
| Update junit2json_output_report_path [junit2json_do_merge: {{ junit2json_do_merge }}] | ansible.builtin.set_fact | True |
| Get xml_old_hash | ansible.builtin.set_fact | True |
| Update global_json_reports_list | ansible.builtin.set_fact | False |
| Ensure json data destination folder | ansible.builtin.file | True |
| Write json data to destination | ansible.builtin.copy | True |
| Write current hash to destination | ansible.builtin.copy | True |

#### File: tasks/merge.yml

| Name | Module | Has Conditions |
| ---- | ------ | --------- |
| Generate XML files list | ansible.builtin.set_fact | False |
| Print XML reports' filenames data | ansible.builtin.debug | True |
| Merge multiple JUnit XML files into single consolidated report | ansible.builtin.shell | False |
| Write merge resulting file | ansible.builtin.copy | True |
| Override the xml files list for conversion | ansible.builtin.set_fact | False |

#### File: tasks/expand.yml

| Name | Module | Has Conditions |
| ---- | ------ | --------- |
| Print path_item value | ansible.builtin.debug | False |
| Setup helper variables | ansible.builtin.set_fact | False |
| Check whether the path_item is a directory | ansible.builtin.stat | False |
| Add path_item as JUnit XML report (it is a file) | ansible.builtin.set_fact | True |
| Find JUnit XML reports under path_item (it is a directory) | ansible.builtin.find | True |
| Setup the default list for conversion | ansible.builtin.set_fact | True |
| Setup the default list for conversion | ansible.builtin.set_fact | True |

#### File: tasks/main.yml

| Name | Module | Has Conditions |
| ---- | ------ | --------- |
| Validate some variables | ansible.builtin.assert | False |
| Initialize input reports variable | ansible.builtin.set_fact | False |
| Initialize input reports variable | ansible.builtin.debug | False |
| Expand the input list to list of existing files | ansible.builtin.include_tasks | False |
| Merge JUnit XML reports into single file for junit2json_do_merge=true | ansible.builtin.include_tasks | True |
| Convert XML to JSON | ansible.builtin.include_tasks | True |




## Playbook

```yml
---

- name: "Test redhatci.ocp.junit2json role :: simple input"
hosts: localhost
tasks:
- name: Test role redhatci.ocp.junit2json
ansible.builtin.include_role:
name: redhatci.ocp.junit2json
vars:
junit2json_input_reports: "{{ role_path }}/../../tests/unit/data/test_junit2obj_simple_input.xml"
junit2json_output_dir: "{{ role_path }}/tests"
junit2json_do_merge: false
- name: Load actual result to variable actual
ansible.builtin.set_fact:
actual: "{{ lookup('file', playbook_dir + '/test_junit2obj_simple_input.json') | from_json }}"
- name: Load expected result to variable expected
ansible.builtin.set_fact:
expected: "{{ lookup('file', playbook_dir + '/../../../tests/unit/data/test_junit2obj_simple_result.json') }}"
- name: Ensure both are identical
ansible.builtin.assert:
that:
- actual == expected

# - name: test system-tests data conversion
# hosts: localhost
# roles:
# - name: junit2json
# vars:
# j2j_reports_path: "{{ roles_path }}/junit2json/tests/data/system-tests/"
# j2j_reports_path_patterns:
# - "*.xml"
#

```


## Author Information
Max Kovgan

#### License

Apache-2.0

#### Minimum Ansible Version

2.9

#### Platforms

No platforms specified.
<!-- DOCSIBLE END -->
17 changes: 17 additions & 0 deletions roles/junit2json/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
# defaults file for role redhatci.ocp.junit2json

################################################################################
# NOTE: The mandatory variables are commented out, for more information
# refer to meta/argument_spec.yml
################################################################################
# junit2json_reports_path: str - mandatory parameter containing xml files
junit2json_hash: sha256
junit2json_supported_archives:
- "zip"
# junit2json_result_json_path: "{{ junit2json_reports_path }}/report.junit.json"
junit2json_do_merge: true
# junit2json_input_merged_report: "{{ junit2json_reports_path }}/report.merged.xml"
# Debug variables:

junit2json_debug: false
46 changes: 46 additions & 0 deletions roles/junit2json/meta/argument_specs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
# arguments spec file for redhatci.ocp.junit2json role
argument_specs:
main:
short_description: Main entry point for role redhatci.ocp.junit2json
description: |
The resulting JSON file(s) are of the same structure for all the teams' and CI systems and used later to be sent to the data collection system.
This is the main entrypoint for the role `redhatci.ocp.junit2json`. Scans all the XML reports passed as `junit2json_input_reports`, folders scanned for files matching `global_reports_path_patterns`.
The variable `junit2json_do_merge` is `true` by default and then the reports are merged into `junit2json_input_merged_report` file under `junit2json_output_dir`.
If `junit2json_do_merge` is `false`, then each file is converted into single `JSON` report file under `junit2json_output_dir`.
If `junit_do_merge` is `true`, the output is written into `junit2json_output_merged_report` file under `junit2json_output_dir` renaming `.xml` to `.json` or just appending `.json`.
author:
- Max Kovgan
options:
junit2json_input_reports:
type: list
required: true
elements: str
description: |
Mixed List files or directories containing the XML report(s) to convert
junit2json_output_dir:
type: str
required: true
description: |
Output directory for resulting report JSON file path(s)
junit2json_do_merge:
type: bool
required: false
default: true
description: |
Should we merge data of converted reports into 1 file or not.
When `false`, each report `XML` file is converted to a corresponding json file appended `.json` extension
Otherwise, resulting merged report is named as the directory, with `.report.json`` extension.
in both cases, the result is stored under `junit2json_output_dir`.
junit2json_input_merged_report:
type: str
required: false
description: |
Relative file name for the Merged XML report (relevant only when `junit2json_do_merge` is `true`),
it is generated under `junit2json_output_dir`
junit2json_output_merged_report:
type: str
required: false
description: |
Relative file name for the JSON report (relevant only when `junit2json_do_merge` is `true`),
it is generated under `junit2json_output_dir`
11 changes: 11 additions & 0 deletions roles/junit2json/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SPDX-License-Identifier: Apache-2.0
---
galaxy_info:
author: Max Kovgan
description: Converts XML junit reports passed or in passed directory into single or fragmented JSON report file(s)
company: Red Hat, Inc.
# issue_tracker_url: http://example.com/issue/tracker
license: Apache-2.0
min_ansible_version: "2.9"
galaxy_tags: []
dependencies: []
Loading

0 comments on commit df3a0a0

Please sign in to comment.