blob: 00e31bd1b59808a96790aef55b6aad63e9833e8c [file] [log] [blame]
Dan Talayco48370102010-03-03 15:17:33 -08001#!/usr/bin/env python
2"""
3@package oft
4
5OpenFlow test framework top level script
6
Rich Laneea5060d2013-01-06 13:59:00 -08007This script is the entry point for running OpenFlow tests using the OFT
8framework. For usage information, see --help or the README.
Dan Talayco48370102010-03-03 15:17:33 -08009
Rich Laneea5060d2013-01-06 13:59:00 -080010To add a new command line option, edit both the config_default dictionary and
11the config_setup function. The option's result will end up in the global
12oftest.config dictionary.
Dan Talayco48370102010-03-03 15:17:33 -080013"""
14
15import sys
Rich Lane95f078b2013-01-06 13:24:58 -080016import optparse
Dan Talayco2c0dba32010-03-06 22:47:06 -080017from subprocess import Popen,PIPE
Dan Talayco48370102010-03-03 15:17:33 -080018import logging
19import unittest
Dan Talayco2c0dba32010-03-06 22:47:06 -080020import time
Brandon Heller446c1432010-04-01 12:43:27 -070021import os
Rich Lane6b452bc2012-07-09 16:52:21 -070022import imp
Rich Lane8592bec2012-09-03 09:06:59 -070023import random
Rich Lane5bd6cf92012-10-04 17:57:24 -070024import signal
Rich Lane943be672012-10-04 19:20:16 -070025import fnmatch
Rich Lane2d6d4822013-01-08 10:49:16 -080026import copy
Dan Talayco48370102010-03-03 15:17:33 -080027
Rich Lanefadf3452012-10-03 16:23:37 -070028root_dir = os.path.dirname(os.path.realpath(__file__))
29
30pydir = os.path.join(root_dir, 'src', 'python')
Rich Lane39878042012-07-09 14:45:35 -070031if os.path.exists(os.path.join(pydir, 'oftest')):
32 # Running from source tree
33 sys.path.insert(0, pydir)
34
Rich Lane477f4812012-10-04 22:49:00 -070035import oftest
36from oftest import config
Rich Lanee55abf72012-07-26 20:11:42 -070037import oftest.ofutils
Rich Lane95f078b2013-01-06 13:24:58 -080038import oftest.help_formatter
Dan Talaycoba3745c2010-07-21 21:51:08 -070039
Dan Talayco02eca0b2010-04-15 16:09:43 -070040try:
41 import scapy.all as scapy
42except:
43 try:
44 import scapy as scapy
45 except:
46 sys.exit("Need to install scapy for packet parsing")
47
Dan Talayco48370102010-03-03 15:17:33 -080048##@var DEBUG_LEVELS
49# Map from strings to debugging levels
50DEBUG_LEVELS = {
51 'debug' : logging.DEBUG,
52 'verbose' : logging.DEBUG,
53 'info' : logging.INFO,
54 'warning' : logging.WARNING,
55 'warn' : logging.WARNING,
56 'error' : logging.ERROR,
57 'critical' : logging.CRITICAL
58}
59
Dan Talayco48370102010-03-03 15:17:33 -080060##@var config_default
61# The default configuration dictionary for OFT
62config_default = {
Rich Lane95f078b2013-01-06 13:24:58 -080063 # Miscellaneous options
64 "list" : False,
65 "list_test_names" : False,
66 "allow_user" : False,
67
68 # Test selection options
Rich Lanec76b09a2013-01-02 16:53:22 -080069 "test_spec" : "",
Rich Lane1a08d232013-01-04 16:03:50 -080070 "test_file" : None,
Rich Lane95f078b2013-01-06 13:24:58 -080071 "test_dir" : os.path.join(root_dir, "tests"),
72
73 # Switch connection options
74 "controller_host" : "0.0.0.0", # For passive bind
75 "controller_port" : 6633,
76 "switch_ip" : None, # If not none, actively connect to switch
Rich Lane15f26322013-01-08 11:23:24 -080077 "platform" : "eth",
Rich Lane95f078b2013-01-06 13:24:58 -080078 "platform_args" : None,
79 "platform_dir" : os.path.join(root_dir, "platforms"),
Rich Lane2d6d4822013-01-08 10:49:16 -080080 "interfaces" : [],
Rich Lane9fd05682013-01-10 15:30:38 -080081 "openflow_version" : "1.0",
Rich Lane95f078b2013-01-06 13:24:58 -080082
83 # Logging options
Dan Talayco48370102010-03-03 15:17:33 -080084 "log_file" : "oft.log",
Dan Talayco69ca4d62012-11-15 11:50:22 -080085 "log_append" : False,
Rich Lane95f078b2013-01-06 13:24:58 -080086 "debug" : "verbose",
87
88 # Test behavior options
89 "relax" : False,
Dan Talaycod7c80d12012-04-03 15:20:57 -070090 "test_params" : "None",
Rich Lane9a84a4f2012-07-17 12:27:42 -070091 "fail_skipped" : False,
Rich Lanee55abf72012-07-26 20:11:42 -070092 "default_timeout" : 2,
Jeffrey Townsend5cb7ed32012-08-17 18:11:01 +000093 "minsize" : 0,
Rich Lane8592bec2012-09-03 09:06:59 -070094 "random_seed" : None,
Rich Lane95f078b2013-01-06 13:24:58 -080095
96 # Other configuration
Rich Lane15f26322013-01-08 11:23:24 -080097 "port_map" : {},
Dan Talayco48370102010-03-03 15:17:33 -080098}
99
Rich Lane95f078b2013-01-06 13:24:58 -0800100def config_setup():
Dan Talayco48370102010-03-03 15:17:33 -0800101 """
102 Set up the configuration including parsing the arguments
103
Dan Talayco48370102010-03-03 15:17:33 -0800104 @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 Lane4113a582013-01-03 10:13:02 -0800108 usage = "usage: %prog [options] (test|group)..."
109
Rich Lane95f078b2013-01-06 13:24:58 -0800110 description = """\
Rich Lane4113a582013-01-03 10:13:02 -0800111OFTest is a framework and set of tests for validating OpenFlow switches.
112
113The default configuration assumes that an OpenFlow 1.0 switch is attempting to
114connect to a controller on the machine running OFTest, port 6633. Additionally,
115the interfaces veth1, veth3, veth5, and veth7 should be connected to the switch's
116dataplane.
117
118If no positional arguments are given then OFTest will run all tests that
119depend only on standard OpenFlow 1.0. Otherwise each positional argument
120is interpreted as either a test name or a test group name. The union of
121these will be executed. To see what groups each test belongs to use the
Rich Lane948f0302013-01-07 10:59:08 -0800122--list option. Tests and groups can be subtracted from the result by
123prefixing them with the '^' character.
Rich Lane4113a582013-01-03 10:13:02 -0800124"""
125
Rich Lane2d6d4822013-01-08 10:49:16 -0800126 # 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 Lane95f078b2013-01-06 13:24:58 -0800140 parser = optparse.OptionParser(version="%prog 0.1",
141 usage=usage,
142 description=description,
Rich Lane2d6d4822013-01-08 10:49:16 -0800143 formatter=oftest.help_formatter.HelpFormatter(),
144 option_class=Option)
Dan Talayco48370102010-03-03 15:17:33 -0800145
146 # Set up default values
Rich Lane95f078b2013-01-06 13:24:58 -0800147 parser.set_defaults(**config_default)
Dan Talayco48370102010-03-03 15:17:33 -0800148
Dan Talayco2c0dba32010-03-06 22:47:06 -0800149 parser.add_option("--list", action="store_true",
Brandon Heller824504e2010-04-01 12:21:37 -0700150 help="List all tests and exit")
Rich Lane95f078b2013-01-06 13:24:58 -0800151 parser.add_option("--list-test-names", action='store_true',
152 help="List test names matching the test spec and exit")
Rich Laneee57ad02012-07-13 15:40:36 -0700153 parser.add_option("--allow-user", action="store_true",
154 help="Proceed even if oftest is not run as root")
Rich Lane95f078b2013-01-06 13:24:58 -0800155
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 Lane2d6d4822013-01-08 10:49:16 -0800172 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 Lane9fd05682013-01-10 15:30:38 -0800174 group.add_option("--of-version", "-V", dest="openflow_version", choices=["1.0", "1.1", "1.2"],
175 help="OpenFlow version to use")
Rich Lane95f078b2013-01-06 13:24:58 -0800176 parser.add_option_group(group)
177
178 group = optparse.OptionGroup(parser, "Logging options")
179 group.add_option("--log-file",
180 help="Name of log file, empty string to log to console (default %default)")
181 group.add_option("--log-append", action="store_true",
182 help="Do not delete log file if specified")
183 dbg_lvl_names = sorted(DEBUG_LEVELS.keys(), key=lambda x: DEBUG_LEVELS[x])
184 group.add_option("--debug", choices=dbg_lvl_names,
185 help="Debug lvl: debug, info, warning, error, critical (default %default)")
186 group.add_option("-v", "--verbose", action="store_const", dest="debug",
187 const="verbose", help="Shortcut for --debug=verbose")
188 group.add_option("-q", "--quiet", action="store_const", dest="debug",
189 const="warning", help="Shortcut for --debug=warning")
190 parser.add_option_group(group)
191
192 group = optparse.OptionGroup(parser, "Test behavior options")
193 group.add_option("--relax", action="store_true",
194 help="Relax packet match checks allowing other packets")
195 test_params_help = """Set test parameters: key=val;... (see --list)
196 """
197 group.add_option("-t", "--test-params", help=test_params_help)
198 group.add_option("--fail-skipped", action="store_true",
Rich Lane9a84a4f2012-07-17 12:27:42 -0700199 help="Return failure if any test was skipped")
Rich Lane95f078b2013-01-06 13:24:58 -0800200 group.add_option("--default-timeout", type="int",
Rich Lanee55abf72012-07-26 20:11:42 -0700201 help="Timeout in seconds for most operations")
Rich Lane95f078b2013-01-06 13:24:58 -0800202 group.add_option("--minsize", type="int",
203 help="Minimum allowable packet size on the dataplane.")
204 group.add_option("--random-seed", type="int",
205 help="Random number generator seed")
206 parser.add_option_group(group)
Jeffrey Townsend5cb7ed32012-08-17 18:11:01 +0000207
Dan Talayco48370102010-03-03 15:17:33 -0800208 # Might need this if other parsers want command line
209 # parser.allow_interspersed_args = False
210 (options, args) = parser.parse_args()
211
Rich Lane95f078b2013-01-06 13:24:58 -0800212 # Convert options from a Namespace to a plain dictionary
213 config = config_default.copy()
214 for key in config.keys():
215 config[key] = getattr(options, key)
Dan Talayco48370102010-03-03 15:17:33 -0800216
217 return (config, args)
218
219def logging_setup(config):
220 """
221 Set up logging based on config
222 """
Rich Lane71d887d2012-12-22 17:05:13 -0800223 _format = "%(asctime)s.%(msecs)d %(name)-10s: %(levelname)-8s: %(message)s"
Dan Talayco48370102010-03-03 15:17:33 -0800224 _datefmt = "%H:%M:%S"
Dan Talayco69ca4d62012-11-15 11:50:22 -0800225 _mode = config["log_append"] and "a" or "w"
Dan Talayco88fc8802010-03-07 11:37:52 -0800226 logging.basicConfig(filename=config["log_file"],
Dan Talayco69ca4d62012-11-15 11:50:22 -0800227 filemode=_mode,
Rich Lane95f078b2013-01-06 13:24:58 -0800228 level=DEBUG_LEVELS[config["debug"]],
Dan Talayco88fc8802010-03-07 11:37:52 -0800229 format=_format, datefmt=_datefmt)
Dan Talayco48370102010-03-03 15:17:33 -0800230
Rich Lane943be672012-10-04 19:20:16 -0700231def load_test_modules(config):
232 """
233 Load tests from the test_dir directory.
Dan Talayco2c0dba32010-03-06 22:47:06 -0800234
Rich Lane943be672012-10-04 19:20:16 -0700235 Test cases are subclasses of unittest.TestCase
Dan Talayco2c0dba32010-03-06 22:47:06 -0800236
Rich Lanecc45b8e2013-01-02 15:55:02 -0800237 Also updates the _groups member to include "standard" and
238 module test groups if appropriate.
239
Dan Talayco2c0dba32010-03-06 22:47:06 -0800240 @param config The oft configuration dictionary
Rich Lane943be672012-10-04 19:20:16 -0700241 @returns A dictionary from test module names to tuples of
242 (module, dictionary from test names to test classes).
Dan Talayco2c0dba32010-03-06 22:47:06 -0800243 """
244
Rich Lane943be672012-10-04 19:20:16 -0700245 result = {}
Dan Talayco2c0dba32010-03-06 22:47:06 -0800246
Rich Lane943be672012-10-04 19:20:16 -0700247 for root, dirs, filenames in os.walk(config["test_dir"]):
248 # Iterate over each python file
249 for filename in fnmatch.filter(filenames, '[!.]*.py'):
250 modname = os.path.splitext(os.path.basename(filename))[0]
Dan Talayco2c0dba32010-03-06 22:47:06 -0800251
Rich Lane943be672012-10-04 19:20:16 -0700252 try:
253 if sys.modules.has_key(modname):
254 mod = sys.modules[modname]
255 else:
256 mod = imp.load_module(modname, *imp.find_module(modname, [root]))
257 except:
258 logging.warning("Could not import file " + filename)
259 raise
Rich Lane520e4152012-07-09 16:18:16 -0700260
Rich Lane943be672012-10-04 19:20:16 -0700261 # Find all testcases defined in the module
262 tests = dict((k, v) for (k, v) in mod.__dict__.items() if type(v) == type and
263 issubclass(v, unittest.TestCase))
264 if tests:
Rich Lanecc45b8e2013-01-02 15:55:02 -0800265 for (testname, test) in tests.items():
266 # Set default annotation values
267 if not hasattr(test, "_groups"):
268 test._groups = []
269 if not hasattr(test, "_nonstandard"):
270 test._nonstandard = False
271 if not hasattr(test, "_disabled"):
272 test._disabled = False
273
274 # Put test in its module's test group
275 if not test._disabled:
276 test._groups.append(modname)
277
278 # Put test in the standard test group
279 if not test._disabled and not test._nonstandard:
280 test._groups.append("standard")
281 test._groups.append("all") # backwards compatibility
282
Rich Lane943be672012-10-04 19:20:16 -0700283 result[modname] = (mod, tests)
Rich Lane520e4152012-07-09 16:18:16 -0700284
Rich Lane943be672012-10-04 19:20:16 -0700285 return result
Dan Talayco2c0dba32010-03-06 22:47:06 -0800286
Rich Lane5a9a1922013-01-11 14:29:30 -0800287def prune_tests(test_specs, test_modules, version):
Rich Lane15f64de2012-10-04 21:25:57 -0700288 """
Rich Lane5a9a1922013-01-11 14:29:30 -0800289 Return tests matching the given test-specs and OpenFlow version
Rich Lanec76b09a2013-01-02 16:53:22 -0800290 @param test_specs A list of group names or test names.
Rich Lane5a9a1922013-01-11 14:29:30 -0800291 @param version An OpenFlow version (e.g. "1.0")
Rich Lane15f64de2012-10-04 21:25:57 -0700292 @param test_modules Same format as the output of load_test_modules.
293 @returns Same format as the output of load_test_modules.
294 """
295 result = {}
Rich Lanec76b09a2013-01-02 16:53:22 -0800296 for e in test_specs:
Rich Lane15f64de2012-10-04 21:25:57 -0700297 matched = False
Rich Lane948f0302013-01-07 10:59:08 -0800298
299 if e.startswith('^'):
300 negated = True
301 e = e[1:]
302 else:
303 negated = False
304
Rich Lane15f64de2012-10-04 21:25:57 -0700305 for (modname, (mod, tests)) in test_modules.items():
Rich Lanecc45b8e2013-01-02 15:55:02 -0800306 for (testname, test) in tests.items():
307 if e in test._groups or e == "%s.%s" % (modname, testname):
308 result.setdefault(modname, (mod, {}))
Rich Lane948f0302013-01-07 10:59:08 -0800309 if not negated:
Rich Lane5a9a1922013-01-11 14:29:30 -0800310 if not hasattr(test, "_versions") or version in test._versions:
311 result[modname][1][testname] = test
Rich Lane948f0302013-01-07 10:59:08 -0800312 else:
313 if modname in result and testname in result[modname][1]:
314 del result[modname][1][testname]
315 if not result[modname][1]:
316 del result[modname]
Rich Lanecc45b8e2013-01-02 15:55:02 -0800317 matched = True
Rich Lane948f0302013-01-07 10:59:08 -0800318
Rich Lane15f64de2012-10-04 21:25:57 -0700319 if not matched:
Rich Lanecc45b8e2013-01-02 15:55:02 -0800320 die("test-spec element %s did not match any tests" % e)
Rich Lane948f0302013-01-07 10:59:08 -0800321
Rich Lane15f64de2012-10-04 21:25:57 -0700322 return result
323
Dan Talayco2c0dba32010-03-06 22:47:06 -0800324def die(msg, exit_val=1):
325 print msg
326 logging.critical(msg)
327 sys.exit(exit_val)
328
Dan Talayco79f36082010-03-11 16:53:53 -0800329def _space_to(n, str):
330 """
331 Generate a string of spaces to achieve width n given string str
332 If length of str >= n, return one space
333 """
334 spaces = n - len(str)
335 if spaces > 0:
336 return " " * spaces
337 return " "
338
Dan Talayco48370102010-03-03 15:17:33 -0800339#
340# Main script
341#
342
Rich Lane477f4812012-10-04 22:49:00 -0700343# Setup global configuration
Rich Lane95f078b2013-01-06 13:24:58 -0800344(new_config, args) = config_setup()
Rich Lane477f4812012-10-04 22:49:00 -0700345oftest.config.update(new_config)
Dan Talayco48370102010-03-03 15:17:33 -0800346
Rich Laned7a85c42012-09-28 15:38:45 -0700347logging_setup(config)
348logging.info("++++++++ " + time.asctime() + " ++++++++")
349
Rich Lane9fd05682013-01-10 15:30:38 -0800350# Pick an OpenFlow protocol module based on the configured version
351# This will go away once we have a single protocol module that
352# can handle all OpenFlow versions.
353if config["openflow_version"] == "1.0":
354 import of10
355 sys.modules["ofp"] = of10
356elif config["openflow_version"] == "1.1":
357 import of11
358 sys.modules["ofp"] = of11
359elif config["openflow_version"] == "1.2":
360 import of12
361 sys.modules["ofp"] = of12
362else:
363 assert(False)
364
365# HACK: testutils.py imports controller.py, which needs the ofp module
366import oftest.testutils
367
Rich Lanee284b6b2012-10-03 09:19:58 -0700368# Allow tests to import each other
369sys.path.append(config["test_dir"])
370
Rich Lanec76b09a2013-01-02 16:53:22 -0800371test_specs = args
372if config["test_spec"] != "":
373 print >> sys.stderr, "WARNING: The --test-spec option is deprecated"
374 test_specs += config["test_spec"].split(',')
Rich Lane1a08d232013-01-04 16:03:50 -0800375if config["test_file"] != None:
376 with open(config["test_file"], 'r') as f:
377 for line in f:
378 line, _, _ = line.partition('#') # remove comments
379 line = line.strip()
380 if line:
381 test_specs.append(line)
Rich Lanec76b09a2013-01-02 16:53:22 -0800382if test_specs == []:
383 test_specs = ["standard"]
384
Rich Laned8e45482013-01-02 17:36:21 -0800385test_modules = load_test_modules(config)
Dan Talayco2c0dba32010-03-06 22:47:06 -0800386
387# Check if test list is requested; display and exit if so
388if config["list"]:
Dan Talayco7f8dba82012-04-12 12:58:52 -0700389 mod_count = 0
390 test_count = 0
Rich Lane37f42112013-01-03 13:41:49 -0800391 all_groups = set()
Rich Lanee811e7b2013-01-03 13:36:54 -0800392 print """\
393Tests are shown grouped by module. If a test is in any groups beyond "standard"
394and its module's group then they are shown in parentheses."""
395 print
396 print """\
397Tests marked with '*' are non-standard and may require vendor extensions or
398special switch configuration. These are not part of the "standard" test group."""
399 print
400 print """\
401Tests marked with '!' are disabled because they are experimental, special-purpose,
402or are too long to be run normally. These are not part of the "standard" test
403group or their module's test group."""
404 print
405 print "Tests marked (TP1) after name take --test-params including:"
406 print " 'vid=N;strip_vlan=bool;add_vlan=bool'"
Rich Lanee811e7b2013-01-03 13:36:54 -0800407 print
408 print "Test List:"
Rich Lane943be672012-10-04 19:20:16 -0700409 for (modname, (mod, tests)) in test_modules.items():
Dan Talayco7f8dba82012-04-12 12:58:52 -0700410 mod_count += 1
Rich Lane80efd652013-01-02 14:05:20 -0800411 desc = (mod.__doc__ or "No description").strip().split('\n')[0]
Dan Talayco79f36082010-03-11 16:53:53 -0800412 start_str = " Module " + mod.__name__ + ": "
413 print start_str + _space_to(22, start_str) + desc
Rich Lane943be672012-10-04 19:20:16 -0700414 for (testname, test) in tests.items():
Dan Talayco551befa2010-07-15 17:05:32 -0700415 try:
Rich Lane0f5b9c72013-01-02 14:05:20 -0800416 desc = (test.__doc__ or "").strip()
Dan Talayco551befa2010-07-15 17:05:32 -0700417 desc = desc.split('\n')[0]
418 except:
419 desc = "No description"
Rich Lanee811e7b2013-01-03 13:36:54 -0800420 groups = set(test._groups) - set(["all", "standard", modname])
Rich Lane37f42112013-01-03 13:41:49 -0800421 all_groups.update(test._groups)
Rich Lanee811e7b2013-01-03 13:36:54 -0800422 if groups:
423 desc = "(%s) %s" % (",".join(groups), desc)
Rich Lane5a9a1922013-01-11 14:29:30 -0800424 if hasattr(test, "_versions"):
425 desc = "(%s) %s" % (",".join(sorted(test._versions)), desc)
Rich Lanee811e7b2013-01-03 13:36:54 -0800426 start_str = " %s%s %s:" % (test._nonstandard and "*" or " ",
427 test._disabled and "!" or " ",
428 testname)
Dan Talayco551befa2010-07-15 17:05:32 -0700429 if len(start_str) > 22:
430 desc = "\n" + _space_to(22, "") + desc
Dan Talayco79f36082010-03-11 16:53:53 -0800431 print start_str + _space_to(22, start_str) + desc
Dan Talayco7f8dba82012-04-12 12:58:52 -0700432 test_count += 1
Dan Talayco79f36082010-03-11 16:53:53 -0800433 print
Rich Lane15f64de2012-10-04 21:25:57 -0700434 print "%d modules shown with a total of %d tests" % \
435 (mod_count, test_count)
436 print
Rich Lane37f42112013-01-03 13:41:49 -0800437 print "Test groups: %s" % (', '.join(sorted(all_groups)))
438
Dan Talayco2c0dba32010-03-06 22:47:06 -0800439 sys.exit(0)
440
Rich Lane5a9a1922013-01-11 14:29:30 -0800441test_modules = prune_tests(test_specs, test_modules, config["openflow_version"])
Rich Laned8e45482013-01-02 17:36:21 -0800442
Jeffrey Townsend6614f972012-08-16 13:27:18 -0700443# Check if test list is requested; display and exit if so
444if config["list_test_names"]:
Rich Lane943be672012-10-04 19:20:16 -0700445 for (modname, (mod, tests)) in test_modules.items():
Rich Lane943be672012-10-04 19:20:16 -0700446 for (testname, test) in tests.items():
Rich Lanecc45b8e2013-01-02 15:55:02 -0800447 print "%s.%s" % (modname, testname)
Jeffrey Townsend6614f972012-08-16 13:27:18 -0700448 sys.exit(0)
449
Dan Talayco2c0dba32010-03-06 22:47:06 -0800450# Generate the test suite
451#@todo Decide if multiple suites are ever needed
452suite = unittest.TestSuite()
453
Rich Lane15f64de2012-10-04 21:25:57 -0700454for (modname, (mod, tests)) in test_modules.items():
455 for (testname, test) in tests.items():
Rich Lanecc45b8e2013-01-02 15:55:02 -0800456 suite.addTest(test())
Dan Talayco2c0dba32010-03-06 22:47:06 -0800457
Rich Lane51590f62012-10-09 15:06:29 -0700458# Allow platforms to import each other
459sys.path.append(config["platform_dir"])
460
Rich Lane8aebc5e2012-09-25 17:57:53 -0700461# Load the platform module
462platform_name = config["platform"]
463logging.info("Importing platform: " + platform_name)
464platform_mod = None
465try:
Rich Lane483e1542012-10-05 09:29:39 -0700466 platform_mod = imp.load_module(platform_name, *imp.find_module(platform_name, [config["platform_dir"]]))
Rich Lane8aebc5e2012-09-25 17:57:53 -0700467except:
468 logging.warn("Failed to import " + platform_name + " platform module")
469 raise
Dan Talayco48370102010-03-03 15:17:33 -0800470
471try:
Rich Lane8aebc5e2012-09-25 17:57:53 -0700472 platform_mod.platform_config_update(config)
Dan Talayco48370102010-03-03 15:17:33 -0800473except:
474 logging.warn("Could not run platform host configuration")
Rich Laneef403e52012-07-09 14:59:43 -0700475 raise
Dan Talayco48370102010-03-03 15:17:33 -0800476
477if not config["port_map"]:
Rich Lane8aebc5e2012-09-25 17:57:53 -0700478 die("Interface port map was not defined by the platform. Exiting.")
Dan Talayco48370102010-03-03 15:17:33 -0800479
480logging.debug("Configuration: " + str(config))
481logging.info("OF port map: " + str(config["port_map"]))
482
Rich Lanee55abf72012-07-26 20:11:42 -0700483oftest.ofutils.default_timeout = config["default_timeout"]
Rich Laneda3b5ad2012-10-03 09:05:32 -0700484oftest.testutils.MINSIZE = config['minsize']
Rich Lanee55abf72012-07-26 20:11:42 -0700485
Rich Laneee57ad02012-07-13 15:40:36 -0700486if os.getuid() != 0 and not config["allow_user"]:
Brandon Heller446c1432010-04-01 12:43:27 -0700487 print "ERROR: Super-user privileges required. Please re-run with " \
488 "sudo or as root."
Rich Laned1d9c282012-10-04 22:07:10 -0700489 sys.exit(1)
Brandon Heller446c1432010-04-01 12:43:27 -0700490
Rich Lane8592bec2012-09-03 09:06:59 -0700491if config["random_seed"] is not None:
492 logging.info("Random seed: %d" % config["random_seed"])
493 random.seed(config["random_seed"])
494
Rich Lane5bd6cf92012-10-04 17:57:24 -0700495# Remove python's signal handler which raises KeyboardError. Exiting from an
496# exception waits for all threads to terminate which might not happen.
497signal.signal(signal.SIGINT, signal.SIG_DFL)
Dan Talaycoac25cf32010-07-20 14:08:28 -0700498
499if __name__ == "__main__":
Rich Lane2c7812c2012-12-27 17:52:23 -0800500 # Set up the dataplane
501 oftest.dataplane_instance = oftest.dataplane.DataPlane(config)
502 for of_port, ifname in config["port_map"].items():
503 oftest.dataplane_instance.port_add(ifname, of_port)
504
Dan Talaycoac25cf32010-07-20 14:08:28 -0700505 logging.info("*** TEST RUN START: " + time.asctime())
Rich Lane68edb3d2013-01-04 17:00:26 -0800506 result = unittest.TextTestRunner(verbosity=2).run(suite)
Rich Laneda3b5ad2012-10-03 09:05:32 -0700507 if oftest.testutils.skipped_test_count > 0:
Dan Talaycoba3745c2010-07-21 21:51:08 -0700508 ts = " tests"
Rich Laneda3b5ad2012-10-03 09:05:32 -0700509 if oftest.testutils.skipped_test_count == 1: ts = " test"
510 logging.info("Skipped " + str(oftest.testutils.skipped_test_count) + ts)
511 print("Skipped " + str(oftest.testutils.skipped_test_count) + ts)
Dan Talaycoac25cf32010-07-20 14:08:28 -0700512 logging.info("*** TEST RUN END : " + time.asctime())
Rich Lane2c7812c2012-12-27 17:52:23 -0800513
514 # Shutdown the dataplane
515 oftest.dataplane_instance.kill()
516 oftest.dataplane_instance = None
517
Rich Lane50d42eb2012-07-16 11:57:03 -0700518 if result.failures or result.errors:
Shudong Zhoud895fcb2012-08-01 19:09:55 -0700519 # exit(1) hangs sometimes
520 os._exit(1)
Rich Laneda3b5ad2012-10-03 09:05:32 -0700521 if oftest.testutils.skipped_test_count > 0 and config["fail_skipped"]:
Shudong Zhoud895fcb2012-08-01 19:09:55 -0700522 os._exit(1)