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