blob: 028759f8c2ba9831991d4a30c2194217cebe5c78 [file] [log] [blame]
kjellander@webrtc.orgb5b155b2011-12-20 08:53:41 +00001#!/usr/bin/env python
turaj@webrtc.org799980f2012-05-11 00:41:48 +00002# Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
kjellander@webrtc.orgb5b155b2011-12-20 08:53:41 +00003#
4# Use of this source code is governed by a BSD-style license
5# that can be found in the LICENSE file in the root of the source
6# tree. An additional intellectual property rights grant can be found
7# in the file PATENTS. All contributing project authors may
8# be found in the AUTHORS file in the root of the source tree.
9
turaj@webrtc.org799980f2012-05-11 00:41:48 +000010'''Runs various WebRTC tests through valgrind_test.py.
kjellander@webrtc.orgb5b155b2011-12-20 08:53:41 +000011
turaj@webrtc.org799980f2012-05-11 00:41:48 +000012This script inherits the chrome_tests.py in Chrome, replacing its tests. We do
13this by taking chrome's faux cmdline test and making that the standard, so that
14we effectively can pass in any binary we feel like. It's also possible to pass
15arguments to the test, provided that the arguments do not contain dashes (these
16can be "escaped" by passing + instead, so -a becomes +a, and --my-option becomes
17++my_option).
kjellander@webrtc.orgb6e4cc72012-05-27 20:59:35 +000018
19Suppression files:
20The Chrome valgrind directory we use as a DEPS dependency contains the following
21suppression files:
22 valgrind/memcheck/suppressions.txt
23 valgrind/memcheck/suppressions_mac.txt
24 valgrind/tsan/suppressions.txt
25 valgrind/tsan/suppressions_mac.txt
26 valgrind/tsan/suppressions_win32.txt
27Since they're referenced from the chrome_tests.py script, we have similar files
28below the directory of this script. When executing, this script will setup both
29Chrome's suppression files and our own, so we can easily maintain WebRTC
30specific suppressions in our own files.
kjellander@webrtc.orgb5b155b2011-12-20 08:53:41 +000031'''
32
33import optparse
kjellander@webrtc.orgb6e4cc72012-05-27 20:59:35 +000034import os
kjellander@webrtc.orgb5b155b2011-12-20 08:53:41 +000035import sys
36
37import logging_utils
kjellander@webrtc.orgb6e4cc72012-05-27 20:59:35 +000038import path_utils
kjellander@webrtc.orgb5b155b2011-12-20 08:53:41 +000039
40import chrome_tests
41
kjellander@webrtc.orgb6e4cc72012-05-27 20:59:35 +000042class WebRTCTests(chrome_tests.ChromeTests):
43 """Class that handles setup of suppressions for WebRTC.
44
45 Everything else is inherited from chrome_tests.ChromeTests.
46 """
47
48 def _DefaultCommand(self, tool, exe=None, valgrind_test_args=None):
49 """Override command-building method so we can add more suppressions."""
50 cmd = chrome_tests.ChromeTests._DefaultCommand(self, tool, exe,
51 valgrind_test_args)
52 # When ChromeTests._DefaultCommand has executed, it has setup suppression
53 # files based on what's found in the memcheck/ or tsan/ subdirectories of
54 # this script's location. If Mac or Windows is executing, additional
55 # platform specific files have also been added.
56 # Since only the ones located below this directory is added, we must also
57 # add the ones maintained by Chrome, located in ../valgrind.
58
59 # The idea is to look for --suppression arguments in the cmd list and add a
60 # modified copy of each suppression file, for the corresponding file in
61 # ../valgrind. If we would simply replace 'valgrind-webrtc' with 'valgrind'
62 # we may produce invalid paths if other parts of the path contain that
63 # string. That's why the code below only replaces the end of the path.
64 old_base, old_dir = _split_script_path()
65 new_dir = old_base + 'valgrind'
66 add_suppressions = []
67 for token in cmd:
68 if '--suppressions' in token:
69 add_suppressions.append(token.replace(old_base + old_dir, new_dir))
70 return add_suppressions + cmd
71
72
73def _split_script_path():
74 """Splits the script's path into a tuple separating the last directory.
75
76 Returns a tuple where the first item is the whole path except the last
77 directory and the second item is the name of the last directory.
78 """
79 script_dir = path_utils.ScriptDir()
80 last_sep_index = script_dir.rfind(os.sep)
81 return script_dir[0:last_sep_index+1], script_dir[last_sep_index+1:]
82
83
kjellander@webrtc.orgb5b155b2011-12-20 08:53:41 +000084def _main(_):
85 parser = optparse.OptionParser("usage: %prog -b <dir> -t <test> "
turaj@webrtc.org799980f2012-05-11 00:41:48 +000086 "[-t <test> ...] <arguments to all tests>"
87 "NOTE: when passing arguments to all tests, "
88 " replace any - with +.")
kjellander@webrtc.orgb5b155b2011-12-20 08:53:41 +000089 parser.add_option("-b", "--build_dir",
90 help="the location of the compiler output")
91 parser.add_option("-t", "--test", action="append", default=[],
92 help="which test to run, supports test:gtest_filter format "
93 "as well.")
94 parser.add_option("", "--baseline", action="store_true", default=False,
95 help="generate baseline data instead of validating")
96 parser.add_option("", "--gtest_filter",
97 help="additional arguments to --gtest_filter")
98 parser.add_option("", "--gtest_repeat",
99 help="argument for --gtest_repeat")
100 parser.add_option("-v", "--verbose", action="store_true", default=False,
101 help="verbose output - enable debug log messages")
102 parser.add_option("", "--tool", dest="valgrind_tool", default="memcheck",
103 help="specify a valgrind tool to run the tests under")
104 parser.add_option("", "--tool_flags", dest="valgrind_tool_flags", default="",
105 help="specify custom flags for the selected valgrind tool")
106 parser.add_option("", "--keep_logs", action="store_true", default=False,
107 help="store memory tool logs in the <tool>.logs directory "
108 "instead of /tmp.\nThis can be useful for tool "
109 "developers/maintainers.\nPlease note that the <tool>"
110 ".logs directory will be clobbered on tool startup.")
111 options, args = parser.parse_args()
112
113 if options.verbose:
114 logging_utils.config_root(logging.DEBUG)
115 else:
116 logging_utils.config_root()
117
118 if not options.test:
119 parser.error("--test not specified")
120
121 if len(options.test) != 1 and options.gtest_filter:
122 parser.error("--gtest_filter and multiple tests don't make sense together")
123
kjellander@webrtc.orgb43f85f2012-09-11 11:22:45 +0000124 # If --build_dir is provided, prepend that path to the test name to make it a
125 # valid path when running on the build slaves using Chromium's runtest.py
kjellander@webrtc.org0f4185f2012-11-15 15:19:28 +0000126 if options.build_dir and 'cmdline' in options.test:
kjellander@webrtc.orgb43f85f2012-09-11 11:22:45 +0000127 args[0] = os.path.join(options.build_dir, args[0])
128
turaj@webrtc.org799980f2012-05-11 00:41:48 +0000129 # Performs the deferred-argument black magic described in the usage.
130 translated_args = map(lambda arg: arg.replace('+', '-'), args)
131
kjellander@webrtc.orgb5b155b2011-12-20 08:53:41 +0000132 for t in options.test:
kjellander@webrtc.orgb6e4cc72012-05-27 20:59:35 +0000133 tests = WebRTCTests(options, translated_args, t)
kjellander@webrtc.orgb5b155b2011-12-20 08:53:41 +0000134 ret = tests.Run()
135 if ret: return ret
136 return 0
137
138if __name__ == "__main__":
turaj@webrtc.org799980f2012-05-11 00:41:48 +0000139 # Overwrite the ChromeTests tests dictionary. The cmdline option allows the
140 # user to pass any executable as parameter to the test script, so we'll use
141 # that to get our binaries in (hackish but convenient).
kjellander@webrtc.orgb5b155b2011-12-20 08:53:41 +0000142 chrome_tests.ChromeTests._test_list = {
143 "cmdline": chrome_tests.ChromeTests.RunCmdLine,
kjellander@webrtc.orgb5b155b2011-12-20 08:53:41 +0000144 }
turaj@webrtc.org799980f2012-05-11 00:41:48 +0000145
146 # We do this so the user can write -t <binary> instead of -t cmdline <binary>.
kjellander@webrtc.orgb764d782012-09-11 09:34:51 +0000147 if '-t' in sys.argv:
148 sys.argv.insert(sys.argv.index('-t') + 1, 'cmdline')
149 elif '--test' in sys.argv:
150 sys.argv.insert(sys.argv.index('--test') + 1, 'cmdline')
151
turaj@webrtc.org799980f2012-05-11 00:41:48 +0000152 print sys.argv
kjellander@webrtc.orgb5b155b2011-12-20 08:53:41 +0000153 ret = _main(sys.argv)
kjellander@webrtc.orgb6e4cc72012-05-27 20:59:35 +0000154 sys.exit(ret)