Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
| 4 | A simple testing framework for lldb using python's unit testing framework. |
| 5 | |
| 6 | Tests for lldb are written as python scripts which take advantage of the script |
| 7 | bridging provided by LLDB.framework to interact with lldb core. |
| 8 | |
| 9 | A specific naming pattern is followed by the .py script to be recognized as |
| 10 | a module which implements a test scenario, namely, Test*.py. |
| 11 | |
| 12 | To specify the directories where "Test*.py" python test scripts are located, |
| 13 | you need to pass in a list of directory names. By default, the current |
| 14 | working directory is searched if nothing is specified on the command line. |
| 15 | """ |
| 16 | |
Johnny Chen | 91960d3 | 2010-09-08 20:56:16 +0000 | [diff] [blame] | 17 | import os, signal, sys, time |
Johnny Chen | 75e28f9 | 2010-08-05 23:42:46 +0000 | [diff] [blame] | 18 | import unittest2 |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 19 | |
Johnny Chen | 877c7e4 | 2010-08-07 00:16:07 +0000 | [diff] [blame] | 20 | class _WritelnDecorator(object): |
| 21 | """Used to decorate file-like objects with a handy 'writeln' method""" |
| 22 | def __init__(self,stream): |
| 23 | self.stream = stream |
| 24 | |
| 25 | def __getattr__(self, attr): |
| 26 | if attr in ('stream', '__getstate__'): |
| 27 | raise AttributeError(attr) |
| 28 | return getattr(self.stream,attr) |
| 29 | |
| 30 | def writeln(self, arg=None): |
| 31 | if arg: |
| 32 | self.write(arg) |
| 33 | self.write('\n') # text-mode streams translate to \r\n if needed |
| 34 | |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 35 | # |
| 36 | # Global variables: |
| 37 | # |
| 38 | |
| 39 | # The test suite. |
Johnny Chen | 75e28f9 | 2010-08-05 23:42:46 +0000 | [diff] [blame] | 40 | suite = unittest2.TestSuite() |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 41 | |
Johnny Chen | 91960d3 | 2010-09-08 20:56:16 +0000 | [diff] [blame] | 42 | # Delay startup in order for the debugger to attach. |
| 43 | delay = False |
| 44 | |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 45 | # Default verbosity is 0. |
| 46 | verbose = 0 |
| 47 | |
| 48 | # By default, search from the current working directory. |
| 49 | testdirs = [ os.getcwd() ] |
| 50 | |
Johnny Chen | 877c7e4 | 2010-08-07 00:16:07 +0000 | [diff] [blame] | 51 | # Separator string. |
| 52 | separator = '-' * 70 |
| 53 | |
Johnny Chen | 840d8e3 | 2010-08-17 23:00:13 +0000 | [diff] [blame] | 54 | # Decorated sys.stderr for our consumption. |
| 55 | err = _WritelnDecorator(sys.stderr) |
Johnny Chen | 877c7e4 | 2010-08-07 00:16:07 +0000 | [diff] [blame] | 56 | |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 57 | |
| 58 | def usage(): |
| 59 | print """ |
| 60 | Usage: dotest.py [option] [args] |
| 61 | where options: |
| 62 | -h : print this help message and exit (also --help) |
Johnny Chen | 91960d3 | 2010-09-08 20:56:16 +0000 | [diff] [blame] | 63 | -d : delay startup for 10 seconds (in order for the debugger to attach) |
Johnny Chen | d0c24b2 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 64 | -t : trace lldb command execution and result |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 65 | -v : do verbose mode of unittest framework |
| 66 | |
| 67 | and: |
| 68 | args : specify a list of directory names to search for python Test*.py scripts |
| 69 | if empty, search from the curret working directory, instead |
Johnny Chen | 58f9392 | 2010-06-29 23:10:39 +0000 | [diff] [blame] | 70 | |
| 71 | Running of this script also sets up the LLDB_TEST environment variable so that |
| 72 | individual test cases can locate their supporting files correctly. |
Johnny Chen | fde69bc | 2010-09-14 22:01:40 +0000 | [diff] [blame] | 73 | |
| 74 | Environment variables related to loggings: |
| 75 | |
| 76 | o LLDB_LOG: if defined, specifies the log file pathname for the 'lldb' subsystem |
| 77 | with a default option of 'event process' if LLDB_LOG_OPTION is not defined. |
| 78 | |
| 79 | o GDB_REMOTE_LOG: if defined, specifies the log file pathname for the |
| 80 | 'process.gdb-remote' subsystem with a default option of 'packets' if |
| 81 | GDB_REMOTE_LOG_OPTION is not defined. |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 82 | """ |
| 83 | |
| 84 | |
| 85 | def setupSysPath(): |
| 86 | """Add LLDB.framework/Resources/Python to the search paths for modules.""" |
| 87 | |
| 88 | # Get the directory containing the current script. |
Johnny Chen | a1affab | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 89 | scriptPath = sys.path[0] |
| 90 | if not scriptPath.endswith('test'): |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 91 | print "This script expects to reside in lldb's test directory." |
| 92 | sys.exit(-1) |
| 93 | |
Johnny Chen | a1affab | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 94 | os.environ["LLDB_TEST"] = scriptPath |
Johnny Chen | 9de4ede | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 95 | pluginPath = os.path.join(scriptPath, 'plugins') |
Johnny Chen | 58f9392 | 2010-06-29 23:10:39 +0000 | [diff] [blame] | 96 | |
Johnny Chen | a1affab | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 97 | base = os.path.abspath(os.path.join(scriptPath, os.pardir)) |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 98 | dbgPath = os.path.join(base, 'build', 'Debug', 'LLDB.framework', |
| 99 | 'Resources', 'Python') |
| 100 | relPath = os.path.join(base, 'build', 'Release', 'LLDB.framework', |
| 101 | 'Resources', 'Python') |
Johnny Chen | c202c46 | 2010-09-15 18:11:19 +0000 | [diff] [blame^] | 102 | baiPath = os.path.join(base, 'build', 'BuildAndIntegration', |
| 103 | 'LLDB.framework', 'Resources', 'Python') |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 104 | |
| 105 | lldbPath = None |
| 106 | if os.path.isfile(os.path.join(dbgPath, 'lldb.py')): |
| 107 | lldbPath = dbgPath |
| 108 | elif os.path.isfile(os.path.join(relPath, 'lldb.py')): |
| 109 | lldbPath = relPath |
Johnny Chen | c202c46 | 2010-09-15 18:11:19 +0000 | [diff] [blame^] | 110 | elif os.path.isfile(os.path.join(baiPath, 'lldb.py')): |
| 111 | lldbPath = baiPath |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 112 | |
| 113 | if not lldbPath: |
Johnny Chen | c202c46 | 2010-09-15 18:11:19 +0000 | [diff] [blame^] | 114 | print 'This script requires lldb.py to be in either ' + dbgPath + ',', |
| 115 | print relPath + ', or ' + baiPath |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 116 | sys.exit(-1) |
| 117 | |
| 118 | sys.path.append(lldbPath) |
Johnny Chen | a1affab | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 119 | sys.path.append(scriptPath) |
Johnny Chen | 9de4ede | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 120 | sys.path.append(pluginPath) |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 121 | |
| 122 | |
| 123 | def initTestdirs(): |
| 124 | """Initialize the list of directories containing our unittest scripts. |
| 125 | |
| 126 | '-h/--help as the first option prints out usage info and exit the program. |
| 127 | """ |
| 128 | |
Johnny Chen | 91960d3 | 2010-09-08 20:56:16 +0000 | [diff] [blame] | 129 | global delay |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 130 | global verbose |
| 131 | global testdirs |
| 132 | |
| 133 | if len(sys.argv) == 1: |
| 134 | pass |
| 135 | elif sys.argv[1].find('-h') != -1: |
| 136 | # Print usage info and exit. |
| 137 | usage() |
| 138 | sys.exit(0) |
| 139 | else: |
Johnny Chen | d0c24b2 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 140 | # Process possible trace and/or verbose flag. |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 141 | index = 1 |
Johnny Chen | fde69bc | 2010-09-14 22:01:40 +0000 | [diff] [blame] | 142 | for i in range(1, len(sys.argv)): |
| 143 | if not sys.argv[index].startswith('-'): |
| 144 | # End of option processing. |
| 145 | break |
| 146 | |
Johnny Chen | 91960d3 | 2010-09-08 20:56:16 +0000 | [diff] [blame] | 147 | if sys.argv[index].startswith('-d'): |
| 148 | delay = True |
| 149 | index += 1 |
Johnny Chen | fde69bc | 2010-09-14 22:01:40 +0000 | [diff] [blame] | 150 | elif sys.argv[index].startswith('-t'): |
Johnny Chen | d0c24b2 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 151 | os.environ["LLDB_COMMAND_TRACE"] = "YES" |
| 152 | index += 1 |
Johnny Chen | fde69bc | 2010-09-14 22:01:40 +0000 | [diff] [blame] | 153 | elif sys.argv[index].startswith('-v'): |
Johnny Chen | d0c24b2 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 154 | verbose = 2 |
| 155 | index += 1 |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 156 | |
| 157 | # Gather all the dirs passed on the command line. |
| 158 | if len(sys.argv) > index: |
| 159 | testdirs = map(os.path.abspath, sys.argv[index:]) |
| 160 | |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 161 | |
| 162 | def visit(prefix, dir, names): |
| 163 | """Visitor function for os.path.walk(path, visit, arg).""" |
| 164 | |
| 165 | global suite |
| 166 | |
| 167 | for name in names: |
| 168 | if os.path.isdir(os.path.join(dir, name)): |
| 169 | continue |
| 170 | |
| 171 | if '.py' == os.path.splitext(name)[1] and name.startswith(prefix): |
| 172 | # We found a pattern match for our test case. Add it to the suite. |
Johnny Chen | a85d7ee | 2010-06-26 00:19:32 +0000 | [diff] [blame] | 173 | if not sys.path.count(dir): |
| 174 | sys.path.append(dir) |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 175 | base = os.path.splitext(name)[0] |
Johnny Chen | 75e28f9 | 2010-08-05 23:42:46 +0000 | [diff] [blame] | 176 | suite.addTests(unittest2.defaultTestLoader.loadTestsFromName(base)) |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 177 | |
| 178 | |
| 179 | # |
| 180 | # Start the actions by first setting up the module search path for lldb, |
| 181 | # followed by initializing the test directories, and then walking the directory |
| 182 | # trees, while collecting the tests into our test suite. |
| 183 | # |
| 184 | setupSysPath() |
| 185 | initTestdirs() |
Johnny Chen | 91960d3 | 2010-09-08 20:56:16 +0000 | [diff] [blame] | 186 | |
| 187 | # |
| 188 | # If '-d' is specified, do a delay of 10 seconds for the debugger to attach. |
| 189 | # |
| 190 | if delay: |
| 191 | def alarm_handler(*args): |
| 192 | raise Exception("timeout") |
| 193 | |
| 194 | signal.signal(signal.SIGALRM, alarm_handler) |
| 195 | signal.alarm(10) |
| 196 | sys.stdout.write("pid=" + str(os.getpid()) + '\n') |
| 197 | sys.stdout.write("Enter RET to proceed (or timeout after 10 seconds):") |
| 198 | sys.stdout.flush() |
| 199 | try: |
| 200 | text = sys.stdin.readline() |
| 201 | except: |
| 202 | text = "" |
| 203 | signal.alarm(0) |
| 204 | sys.stdout.write("proceeding...\n") |
| 205 | pass |
| 206 | |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 207 | for testdir in testdirs: |
| 208 | os.path.walk(testdir, visit, 'Test') |
| 209 | |
| 210 | # Now that we have loaded all the test cases, run the whole test suite. |
Johnny Chen | 840d8e3 | 2010-08-17 23:00:13 +0000 | [diff] [blame] | 211 | err.writeln(separator) |
| 212 | err.writeln("Collected %d test%s" % (suite.countTestCases(), |
Johnny Chen | 877c7e4 | 2010-08-07 00:16:07 +0000 | [diff] [blame] | 213 | suite.countTestCases() != 1 and "s" or "")) |
Johnny Chen | 840d8e3 | 2010-08-17 23:00:13 +0000 | [diff] [blame] | 214 | err.writeln() |
Johnny Chen | 1bfbd41 | 2010-06-29 19:44:16 +0000 | [diff] [blame] | 215 | |
| 216 | # For the time being, let's bracket the test runner within the |
| 217 | # lldb.SBDebugger.Initialize()/Terminate() pair. |
Johnny Chen | 01f2a6a | 2010-08-10 20:23:55 +0000 | [diff] [blame] | 218 | import lldb, atexit |
Johnny Chen | 1bfbd41 | 2010-06-29 19:44:16 +0000 | [diff] [blame] | 219 | lldb.SBDebugger.Initialize() |
Johnny Chen | 01f2a6a | 2010-08-10 20:23:55 +0000 | [diff] [blame] | 220 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
Johnny Chen | 1bfbd41 | 2010-06-29 19:44:16 +0000 | [diff] [blame] | 221 | |
Johnny Chen | 909e5a6 | 2010-07-01 22:52:57 +0000 | [diff] [blame] | 222 | # Create a singleton SBDebugger in the lldb namespace. |
| 223 | lldb.DBG = lldb.SBDebugger.Create() |
| 224 | |
| 225 | # Turn on logging for debugging purposes if ${LLDB_LOG} environment variable is |
Johnny Chen | 1a43726 | 2010-08-16 22:37:45 +0000 | [diff] [blame] | 226 | # defined. Use ${LLDB_LOG} to specify the log file. |
Johnny Chen | 909e5a6 | 2010-07-01 22:52:57 +0000 | [diff] [blame] | 227 | ci = lldb.DBG.GetCommandInterpreter() |
| 228 | res = lldb.SBCommandReturnObject() |
| 229 | if ("LLDB_LOG" in os.environ): |
Johnny Chen | 4e6c888 | 2010-08-17 21:36:09 +0000 | [diff] [blame] | 230 | if ("LLDB_LOG_OPTION" in os.environ): |
| 231 | lldb_log_option = os.environ["LLDB_LOG_OPTION"] |
| 232 | else: |
| 233 | lldb_log_option = "event process" |
Johnny Chen | 909e5a6 | 2010-07-01 22:52:57 +0000 | [diff] [blame] | 234 | ci.HandleCommand( |
Johnny Chen | 4e6c888 | 2010-08-17 21:36:09 +0000 | [diff] [blame] | 235 | "log enable -f " + os.environ["LLDB_LOG"] + " lldb " + lldb_log_option, |
| 236 | res) |
Johnny Chen | 797674b | 2010-07-02 20:35:23 +0000 | [diff] [blame] | 237 | if not res.Succeeded(): |
Johnny Chen | 4e6c888 | 2010-08-17 21:36:09 +0000 | [diff] [blame] | 238 | raise Exception('log enable failed (check LLDB_LOG env variable.') |
Johnny Chen | fde69bc | 2010-09-14 22:01:40 +0000 | [diff] [blame] | 239 | # Ditto for gdb-remote logging if ${GDB_REMOTE_LOG} environment variable is defined. |
Johnny Chen | 1a43726 | 2010-08-16 22:37:45 +0000 | [diff] [blame] | 240 | # Use ${GDB_REMOTE_LOG} to specify the log file. |
| 241 | if ("GDB_REMOTE_LOG" in os.environ): |
Johnny Chen | 4e6c888 | 2010-08-17 21:36:09 +0000 | [diff] [blame] | 242 | if ("GDB_REMOTE_LOG_OPTION" in os.environ): |
| 243 | gdb_remote_log_option = os.environ["GDB_REMOTE_LOG_OPTION"] |
| 244 | else: |
| 245 | gdb_remote_log_option = "packets" |
Johnny Chen | 1a43726 | 2010-08-16 22:37:45 +0000 | [diff] [blame] | 246 | ci.HandleCommand( |
Johnny Chen | 4e6c888 | 2010-08-17 21:36:09 +0000 | [diff] [blame] | 247 | "log enable -f " + os.environ["GDB_REMOTE_LOG"] + " process.gdb-remote " |
| 248 | + gdb_remote_log_option, |
| 249 | res) |
Johnny Chen | 1a43726 | 2010-08-16 22:37:45 +0000 | [diff] [blame] | 250 | if not res.Succeeded(): |
Johnny Chen | 4e6c888 | 2010-08-17 21:36:09 +0000 | [diff] [blame] | 251 | raise Exception('log enable failed (check GDB_REMOTE_LOG env variable.') |
Johnny Chen | 909e5a6 | 2010-07-01 22:52:57 +0000 | [diff] [blame] | 252 | |
Johnny Chen | 7987ac9 | 2010-08-09 20:40:52 +0000 | [diff] [blame] | 253 | # Install the control-c handler. |
| 254 | unittest2.signals.installHandler() |
| 255 | |
| 256 | # Invoke the default TextTestRunner to run the test suite. |
Johnny Chen | 01f2a6a | 2010-08-10 20:23:55 +0000 | [diff] [blame] | 257 | result = unittest2.TextTestRunner(verbosity=verbose).run(suite) |
Johnny Chen | 1bfbd41 | 2010-06-29 19:44:16 +0000 | [diff] [blame] | 258 | |
Johnny Chen | 83f6e51 | 2010-08-13 22:58:44 +0000 | [diff] [blame] | 259 | if ("LLDB_TESTSUITE_FORCE_FINISH" in os.environ): |
| 260 | import subprocess |
| 261 | print "Terminating Test suite..." |
| 262 | subprocess.Popen(["/bin/sh", "-c", "kill %s; exit 0" % (os.getpid())]) |
| 263 | |
Johnny Chen | 01f2a6a | 2010-08-10 20:23:55 +0000 | [diff] [blame] | 264 | # Exiting. |
| 265 | sys.exit(not result.wasSuccessful) |