Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | """ |
| 3 | @package oft |
| 4 | |
| 5 | OpenFlow test framework top level script |
| 6 | |
Rich Lane | ea5060d | 2013-01-06 13:59:00 -0800 | [diff] [blame] | 7 | This script is the entry point for running OpenFlow tests using the OFT |
| 8 | framework. For usage information, see --help or the README. |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 9 | |
Rich Lane | ea5060d | 2013-01-06 13:59:00 -0800 | [diff] [blame] | 10 | To add a new command line option, edit both the config_default dictionary and |
| 11 | the config_setup function. The option's result will end up in the global |
| 12 | oftest.config dictionary. |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 13 | """ |
| 14 | |
| 15 | import sys |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 16 | import optparse |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 17 | from subprocess import Popen,PIPE |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 18 | import logging |
| 19 | import unittest |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 20 | import time |
Brandon Heller | 446c143 | 2010-04-01 12:43:27 -0700 | [diff] [blame] | 21 | import os |
Rich Lane | 6b452bc | 2012-07-09 16:52:21 -0700 | [diff] [blame] | 22 | import imp |
Rich Lane | 8592bec | 2012-09-03 09:06:59 -0700 | [diff] [blame] | 23 | import random |
Rich Lane | 5bd6cf9 | 2012-10-04 17:57:24 -0700 | [diff] [blame] | 24 | import signal |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 25 | import fnmatch |
Rich Lane | 2d6d482 | 2013-01-08 10:49:16 -0800 | [diff] [blame] | 26 | import copy |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 27 | |
Rich Lane | fadf345 | 2012-10-03 16:23:37 -0700 | [diff] [blame] | 28 | root_dir = os.path.dirname(os.path.realpath(__file__)) |
| 29 | |
| 30 | pydir = os.path.join(root_dir, 'src', 'python') |
Rich Lane | 3987804 | 2012-07-09 14:45:35 -0700 | [diff] [blame] | 31 | if os.path.exists(os.path.join(pydir, 'oftest')): |
| 32 | # Running from source tree |
| 33 | sys.path.insert(0, pydir) |
| 34 | |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 35 | import oftest |
| 36 | from oftest import config |
Rich Lane | da3b5ad | 2012-10-03 09:05:32 -0700 | [diff] [blame] | 37 | import oftest.testutils |
Rich Lane | e55abf7 | 2012-07-26 20:11:42 -0700 | [diff] [blame] | 38 | import oftest.ofutils |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 39 | import oftest.help_formatter |
Dan Talayco | ba3745c | 2010-07-21 21:51:08 -0700 | [diff] [blame] | 40 | |
Dan Talayco | 02eca0b | 2010-04-15 16:09:43 -0700 | [diff] [blame] | 41 | try: |
| 42 | import scapy.all as scapy |
| 43 | except: |
| 44 | try: |
| 45 | import scapy as scapy |
| 46 | except: |
| 47 | sys.exit("Need to install scapy for packet parsing") |
| 48 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 49 | ##@var DEBUG_LEVELS |
| 50 | # Map from strings to debugging levels |
| 51 | DEBUG_LEVELS = { |
| 52 | 'debug' : logging.DEBUG, |
| 53 | 'verbose' : logging.DEBUG, |
| 54 | 'info' : logging.INFO, |
| 55 | 'warning' : logging.WARNING, |
| 56 | 'warn' : logging.WARNING, |
| 57 | 'error' : logging.ERROR, |
| 58 | 'critical' : logging.CRITICAL |
| 59 | } |
| 60 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 61 | ##@var config_default |
| 62 | # The default configuration dictionary for OFT |
| 63 | config_default = { |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 64 | # Miscellaneous options |
| 65 | "list" : False, |
| 66 | "list_test_names" : False, |
| 67 | "allow_user" : False, |
| 68 | |
| 69 | # Test selection options |
Rich Lane | c76b09a | 2013-01-02 16:53:22 -0800 | [diff] [blame] | 70 | "test_spec" : "", |
Rich Lane | 1a08d23 | 2013-01-04 16:03:50 -0800 | [diff] [blame] | 71 | "test_file" : None, |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 72 | "test_dir" : os.path.join(root_dir, "tests"), |
| 73 | |
| 74 | # Switch connection options |
| 75 | "controller_host" : "0.0.0.0", # For passive bind |
| 76 | "controller_port" : 6633, |
| 77 | "switch_ip" : None, # If not none, actively connect to switch |
Rich Lane | 15f2632 | 2013-01-08 11:23:24 -0800 | [diff] [blame^] | 78 | "platform" : "eth", |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 79 | "platform_args" : None, |
| 80 | "platform_dir" : os.path.join(root_dir, "platforms"), |
Rich Lane | 2d6d482 | 2013-01-08 10:49:16 -0800 | [diff] [blame] | 81 | "interfaces" : [], |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 82 | |
| 83 | # Logging options |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 84 | "log_file" : "oft.log", |
Dan Talayco | 69ca4d6 | 2012-11-15 11:50:22 -0800 | [diff] [blame] | 85 | "log_append" : False, |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 86 | "debug" : "verbose", |
| 87 | |
| 88 | # Test behavior options |
| 89 | "relax" : False, |
Dan Talayco | d7c80d1 | 2012-04-03 15:20:57 -0700 | [diff] [blame] | 90 | "test_params" : "None", |
Rich Lane | 9a84a4f | 2012-07-17 12:27:42 -0700 | [diff] [blame] | 91 | "fail_skipped" : False, |
Rich Lane | e55abf7 | 2012-07-26 20:11:42 -0700 | [diff] [blame] | 92 | "default_timeout" : 2, |
Jeffrey Townsend | 5cb7ed3 | 2012-08-17 18:11:01 +0000 | [diff] [blame] | 93 | "minsize" : 0, |
Rich Lane | 8592bec | 2012-09-03 09:06:59 -0700 | [diff] [blame] | 94 | "random_seed" : None, |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 95 | |
| 96 | # Other configuration |
Rich Lane | 15f2632 | 2013-01-08 11:23:24 -0800 | [diff] [blame^] | 97 | "port_map" : {}, |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 100 | def config_setup(): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 101 | """ |
| 102 | Set up the configuration including parsing the arguments |
| 103 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 104 | @return A pair (config, args) where config is an config |
| 105 | object and args is any additional arguments from the command line |
| 106 | """ |
| 107 | |
Rich Lane | 4113a58 | 2013-01-03 10:13:02 -0800 | [diff] [blame] | 108 | usage = "usage: %prog [options] (test|group)..." |
| 109 | |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 110 | description = """\ |
Rich Lane | 4113a58 | 2013-01-03 10:13:02 -0800 | [diff] [blame] | 111 | OFTest is a framework and set of tests for validating OpenFlow switches. |
| 112 | |
| 113 | The default configuration assumes that an OpenFlow 1.0 switch is attempting to |
| 114 | connect to a controller on the machine running OFTest, port 6633. Additionally, |
| 115 | the interfaces veth1, veth3, veth5, and veth7 should be connected to the switch's |
| 116 | dataplane. |
| 117 | |
| 118 | If no positional arguments are given then OFTest will run all tests that |
| 119 | depend only on standard OpenFlow 1.0. Otherwise each positional argument |
| 120 | is interpreted as either a test name or a test group name. The union of |
| 121 | these will be executed. To see what groups each test belongs to use the |
Rich Lane | 948f030 | 2013-01-07 10:59:08 -0800 | [diff] [blame] | 122 | --list option. Tests and groups can be subtracted from the result by |
| 123 | prefixing them with the '^' character. |
Rich Lane | 4113a58 | 2013-01-03 10:13:02 -0800 | [diff] [blame] | 124 | """ |
| 125 | |
Rich Lane | 2d6d482 | 2013-01-08 10:49:16 -0800 | [diff] [blame] | 126 | # Parse --interface |
| 127 | def check_interface(option, opt, value): |
| 128 | try: |
| 129 | ofport, interface = value.split('@', 1) |
| 130 | ofport = int(ofport) |
| 131 | except ValueError: |
| 132 | raise optparse.OptionValueError("incorrect interface syntax (got %s, expected 'ofport@interface')" % repr(value)) |
| 133 | return (ofport, interface) |
| 134 | |
| 135 | class Option(optparse.Option): |
| 136 | TYPES = optparse.Option.TYPES + ("interface",) |
| 137 | TYPE_CHECKER = copy.copy(optparse.Option.TYPE_CHECKER) |
| 138 | TYPE_CHECKER["interface"] = check_interface |
| 139 | |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 140 | parser = optparse.OptionParser(version="%prog 0.1", |
| 141 | usage=usage, |
| 142 | description=description, |
Rich Lane | 2d6d482 | 2013-01-08 10:49:16 -0800 | [diff] [blame] | 143 | formatter=oftest.help_formatter.HelpFormatter(), |
| 144 | option_class=Option) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 145 | |
| 146 | # Set up default values |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 147 | parser.set_defaults(**config_default) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 148 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 149 | parser.add_option("--list", action="store_true", |
Brandon Heller | 824504e | 2010-04-01 12:21:37 -0700 | [diff] [blame] | 150 | help="List all tests and exit") |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 151 | parser.add_option("--list-test-names", action='store_true', |
| 152 | help="List test names matching the test spec and exit") |
Rich Lane | ee57ad0 | 2012-07-13 15:40:36 -0700 | [diff] [blame] | 153 | parser.add_option("--allow-user", action="store_true", |
| 154 | help="Proceed even if oftest is not run as root") |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 155 | |
| 156 | group = optparse.OptionGroup(parser, "Test selection options") |
| 157 | group.add_option("-T", "--test-spec", "--test-list", help="Tests to run, separated by commas") |
| 158 | group.add_option("-f", "--test-file", help="File of tests to run, one per line") |
| 159 | group.add_option("--test-dir", type="string", help="Directory containing tests") |
| 160 | parser.add_option_group(group) |
| 161 | |
| 162 | group = optparse.OptionGroup(parser, "Switch connection options") |
| 163 | group.add_option("-H", "--host", dest="controller_host", |
| 164 | help="IP address to listen on (default %default)") |
| 165 | group.add_option("-p", "--port", dest="controller_port", |
| 166 | type="int", help="Port number to listen on (default %default)") |
| 167 | group.add_option("-S", "--switch-ip", dest="switch_ip", |
| 168 | help="If set, actively connect to this switch by IP") |
| 169 | group.add_option("-P", "--platform", help="Platform module name (default %default)") |
| 170 | group.add_option("-a", "--platform-args", help="Custom arguments for the platform") |
| 171 | group.add_option("--platform-dir", type="string", help="Directory containing platform modules") |
Rich Lane | 2d6d482 | 2013-01-08 10:49:16 -0800 | [diff] [blame] | 172 | group.add_option("--interface", "-i", type="interface", dest="interfaces", metavar="INTERFACE", action="append", |
| 173 | help="Specify a OpenFlow port number and the dataplane interface to use. May be given multiple times. Example: 1@eth1") |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 174 | parser.add_option_group(group) |
| 175 | |
| 176 | group = optparse.OptionGroup(parser, "Logging options") |
| 177 | group.add_option("--log-file", |
| 178 | help="Name of log file, empty string to log to console (default %default)") |
| 179 | group.add_option("--log-append", action="store_true", |
| 180 | help="Do not delete log file if specified") |
| 181 | dbg_lvl_names = sorted(DEBUG_LEVELS.keys(), key=lambda x: DEBUG_LEVELS[x]) |
| 182 | group.add_option("--debug", choices=dbg_lvl_names, |
| 183 | help="Debug lvl: debug, info, warning, error, critical (default %default)") |
| 184 | group.add_option("-v", "--verbose", action="store_const", dest="debug", |
| 185 | const="verbose", help="Shortcut for --debug=verbose") |
| 186 | group.add_option("-q", "--quiet", action="store_const", dest="debug", |
| 187 | const="warning", help="Shortcut for --debug=warning") |
| 188 | parser.add_option_group(group) |
| 189 | |
| 190 | group = optparse.OptionGroup(parser, "Test behavior options") |
| 191 | group.add_option("--relax", action="store_true", |
| 192 | help="Relax packet match checks allowing other packets") |
| 193 | test_params_help = """Set test parameters: key=val;... (see --list) |
| 194 | """ |
| 195 | group.add_option("-t", "--test-params", help=test_params_help) |
| 196 | group.add_option("--fail-skipped", action="store_true", |
Rich Lane | 9a84a4f | 2012-07-17 12:27:42 -0700 | [diff] [blame] | 197 | help="Return failure if any test was skipped") |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 198 | group.add_option("--default-timeout", type="int", |
Rich Lane | e55abf7 | 2012-07-26 20:11:42 -0700 | [diff] [blame] | 199 | help="Timeout in seconds for most operations") |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 200 | group.add_option("--minsize", type="int", |
| 201 | help="Minimum allowable packet size on the dataplane.") |
| 202 | group.add_option("--random-seed", type="int", |
| 203 | help="Random number generator seed") |
| 204 | parser.add_option_group(group) |
Jeffrey Townsend | 5cb7ed3 | 2012-08-17 18:11:01 +0000 | [diff] [blame] | 205 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 206 | # Might need this if other parsers want command line |
| 207 | # parser.allow_interspersed_args = False |
| 208 | (options, args) = parser.parse_args() |
| 209 | |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 210 | # Convert options from a Namespace to a plain dictionary |
| 211 | config = config_default.copy() |
| 212 | for key in config.keys(): |
| 213 | config[key] = getattr(options, key) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 214 | |
| 215 | return (config, args) |
| 216 | |
| 217 | def logging_setup(config): |
| 218 | """ |
| 219 | Set up logging based on config |
| 220 | """ |
Rich Lane | 71d887d | 2012-12-22 17:05:13 -0800 | [diff] [blame] | 221 | _format = "%(asctime)s.%(msecs)d %(name)-10s: %(levelname)-8s: %(message)s" |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 222 | _datefmt = "%H:%M:%S" |
Dan Talayco | 69ca4d6 | 2012-11-15 11:50:22 -0800 | [diff] [blame] | 223 | _mode = config["log_append"] and "a" or "w" |
Dan Talayco | 88fc880 | 2010-03-07 11:37:52 -0800 | [diff] [blame] | 224 | logging.basicConfig(filename=config["log_file"], |
Dan Talayco | 69ca4d6 | 2012-11-15 11:50:22 -0800 | [diff] [blame] | 225 | filemode=_mode, |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 226 | level=DEBUG_LEVELS[config["debug"]], |
Dan Talayco | 88fc880 | 2010-03-07 11:37:52 -0800 | [diff] [blame] | 227 | format=_format, datefmt=_datefmt) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 228 | |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 229 | def load_test_modules(config): |
| 230 | """ |
| 231 | Load tests from the test_dir directory. |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 232 | |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 233 | Test cases are subclasses of unittest.TestCase |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 234 | |
Rich Lane | cc45b8e | 2013-01-02 15:55:02 -0800 | [diff] [blame] | 235 | Also updates the _groups member to include "standard" and |
| 236 | module test groups if appropriate. |
| 237 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 238 | @param config The oft configuration dictionary |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 239 | @returns A dictionary from test module names to tuples of |
| 240 | (module, dictionary from test names to test classes). |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 241 | """ |
| 242 | |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 243 | result = {} |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 244 | |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 245 | for root, dirs, filenames in os.walk(config["test_dir"]): |
| 246 | # Iterate over each python file |
| 247 | for filename in fnmatch.filter(filenames, '[!.]*.py'): |
| 248 | modname = os.path.splitext(os.path.basename(filename))[0] |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 249 | |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 250 | try: |
| 251 | if sys.modules.has_key(modname): |
| 252 | mod = sys.modules[modname] |
| 253 | else: |
| 254 | mod = imp.load_module(modname, *imp.find_module(modname, [root])) |
| 255 | except: |
| 256 | logging.warning("Could not import file " + filename) |
| 257 | raise |
Rich Lane | 520e415 | 2012-07-09 16:18:16 -0700 | [diff] [blame] | 258 | |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 259 | # Find all testcases defined in the module |
| 260 | tests = dict((k, v) for (k, v) in mod.__dict__.items() if type(v) == type and |
| 261 | issubclass(v, unittest.TestCase)) |
| 262 | if tests: |
Rich Lane | cc45b8e | 2013-01-02 15:55:02 -0800 | [diff] [blame] | 263 | for (testname, test) in tests.items(): |
| 264 | # Set default annotation values |
| 265 | if not hasattr(test, "_groups"): |
| 266 | test._groups = [] |
| 267 | if not hasattr(test, "_nonstandard"): |
| 268 | test._nonstandard = False |
| 269 | if not hasattr(test, "_disabled"): |
| 270 | test._disabled = False |
| 271 | |
| 272 | # Put test in its module's test group |
| 273 | if not test._disabled: |
| 274 | test._groups.append(modname) |
| 275 | |
| 276 | # Put test in the standard test group |
| 277 | if not test._disabled and not test._nonstandard: |
| 278 | test._groups.append("standard") |
| 279 | test._groups.append("all") # backwards compatibility |
| 280 | |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 281 | result[modname] = (mod, tests) |
Rich Lane | 520e415 | 2012-07-09 16:18:16 -0700 | [diff] [blame] | 282 | |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 283 | return result |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 284 | |
Rich Lane | c76b09a | 2013-01-02 16:53:22 -0800 | [diff] [blame] | 285 | def prune_tests(test_specs, test_modules): |
Rich Lane | 15f64de | 2012-10-04 21:25:57 -0700 | [diff] [blame] | 286 | """ |
Rich Lane | c76b09a | 2013-01-02 16:53:22 -0800 | [diff] [blame] | 287 | Return tests matching the given test-specs |
| 288 | @param test_specs A list of group names or test names. |
Rich Lane | 15f64de | 2012-10-04 21:25:57 -0700 | [diff] [blame] | 289 | @param test_modules Same format as the output of load_test_modules. |
| 290 | @returns Same format as the output of load_test_modules. |
| 291 | """ |
| 292 | result = {} |
Rich Lane | c76b09a | 2013-01-02 16:53:22 -0800 | [diff] [blame] | 293 | for e in test_specs: |
Rich Lane | 15f64de | 2012-10-04 21:25:57 -0700 | [diff] [blame] | 294 | matched = False |
Rich Lane | 948f030 | 2013-01-07 10:59:08 -0800 | [diff] [blame] | 295 | |
| 296 | if e.startswith('^'): |
| 297 | negated = True |
| 298 | e = e[1:] |
| 299 | else: |
| 300 | negated = False |
| 301 | |
Rich Lane | 15f64de | 2012-10-04 21:25:57 -0700 | [diff] [blame] | 302 | for (modname, (mod, tests)) in test_modules.items(): |
Rich Lane | cc45b8e | 2013-01-02 15:55:02 -0800 | [diff] [blame] | 303 | for (testname, test) in tests.items(): |
| 304 | if e in test._groups or e == "%s.%s" % (modname, testname): |
| 305 | result.setdefault(modname, (mod, {})) |
Rich Lane | 948f030 | 2013-01-07 10:59:08 -0800 | [diff] [blame] | 306 | if not negated: |
| 307 | result[modname][1][testname] = test |
| 308 | else: |
| 309 | if modname in result and testname in result[modname][1]: |
| 310 | del result[modname][1][testname] |
| 311 | if not result[modname][1]: |
| 312 | del result[modname] |
Rich Lane | cc45b8e | 2013-01-02 15:55:02 -0800 | [diff] [blame] | 313 | matched = True |
Rich Lane | 948f030 | 2013-01-07 10:59:08 -0800 | [diff] [blame] | 314 | |
Rich Lane | 15f64de | 2012-10-04 21:25:57 -0700 | [diff] [blame] | 315 | if not matched: |
Rich Lane | cc45b8e | 2013-01-02 15:55:02 -0800 | [diff] [blame] | 316 | die("test-spec element %s did not match any tests" % e) |
Rich Lane | 948f030 | 2013-01-07 10:59:08 -0800 | [diff] [blame] | 317 | |
Rich Lane | 15f64de | 2012-10-04 21:25:57 -0700 | [diff] [blame] | 318 | return result |
| 319 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 320 | def die(msg, exit_val=1): |
| 321 | print msg |
| 322 | logging.critical(msg) |
| 323 | sys.exit(exit_val) |
| 324 | |
Dan Talayco | 79f3608 | 2010-03-11 16:53:53 -0800 | [diff] [blame] | 325 | def _space_to(n, str): |
| 326 | """ |
| 327 | Generate a string of spaces to achieve width n given string str |
| 328 | If length of str >= n, return one space |
| 329 | """ |
| 330 | spaces = n - len(str) |
| 331 | if spaces > 0: |
| 332 | return " " * spaces |
| 333 | return " " |
| 334 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 335 | # |
| 336 | # Main script |
| 337 | # |
| 338 | |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 339 | # Setup global configuration |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 340 | (new_config, args) = config_setup() |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 341 | oftest.config.update(new_config) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 342 | |
Rich Lane | d7a85c4 | 2012-09-28 15:38:45 -0700 | [diff] [blame] | 343 | logging_setup(config) |
| 344 | logging.info("++++++++ " + time.asctime() + " ++++++++") |
| 345 | |
Rich Lane | e284b6b | 2012-10-03 09:19:58 -0700 | [diff] [blame] | 346 | # Allow tests to import each other |
| 347 | sys.path.append(config["test_dir"]) |
| 348 | |
Rich Lane | c76b09a | 2013-01-02 16:53:22 -0800 | [diff] [blame] | 349 | test_specs = args |
| 350 | if config["test_spec"] != "": |
| 351 | print >> sys.stderr, "WARNING: The --test-spec option is deprecated" |
| 352 | test_specs += config["test_spec"].split(',') |
Rich Lane | 1a08d23 | 2013-01-04 16:03:50 -0800 | [diff] [blame] | 353 | if config["test_file"] != None: |
| 354 | with open(config["test_file"], 'r') as f: |
| 355 | for line in f: |
| 356 | line, _, _ = line.partition('#') # remove comments |
| 357 | line = line.strip() |
| 358 | if line: |
| 359 | test_specs.append(line) |
Rich Lane | c76b09a | 2013-01-02 16:53:22 -0800 | [diff] [blame] | 360 | if test_specs == []: |
| 361 | test_specs = ["standard"] |
| 362 | |
Rich Lane | d8e4548 | 2013-01-02 17:36:21 -0800 | [diff] [blame] | 363 | test_modules = load_test_modules(config) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 364 | |
| 365 | # Check if test list is requested; display and exit if so |
| 366 | if config["list"]: |
Dan Talayco | 7f8dba8 | 2012-04-12 12:58:52 -0700 | [diff] [blame] | 367 | mod_count = 0 |
| 368 | test_count = 0 |
Rich Lane | 37f4211 | 2013-01-03 13:41:49 -0800 | [diff] [blame] | 369 | all_groups = set() |
Rich Lane | e811e7b | 2013-01-03 13:36:54 -0800 | [diff] [blame] | 370 | print """\ |
| 371 | Tests are shown grouped by module. If a test is in any groups beyond "standard" |
| 372 | and its module's group then they are shown in parentheses.""" |
| 373 | print |
| 374 | print """\ |
| 375 | Tests marked with '*' are non-standard and may require vendor extensions or |
| 376 | special switch configuration. These are not part of the "standard" test group.""" |
| 377 | print |
| 378 | print """\ |
| 379 | Tests marked with '!' are disabled because they are experimental, special-purpose, |
| 380 | or are too long to be run normally. These are not part of the "standard" test |
| 381 | group or their module's test group.""" |
| 382 | print |
| 383 | print "Tests marked (TP1) after name take --test-params including:" |
| 384 | print " 'vid=N;strip_vlan=bool;add_vlan=bool'" |
Rich Lane | e811e7b | 2013-01-03 13:36:54 -0800 | [diff] [blame] | 385 | print |
| 386 | print "Test List:" |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 387 | for (modname, (mod, tests)) in test_modules.items(): |
Dan Talayco | 7f8dba8 | 2012-04-12 12:58:52 -0700 | [diff] [blame] | 388 | mod_count += 1 |
Rich Lane | 80efd65 | 2013-01-02 14:05:20 -0800 | [diff] [blame] | 389 | desc = (mod.__doc__ or "No description").strip().split('\n')[0] |
Dan Talayco | 79f3608 | 2010-03-11 16:53:53 -0800 | [diff] [blame] | 390 | start_str = " Module " + mod.__name__ + ": " |
| 391 | print start_str + _space_to(22, start_str) + desc |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 392 | for (testname, test) in tests.items(): |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 393 | try: |
Rich Lane | 0f5b9c7 | 2013-01-02 14:05:20 -0800 | [diff] [blame] | 394 | desc = (test.__doc__ or "").strip() |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 395 | desc = desc.split('\n')[0] |
| 396 | except: |
| 397 | desc = "No description" |
Rich Lane | e811e7b | 2013-01-03 13:36:54 -0800 | [diff] [blame] | 398 | groups = set(test._groups) - set(["all", "standard", modname]) |
Rich Lane | 37f4211 | 2013-01-03 13:41:49 -0800 | [diff] [blame] | 399 | all_groups.update(test._groups) |
Rich Lane | e811e7b | 2013-01-03 13:36:54 -0800 | [diff] [blame] | 400 | if groups: |
| 401 | desc = "(%s) %s" % (",".join(groups), desc) |
| 402 | start_str = " %s%s %s:" % (test._nonstandard and "*" or " ", |
| 403 | test._disabled and "!" or " ", |
| 404 | testname) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 405 | if len(start_str) > 22: |
| 406 | desc = "\n" + _space_to(22, "") + desc |
Dan Talayco | 79f3608 | 2010-03-11 16:53:53 -0800 | [diff] [blame] | 407 | print start_str + _space_to(22, start_str) + desc |
Dan Talayco | 7f8dba8 | 2012-04-12 12:58:52 -0700 | [diff] [blame] | 408 | test_count += 1 |
Dan Talayco | 79f3608 | 2010-03-11 16:53:53 -0800 | [diff] [blame] | 409 | print |
Rich Lane | 15f64de | 2012-10-04 21:25:57 -0700 | [diff] [blame] | 410 | print "%d modules shown with a total of %d tests" % \ |
| 411 | (mod_count, test_count) |
| 412 | print |
Rich Lane | 37f4211 | 2013-01-03 13:41:49 -0800 | [diff] [blame] | 413 | print "Test groups: %s" % (', '.join(sorted(all_groups))) |
| 414 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 415 | sys.exit(0) |
| 416 | |
Rich Lane | d8e4548 | 2013-01-02 17:36:21 -0800 | [diff] [blame] | 417 | test_modules = prune_tests(test_specs, test_modules) |
| 418 | |
Jeffrey Townsend | 6614f97 | 2012-08-16 13:27:18 -0700 | [diff] [blame] | 419 | # Check if test list is requested; display and exit if so |
| 420 | if config["list_test_names"]: |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 421 | for (modname, (mod, tests)) in test_modules.items(): |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 422 | for (testname, test) in tests.items(): |
Rich Lane | cc45b8e | 2013-01-02 15:55:02 -0800 | [diff] [blame] | 423 | print "%s.%s" % (modname, testname) |
Jeffrey Townsend | 6614f97 | 2012-08-16 13:27:18 -0700 | [diff] [blame] | 424 | sys.exit(0) |
| 425 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 426 | # Generate the test suite |
| 427 | #@todo Decide if multiple suites are ever needed |
| 428 | suite = unittest.TestSuite() |
| 429 | |
Rich Lane | 15f64de | 2012-10-04 21:25:57 -0700 | [diff] [blame] | 430 | for (modname, (mod, tests)) in test_modules.items(): |
| 431 | for (testname, test) in tests.items(): |
Rich Lane | cc45b8e | 2013-01-02 15:55:02 -0800 | [diff] [blame] | 432 | logging.info("Adding test " + modname + "." + testname) |
| 433 | suite.addTest(test()) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 434 | |
Rich Lane | 51590f6 | 2012-10-09 15:06:29 -0700 | [diff] [blame] | 435 | # Allow platforms to import each other |
| 436 | sys.path.append(config["platform_dir"]) |
| 437 | |
Rich Lane | 8aebc5e | 2012-09-25 17:57:53 -0700 | [diff] [blame] | 438 | # Load the platform module |
| 439 | platform_name = config["platform"] |
| 440 | logging.info("Importing platform: " + platform_name) |
| 441 | platform_mod = None |
| 442 | try: |
Rich Lane | 483e154 | 2012-10-05 09:29:39 -0700 | [diff] [blame] | 443 | platform_mod = imp.load_module(platform_name, *imp.find_module(platform_name, [config["platform_dir"]])) |
Rich Lane | 8aebc5e | 2012-09-25 17:57:53 -0700 | [diff] [blame] | 444 | except: |
| 445 | logging.warn("Failed to import " + platform_name + " platform module") |
| 446 | raise |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 447 | |
| 448 | try: |
Rich Lane | 8aebc5e | 2012-09-25 17:57:53 -0700 | [diff] [blame] | 449 | platform_mod.platform_config_update(config) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 450 | except: |
| 451 | logging.warn("Could not run platform host configuration") |
Rich Lane | ef403e5 | 2012-07-09 14:59:43 -0700 | [diff] [blame] | 452 | raise |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 453 | |
| 454 | if not config["port_map"]: |
Rich Lane | 8aebc5e | 2012-09-25 17:57:53 -0700 | [diff] [blame] | 455 | die("Interface port map was not defined by the platform. Exiting.") |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 456 | |
| 457 | logging.debug("Configuration: " + str(config)) |
| 458 | logging.info("OF port map: " + str(config["port_map"])) |
| 459 | |
Rich Lane | e55abf7 | 2012-07-26 20:11:42 -0700 | [diff] [blame] | 460 | oftest.ofutils.default_timeout = config["default_timeout"] |
Rich Lane | da3b5ad | 2012-10-03 09:05:32 -0700 | [diff] [blame] | 461 | oftest.testutils.MINSIZE = config['minsize'] |
Rich Lane | e55abf7 | 2012-07-26 20:11:42 -0700 | [diff] [blame] | 462 | |
Rich Lane | ee57ad0 | 2012-07-13 15:40:36 -0700 | [diff] [blame] | 463 | if os.getuid() != 0 and not config["allow_user"]: |
Brandon Heller | 446c143 | 2010-04-01 12:43:27 -0700 | [diff] [blame] | 464 | print "ERROR: Super-user privileges required. Please re-run with " \ |
| 465 | "sudo or as root." |
Rich Lane | d1d9c28 | 2012-10-04 22:07:10 -0700 | [diff] [blame] | 466 | sys.exit(1) |
Brandon Heller | 446c143 | 2010-04-01 12:43:27 -0700 | [diff] [blame] | 467 | |
Rich Lane | 8592bec | 2012-09-03 09:06:59 -0700 | [diff] [blame] | 468 | if config["random_seed"] is not None: |
| 469 | logging.info("Random seed: %d" % config["random_seed"]) |
| 470 | random.seed(config["random_seed"]) |
| 471 | |
Rich Lane | 5bd6cf9 | 2012-10-04 17:57:24 -0700 | [diff] [blame] | 472 | # Remove python's signal handler which raises KeyboardError. Exiting from an |
| 473 | # exception waits for all threads to terminate which might not happen. |
| 474 | signal.signal(signal.SIGINT, signal.SIG_DFL) |
Dan Talayco | ac25cf3 | 2010-07-20 14:08:28 -0700 | [diff] [blame] | 475 | |
| 476 | if __name__ == "__main__": |
Rich Lane | 2c7812c | 2012-12-27 17:52:23 -0800 | [diff] [blame] | 477 | # Set up the dataplane |
| 478 | oftest.dataplane_instance = oftest.dataplane.DataPlane(config) |
| 479 | for of_port, ifname in config["port_map"].items(): |
| 480 | oftest.dataplane_instance.port_add(ifname, of_port) |
| 481 | |
Dan Talayco | ac25cf3 | 2010-07-20 14:08:28 -0700 | [diff] [blame] | 482 | logging.info("*** TEST RUN START: " + time.asctime()) |
Rich Lane | 68edb3d | 2013-01-04 17:00:26 -0800 | [diff] [blame] | 483 | result = unittest.TextTestRunner(verbosity=2).run(suite) |
Rich Lane | da3b5ad | 2012-10-03 09:05:32 -0700 | [diff] [blame] | 484 | if oftest.testutils.skipped_test_count > 0: |
Dan Talayco | ba3745c | 2010-07-21 21:51:08 -0700 | [diff] [blame] | 485 | ts = " tests" |
Rich Lane | da3b5ad | 2012-10-03 09:05:32 -0700 | [diff] [blame] | 486 | if oftest.testutils.skipped_test_count == 1: ts = " test" |
| 487 | logging.info("Skipped " + str(oftest.testutils.skipped_test_count) + ts) |
| 488 | print("Skipped " + str(oftest.testutils.skipped_test_count) + ts) |
Dan Talayco | ac25cf3 | 2010-07-20 14:08:28 -0700 | [diff] [blame] | 489 | logging.info("*** TEST RUN END : " + time.asctime()) |
Rich Lane | 2c7812c | 2012-12-27 17:52:23 -0800 | [diff] [blame] | 490 | |
| 491 | # Shutdown the dataplane |
| 492 | oftest.dataplane_instance.kill() |
| 493 | oftest.dataplane_instance = None |
| 494 | |
Rich Lane | 50d42eb | 2012-07-16 11:57:03 -0700 | [diff] [blame] | 495 | if result.failures or result.errors: |
Shudong Zhou | d895fcb | 2012-08-01 19:09:55 -0700 | [diff] [blame] | 496 | # exit(1) hangs sometimes |
| 497 | os._exit(1) |
Rich Lane | da3b5ad | 2012-10-03 09:05:32 -0700 | [diff] [blame] | 498 | if oftest.testutils.skipped_test_count > 0 and config["fail_skipped"]: |
Shudong Zhou | d895fcb | 2012-08-01 19:09:55 -0700 | [diff] [blame] | 499 | os._exit(1) |