aboutsummaryrefslogtreecommitdiff
path: root/tools/cts.py
blob: 04779a411ecfdec3c30e94092708532374682f64 (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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
from common import get_auth, write_to_file
from constants import (
    GREEN,
    YELLOW,
    ORANGE,
    RED,
)
from jinja2 import Environment, FileSystemLoader
from lava import LavaServerProxy
from sets import Set
from StringIO import StringIO
from tabulate import tabulate
from urlparse import urlsplit
from xml.dom.minidom import parse
from xml.etree import ElementTree as ET
import lxml.etree as LET
import base64
import codecs
import os
import shutil
import zipfile


RESULT_FILE_NAME = "test_result.xml"
FINAL_RESULTS = "results.xml"
FINAL_REPORT = "results.html"
XSLT = "compatibility_result.xsl"
CSS = "compatibility_result.css"
LOGO = "logo.png"
FOCUSED_LIST = [
    "CtsAndroidAppTestCases",
    "CtsAppTestCases",
    "CtsAppWidgetTestCases",
    "CtsBionicTestCases",
    "CtsContentTestCases",
    "CtsGraphicsTestCases",
    "CtsLibcoreLegacy22TestCases ",
    "CtsLibcoreOkHttpTestCases",
    "CtsLibcoreTestCases",
    "CtsWidgetTestCases",
    "CtsSecurityHostTestCases",
    "CtsSecurityTestCases",
    "CtsViewTestCases",
    "CtsVmTestCases",
    "CtsWebkitTestCases",
    "vm-tests-tf"
]

class CtsRow(object):
    def __init__(self, row, colour=GREEN):
        self.row = row
        self.colour = colour


def download_results(args):
    lava_base_url = urlsplit(args.server).netloc
    lava_username, _, lava_password = get_auth(lava_base_url)
    lava_proxy = LavaServerProxy(lava_base_url, lava_username, lava_password)
    basedir = os.path.join(os.getcwd(), args.output)
    try:
        os.makedirs(basedir)
    except:
        if not args.force:
            print "{0} already exists".format(basedir)
            exit(1)
        else:
            shutil.rmtree(basedir)
            os.makedirs(basedir)

    for lava_job_id in args.joblist:
        if not lava_proxy.test_results_available(lava_job_id):
            print "Results not available for %s" % lava_job_id
            continue

        print "Downloading results for job: %s" % lava_job_id
        bundle = lava_proxy.get_test_job_results(lava_job_id)
        host = [t for t in bundle['test_runs'] if t['test_id'] == 'cts-host']
        if host:
            host = host[0]
        else:
            os.exit(1)
        cts_results_base64 = [a['content'] for a in host['attachments'] if a['pathname'].startswith('android-cts')]
        if len(cts_results_base64) == 0:
            print "No CTS results found in job: %s" % lava_job_id
            continue

        print "Extracting results for job: %s" % lava_job_id
        infile = StringIO(cts_results_base64[0])
        outfile = StringIO()
        base64.decode(infile, outfile)
        compressed = zipfile.ZipFile(outfile)
        print "CTS results for job %s: %s" % (lava_job_id, compressed.namelist()[0])

        dirpath = os.path.join(basedir, compressed.namelist()[0])
        print dirpath
        if os.path.exists(dirpath) and os.path.isdir(dirpath):
            if not args.force:
                print "Directory %s exists. Skipping" % compressed.namelist()[0]
                continue
            else:
                shutil.rmtree(dirpath)
        compressed.extractall(basedir)


def combine_results(args):
    basedom = None
    current_dir = os.path.join(os.getcwd(), args.output)
    current_index = 0
    print "Combining CTS results"
    for (index, directory) in enumerate(os.walk(current_dir).next()[1]):
        print directory
        if not directory.startswith(".") and directory not in ['device-info-files', 'tools']:
            if basedom is None:
                shutil.copy("%s/%s" % (os.path.join(current_dir, directory),RESULT_FILE_NAME), current_dir)
                currentsource = "%s/%s" % (os.path.join(current_dir, directory), RESULT_FILE_NAME)
                basedom = parse(currentsource)
                testpackage_list = basedom.getElementsByTagName("Module")
                for testpackage in testpackage_list:
                    packagename = testpackage.getAttribute("name")
                    packageabi = testpackage.getAttribute("abi")
                    print "\t%s (%s)" % (packagename, packageabi)
            else:
                currentsource = "%s/%s" % (os.path.join(current_dir, directory), RESULT_FILE_NAME)
                currentdom = parse(currentsource)
                result = basedom.getElementsByTagName("Result")[0]
                basesummary = basedom.getElementsByTagName("Summary")[0]
                summary = currentdom.getElementsByTagName("Summary")[0]
                for attr in summary.attributes.keys():
                    basevalue = basesummary.getAttribute(attr)
                    basesummary.setAttribute(attr, str(int(summary.getAttribute(attr)) + int(basevalue)))
                testpackage_list = currentdom.getElementsByTagName("Module")
                for testpackage in testpackage_list:
                    packagename = testpackage.getAttribute("name")
                    packageabi = testpackage.getAttribute("abi")
                    print "\t%s (%s)" % (packagename, packageabi)
                    added = False
                    for package in result.getElementsByTagName("Module"):
                        #name = package.getAttribute("appPackageName")
                        name = package.getAttribute("name")
                        if sorted([name, packagename])[0] == packagename:
                            result.insertBefore(testpackage, package)
                            added = True
                            break
                    if not added:
                        result.appendChild(testpackage)

    finalfile_path = os.path.join(current_dir, FINAL_RESULTS)
    finalfile = codecs.open(finalfile_path, "w", "utf-8")
    basedom.writexml(finalfile)
    finalfile.close()

def cts_summary(args, focused_list=None, generate_style=False):
    sourcepath = os.path.join(os.getcwd(), args.output)
    sourcefile = os.path.join(sourcepath, args.sourcefile)
    tree = ET.parse(sourcefile)
    root = tree.getroot()

    testpackages = root.findall(".//Module[@abi='%s']" % args.abi)
    results = []
    cts_styles = []
    totalpass = 0
    totalfail = 0
    totaltimeout = 0
    totalnotexec = 0
    for package in testpackages:
        if focused_list:
            if package.attrib['name'] not in focused_list:
                continue
        testpass = package.findall(".//Test[@result='pass']")
        totalpass = totalpass + len(testpass)
        testfail = package.findall(".//Test[@result='fail']")
        totalfail = totalfail + len(testfail)
        testtimeout = package.findall(".//Test[@result='timeout']")
        totaltimeout = totaltimeout + len(testtimeout)
        testnotexec = package.findall(".//Test[@result='notExecuted']")
        totalnotexec = totalnotexec + len(testnotexec)
        #import pdb;pdb.set_trace()
        if len(testpass) + len(testfail) + len(testtimeout) + len(testnotexec) == 0:
            print "No results for {0}".format(package.attrib['name'])
            continue
        passrate = float(
            len(testpass))/float(len(testpass) + len(testfail) + \
                len(testtimeout) + len(testnotexec))
        results.append([
            package.attrib['name'],
            len(testpass),
            len(testfail),
            len(testtimeout),
            len(testnotexec),
            "{0:.2f}".format(passrate*100)])
        index = len(results)
        ctsrow = CtsRow(index)
        if passrate < 1 and passrate >= 0.5:
            ctsrow.colour = YELLOW
        elif passrate < 0.5 and passrate > 0:
            ctsrow.colour = ORANGE
        elif passrate == 0:
            ctsrow.colour = RED
        cts_styles.append(ctsrow)

    if totalpass + totalfail + totaltimeout + totalnotexec == 0:
        print "No CTS results"
        return
    results.append(["**Total**",
        "**{}**".format(totalpass),
        "**{}**".format(totalfail),
        "**{}**".format(totaltimeout),
        "**{}**".format(totalnotexec),
        "**{0:.2f}**".format(100*float(totalpass)/float(totalpass + \
                totalfail + \
                totaltimeout + \
                totalnotexec))])
    headers=["**package**",
            "**pass**",
            "**fail**",
            "**timeout**",
            "**not executed**",
            "**%**"]

    name = "summary"
    if focused_list is not None:
        name = "focused"
    env = Environment(loader=FileSystemLoader('templates'))
    cts_template = env.get_template("cts")
    cts_output = cts_template.render(
        name = name,
        ctstable = tabulate(results,
        headers=headers,
        floatfmt=".2f",
        tablefmt="rst")
    )
    write_to_file(args, "cts-{0}.rst".format(name), cts_output)

    cts_style = env.get_template("cts.styles")
    styles_output = cts_style.render(
        name = name,
        ctsrows = cts_styles
    )
    write_to_file(args, "cts-{0}.styles".format(name), styles_output)

    if args.savecsv:
        import csv
        ctspath = os.path.join(os.getcwd(), args.output)
        ctsfile = os.path.join(ctspath, "cts.csv")
        with open(ctsfile, "wb") as fp:
            writer = csv.writer(fp, quoting=csv.QUOTE_ALL)
            writer.writerows(results)

def focused_summary(args):
    cts_summary(args, FOCUSED_LIST)

def purge_results(args):
    #remove only files produced by CTS module
    # go through subdirs and check if they contain CTS results
    results_dir = os.path.join(os.getcwd(), args.output)
    cts_dirs = Set()
    # remove files generated by script
    files = ['cts-summary.rst',
        'cts-focused.rst',
        'summary.styles',
        'focused.styles',
        'results.xml',
        'test_result.xml',
        'compatibility_result.css',
        'results.html',
        'logo.png'
    ]
    for f in files:
        path = os.path.join(results_dir, f)
        if os.path.exists(path) and os.path.isfile(path):
            os.unlink(path)
    for subdir, dirs, files in os.walk(results_dir):
        if RESULT_FILE_NAME in files:
            cts_dirs.add(subdir)
            continue
    print cts_dirs
    for subdir in cts_dirs:
        shutil.rmtree(os.path.join(results_dir, subdir))

def __copy_file(expected, final_report_path):
    filename = expected.rsplit("/", 1)[1]
    if not os.path.exists(expected):
        for subdir, dirs, files in os.walk(final_report_path):
            if filename in files:
                shutil.copyfile(os.path.join(final_report_path, subdir, filename),
                    expected)


def produce_html(args):
    final_report_path = os.path.join(os.path.join(os.getcwd(), args.output))
    final_result_file = os.path.join(final_report_path, FINAL_RESULTS)
    final_report_file = os.path.join(final_report_path, FINAL_REPORT)
    xslt_result_file = os.path.join(os.getcwd(), XSLT)
    css_result_file = os.path.join(final_report_path, CSS)
    logo_result_file = os.path.join(final_report_path, LOGO)
    if not os.path.exists(final_result_file):
        print "{0} not found".format(final_result_file)
        return
    for filename in [css_result_file, logo_result_file]:
        __copy_file(filename, final_report_path)

    if os.path.exists(final_report_file) and not args.force:
        return
    dom = LET.parse(final_result_file)
    xslt = LET.parse(xslt_result_file)
    transform = LET.XSLT(xslt)
    newdom = transform(dom)
    write_to_file(args, FINAL_REPORT, LET.tostring(newdom, pretty_print=True))

def produce_pdf(args):
    # call wkhtmltopdf
    pass