Leonardo Sandoval | 9dfdd1b | 2020-08-06 17:08:11 -0500 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 2 | # |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 3 | # Copyright (c) 2019-2023, Arm Limited. All rights reserved. |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 4 | # |
| 5 | # SPDX-License-Identifier: BSD-3-Clause |
| 6 | # |
| 7 | |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 8 | set -x |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 9 | REPORT_JSON=$1 |
| 10 | REPORT_HTML=$2 |
Paul Sokolovsky | 301710a | 2022-02-21 14:56:33 +0300 | [diff] [blame] | 11 | |
Arthur She | a813bdf | 2025-02-03 21:39:57 -0800 | [diff] [blame] | 12 | if echo "$JENKINS_PUBLIC_URL" | grep -q "oss.arm.com"; then |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 13 | ARTIFACT_PATH='artifact/html/qa-code-coverage' |
| 14 | INFO_PATH='coverage.info' |
| 15 | JSON_PATH='intermediate_layer.json' |
Paul Sokolovsky | 301710a | 2022-02-21 14:56:33 +0300 | [diff] [blame] | 16 | else |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 17 | ARTIFACT_PATH='artifact' |
| 18 | INFO_PATH='trace_report/coverage.info' |
| 19 | JSON_PATH='config_file.json' |
Paul Sokolovsky | 301710a | 2022-02-21 14:56:33 +0300 | [diff] [blame] | 20 | fi |
| 21 | |
Saul Romero | 072236d | 2023-12-06 11:57:16 +0000 | [diff] [blame] | 22 | ############################################################################### |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 23 | # Create json file for input to the merge.sh for Code Coverage |
| 24 | # Globals: |
Saul Romero | 072236d | 2023-12-06 11:57:16 +0000 | [diff] [blame] | 25 | # REPORT_JSON: Json file with TF ci gateway builder test results |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 26 | # MERGE_CONFIGURATION: Json file to be used as input to the merge.sh |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 27 | # Arguments: |
| 28 | # None |
| 29 | # Outputs: |
| 30 | # Print number of files to be merged |
Saul Romero | 072236d | 2023-12-06 11:57:16 +0000 | [diff] [blame] | 31 | ############################################################################### |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 32 | create_merge_cfg() { |
| 33 | python3 - << EOF |
| 34 | import json |
| 35 | import os |
Saul Romero | 568f5d7 | 2023-11-29 11:58:34 +0000 | [diff] [blame] | 36 | import re |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 37 | |
Arthur She | a813bdf | 2025-02-03 21:39:57 -0800 | [diff] [blame] | 38 | server = os.getenv("JENKINS_PUBLIC_URL", "https://jenkins.oss.arm.com/") |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 39 | merge_json = {} # json object |
| 40 | _files = [] |
| 41 | with open("$REPORT_JSON") as json_file: |
| 42 | data = json.load(json_file) |
| 43 | merge_number = 0 |
| 44 | test_results = data['test_results'] |
| 45 | test_files = data['test_files'] |
| 46 | for index, build_number in enumerate(test_results): |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 47 | if ("bmcov" in test_files[index] or |
| 48 | "code-coverage" in test_files[index]) and test_results[build_number] == "SUCCESS": |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 49 | merge_number += 1 |
Paul Sokolovsky | dd04dea | 2022-02-21 13:40:43 +0300 | [diff] [blame] | 50 | base_url = "{}job/{}/{}/{}".format( |
| 51 | server, data['job'], build_number, "$ARTIFACT_PATH") |
Saul Romero | 568f5d7 | 2023-11-29 11:58:34 +0000 | [diff] [blame] | 52 | _group_test_config = re.match(f'^[0-9%]*(?:${TEST_GROUPS}%)?(.+?)\.test', test_files[index]) |
| 53 | tf_configuration = _group_test_config.groups()[0] if _group_test_config else 'N/A' |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 54 | _files.append( {'id': build_number, |
| 55 | 'config': { |
| 56 | 'type': 'http', |
Paul Sokolovsky | dd04dea | 2022-02-21 13:40:43 +0300 | [diff] [blame] | 57 | 'origin': "{}/{}".format( |
| 58 | base_url, "$JSON_PATH") |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 59 | }, |
| 60 | 'info': { |
| 61 | 'type': 'http', |
Paul Sokolovsky | dd04dea | 2022-02-21 13:40:43 +0300 | [diff] [blame] | 62 | 'origin': "{}/{}".format( |
| 63 | base_url, "$INFO_PATH") |
Saul Romero | 568f5d7 | 2023-11-29 11:58:34 +0000 | [diff] [blame] | 64 | }, |
| 65 | 'tf-configuration': tf_configuration |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 66 | }) |
| 67 | merge_json = { 'files' : _files } |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 68 | with open("$MERGE_CONFIGURATION", 'w') as outfile: |
| 69 | json.dump(merge_json, outfile, indent=4) |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 70 | print(merge_number) |
| 71 | EOF |
| 72 | } |
| 73 | |
Saul Romero | 072236d | 2023-12-06 11:57:16 +0000 | [diff] [blame] | 74 | ############################################################################### |
| 75 | # Append a summary table to an html file (report)that will be interpreted by |
| 76 | # the Jenkins html plugin |
| 77 | # |
| 78 | # If there is more than one code coverage report and is merged successfully, |
| 79 | # then a summary html/javascript table is created at the end of the |
| 80 | # html file containing the merged function, line and branch coverage |
| 81 | # percentages. |
| 82 | # Globals: |
| 83 | # OUTDIR: Path where the output folders are |
| 84 | # COVERAGE_FOLDER: Folder name where the LCOV files are |
| 85 | # REPORT_JSON: Json file with TF ci gateway builder test results |
| 86 | # jenkins_archive_folder: Folder name where Jenkins archives files |
| 87 | # list_of_merged_builds: Array with a list of individual successfully merged |
| 88 | # jenkins build id's |
| 89 | # number_of_files_to_merge: Indicates the number of individual jobs that have |
| 90 | # code coverage and ran successfully |
| 91 | # Arguments: |
| 92 | # 1: HTML report to be appended the summary table |
| 93 | # Outputs: |
| 94 | # Appended HTML file with the summary table of merged code coverage |
| 95 | ############################################################################### |
| 96 | generate_code_coverage_summary() { |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 97 | local cov_html=${OUTDIR}/${COVERAGE_FOLDER}/index.html |
| 98 | local out_report=$1 |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 99 | python3 - << EOF |
| 100 | import re |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 101 | import json |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 102 | cov_html="$cov_html" |
| 103 | out_report = "$out_report" |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 104 | confs = "" |
| 105 | with open("$REPORT_JSON") as json_file: |
| 106 | data = json.load(json_file) |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 107 | |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 108 | with open(cov_html, "r") as f: |
| 109 | html_content = f.read() |
| 110 | items = ["Lines", "Functions", "Branches"] |
| 111 | s = """ |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 112 | <style> |
| 113 | .dropbtn { |
| 114 | background-color: #04AA6D; |
| 115 | color: white; |
| 116 | padding: 16px; |
| 117 | font-size: 16px; |
| 118 | border: none; |
| 119 | } |
| 120 | |
| 121 | /* The container <div> - needed to position the dropdown content */ |
| 122 | .dropdown { |
| 123 | position: relative; |
| 124 | display: inline-block; |
| 125 | } |
| 126 | |
| 127 | /* Dropdown Content (Hidden by Default) */ |
| 128 | .dropdown-content { |
| 129 | display: none; |
| 130 | position: absolute; |
| 131 | background-color: #f1f1f1; |
| 132 | min-width: 160px; |
| 133 | box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); |
| 134 | z-index: 1; |
| 135 | } |
| 136 | |
| 137 | /* Links inside the dropdown */ |
| 138 | .dropdown-content a { |
| 139 | color: black; |
| 140 | padding: 12px 16px; |
| 141 | text-decoration: none; |
| 142 | display: block; |
| 143 | } |
| 144 | |
| 145 | /* Change color of dropdown links on hover */ |
| 146 | .dropdown-content a:hover {background-color: #ddd;} |
| 147 | |
| 148 | /* Show the dropdown menu on hover */ |
| 149 | .dropdown:hover .dropdown-content {display: block;} |
| 150 | |
| 151 | /* Change the background color of the dropdown button when the dropdown content is shown */ |
| 152 | .dropdown:hover .dropbtn {background-color: #3e8e41;} |
| 153 | </style> |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 154 | <div id="div-cov"> |
| 155 | <hr> |
| 156 | <table id="table-cov"> |
| 157 | <tbody> |
| 158 | <tr> |
| 159 | <td>Type</td> |
| 160 | <td>Hit</td> |
| 161 | <td>Total</td> |
| 162 | <td>Coverage</td> |
| 163 | </tr> |
| 164 | """ |
| 165 | for item in items: |
| 166 | data = re.findall(r'<td class="headerItem">{}:</td>\n\s+<td class="headerCovTableEntry">(.+?)</td>\n\s+<td class="headerCovTableEntry">(.+?)</td>\n\s+'.format(item), |
| 167 | html_content, re.DOTALL) |
| 168 | if data is None: |
| 169 | continue |
| 170 | hit, total = data[0] |
| 171 | cov = round(float(hit)/float(total) * 100.0, 2) |
| 172 | color = "success" |
| 173 | if cov < 90: |
| 174 | color = "unstable" |
| 175 | if cov < 75: |
| 176 | color = "failure" |
| 177 | s = s + """ |
| 178 | <tr> |
| 179 | <td>{}</td> |
| 180 | <td>{}</td> |
| 181 | <td>{}</td> |
| 182 | <td class='{}'>{} %</td> |
| 183 | </tr> |
| 184 | """.format(item, hit, total, color, cov) |
| 185 | s = s + """ |
| 186 | </tbody> |
| 187 | </table> |
| 188 | <p> |
Saul Romero | 568f5d7 | 2023-11-29 11:58:34 +0000 | [diff] [blame] | 189 | <button onclick="window.open('artifact/${jenkins_archive_folder}/${COVERAGE_FOLDER}/index.html','_blank');">Total Coverage Report (${#list_of_merged_builds[@]} out of ${number_of_files_to_merge})</button> |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 190 | </p> |
| 191 | </div> |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 192 | |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 193 | <script> |
| 194 | document.getElementById('tf-report-main').appendChild(document.getElementById("div-cov")); |
| 195 | </script> |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 196 | |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 197 | """ |
| 198 | with open(out_report, "a") as f: |
| 199 | f.write(s) |
| 200 | EOF |
| 201 | } |
Saul Romero | 568f5d7 | 2023-11-29 11:58:34 +0000 | [diff] [blame] | 202 | |
Saul Romero | 072236d | 2023-12-06 11:57:16 +0000 | [diff] [blame] | 203 | ############################################################################### |
| 204 | # Append a column for each row corresponding to each build with a successful |
| 205 | # code coverage report |
| 206 | # |
| 207 | # The column contains an html button that links to the individual code coverage |
| 208 | # html report or 'N/A' if report cannot be found or build was a failure. |
| 209 | # The column is added to the main table where all the tests configurations |
| 210 | # status are shown. |
| 211 | # Globals: |
| 212 | # list_of_merged_builds: Array with a list of individual successfully merged |
| 213 | # jenkins build id's |
Saul Romero | 95e8d4e | 2023-12-11 10:03:55 +0000 | [diff] [blame] | 214 | # individual_report_folder: Location within the jenkins job worker where |
| 215 | # resides the code coverage html report. |
Saul Romero | 072236d | 2023-12-06 11:57:16 +0000 | [diff] [blame] | 216 | # Arguments: |
| 217 | # 1: HTML report to be appended the summary table |
| 218 | # Outputs: |
| 219 | # Appended HTML file with the column added to the main hmtl table |
| 220 | ############################################################################### |
| 221 | generate_code_coverage_column() { |
Saul Romero | 568f5d7 | 2023-11-29 11:58:34 +0000 | [diff] [blame] | 222 | echo "List of merged build ids:${list_of_merged_builds[@]}" |
| 223 | python3 - << EOF |
| 224 | merged_ids=[int(i) for i in "${list_of_merged_builds[@]}".split()] |
| 225 | s = """ |
| 226 | |
| 227 | <script> |
| 228 | window.onload = function() { |
| 229 | """ + f"const mergedIds={merged_ids}" + """ |
| 230 | document.querySelector('#tf-report-main table').querySelectorAll("tr").forEach((row,i) => { |
| 231 | const cell = document.createElement(i ? "td" : "th") |
| 232 | const button = document.createElement("button") |
| 233 | button.textContent = "Report" |
| 234 | if (i) { |
| 235 | merged = false |
| 236 | if (q = row.querySelector('td.success a.buildlink')) { |
| 237 | href = q.href |
Saul Romero | 690348e | 2024-08-20 16:05:40 +0100 | [diff] [blame] | 238 | buildId = Number(href.split("/").at(-2)) |
| 239 | if (mergedIds.includes(buildId)) { |
Saul Romero | 568f5d7 | 2023-11-29 11:58:34 +0000 | [diff] [blame] | 240 | cell.classList.add("success") |
Saul Romero | 95e8d4e | 2023-12-11 10:03:55 +0000 | [diff] [blame] | 241 | const url = href.replace('console', 'artifact/${individual_report_folder}') |
Saul Romero | 568f5d7 | 2023-11-29 11:58:34 +0000 | [diff] [blame] | 242 | button.addEventListener('click', () => { |
| 243 | window.open(url, "_blank") |
| 244 | }) |
| 245 | cell.appendChild(button) |
| 246 | merged = true |
| 247 | } |
| 248 | } |
| 249 | if (!merged) { |
| 250 | cell.innerText = "N/A" |
| 251 | cell.classList.add("failure") |
| 252 | } |
| 253 | } |
| 254 | else { |
| 255 | cell.innerText = "Code Coverage" |
| 256 | } |
| 257 | row.appendChild(cell) |
| 258 | }) |
| 259 | } |
| 260 | </script> |
| 261 | """ |
| 262 | with open("$1", "a") as f: |
| 263 | f.write(s) |
| 264 | EOF |
| 265 | } |
| 266 | |
| 267 | |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 268 | OUTDIR="" |
| 269 | index="" |
| 270 | case "$TEST_GROUPS" in |
| 271 | scp*) |
| 272 | project="scp" |
Saul Romero | 95e8d4e | 2023-12-11 10:03:55 +0000 | [diff] [blame] | 273 | jenkins_archive_folder=reports |
| 274 | individual_report_folder=html/qa-code-coverage/lcov/index.html |
| 275 | ;; |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 276 | tf*) |
| 277 | project="trusted_firmware" |
Saul Romero | 95e8d4e | 2023-12-11 10:03:55 +0000 | [diff] [blame] | 278 | jenkins_archive_folder=merge/outdir |
| 279 | individual_report_folder=trace_report/index.html |
| 280 | ;; |
Saul Romero | 82bcfb0 | 2023-06-27 16:24:13 +0100 | [diff] [blame] | 281 | spm*) |
| 282 | project="hafnium" |
Saul Romero | 95e8d4e | 2023-12-11 10:03:55 +0000 | [diff] [blame] | 283 | jenkins_archive_folder=merge/outdir |
| 284 | individual_report_folder=trace_report/index.html |
| 285 | ;; |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 286 | *) |
| 287 | exit 0;; |
| 288 | esac |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 289 | OUTDIR=${WORKSPACE}/${jenkins_archive_folder} |
| 290 | source "$CI_ROOT/script/qa-code-coverage.sh" |
| 291 | export MERGE_CONFIGURATION="$OUTDIR/merge_configuration.json" |
| 292 | COVERAGE_FOLDER=lcov |
| 293 | cd $WORKSPACE |
| 294 | deploy_qa_tools |
| 295 | cd - |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 296 | mkdir -p $OUTDIR |
| 297 | pushd $OUTDIR |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 298 | number_of_files_to_merge=$(create_merge_cfg) |
Saul Romero | 568f5d7 | 2023-11-29 11:58:34 +0000 | [diff] [blame] | 299 | echo "Merging from $number_of_files_to_merge code coverage reports..." |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 300 | # Only merge when more than 1 test result |
Saul Romero | 072236d | 2023-12-06 11:57:16 +0000 | [diff] [blame] | 301 | if [ "$number_of_files_to_merge" -lt 2 ]; then |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 302 | echo "Only one file to merge." |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 303 | exit 0 |
| 304 | fi |
Paul Sokolovsky | 227f2cc | 2021-12-21 10:58:26 +0300 | [diff] [blame] | 305 | |
Saul Romero | 568f5d7 | 2023-11-29 11:58:34 +0000 | [diff] [blame] | 306 | source ${WORKSPACE}/qa-tools/coverage-tool/coverage-reporting/merge.sh \ |
Saul Romero | 690348e | 2024-08-20 16:05:40 +0100 | [diff] [blame] | 307 | -j $MERGE_CONFIGURATION -l ${OUTDIR}/${COVERAGE_FOLDER} \ |
| 308 | -w $WORKSPACE -c -i -d |
Saul Romero | 072236d | 2023-12-06 11:57:16 +0000 | [diff] [blame] | 309 | # backward compatibility with old qa-tools |
| 310 | [ $? -eq 0 ] && status=true || status=false |
Paul Sokolovsky | 227f2cc | 2021-12-21 10:58:26 +0300 | [diff] [blame] | 311 | |
Saul Romero | 072236d | 2023-12-06 11:57:16 +0000 | [diff] [blame] | 312 | # merged_status is set at 'merge.sh' indicating if merging reports was ok |
| 313 | ${merged_status:-$status} && generate_code_coverage_summary "${REPORT_HTML}" |
Saul Romero | 690348e | 2024-08-20 16:05:40 +0100 | [diff] [blame] | 314 | generate_code_coverage_column "${REPORT_HTML}" || true |
Saul Romero | 072236d | 2023-12-06 11:57:16 +0000 | [diff] [blame] | 315 | cp "${REPORT_HTML}" "$OUTDIR" |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 316 | popd |