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 | |
Gary Morrison | 999a9d7 | 2022-03-14 18:29:06 -0500 | [diff] [blame] | 12 | if echo "$JENKINS_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 | |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 22 | ################################################################# |
| 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 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 |
| 31 | ################################################################ |
| 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 | |
| 38 | server = os.getenv("JENKINS_URL", "https://jenkins.oss.arm.com/") |
| 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 | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 74 | generate_header() { |
| 75 | local cov_html=${OUTDIR}/${COVERAGE_FOLDER}/index.html |
| 76 | local out_report=$1 |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 77 | python3 - << EOF |
| 78 | import re |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 79 | import json |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 80 | cov_html="$cov_html" |
| 81 | out_report = "$out_report" |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 82 | confs = "" |
| 83 | with open("$REPORT_JSON") as json_file: |
| 84 | data = json.load(json_file) |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 85 | |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 86 | with open(cov_html, "r") as f: |
| 87 | html_content = f.read() |
| 88 | items = ["Lines", "Functions", "Branches"] |
| 89 | s = """ |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 90 | <style> |
| 91 | .dropbtn { |
| 92 | background-color: #04AA6D; |
| 93 | color: white; |
| 94 | padding: 16px; |
| 95 | font-size: 16px; |
| 96 | border: none; |
| 97 | } |
| 98 | |
| 99 | /* The container <div> - needed to position the dropdown content */ |
| 100 | .dropdown { |
| 101 | position: relative; |
| 102 | display: inline-block; |
| 103 | } |
| 104 | |
| 105 | /* Dropdown Content (Hidden by Default) */ |
| 106 | .dropdown-content { |
| 107 | display: none; |
| 108 | position: absolute; |
| 109 | background-color: #f1f1f1; |
| 110 | min-width: 160px; |
| 111 | box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); |
| 112 | z-index: 1; |
| 113 | } |
| 114 | |
| 115 | /* Links inside the dropdown */ |
| 116 | .dropdown-content a { |
| 117 | color: black; |
| 118 | padding: 12px 16px; |
| 119 | text-decoration: none; |
| 120 | display: block; |
| 121 | } |
| 122 | |
| 123 | /* Change color of dropdown links on hover */ |
| 124 | .dropdown-content a:hover {background-color: #ddd;} |
| 125 | |
| 126 | /* Show the dropdown menu on hover */ |
| 127 | .dropdown:hover .dropdown-content {display: block;} |
| 128 | |
| 129 | /* Change the background color of the dropdown button when the dropdown content is shown */ |
| 130 | .dropdown:hover .dropbtn {background-color: #3e8e41;} |
| 131 | </style> |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 132 | <div id="div-cov"> |
| 133 | <hr> |
| 134 | <table id="table-cov"> |
| 135 | <tbody> |
| 136 | <tr> |
| 137 | <td>Type</td> |
| 138 | <td>Hit</td> |
| 139 | <td>Total</td> |
| 140 | <td>Coverage</td> |
| 141 | </tr> |
| 142 | """ |
| 143 | for item in items: |
| 144 | data = re.findall(r'<td class="headerItem">{}:</td>\n\s+<td class="headerCovTableEntry">(.+?)</td>\n\s+<td class="headerCovTableEntry">(.+?)</td>\n\s+'.format(item), |
| 145 | html_content, re.DOTALL) |
| 146 | if data is None: |
| 147 | continue |
| 148 | hit, total = data[0] |
| 149 | cov = round(float(hit)/float(total) * 100.0, 2) |
| 150 | color = "success" |
| 151 | if cov < 90: |
| 152 | color = "unstable" |
| 153 | if cov < 75: |
| 154 | color = "failure" |
| 155 | s = s + """ |
| 156 | <tr> |
| 157 | <td>{}</td> |
| 158 | <td>{}</td> |
| 159 | <td>{}</td> |
| 160 | <td class='{}'>{} %</td> |
| 161 | </tr> |
| 162 | """.format(item, hit, total, color, cov) |
| 163 | s = s + """ |
| 164 | </tbody> |
| 165 | </table> |
| 166 | <p> |
Saul Romero | 568f5d7 | 2023-11-29 11:58:34 +0000 | [diff] [blame] | 167 | <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] | 168 | </p> |
| 169 | </div> |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 170 | |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 171 | <script> |
| 172 | document.getElementById('tf-report-main').appendChild(document.getElementById("div-cov")); |
| 173 | </script> |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 174 | |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 175 | """ |
| 176 | with open(out_report, "a") as f: |
| 177 | f.write(s) |
| 178 | EOF |
| 179 | } |
Saul Romero | 568f5d7 | 2023-11-29 11:58:34 +0000 | [diff] [blame] | 180 | |
| 181 | generate_cols() { |
| 182 | echo "List of merged build ids:${list_of_merged_builds[@]}" |
| 183 | python3 - << EOF |
| 184 | merged_ids=[int(i) for i in "${list_of_merged_builds[@]}".split()] |
| 185 | s = """ |
| 186 | |
| 187 | <script> |
| 188 | window.onload = function() { |
| 189 | """ + f"const mergedIds={merged_ids}" + """ |
| 190 | document.querySelector('#tf-report-main table').querySelectorAll("tr").forEach((row,i) => { |
| 191 | const cell = document.createElement(i ? "td" : "th") |
| 192 | const button = document.createElement("button") |
| 193 | button.textContent = "Report" |
| 194 | if (i) { |
| 195 | merged = false |
| 196 | if (q = row.querySelector('td.success a.buildlink')) { |
| 197 | href = q.href |
| 198 | buildId = href.split("/").at(-2) |
| 199 | if (mergedIds.include(buildId)) { |
| 200 | cell.classList.add("success") |
| 201 | const url = href.replace('console', 'artifact/trace_report/index.html') |
| 202 | button.addEventListener('click', () => { |
| 203 | window.open(url, "_blank") |
| 204 | }) |
| 205 | cell.appendChild(button) |
| 206 | merged = true |
| 207 | } |
| 208 | } |
| 209 | if (!merged) { |
| 210 | cell.innerText = "N/A" |
| 211 | cell.classList.add("failure") |
| 212 | } |
| 213 | } |
| 214 | else { |
| 215 | cell.innerText = "Code Coverage" |
| 216 | } |
| 217 | row.appendChild(cell) |
| 218 | }) |
| 219 | } |
| 220 | </script> |
| 221 | """ |
| 222 | with open("$1", "a") as f: |
| 223 | f.write(s) |
| 224 | EOF |
| 225 | } |
| 226 | |
| 227 | |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 228 | OUTDIR="" |
| 229 | index="" |
| 230 | case "$TEST_GROUPS" in |
| 231 | scp*) |
| 232 | project="scp" |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 233 | jenkins_archive_folder=reports;; |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 234 | tf*) |
| 235 | project="trusted_firmware" |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 236 | jenkins_archive_folder=merge/outdir;; |
Saul Romero | 82bcfb0 | 2023-06-27 16:24:13 +0100 | [diff] [blame] | 237 | spm*) |
| 238 | project="hafnium" |
| 239 | jenkins_archive_folder=merge/outdir;; |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 240 | *) |
| 241 | exit 0;; |
| 242 | esac |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 243 | OUTDIR=${WORKSPACE}/${jenkins_archive_folder} |
| 244 | source "$CI_ROOT/script/qa-code-coverage.sh" |
| 245 | export MERGE_CONFIGURATION="$OUTDIR/merge_configuration.json" |
| 246 | COVERAGE_FOLDER=lcov |
| 247 | cd $WORKSPACE |
| 248 | deploy_qa_tools |
| 249 | cd - |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 250 | mkdir -p $OUTDIR |
| 251 | pushd $OUTDIR |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 252 | number_of_files_to_merge=$(create_merge_cfg) |
Saul Romero | 568f5d7 | 2023-11-29 11:58:34 +0000 | [diff] [blame] | 253 | echo "Merging from $number_of_files_to_merge code coverage reports..." |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 254 | # Only merge when more than 1 test result |
Saul Romero | cacda17 | 2023-03-10 14:23:41 +0000 | [diff] [blame] | 255 | if [ "$number_of_files_to_merge" -lt 2 ] ; then |
| 256 | echo "Only one file to merge." |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 257 | exit 0 |
| 258 | fi |
Paul Sokolovsky | 227f2cc | 2021-12-21 10:58:26 +0300 | [diff] [blame] | 259 | |
Saul Romero | 568f5d7 | 2023-11-29 11:58:34 +0000 | [diff] [blame] | 260 | source ${WORKSPACE}/qa-tools/coverage-tool/coverage-reporting/merge.sh \ |
| 261 | -j $MERGE_CONFIGURATION -l ${OUTDIR}/${COVERAGE_FOLDER} -w $WORKSPACE -c |
Paul Sokolovsky | 227f2cc | 2021-12-21 10:58:26 +0300 | [diff] [blame] | 262 | |
Saul Romero | 568f5d7 | 2023-11-29 11:58:34 +0000 | [diff] [blame] | 263 | merged_status && generate_header ${REPORT_HTML} |
| 264 | generate_cols ${REPORT_HTML} |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 265 | cp ${REPORT_HTML} $OUTDIR |
Saul Romero | 568f5d7 | 2023-11-29 11:58:34 +0000 | [diff] [blame] | 266 | |
Zelalem | 1af7a7b | 2020-08-04 17:34:32 -0500 | [diff] [blame] | 267 | popd |