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