4 A simple testing framework for lldb using python's unit testing framework.
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.
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.
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.
20 for available options.
23 import os, signal, sys, time
26 class _WritelnDecorator(object):
27 """Used to decorate file-like objects with a handy 'writeln' method"""
28 def __init__(self,stream):
31 def __getattr__(self, attr):
32 if attr in ('stream', '__getstate__'):
33 raise AttributeError(attr)
34 return getattr(self.stream,attr)
36 def writeln(self, arg=None):
39 self.write('\n') # text-mode streams translate to \r\n if needed
46 suite = unittest2.TestSuite()
48 # The config file is optional.
51 # The dictionary as a result of sourcing configFile.
54 # Delay startup in order for the debugger to attach.
57 # The filter (testcase.testmethod) used to admit tests into our test suite.
60 # If '-g' is specified, the filterspec must be consulted for each test module, default to False.
63 # Ignore the build search path relative to this script to locate the lldb.py module.
66 # By default, we skip long running test case. Use '-l' option to override.
67 skipLongRunningTest = True
69 # The regular expression pattern to match against eligible filenames as our test cases.
72 # By default, tests are executed in place and cleanups are performed afterwards.
73 # Use '-r dir' option to relocate the tests and their intermediate files to a
74 # different directory and to forgo any cleanups. The directory specified must
78 # Default verbosity is 0.
81 # By default, search from the current working directory.
82 testdirs = [ os.getcwd() ]
90 Usage: dotest.py [option] [args]
92 -h : print this help message and exit (also --help)
93 -c : read a config file specified after this option
94 (see also lldb-trunk/example/test/usage-config)
95 -d : delay startup for 10 seconds (in order for the debugger to attach)
96 -f : specify a filter, which consists of the test class name, a dot, followed by
97 the test method, to admit tests into the test suite
98 e.g., -f 'ClassTypesTestCase.test_with_dwarf_and_python_api'
99 -g : if specified, only the modules with the corect filter will be run
100 it has no effect if no '-f' option is present
101 '-f filterspec -g' can be used with '-p filename-regexp' to select only
102 the testfile.testclass.testmethod to run
103 -i : ignore (don't bailout) if 'lldb.py' module cannot be located in the build
104 tree relative to this script; use PYTHONPATH to locate the module
105 -l : don't skip long running test
106 -p : specify a regexp filename pattern for inclusion in the test suite
107 -r : specify a dir to relocate the tests and their intermediate files to;
108 the directory must not exist before running this test driver;
109 no cleanup of intermediate test files is performed in this case
110 -t : trace lldb command execution and result
111 -v : do verbose mode of unittest framework
112 -w : insert some wait time (currently 0.5 sec) between consecutive test cases
115 args : specify a list of directory names to search for python Test*.py scripts
116 if empty, search from the curret working directory, instead
118 Running of this script also sets up the LLDB_TEST environment variable so that
119 individual test cases can locate their supporting files correctly. The script
120 tries to set up Python's search paths for modules by looking at the build tree
121 relative to this script. See also the '-i' option.
123 Environment variables related to loggings:
125 o LLDB_LOG: if defined, specifies the log file pathname for the 'lldb' subsystem
126 with a default option of 'event process' if LLDB_LOG_OPTION is not defined.
128 o GDB_REMOTE_LOG: if defined, specifies the log file pathname for the
129 'process.gdb-remote' subsystem with a default option of 'packets' if
130 GDB_REMOTE_LOG_OPTION is not defined.
135 def parseOptionsAndInitTestdirs():
136 """Initialize the list of directories containing our unittest scripts.
138 '-h/--help as the first option prints out usage info and exit the program.
146 global skipLongRunningTest
152 if len(sys.argv) == 1:
155 # Process possible trace and/or verbose flag, among other things.
157 while index < len(sys.argv):
158 if not sys.argv[index].startswith('-'):
159 # End of option processing.
162 if sys.argv[index].find('-h') != -1:
164 elif sys.argv[index].startswith('-c'):
165 # Increment by 1 to fetch the config file name option argument.
167 if index >= len(sys.argv) or sys.argv[index].startswith('-'):
169 configFile = sys.argv[index]
170 if not os.path.isfile(configFile):
171 print "Config file:", configFile, "does not exist!"
174 elif sys.argv[index].startswith('-d'):
177 elif sys.argv[index].startswith('-f'):
178 # Increment by 1 to fetch the filter spec.
180 if index >= len(sys.argv) or sys.argv[index].startswith('-'):
182 filterspec = sys.argv[index]
184 elif sys.argv[index].startswith('-g'):
187 elif sys.argv[index].startswith('-i'):
190 elif sys.argv[index].startswith('-l'):
191 skipLongRunningTest = False
193 elif sys.argv[index].startswith('-p'):
194 # Increment by 1 to fetch the reg exp pattern argument.
196 if index >= len(sys.argv) or sys.argv[index].startswith('-'):
198 regexp = sys.argv[index]
200 elif sys.argv[index].startswith('-r'):
201 # Increment by 1 to fetch the relocated directory argument.
203 if index >= len(sys.argv) or sys.argv[index].startswith('-'):
205 rdir = os.path.abspath(sys.argv[index])
206 if os.path.exists(rdir):
207 print "Relocated directory:", rdir, "must not exist!"
210 elif sys.argv[index].startswith('-t'):
211 os.environ["LLDB_COMMAND_TRACE"] = "YES"
213 elif sys.argv[index].startswith('-v'):
216 elif sys.argv[index].startswith('-w'):
217 os.environ["LLDB_WAIT_BETWEEN_TEST_CASES"] = 'YES'
220 print "Unknown option: ", sys.argv[index]
223 # Gather all the dirs passed on the command line.
224 if len(sys.argv) > index:
225 testdirs = map(os.path.abspath, sys.argv[index:])
227 # If '-r dir' is specified, the tests should be run under the relocated
228 # directory. Let's copy the testdirs over.
230 from shutil import copytree, ignore_patterns
233 for srcdir in testdirs:
234 dstdir = os.path.join(rdir, os.path.basename(srcdir))
235 # Don't copy the *.pyc and .svn stuffs.
236 copytree(srcdir, dstdir, ignore=ignore_patterns('*.pyc', '.svn'))
237 tmpdirs.append(dstdir)
239 # This will be our modified testdirs.
242 # With '-r dir' specified, there's no cleanup of intermediate test files.
243 os.environ["LLDB_DO_CLEANUP"] = 'NO'
245 # If testdirs is ['test'], the make directory has already been copied
246 # recursively and is contained within the rdir/test dir. For anything
247 # else, we would need to copy over the make directory and its contents,
248 # so that, os.listdir(rdir) looks like, for example:
250 # array_types conditional_break make
252 # where the make directory contains the Makefile.rules file.
253 if len(testdirs) != 1 or os.path.basename(testdirs[0]) != 'test':
254 # Don't copy the .svn stuffs.
255 copytree('make', os.path.join(rdir, 'make'),
256 ignore=ignore_patterns('.svn'))
258 #print "testdirs:", testdirs
260 # Source the configFile if specified.
261 # The side effect, if any, will be felt from this point on. An example
262 # config file may be these simple two lines:
264 # sys.stderr = open("/tmp/lldbtest-stderr", "w")
265 # sys.stdout = open("/tmp/lldbtest-stdout", "w")
267 # which will reassign the two file objects to sys.stderr and sys.stdout,
270 # See also lldb-trunk/example/test/usage-config.
273 # Pass config (a dictionary) as the locals namespace for side-effect.
274 execfile(configFile, globals(), config)
275 #print "config:", config
276 #print "sys.stderr:", sys.stderr
277 #print "sys.stdout:", sys.stdout
281 """Add LLDB.framework/Resources/Python to the search paths for modules."""
286 # Get the directory containing the current script.
287 scriptPath = sys.path[0]
288 if not scriptPath.endswith('test'):
289 print "This script expects to reside in lldb's test directory."
293 # Set up the LLDB_TEST environment variable appropriately, so that the
294 # individual tests can be located relatively.
296 # See also lldbtest.TestBase.setUpClass(cls).
297 if len(testdirs) == 1 and os.path.basename(testdirs[0]) == 'test':
298 os.environ["LLDB_TEST"] = os.path.join(rdir, 'test')
300 os.environ["LLDB_TEST"] = rdir
302 os.environ["LLDB_TEST"] = scriptPath
303 pluginPath = os.path.join(scriptPath, 'plugins')
305 # Append script dir and plugin dir to the sys.path.
306 sys.path.append(scriptPath)
307 sys.path.append(pluginPath)
311 # The '-i' option is used to skip looking for lldb.py in the build tree.
315 base = os.path.abspath(os.path.join(scriptPath, os.pardir))
316 dbgPath = os.path.join(base, 'build', 'Debug', 'LLDB.framework',
317 'Resources', 'Python')
318 relPath = os.path.join(base, 'build', 'Release', 'LLDB.framework',
319 'Resources', 'Python')
320 baiPath = os.path.join(base, 'build', 'BuildAndIntegration',
321 'LLDB.framework', 'Resources', 'Python')
324 if os.path.isfile(os.path.join(dbgPath, 'lldb.py')):
326 elif os.path.isfile(os.path.join(relPath, 'lldb.py')):
328 elif os.path.isfile(os.path.join(baiPath, 'lldb.py')):
332 print 'This script requires lldb.py to be in either ' + dbgPath + ',',
333 print relPath + ', or ' + baiPath
336 # This is to locate the lldb.py module. Insert it right after sys.path[0].
337 sys.path[1:1] = [lldbPath]
341 """Delaying startup for delta-seconds to facilitate debugger attachment."""
342 def alarm_handler(*args):
343 raise Exception("timeout")
345 signal.signal(signal.SIGALRM, alarm_handler)
347 sys.stdout.write("pid=%d\n" % os.getpid())
348 sys.stdout.write("Enter RET to proceed (or timeout after %d seconds):" %
352 text = sys.stdin.readline()
356 sys.stdout.write("proceeding...\n")
360 def visit(prefix, dir, names):
361 """Visitor function for os.path.walk(path, visit, arg)."""
369 if os.path.isdir(os.path.join(dir, name)):
372 if '.py' == os.path.splitext(name)[1] and name.startswith(prefix):
373 # Try to match the regexp pattern, if specified.
376 if re.search(regexp, name):
377 #print "Filename: '%s' matches pattern: '%s'" % (name, regexp)
380 #print "Filename: '%s' does not match pattern: '%s'" % (name, regexp)
383 # We found a match for our test. Add it to the suite.
385 # Update the sys.path first.
386 if not sys.path.count(dir):
387 sys.path.insert(0, dir)
388 base = os.path.splitext(name)[0]
390 # Thoroughly check the filterspec against the base module and admit
391 # the (base, filterspec) combination only when it makes sense.
393 # Optimistically set the flag to True.
395 module = __import__(base)
396 parts = filterspec.split('.')
400 parent, obj = obj, getattr(obj, part)
401 except AttributeError:
402 # The filterspec has failed.
405 # Forgo this module if the (base, filterspec) combo is invalid
406 # and the '-g' option is present.
407 if fs4all and not filtered:
410 # Add either the filtered test case or the entire test class.
411 if filterspec and filtered:
413 unittest2.defaultTestLoader.loadTestsFromName(filterspec, module))
415 # A simple case of just the module name. Also the failover case
416 # from the filterspec branch when the (base, filterspec) combo
417 # doesn't make sense.
418 suite.addTests(unittest2.defaultTestLoader.loadTestsFromName(base))
422 """Check and do lldb loggings if necessary."""
424 # Turn on logging for debugging purposes if ${LLDB_LOG} environment variable is
425 # defined. Use ${LLDB_LOG} to specify the log file.
426 ci = lldb.DBG.GetCommandInterpreter()
427 res = lldb.SBCommandReturnObject()
428 if ("LLDB_LOG" in os.environ):
429 if ("LLDB_LOG_OPTION" in os.environ):
430 lldb_log_option = os.environ["LLDB_LOG_OPTION"]
432 lldb_log_option = "event process"
434 "log enable -f " + os.environ["LLDB_LOG"] + " lldb " + lldb_log_option,
436 if not res.Succeeded():
437 raise Exception('log enable failed (check LLDB_LOG env variable.')
438 # Ditto for gdb-remote logging if ${GDB_REMOTE_LOG} environment variable is defined.
439 # Use ${GDB_REMOTE_LOG} to specify the log file.
440 if ("GDB_REMOTE_LOG" in os.environ):
441 if ("GDB_REMOTE_LOG_OPTION" in os.environ):
442 gdb_remote_log_option = os.environ["GDB_REMOTE_LOG_OPTION"]
444 gdb_remote_log_option = "packets"
446 "log enable -f " + os.environ["GDB_REMOTE_LOG"] + " process.gdb-remote "
447 + gdb_remote_log_option,
449 if not res.Succeeded():
450 raise Exception('log enable failed (check GDB_REMOTE_LOG env variable.')
453 ############################################
455 # Execution of the test driver starts here #
457 ############################################
460 # Start the actions by first parsing the options while setting up the test
461 # directories, followed by setting up the search paths for lldb utilities;
462 # then, we walk the directory trees and collect the tests into our test suite.
464 parseOptionsAndInitTestdirs()
468 # If '-d' is specified, do a delay of 10 seconds for the debugger to attach.
474 # If '-l' is specified, do not skip the long running tests.
475 if not skipLongRunningTest:
476 os.environ["LLDB_SKIP_LONG_RUNNING_TEST"] = "NO"
479 # Walk through the testdirs while collecting tests.
481 for testdir in testdirs:
482 os.path.walk(testdir, visit, 'Test')
485 # Now that we have loaded all the test cases, run the whole test suite.
488 # For the time being, let's bracket the test runner within the
489 # lldb.SBDebugger.Initialize()/Terminate() pair.
491 lldb.SBDebugger.Initialize()
492 atexit.register(lambda: lldb.SBDebugger.Terminate())
494 # Create a singleton SBDebugger in the lldb namespace.
495 lldb.DBG = lldb.SBDebugger.Create()
497 # Turn on lldb loggings if necessary.
500 # Install the control-c handler.
501 unittest2.signals.installHandler()
504 # Invoke the default TextTestRunner to run the test suite, possibly iterating
505 # over different configurations.
509 iterCompilers = False
512 if "archs" in config:
513 archs = config["archs"]
514 if type(archs) is ListType and len(archs) >= 1:
516 if "compilers" in config:
517 compilers = config["compilers"]
518 if type(compilers) is ListType and len(compilers) >= 1:
521 # Make a shallow copy of sys.path, we need to manipulate the search paths later.
522 # This is only necessary if we are relocated and with different configurations.
523 if rdir and (iterArchs or iterCompilers):
524 old_sys_path = sys.path[:]
525 old_stderr = sys.stderr
526 old_stdout = sys.stdout
530 for ia in range(len(archs) if iterArchs else 1):
533 os.environ["ARCH"] = archs[ia]
534 archConfig = "arch=%s" % archs[ia]
535 for ic in range(len(compilers) if iterCompilers else 1):
537 os.environ["CC"] = compilers[ic]
538 configString = "%s compiler=%s" % (archConfig, compilers[ic])
540 configString = archConfig
542 if iterArchs or iterCompilers:
543 # If we specified a relocated directory to run the test suite, do
544 # the extra housekeeping to copy the testdirs to a configStringified
545 # directory and to update sys.path before invoking the test runner.
546 # The purpose is to separate the configuration-specific directories
549 from string import maketrans
550 from shutil import copytree, ignore_patterns
552 # Translate ' ' to '-' for dir name.
553 tbl = maketrans(' ', '-')
554 configPostfix = configString.translate(tbl)
555 newrdir = "%s.%s" % (rdir, configPostfix)
557 # Copy the tree to a new directory with postfix name configPostfix.
558 copytree(rdir, newrdir, ignore=ignore_patterns('*.pyc', '*.o', '*.d'))
560 # Check whether we need to split stderr/stdout into configuration
562 if old_stderr.name != '<stderr>' and config.get('split_stderr'):
565 new_stderr = open("%s.%s" % (old_stderr.name, configPostfix), "w")
566 sys.stderr = new_stderr
567 if old_stdout.name != '<stderr>' and config.get('split_stderr'):
570 new_stdout = open("%s.%s" % (old_stdout.name, configPostfix), "w")
571 sys.stdout = new_stdout
573 # Update the LLDB_TEST environment variable to reflect new top
574 # level test directory.
576 # See also lldbtest.TestBase.setUpClass(cls).
577 if len(testdirs) == 1 and os.path.basename(testdirs[0]) == 'test':
578 os.environ["LLDB_TEST"] = os.path.join(newrdir, 'test')
580 os.environ["LLDB_TEST"] = newrdir
582 # And update the Python search paths for modules.
583 sys.path = [x.replace(rdir, newrdir, 1) for x in old_sys_path]
585 # Output the configuration.
586 sys.stderr.write("\nConfiguration: " + configString + "\n")
588 #print "sys.stderr name is", sys.stderr.name
589 #print "sys.stdout name is", sys.stdout.name
591 # First, write out the number of collected test cases.
592 sys.stderr.write(separator + "\n")
593 sys.stderr.write("Collected %d test%s\n\n"
594 % (suite.countTestCases(),
595 suite.countTestCases() != 1 and "s" or ""))
597 # Invoke the test runner.
598 result = unittest2.TextTestRunner(stream=sys.stderr, verbosity=verbose).run(suite)
601 # Terminate the test suite if ${LLDB_TESTSUITE_FORCE_FINISH} is defined.
602 # This should not be necessary now.
603 if ("LLDB_TESTSUITE_FORCE_FINISH" in os.environ):
605 print "Terminating Test suite..."
606 subprocess.Popen(["/bin/sh", "-c", "kill %s; exit 0" % (os.getpid())])
609 sys.exit(not result.wasSuccessful)