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') |
| 102 | |
| 103 | lldbPath = None |
| 104 | if os.path.isfile(os.path.join(dbgPath, 'lldb.py')): |
| 105 | lldbPath = dbgPath |
| 106 | elif os.path.isfile(os.path.join(relPath, 'lldb.py')): |
| 107 | lldbPath = relPath |
| 108 | |
| 109 | if not lldbPath: |
| 110 | print 'This script requires lldb.py to be in either ' + dbgPath, |
| 111 | print ' or' + relPath |
| 112 | sys.exit(-1) |
| 113 | |
| 114 | sys.path.append(lldbPath) |
Johnny Chen | a1affab | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 115 | sys.path.append(scriptPath) |
Johnny Chen | 9de4ede | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 116 | sys.path.append(pluginPath) |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 117 | |
| 118 | |
| 119 | def initTestdirs(): |
| 120 | """Initialize the list of directories containing our unittest scripts. |
| 121 | |
| 122 | '-h/--help as the first option prints out usage info and exit the program. |
| 123 | """ |
| 124 | |
Johnny Chen | 91960d3 | 2010-09-08 20:56:16 +0000 | [diff] [blame] | 125 | global delay |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 126 | global verbose |
| 127 | global testdirs |
| 128 | |
| 129 | if len(sys.argv) == 1: |
| 130 | pass |
| 131 | elif sys.argv[1].find('-h') != -1: |
| 132 | # Print usage info and exit. |
| 133 | usage() |
| 134 | sys.exit(0) |
| 135 | else: |
Johnny Chen | d0c24b2 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 136 | # Process possible trace and/or verbose flag. |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 137 | index = 1 |
Johnny Chen | fde69bc | 2010-09-14 22:01:40 +0000 | [diff] [blame] | 138 | for i in range(1, len(sys.argv)): |
| 139 | if not sys.argv[index].startswith('-'): |
| 140 | # End of option processing. |
| 141 | break |
| 142 | |
Johnny Chen | 91960d3 | 2010-09-08 20:56:16 +0000 | [diff] [blame] | 143 | if sys.argv[index].startswith('-d'): |
| 144 | delay = True |
| 145 | index += 1 |
Johnny Chen | fde69bc | 2010-09-14 22:01:40 +0000 | [diff] [blame] | 146 | elif sys.argv[index].startswith('-t'): |
Johnny Chen | d0c24b2 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 147 | os.environ["LLDB_COMMAND_TRACE"] = "YES" |
| 148 | index += 1 |
Johnny Chen | fde69bc | 2010-09-14 22:01:40 +0000 | [diff] [blame] | 149 | elif sys.argv[index].startswith('-v'): |
Johnny Chen | d0c24b2 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 150 | verbose = 2 |
| 151 | index += 1 |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 152 | |
| 153 | # Gather all the dirs passed on the command line. |
| 154 | if len(sys.argv) > index: |
| 155 | testdirs = map(os.path.abspath, sys.argv[index:]) |
| 156 | |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 157 | |
| 158 | def visit(prefix, dir, names): |
| 159 | """Visitor function for os.path.walk(path, visit, arg).""" |
| 160 | |
| 161 | global suite |
| 162 | |
| 163 | for name in names: |
| 164 | if os.path.isdir(os.path.join(dir, name)): |
| 165 | continue |
| 166 | |
| 167 | if '.py' == os.path.splitext(name)[1] and name.startswith(prefix): |
| 168 | # 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] | 169 | if not sys.path.count(dir): |
| 170 | sys.path.append(dir) |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 171 | base = os.path.splitext(name)[0] |
Johnny Chen | 75e28f9 | 2010-08-05 23:42:46 +0000 | [diff] [blame] | 172 | suite.addTests(unittest2.defaultTestLoader.loadTestsFromName(base)) |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 173 | |
| 174 | |
| 175 | # |
| 176 | # Start the actions by first setting up the module search path for lldb, |
| 177 | # followed by initializing the test directories, and then walking the directory |
| 178 | # trees, while collecting the tests into our test suite. |
| 179 | # |
| 180 | setupSysPath() |
| 181 | initTestdirs() |
Johnny Chen | 91960d3 | 2010-09-08 20:56:16 +0000 | [diff] [blame] | 182 | |
| 183 | # |
| 184 | # If '-d' is specified, do a delay of 10 seconds for the debugger to attach. |
| 185 | # |
| 186 | if delay: |
| 187 | def alarm_handler(*args): |
| 188 | raise Exception("timeout") |
| 189 | |
| 190 | signal.signal(signal.SIGALRM, alarm_handler) |
| 191 | signal.alarm(10) |
| 192 | sys.stdout.write("pid=" + str(os.getpid()) + '\n') |
| 193 | sys.stdout.write("Enter RET to proceed (or timeout after 10 seconds):") |
| 194 | sys.stdout.flush() |
| 195 | try: |
| 196 | text = sys.stdin.readline() |
| 197 | except: |
| 198 | text = "" |
| 199 | signal.alarm(0) |
| 200 | sys.stdout.write("proceeding...\n") |
| 201 | pass |
| 202 | |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 203 | for testdir in testdirs: |
| 204 | os.path.walk(testdir, visit, 'Test') |
| 205 | |
| 206 | # 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] | 207 | err.writeln(separator) |
| 208 | err.writeln("Collected %d test%s" % (suite.countTestCases(), |
Johnny Chen | 877c7e4 | 2010-08-07 00:16:07 +0000 | [diff] [blame] | 209 | suite.countTestCases() != 1 and "s" or "")) |
Johnny Chen | 840d8e3 | 2010-08-17 23:00:13 +0000 | [diff] [blame] | 210 | err.writeln() |
Johnny Chen | 1bfbd41 | 2010-06-29 19:44:16 +0000 | [diff] [blame] | 211 | |
| 212 | # For the time being, let's bracket the test runner within the |
| 213 | # lldb.SBDebugger.Initialize()/Terminate() pair. |
Johnny Chen | 01f2a6a | 2010-08-10 20:23:55 +0000 | [diff] [blame] | 214 | import lldb, atexit |
Johnny Chen | 1bfbd41 | 2010-06-29 19:44:16 +0000 | [diff] [blame] | 215 | lldb.SBDebugger.Initialize() |
Johnny Chen | 01f2a6a | 2010-08-10 20:23:55 +0000 | [diff] [blame] | 216 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
Johnny Chen | 1bfbd41 | 2010-06-29 19:44:16 +0000 | [diff] [blame] | 217 | |
Johnny Chen | 909e5a6 | 2010-07-01 22:52:57 +0000 | [diff] [blame] | 218 | # Create a singleton SBDebugger in the lldb namespace. |
| 219 | lldb.DBG = lldb.SBDebugger.Create() |
| 220 | |
| 221 | # Turn on logging for debugging purposes if ${LLDB_LOG} environment variable is |
Johnny Chen | 1a43726 | 2010-08-16 22:37:45 +0000 | [diff] [blame] | 222 | # defined. Use ${LLDB_LOG} to specify the log file. |
Johnny Chen | 909e5a6 | 2010-07-01 22:52:57 +0000 | [diff] [blame] | 223 | ci = lldb.DBG.GetCommandInterpreter() |
| 224 | res = lldb.SBCommandReturnObject() |
| 225 | if ("LLDB_LOG" in os.environ): |
Johnny Chen | 4e6c888 | 2010-08-17 21:36:09 +0000 | [diff] [blame] | 226 | if ("LLDB_LOG_OPTION" in os.environ): |
| 227 | lldb_log_option = os.environ["LLDB_LOG_OPTION"] |
| 228 | else: |
| 229 | lldb_log_option = "event process" |
Johnny Chen | 909e5a6 | 2010-07-01 22:52:57 +0000 | [diff] [blame] | 230 | ci.HandleCommand( |
Johnny Chen | 4e6c888 | 2010-08-17 21:36:09 +0000 | [diff] [blame] | 231 | "log enable -f " + os.environ["LLDB_LOG"] + " lldb " + lldb_log_option, |
| 232 | res) |
Johnny Chen | 797674b | 2010-07-02 20:35:23 +0000 | [diff] [blame] | 233 | if not res.Succeeded(): |
Johnny Chen | 4e6c888 | 2010-08-17 21:36:09 +0000 | [diff] [blame] | 234 | raise Exception('log enable failed (check LLDB_LOG env variable.') |
Johnny Chen | fde69bc | 2010-09-14 22:01:40 +0000 | [diff] [blame] | 235 | # 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] | 236 | # Use ${GDB_REMOTE_LOG} to specify the log file. |
| 237 | if ("GDB_REMOTE_LOG" in os.environ): |
Johnny Chen | 4e6c888 | 2010-08-17 21:36:09 +0000 | [diff] [blame] | 238 | if ("GDB_REMOTE_LOG_OPTION" in os.environ): |
| 239 | gdb_remote_log_option = os.environ["GDB_REMOTE_LOG_OPTION"] |
| 240 | else: |
| 241 | gdb_remote_log_option = "packets" |
Johnny Chen | 1a43726 | 2010-08-16 22:37:45 +0000 | [diff] [blame] | 242 | ci.HandleCommand( |
Johnny Chen | 4e6c888 | 2010-08-17 21:36:09 +0000 | [diff] [blame] | 243 | "log enable -f " + os.environ["GDB_REMOTE_LOG"] + " process.gdb-remote " |
| 244 | + gdb_remote_log_option, |
| 245 | res) |
Johnny Chen | 1a43726 | 2010-08-16 22:37:45 +0000 | [diff] [blame] | 246 | if not res.Succeeded(): |
Johnny Chen | 4e6c888 | 2010-08-17 21:36:09 +0000 | [diff] [blame] | 247 | raise Exception('log enable failed (check GDB_REMOTE_LOG env variable.') |
Johnny Chen | 909e5a6 | 2010-07-01 22:52:57 +0000 | [diff] [blame] | 248 | |
Johnny Chen | 7987ac9 | 2010-08-09 20:40:52 +0000 | [diff] [blame] | 249 | # Install the control-c handler. |
| 250 | unittest2.signals.installHandler() |
| 251 | |
| 252 | # Invoke the default TextTestRunner to run the test suite. |
Johnny Chen | 01f2a6a | 2010-08-10 20:23:55 +0000 | [diff] [blame] | 253 | result = unittest2.TextTestRunner(verbosity=verbose).run(suite) |
Johnny Chen | 1bfbd41 | 2010-06-29 19:44:16 +0000 | [diff] [blame] | 254 | |
Johnny Chen | 83f6e51 | 2010-08-13 22:58:44 +0000 | [diff] [blame] | 255 | if ("LLDB_TESTSUITE_FORCE_FINISH" in os.environ): |
| 256 | import subprocess |
| 257 | print "Terminating Test suite..." |
| 258 | subprocess.Popen(["/bin/sh", "-c", "kill %s; exit 0" % (os.getpid())]) |
| 259 | |
Johnny Chen | 01f2a6a | 2010-08-10 20:23:55 +0000 | [diff] [blame] | 260 | # Exiting. |
| 261 | sys.exit(not result.wasSuccessful) |