summaryrefslogtreecommitdiff
path: root/util/zephyr_to_resultdb.py
blob: 6c378bde5ccc98e4afff9bc75cc2b97120496be3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env python3
# Copyright 2022 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
""" Upload twister results to ResultDB

    Usage:
    $ rdb stream -new -realm chromium:public -- ./util/zephyr_to_resultdb.py
      --results=twister-out/twister.json --upload=True
"""

import argparse
import base64
import datetime
import json
import os
import pathlib
import re

import requests  # pylint: disable=import-error


def translate_status(status):
    """Translates ZTEST status to ResultDB status"""
    ret_status = "SKIP"

    if status == "passed":
        ret_status = "PASS"
    elif status == "failed":
        ret_status = "FAIL"
    elif status in ["skipped", "filtered"]:
        ret_status = "SKIP"

    return ret_status


def translate_expected(status):
    """Translates ZTEST status to ResultDB expected"""
    flag = False

    if status in ["passed", "filtered"]:
        flag = True

    return flag


def translate_duration(testcase):
    """Translates ZTEST execution_time to ResultDB duration"""
    time = testcase.get("execution_time")
    if not time:
        return None

    return f"{float(time)/1000:.9f}s"


def testcase_summary(testcase):
    """Translates ZTEST testcase to ResultDB summaryHtml"""
    html = "<p>None</p>"

    if (
        "log" in testcase
        or "reason" in testcase
        or translate_status(testcase["status"]) == "SKIP"
    ):
        html = '<p><text-artifact artifact-id="test_log"></p>'

    return html


def testcase_artifact(testcase):
    """Translates ZTEST testcase to ResultDB artifact"""
    artifact = "Unknown"

    if "log" in testcase and testcase["log"]:
        artifact = testcase["log"]
    elif "reason" in testcase and testcase["reason"]:
        artifact = testcase["reason"]
    elif testcase["status"] == "filtered":
        artifact = "filtered"
    elif testcase["status"] == "skipped":
        artifact = "skipped"

    return base64.b64encode(artifact.encode())


def testsuite_artifact(testsuite):
    """Translates ZTEST testcase to ResultDB artifact"""
    artifact = "Unknown"

    if "log" in testsuite and testsuite["log"]:
        artifact = testsuite["log"]

    return base64.b64encode(artifact.encode())


def testcase_to_result(testsuite, testcase, base_tags, config_tags):
    """Translates ZTEST testcase to ResultDB format
    See TestResult type in
    https://crsrc.org/i/go/src/go.chromium.org/luci/resultdb/sink/proto/v1/test_result.proto
    """
    result = {
        "testId": testcase["identifier"],
        "status": translate_status(testcase["status"]),
        "expected": translate_expected(testcase["status"]),
        "summaryHtml": testcase_summary(testcase),
        "artifacts": {
            "test_log": {
                "contents": testcase_artifact(testcase),
            },
            "testsuite_log": {
                "contents": testsuite_artifact(testsuite),
            },
        },
        "tags": [
            {"key": "platform", "value": testsuite["platform"]},
        ],
        "duration": translate_duration(testcase),
        "testMetadata": {"name": testcase["identifier"]},
    }

    for (key, value) in base_tags:
        result["tags"].append({"key": key, "value": value})

    for (key, value) in config_tags:
        result["tags"].append({"key": key.lower(), "value": value})

    if result["status"] == "FAIL" and "log" in testcase and testcase["log"]:
        assert_msg = re.findall(
            r"Assertion failed.*$", testcase["log"], re.MULTILINE
        )
        result["failureReason"] = {"primaryErrorMessage": assert_msg[0]}

    return result


def get_testsuite_config_tags(twister_dir, testsuite):
    """Creates config tags from the testsuite"""
    config_tags = []
    suite_path = f"{twister_dir}/{testsuite['platform']}/{testsuite['name']}"
    dot_config = f"{suite_path}/zephyr/.config"

    if pathlib.Path(dot_config).exists():
        with open(dot_config) as file:
            lines = file.readlines()

            for line in lines:
                # Ignore empty lines and comments
                if line.strip() and not line.startswith("#"):
                    result = re.search(r"(\w+)=(.+$)", line)
                    config_tags.append((result.group(1), result.group(2)))
    else:
        print(f"Can't find config file for {testsuite['name']}")

    return config_tags


def create_base_tags(data):
    """Creates base tags needed for Testhaus"""
    base_tags = []

    queued_time = datetime.datetime.fromisoformat(
        data["environment"]["run_date"]
    )
    base_tags.append(
        ("queued_time", queued_time.strftime("%Y-%m-%d %H:%M:%S.%f UTC"))
    )

    base_tags.append(("zephyr_version", data["environment"]["zephyr_version"]))
    base_tags.append(("board", data["environment"]["os"]))
    base_tags.append(("toolchain", data["environment"]["toolchain"]))

    return base_tags


def json_to_resultdb(result_file):
    """Translates Twister json test report to ResultDB format"""
    with open(result_file) as file:
        data = json.load(file)
        results = []
        base_tags = create_base_tags(data)

        for testsuite in data["testsuites"]:
            config_tags = get_testsuite_config_tags(
                os.path.dirname(result_file), testsuite
            )
            for testcase in testsuite["testcases"]:
                if testcase["status"]:
                    results.append(
                        testcase_to_result(
                            testsuite, testcase, base_tags, config_tags
                        )
                    )

        file.close()

    return results


class BytesEncoder(json.JSONEncoder):
    """Encoder for ResultDB format"""

    def default(self, obj):
        if isinstance(obj, bytes):
            return obj.decode("utf-8")
        return json.JSONEncoder.default(self, obj)


def upload_results(results):
    """Upload results to ResultDB"""
    with open(os.environ["LUCI_CONTEXT"]) as file:
        sink = json.load(file)["result_sink"]

    # Uploads all test results at once.
    res = requests.post(
        url="http://%s/prpc/luci.resultsink.v1.Sink/ReportTestResults"
        % sink["address"],
        headers={
            "Content-Type": "application/json",
            "Accept": "application/json",
            "Authorization": "ResultSink %s" % sink["auth_token"],
        },
        data=json.dumps({"testResults": results}, cls=BytesEncoder),
    )
    res.raise_for_status()


def main():
    """main"""
    # Set up argument parser.
    parser = argparse.ArgumentParser(
        description=("Upload Zephyr Twister test results to ResultDB")
    )
    parser.add_argument("--results")
    parser.add_argument("--upload", default=False)
    args = parser.parse_args()

    if args.results:
        print("Converting:", args.results)
        rdb_results = json_to_resultdb(args.results)
        if args.upload:
            upload_results(rdb_results)
    else:
        raise Exception("Missing test result file for conversion")


if __name__ == "__main__":
    main()