1#!/usr/bin/python
2###
3### Copyright (C) 2011 Texas Instruments
4###
5### Licensed under the Apache License, Version 2.0 (the "License");
6### you may not use this file except in compliance with the License.
7### You may obtain a copy of the License at
8###
9###      http://www.apache.org/licenses/LICENSE-2.0
10###
11### Unless required by applicable law or agreed to in writing, software
12### distributed under the License is distributed on an "AS IS" BASIS,
13### WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14### See the License for the specific language governing permissions and
15### limitations under the License.
16###
17
18#
19# TI Android Audio team master test runner script.
20#
21
22import sys, os, os.path, time
23import signal
24import TestFlinger
25
26###
27### Configure tests here
28###
29### Each element is a dict that will be passed to the TestCase
30### constructor.  See the documentation for TestCase.__init__()
31### for key/value pair documentation.
32###
33
34g_tests = [
35    # These first tests are simply to verify that the test
36    # framework is working.
37    { 'filename': '/bin/true',
38      'args':     None,
39      'timeout':  None,
40      'expect-fail': False, },
41    { 'filename': '/bin/false',
42      'args':     None,
43      'timeout':  None,
44      'expect-fail': True, },
45    { 'filename': 'test-signal',
46      'args':     None,
47      'timeout':  None,
48      'expect-signal': True, },
49
50    # Actual product tests here
51
52    { 'filename': 'music-monkey.py',
53      'timeout':  60*30, },
54    ]
55
56# These executables are known to be needed for the tests.
57# They must be available explicitly or in the current PATH.
58
59g_required_executables = [
60    '/bin/bash',
61    '/bin/false',
62    '/bin/sh',
63    '/bin/true',
64    'adb',
65    'bash',
66    'monkeyrunner',
67    'pgrep',
68    'python',
69    'sh',
70    ]
71
72# These are files that
73g_required_files = [
74    ]
75
76###
77### Signal handler
78###
79
80g_tcase = None
81def sigint_handler(signum, stack_frame):
82    global g_tcase
83    print
84    print "Received SIGINT, aborting current test..."
85    if g_tcase is not None:
86        g_tcase.kill()
87    sys.exit(signum)
88
89###
90### Utility functions
91###
92
93def check_for_executable(name):
94    """Checks that executable is available (either explicitly or in the PATH
95    returns True if so, False if not.
96    """
97    err = os.system("which %s > /dev/null" % (name,))
98    return (err == 0)
99
100def check_for_file(name):
101    return os.path.exists(name)
102
103def check_adb_server_running():
104    """Checks that the ADB server is currently running.  Returns  True if
105    so, False otherwise.  Uses the pgrep command.
106    """
107    err = os.system("pgrep adb > /dev/null")
108    return (err == 0)
109
110def sanity_check():
111    """Checks that required binaries are available and functioning in a sane manner,
112    returns True if so, False if something is missing.  It checks that things like
113    adb and monkeyrunner are in the path
114    """
115
116    rv = True
117    for F in g_required_executables:
118        ok = check_for_executable(F)
119        if not ok:
120            print "ERROR: cannot find the executable '%s'" % (F,)
121            rv = False
122    for F in g_required_files:
123        ok = check_for_file(F)
124        if not ok:
125            print "ERROR: cannot find the file '%s'" % (F,)
126            rv = False
127
128    ok = check_adb_server_running()
129    if not ok:
130        print "ERROR: the adb server must be running before starting tests"
131        rv = False
132
133    return rv
134
135###
136### Main test script
137###
138
139def main(argv = []):
140    global g_tests, g_tcase
141
142    g_tcase = None
143    signal.signal(signal.SIGINT, sigint_handler)
144
145    if not sanity_check():
146        return 1
147
148    err = os.system("adb root")
149    if err != 0:
150        print "ERROR: could not set adb to run as root.  Aborting."
151        return 1
152
153    time.sleep(2.0) # Wait for device to restart its server
154
155    log = TestFlinger.setup_logfile()
156
157    for N in g_tests:
158        tcase = TestFlinger.TestCase(N, log)
159        g_tcase = tcase
160
161        ok = tcase.start()
162        if not ok:
163            print "ERROR: could not start test '%s'.  Skipping" % (N['filename'],)
164
165        if ok:
166            tcase.wait()
167            verdict = tcase.verdict()
168            verdict = N['filename'] + ": " + verdict
169        else:
170            verdict = N['filename'] + ": FAIL"
171
172        print verdict
173        log.write("\n" + verdict + "\n")
174        log.flush()
175
176    TestFlinger.close_logfile(log)
177
178    return 0
179
180if __name__ == "__main__":
181    rv = main(sys.argv)
182    sys.exit(rv)
183
184