File: insanity-dumpresults-json

package info (click to toggle)
gst-qa-system 0.0%2Bgit20110920.4750a8e8-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 980 kB
  • sloc: python: 8,498; xml: 855; makefile: 42
file content (274 lines) | stat: -rwxr-xr-x 10,923 bytes parent folder | download
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/env python

# GStreamer QA system
#
#       insanity-dumpresults-json
#        - Output vaguely lava-dashboard compatible json.
#
# Copyright (c) 2008, Edward Hervey <bilboed@bilboed.com>
# Copyright (c) 2011, Collabora Ltd. <david.laban@collabora.co.uk>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

"""
Dumps the results of a test results DB
"""

import sys
import time
from optparse import OptionParser
from insanity.log import initLogging, warning
import simplejson

# Since the dashboard model puts a limit on the length of attribute keys,
# we may need to truncate them.
MAX_ATTR_LEN = 32

def printTestRunInfo(db, testrunid, verbose=False):
    # id , date, nbtests, client
    cid, starttime, stoptime = db.getTestRun(testrunid)
    softname, clientname, clientuser = db.getClientInfoForTestRun(testrunid)
    nbtests = db.getNbTestsForTestrun(testrunid)
    nbfailed = db.getNbTestsForTestrun(testrunid, failedonly=True)
    print "[% 3d]\tDate:%s\tNbTests:% 5d\tFailed:% 5d\tClient: %s/%s/%s" % (testrunid,
                                                                           time.ctime(starttime),
                                                                           nbtests,
                                                                           nbfailed,
                                                                           softname,
                                                                           clientname,
                                                                           clientuser)
def getMonitorsInfo(db, testid):
    # Return the union of all info about monitors for this test.
    ret = {"args": {}, "results": {}, "extras": {}, "outputfiles": {}}
    monitors = db.getMonitorsIDForTest(testid)

    for mid in monitors or []:
        tid,mtyp,args,results,resperc,extras,outputfiles = db.getFullMonitorInfo(mid)
        ret["args"].update(args)
        ret["results"].update(results)
        ret["extras"].update(extras)
        ret["outputfiles"].update(outputfiles)
    return ret

def getTestName(db, testid):
    _trid, ttype, args, _checks, _resperc, _extras, _outputfiles, parentid, \
            ismon, isscen = db.getFullTestInfo(testid)
    assert not ismon, "Monitors do not have well defined names."

    if parentid:
        parent_name = getTestName(db, parentid)
        # strip the rerun mechanism from results names.
        if "GstMediaTestScenario" in parent_name:
            parent_name = ""
    else:
        parent_name = ""

    if "instance-name" in args:
        name = args["instance-name"]
    else:
        name = ttype

    # Top level tests get the media filename prepended.
    media_name = args.get("uri", "").rpartition("/")[-1]
    if media_name and media_name not in parent_name:
        name = "%s.%s" % (media_name, name)

    if parent_name:
        return "%s.%s" % (parent_name, name)
    else:
        return name

def getTestInfo(db, testid):
    data = {}
    trid, ttype, args, checks, resperc, extras, outputfiles, parentid, \
            ismon, isscen = db.getFullTestInfo(testid)

    if resperc is None:
        # test didn't end up in the database
        return {}

    if resperc == 100:
        data["result"] = "pass"
    elif str(dict(checks).get("no-unexpected-failures")) == "1":
        data["result"] = "expected-failure"
    else:
        data["result"] = "fail"

    data["test_case_id"] = getTestName(db, testid)

    # strip the rerun mechanism from results unless something is wrong.
    if ttype == "GstMediaTestScenario" and \
            dict(checks)["similar-results"]:
        return {}

    monitors = getMonitorsInfo(db, testid)

    logfile = monitors["outputfiles"].get("gst-log-file", "")
    if logfile:
        data["log_filename"] = logfile

    data["duration"] = "0d 0s %u000us" % dict(extras)["test-total-duration"]

    # Flatten extra information into the attributes dict for debugging.
    attributes = {}
    attributes["success-percentage"] = "%0.1f" % resperc
    for k, v in args.items():
        attributes[("arg." + k)[:MAX_ATTR_LEN]] = str(v)
    for k, v in checks:
        attributes[("check." + k)[:MAX_ATTR_LEN]] = str(v)
    for k, v in extras:
        attributes[("extra." + k)[:MAX_ATTR_LEN]] = str(v)
    for k, v in outputfiles.items():
        attributes[("out." + k)[:MAX_ATTR_LEN]] = str(v)
    for k1, subdict in monitors.items():
        for k2, v in subdict.items():
            attributes[("monitor.%s.%s" % (k1, k2))[:MAX_ATTR_LEN]] = str(v)
    data["attributes"] = attributes

    return data

