summaryrefslogtreecommitdiff
path: root/bin/insanity-run
blob: bc73f2c8fcbb7d18a55fea556a92af16f5d16ffe (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/env python
# -*- mode: python; -*-
#
# Copyright (c) 2008 Nokia Corporation
#
# 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., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301, USA.
#
# Authors: Rene Stadler <rene.stadler@nokia.com>
#

import sys
import os
import argparse

import pygtk
pygtk.require("2.0")
del pygtk

import insanity
import insanity.config

from insanity.client import CommandLineTesterClient
from insanity.testrun import TestRun, XmlTestRun

from insanity.storage.sqlite import SQLiteStorage
from insanity.generators.filesystem import FileSystemGenerator, URIFileSystemGenerator
from insanity.generators.playlist import PlaylistGenerator
from insanity.generators.external import ExternalGenerator
from insanity.generators.constant import ConstantGenerator
from insanity.monitor import ValgrindMemCheckMonitor, GDBMonitor, TerminalRedirectionMonitor

generators = {"filesystem": FileSystemGenerator,
              "urifilesystem": URIFileSystemGenerator,
              "playlist": PlaylistGenerator,
              "external": ExternalGenerator,
              "constant": ConstantGenerator}

class Client(CommandLineTesterClient):

    __software_name__ = "insanity-run"

    def __init__(self, verbose=False, singlerun=True, *a, **kw):

        CommandLineTesterClient.__init__(self, verbose=verbose, singlerun=singlerun, *a, **kw)

class ArgumentParser(argparse.ArgumentParser):

    def __init__(self):

        argparse.ArgumentParser.__init__(self)

        self.add_argument("-s",
                        "--storage",
                        dest="storage",
                        action="store",
                        help="configure data storage (default: sqlite:testrun.db)",
                        metavar="SPEC",
                        default="sqlite:testrun.db")
        self.add_argument("-o",
                        "--output",
                        dest="output",
                        action="store",
                        help="output directory (default: current)",
                        metavar="DIRECTORY",
                        default=".")
        self.add_argument("-x",
                        "--xmlpath",
                        dest="xmlpath",
                        action="store",
                        help="Path to an XML file describing the tests to run",
                        metavar="XMLPATH",
                        default=None)
        self.add_argument("-T",
                        "--tests",
                        dest="tests",
                        action="store",
                        help="tests directory (default: %s)" % insanity.config.Config.test_dir,
                        metavar="TESTS",
                        default=insanity.config.Config.test_dir)
        self.add_argument("-l",
                        "--substitutes-list",
                        dest="substitutes",
                        help="List of words to substitues in the XML file " \
                            "in the form of '-l old:new,old1:new1'",
                        metavar="SUBSTITUTES",
                        default=None)
        self.add_argument("-t",
                        "--test",
                        dest="test",
                        help="test or scenario to run (pass help for list of tests)",
                        metavar="TESTNAME",
                        default=None)
        self.add_argument("-a",
                        "--args",
                        dest="args",
                        nargs="+",
                        action="store",
                        help="set test arguments (pass help for list of arguments)",
                        metavar="SPEC",
                        default=None)
        self.add_argument("--gdb",
                        dest="gdb",
                        action="store_true",
                        help="Use gdb to gather a stack trace after a crash",
                        default=None)
        self.add_argument("--valgrind",
                        dest="valgrind",
                        action="store_true",
                        help="run tests on valgrind",
                        default=None)
        self.add_argument("--valgrind-supp",
                        dest="supp",
                        action="append",
                        help="add a valgrind suppression file to use",
                        metavar="SUPP",
                        default=None)
        self.add_argument("--compress-output-files",
                        dest="compress_output",
                        action="store_true",
                        help="Whether to compress the output files",
                        default=False)

    def parse_args(self, a):
        options = argparse.ArgumentParser.parse_args(self, a)
        options.storage = self.__parse_storage(options.storage)
        options.args = self.__parse_args(options.args)
        options.substitutes = self.__parse_subsitutes(options.substitutes)

        return options

    def __parse_subsitutes(self, value):
        dic = {}

        if not value:
            return dic

        for sub in value.split(","):
            try:
                o, n = sub.split(":")
            except ValueError, e:
                print "Wrong key value pair: %s, Reason %s" %(sub, e)
                continue
            dic[o] = n

        return dic

    def __parse_storage(self, value):

        if not value or value == "help" or not ":" in value:
            return "help"

        type_ = value.split(":")[0]
        arg = value[len(type_)+1:]

        return (type_, arg,)

    def __parse_args(self, args):
        if args is None:
            return None

        if args == "help" or "help" in args:
            return "help"

        result = []
        for arg in args:
            if not ":" in arg:
                return "help"
            (arg_name, rest) = arg.split(":", 1)

            found = False
            for generator in generators.keys():
                if rest.startswith(generator + ":"):
                    found = True
                    break

            if not found:
                gen_name = "constant"
                gen_args = rest
            else:
                (gen_name, gen_args) = rest.split(":", 1)
            result.append((arg_name, gen_name, gen_args,))

        return result

def storage_help():

    print "Possible arguments for --storage (-s):"
    # TODO: Just supporting sqlite for now:
    print "  sqlite:<DATABASE-FILENAME>"

def test_help():

    print "Possible arguments for --test (-t):"
    all_tests = list(insanity.utils.list_available_tests())
    all_tests.extend(insanity.utils.list_available_scenarios())
    for test in sorted(all_tests):
        print "  %s (%s)" % (test.__test_name__, test.__test_description__,)

def args_help(test_name):

    print "Usage for --args (-a) option:"
    print "  --args ARG ARG1..."
    print "Each ARG in the space separated list takes the following form:"
    print "  ARGLIST:[GENERATOR:]GENERATOR-ARGUMENTS"
    print "ARGLIST is a single argument. If no generator is provided a constant"
    print "value is used for the argument, otherwise the generator should generate"
    print "the appropriate number of arguments."
    print ""
    print "Possible generators and arguments:"
    print "  filesystem:PATH"
    print "  urifilesystem:PATH"
    print "  playlist:PATH"
    print "  external:COMMANDLINE"
    print ""
    print "Examples:"
    print "  uri:file://foo/bar"
    print "  uri:urifilesystem:/testclips"
    print "  uri:playlist:/home/user/playlist.txt"
    print "  uri:external:\"find `pwd` | sed -e s:^:file\\\://:\""
    print "  uri:urifilesystem:/testclips videodec:playlist:/decoders"

    if not test_name:
        return

    test = insanity.utils.get_test_metadata(test_name)
    print ""
    print "Arguments for test %s:" % test_name

    args = test.getFullArgumentList()
    for arg in args:
        print "  %s: %s" % (arg, args[arg]["description"])
        print "    description: %s" % (args[arg]["full_description"])
        print "    type: %s" % (args[arg]["type"])
        print "    global: %d" % (args[arg]["global"])
        print "    default value: %s" % (str(args[arg]["default_value"]))
        print ""

def storage_closed():
    pass

def main():

    error = False
    parser = ArgumentParser()
    options = parser.parse_args(sys.argv[1:])

    if options.storage == "help":
        storage_help()
        return True

    if options.args == "help":
        args_help(options.test)
        return True

    insanity.utils.scan_for_tests(options.tests)

    if options.test == "help":
        test_help()
        return True
    elif options.test is None and options.xmlpath is None:
        parser.print_help()
        return True

    if options.test:
        test = insanity.utils.get_test_metadata(options.test)

        # our monitors
        monitors = []

        if options.gdb:
            gdbscriptfile = os.path.join(os.path.dirname(os.path.abspath(__file__)), "gdb.instructions")
            if not os.path.exists (gdbscriptfile):
                gdbscriptfile = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "share", "insanity", "gdb.instructions")
            if not os.path.exists (gdbscriptfile):
                print
                return True
            else:
                monitors.append((GDBMonitor, {"gdb-script" : gdbscriptfile}))

        if options.valgrind:
            monitors.append((ValgrindMemCheckMonitor,
                             {"suppression-files":options.supp}))

        monitors.append((TerminalRedirectionMonitor,
                         {"compress-outputfiles":options.compress_output}))

        test_arguments = {}
        for arg_name, gen_name, gen_args in options.args or []:
            if not gen_name or not gen_name in generators.keys():
                args_help(options.test)
                return True

            # FIXME: Hardcoded list.
            gen_class = generators[gen_name]

            if gen_args:
                # FIXME:
                if gen_class == PlaylistGenerator:
                    gen = gen_class(location=gen_args)
                elif gen_class == ExternalGenerator:
                    gen = gen_class(command=gen_args)
                elif gen_class == ConstantGenerator:
                    gen = gen_class(constant=gen_args)
                else:
                    gen = gen_class(paths=[gen_args])
            else:
                gen = gen_class()

            test_arguments[arg_name] = gen

        test_run = TestRun(maxnbtests=1, workingdir=options.output)
        try:
            test_run.addTest(test, arguments=test_arguments, monitors=monitors)
        except Exception, e:
            print 'Error: exception adding test: ', e
            error = True
    else:
        try:
            test_run = XmlTestRun(options.xmlpath, substitutes=options.substitutes, workingdir=options.output)
        except Exception, e:
            print 'Error: creating XmlTestRun ', e
            error = True

    if not error:
        storage_name, storage_args = options.storage
        if storage_name == "sqlite":
            storage = SQLiteStorage(path=storage_args)
        else:
            # FIXME: Support other storage backends.
            storage_help()
            return True

            # From now on, when returning on error, call: storage.close(callback=storage_closed)

            storage.close(callback=storage_closed)
            error = True

        client = Client()
        client.setStorage(storage)
        client.addTestRun(test_run)
        client.run()

    return error

if __name__ == "__main__":
    if main():
        sys.exit(1)