blob: 6045a3b2163a580946aec16072d70c109aa44c29 [file] [log] [blame]
Leonardo Sandoval9dfdd1b2020-08-06 17:08:11 -05001#!/usr/bin/env bash
Zelalem1af7a7b2020-08-04 17:34:32 -05002#
Saul Romerocacda172023-03-10 14:23:41 +00003# Copyright (c) 2019-2023, Arm Limited. All rights reserved.
Zelalem1af7a7b2020-08-04 17:34:32 -05004#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7
Saul Romerocacda172023-03-10 14:23:41 +00008set -x
Zelalem1af7a7b2020-08-04 17:34:32 -05009REPORT_JSON=$1
10REPORT_HTML=$2
Paul Sokolovsky301710a2022-02-21 14:56:33 +030011
Gary Morrison999a9d72022-03-14 18:29:06 -050012if echo "$JENKINS_URL" | grep -q "oss.arm.com"; then
Saul Romerocacda172023-03-10 14:23:41 +000013 ARTIFACT_PATH='artifact/html/qa-code-coverage'
14 INFO_PATH='coverage.info'
15 JSON_PATH='intermediate_layer.json'
Paul Sokolovsky301710a2022-02-21 14:56:33 +030016else
Saul Romerocacda172023-03-10 14:23:41 +000017 ARTIFACT_PATH='artifact'
18 INFO_PATH='trace_report/coverage.info'
19 JSON_PATH='config_file.json'
Paul Sokolovsky301710a2022-02-21 14:56:33 +030020fi
21
Zelalem1af7a7b2020-08-04 17:34:32 -050022#################################################################
23# Create json file for input to the merge.sh for Code Coverage
24# Globals:
25# REPORT_JSON: Json file for SCP and TF ci gateway test results
Saul Romerocacda172023-03-10 14:23:41 +000026# MERGE_CONFIGURATION: Json file to be used as input to the merge.sh
Zelalem1af7a7b2020-08-04 17:34:32 -050027# Arguments:
28# None
29# Outputs:
30# Print number of files to be merged
31################################################################
32create_merge_cfg() {
33python3 - << EOF
34import json
35import os
36
37server = os.getenv("JENKINS_URL", "https://jenkins.oss.arm.com/")
38merge_json = {} # json object
39_files = []
40with open("$REPORT_JSON") as json_file:
41 data = json.load(json_file)
42merge_number = 0
43test_results = data['test_results']
44test_files = data['test_files']
45for index, build_number in enumerate(test_results):
Saul Romerocacda172023-03-10 14:23:41 +000046 if ("bmcov" in test_files[index] or
47 "code-coverage" in test_files[index]) and test_results[build_number] == "SUCCESS":
Zelalem1af7a7b2020-08-04 17:34:32 -050048 merge_number += 1
Paul Sokolovskydd04dea2022-02-21 13:40:43 +030049 base_url = "{}job/{}/{}/{}".format(
50 server, data['job'], build_number, "$ARTIFACT_PATH")
Zelalem1af7a7b2020-08-04 17:34:32 -050051 _files.append( {'id': build_number,
52 'config': {
53 'type': 'http',
Paul Sokolovskydd04dea2022-02-21 13:40:43 +030054 'origin': "{}/{}".format(
55 base_url, "$JSON_PATH")
Zelalem1af7a7b2020-08-04 17:34:32 -050056 },
57 'info': {
58 'type': 'http',
Paul Sokolovskydd04dea2022-02-21 13:40:43 +030059 'origin': "{}/{}".format(
60 base_url, "$INFO_PATH")
Zelalem1af7a7b2020-08-04 17:34:32 -050061 }
62 })
63merge_json = { 'files' : _files }
Saul Romerocacda172023-03-10 14:23:41 +000064with open("$MERGE_CONFIGURATION", 'w') as outfile:
65 json.dump(merge_json, outfile, indent=4)
Zelalem1af7a7b2020-08-04 17:34:32 -050066print(merge_number)
67EOF
68}
69
Saul Romerocacda172023-03-10 14:23:41 +000070generate_header() {
71 local cov_html=${OUTDIR}/${COVERAGE_FOLDER}/index.html
72 local out_report=$1
Zelalem1af7a7b2020-08-04 17:34:32 -050073python3 - << EOF
74import re
Saul Romerocacda172023-03-10 14:23:41 +000075import json
Zelalem1af7a7b2020-08-04 17:34:32 -050076cov_html="$cov_html"
77out_report = "$out_report"
Saul Romerocacda172023-03-10 14:23:41 +000078confs = ""
79with open("$REPORT_JSON") as json_file:
80 data = json.load(json_file)
81test_files = data['test_files']
82test_results = data['test_results']
83for index, build_number in enumerate(test_results):
84 test_file = test_files[index]
85 test_configuration = test_file.rsplit('%', 1)
86 if len(test_configuration) > 1:
87 confs += '<a target="_blank" href="artifact/${jenkins_archive_folder}/${COVERAGE_FOLDER}/{}/index.html">{}</a>'.format(build_number,
88 test_configuration[1])
89
Zelalem1af7a7b2020-08-04 17:34:32 -050090with open(cov_html, "r") as f:
91 html_content = f.read()
92items = ["Lines", "Functions", "Branches"]
93s = """
Saul Romerocacda172023-03-10 14:23:41 +000094<style>
95.dropbtn {
96 background-color: #04AA6D;
97 color: white;
98 padding: 16px;
99 font-size: 16px;
100 border: none;
101}
102
103/* The container <div> - needed to position the dropdown content */
104.dropdown {
105 position: relative;
106 display: inline-block;
107}
108
109/* Dropdown Content (Hidden by Default) */
110.dropdown-content {
111 display: none;
112 position: absolute;
113 background-color: #f1f1f1;
114 min-width: 160px;
115 box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
116 z-index: 1;
117}
118
119/* Links inside the dropdown */
120.dropdown-content a {
121 color: black;
122 padding: 12px 16px;
123 text-decoration: none;
124 display: block;
125}
126
127/* Change color of dropdown links on hover */
128.dropdown-content a:hover {background-color: #ddd;}
129
130/* Show the dropdown menu on hover */
131.dropdown:hover .dropdown-content {display: block;}
132
133/* Change the background color of the dropdown button when the dropdown content is shown */
134.dropdown:hover .dropbtn {background-color: #3e8e41;}
135</style>
Zelalem1af7a7b2020-08-04 17:34:32 -0500136 <div id="div-cov">
137 <hr>
138 <table id="table-cov">
139 <tbody>
140 <tr>
141 <td>Type</td>
142 <td>Hit</td>
143 <td>Total</td>
144 <td>Coverage</td>
145 </tr>
146"""
147for item in items:
148 data = re.findall(r'<td class="headerItem">{}:</td>\n\s+<td class="headerCovTableEntry">(.+?)</td>\n\s+<td class="headerCovTableEntry">(.+?)</td>\n\s+'.format(item),
149 html_content, re.DOTALL)
150 if data is None:
151 continue
152 hit, total = data[0]
153 cov = round(float(hit)/float(total) * 100.0, 2)
154 color = "success"
155 if cov < 90:
156 color = "unstable"
157 if cov < 75:
158 color = "failure"
159 s = s + """
160 <tr>
161 <td>{}</td>
162 <td>{}</td>
163 <td>{}</td>
164 <td class='{}'>{} %</td>
165 </tr>
166""".format(item, hit, total, color, cov)
167s = s + """
168 </tbody>
169 </table>
170 <p>
Saul Romerocacda172023-03-10 14:23:41 +0000171 <button onclick="window.open('artifact/${jenkins_archive_folder}/${COVERAGE_FOLDER}/index.html','_blank');">Total Coverage Report</button>
Zelalem1af7a7b2020-08-04 17:34:32 -0500172 </p>
Saul Romerocacda172023-03-10 14:23:41 +0000173 <div class="dropdown">
174 <button class="dropbtn">Coverage Reports($number_of_files_to_merge)</button>
175 <div class="dropdown-content">
176 """ + confs + """
177 </div>
178 </div>
Zelalem1af7a7b2020-08-04 17:34:32 -0500179 </div>
Saul Romerocacda172023-03-10 14:23:41 +0000180
Zelalem1af7a7b2020-08-04 17:34:32 -0500181<script>
182 document.getElementById('tf-report-main').appendChild(document.getElementById("div-cov"));
183</script>
Saul Romerocacda172023-03-10 14:23:41 +0000184
Zelalem1af7a7b2020-08-04 17:34:32 -0500185"""
186with open(out_report, "a") as f:
187 f.write(s)
188EOF
189}
190OUTDIR=""
191index=""
192case "$TEST_GROUPS" in
193 scp*)
194 project="scp"
Saul Romerocacda172023-03-10 14:23:41 +0000195 jenkins_archive_folder=reports;;
Zelalem1af7a7b2020-08-04 17:34:32 -0500196 tf*)
197 project="trusted_firmware"
Saul Romerocacda172023-03-10 14:23:41 +0000198 jenkins_archive_folder=merge/outdir;;
Zelalem1af7a7b2020-08-04 17:34:32 -0500199 *)
200 exit 0;;
201esac
Saul Romerocacda172023-03-10 14:23:41 +0000202OUTDIR=${WORKSPACE}/${jenkins_archive_folder}
203source "$CI_ROOT/script/qa-code-coverage.sh"
204export MERGE_CONFIGURATION="$OUTDIR/merge_configuration.json"
205COVERAGE_FOLDER=lcov
206cd $WORKSPACE
207deploy_qa_tools
208cd -
Zelalem1af7a7b2020-08-04 17:34:32 -0500209mkdir -p $OUTDIR
210pushd $OUTDIR
Saul Romerocacda172023-03-10 14:23:41 +0000211 number_of_files_to_merge=$(create_merge_cfg)
212 echo "Merging $number_of_files_to_merge coverage files..."
Zelalem1af7a7b2020-08-04 17:34:32 -0500213 # Only merge when more than 1 test result
Saul Romerocacda172023-03-10 14:23:41 +0000214 if [ "$number_of_files_to_merge" -lt 2 ] ; then
215 echo "Only one file to merge."
Zelalem1af7a7b2020-08-04 17:34:32 -0500216 exit 0
217 fi
Paul Sokolovsky227f2cc2021-12-21 10:58:26 +0300218
Saul Romerocacda172023-03-10 14:23:41 +0000219 bash ${WORKSPACE}/qa-tools/coverage-tool/coverage-reporting/merge.sh \
220 -j $MERGE_CONFIGURATION -l ${OUTDIR}/${COVERAGE_FOLDER} -w $WORKSPACE -c -g
Paul Sokolovsky227f2cc2021-12-21 10:58:26 +0300221
Saul Romerocacda172023-03-10 14:23:41 +0000222 generate_header ${REPORT_HTML}
Zelalem1af7a7b2020-08-04 17:34:32 -0500223 cp ${REPORT_HTML} $OUTDIR
224popd