def getTestRun(db, testrunid, failedonly=False, hidescenarios=False):
    # let's output everything !
    cid, starttime, stoptime = db.getTestRun(testrunid)
    softname, clientname, clientuser = db.getClientInfoForTestRun(testrunid)
    environ = db.getEnvironmentForTestRun(testrunid)
    tests = db.getTestsForTestRun(testrunid, withscenarios=not hidescenarios,
                                  failedonly=failedonly)

    test_results = []
    tests_by_name = {}
    for testid in tests:
        data = getTestInfo(db, testid)
        if not data:
            continue
        name = data["test_case_id"]
        tests_by_name[name] = data
        test_results.append(data)

    for name, data in tests_by_name.items():
        if "rerun." in name:
            canonical_name = name.replace("rerun.", "")
            if canonical_name in tests_by_name:
                if tests_by_name[canonical_name]["attributes"]["success-percentage"] == \
                        data["attributes"]["success-percentage"]:
                    test_results.remove(tests_by_name[canonical_name])
                    data["test_case_id"] = canonical_name
                    tests_by_name[canonical_name] = data
                else:
                    warning("%s and %s have different results. " \
                            "Leaving both in report.", canonical_name, name)
            else:
                warning("%s is a rerun, but %s doesn't exist",
                        name, canonical_name)

    for name, data in tests_by_name.items():
        attributes = data.get("attributes", {})
        if "extra.subtest-names" in attributes:
            subtest_names = simplejson.loads(
                    attributes["extra.subtest-names"].replace("'", '"'))
            for subtest_name in subtest_names:
                fullname = "%s.%s" % (name, subtest_name)
                if fullname not in tests_by_name:
                    # It didn't make it into the database. Treat it as
                    # skipped.
                    fake_data = {"test_case_id": fullname, "result": "skip"}
                    tests_by_name[fullname] = fake_data
                    test_results.append(data)

    test_results.sort(key=lambda r: r["test_case_id"])
    return test_results

def printTestRuns(db, testrunids, failedonly=False, hidescenarios=False,
                  indent=None):
    output = {}
    test_results = []

    for testrunid in testrunids:
        result = getTestRun(db, testrunid, failedonly, hidescenarios)
        test_results.extend(result)

    output["test_results"] = test_results

    simplejson.dump(output, sys.stdout, indent=indent, sort_keys=True)
    print # Newline at end of file.

if __name__ == "__main__":
    usage = "usage: %prog database [options]"
    parser = OptionParser(usage=usage)
    parser.add_option("-l", "--list", dest="list",
                      help="List the available test runs with summary",
                      action="store_true",
                      default=False)
    parser.add_option("-a", "--all", dest="all",
                      help="Dump all testruns.",
                      action="store_true",
                      default=False)
    parser.add_option("-t", "--testrun", dest="testrun",
                      help="Specify a testrun id",
                      type=int,
                      default=-1)
    parser.add_option("-i", "--indent", dest="indent",
                      help="Indentation for pretty-printed json. "
                           "Default is to use a compact representation.",
                      type=int,
                      default=None)
    parser.add_option("-f", "--failed", dest="failed",
                      help="Only show failed tests",
                      action="store_true", default=False)
    parser.add_option("-x", "--hidescenarios", dest="hidescenarios",
                      help="Do not show scenarios",
                      action="store_true", default=False)
    parser.add_option("-m", "--mysql", dest="usemysql",
                      default=False, action="store_true",
                      help="Connect to a MySQL database for storage")
    (options, args) = parser.parse_args(sys.argv[1:])
    if not options.usemysql and len(args) != 1:
        print >> sys.stderr, "You need to specify a database file !"
        parser.print_help()
        sys.exit(1)
    initLogging()
    if options.usemysql:
        from insanity.storage.mysql import MySQLStorage
        if len(args):
            kw = MySQLStorage.parse_uri(args[0])
            db = MySQLStorage(async=False, **kw)
        else:
            # use default values
            db = MySQLStorage(async=False)
    else:
        from insanity.storage.sqlite import SQLiteStorage
        db = SQLiteStorage(path=args[0], async=False)
    if options.list:
        # list all available test runs
        testruns = db.listTestRuns()
        for runid in testruns:
            printTestRunInfo(db, runid)
    else:
        testruns = db.listTestRuns()
        if options.testrun and not options.all:
            if not options.testrun in testruns:
                print >> sys.stderr, "Specified testrunid not available !"
                parser.print_help()
                sys.exit(1)
            printTestRuns(db, [options.testrun], options.failed,
                          options.hidescenarios, options.indent)
        else:
            if not testruns:
                print >> sys.stderr, "This file contains no test runs."
                sys.exit(1)
            printTestRuns(db, testruns, options.failed,
                          options.hidescenarios, options.indent)