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 | |
Stephen Finucane | 7424a74 | 2014-05-15 19:46:06 +0100 | [diff] [blame] | 10 | To add a new command line option, edit both the CONFIG_DEFAULT dictionary and |
Rich Lane | ea5060d | 2013-01-06 13:59:00 -0800 | [diff] [blame] | 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 | |
Stephen Finucane | 0158865 | 2014-05-15 20:03:12 +0100 | [diff] [blame] | 15 | from __future__ import print_function |
| 16 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 17 | import sys |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 18 | import optparse |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 19 | import logging |
| 20 | import unittest |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 21 | import time |
Brandon Heller | 446c143 | 2010-04-01 12:43:27 -0700 | [diff] [blame] | 22 | import os |
Rich Lane | 6b452bc | 2012-07-09 16:52:21 -0700 | [diff] [blame] | 23 | import imp |
Rich Lane | 8592bec | 2012-09-03 09:06:59 -0700 | [diff] [blame] | 24 | import random |
Rich Lane | 5bd6cf9 | 2012-10-04 17:57:24 -0700 | [diff] [blame] | 25 | import signal |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 26 | import fnmatch |
Rich Lane | 2d6d482 | 2013-01-08 10:49:16 -0800 | [diff] [blame] | 27 | import copy |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 28 | |
Stephen Finucane | 7424a74 | 2014-05-15 19:46:06 +0100 | [diff] [blame] | 29 | ROOT_DIR = os.path.dirname(os.path.realpath(__file__)) |
Rich Lane | fadf345 | 2012-10-03 16:23:37 -0700 | [diff] [blame] | 30 | |
Stephen Finucane | 7424a74 | 2014-05-15 19:46:06 +0100 | [diff] [blame] | 31 | PY_SRC_DIR = os.path.join(ROOT_DIR, 'src', 'python') |
| 32 | if os.path.exists(os.path.join(PY_SRC_DIR, 'oftest')): |
Rich Lane | 3987804 | 2012-07-09 14:45:35 -0700 | [diff] [blame] | 33 | # Running from source tree |
Stephen Finucane | 7424a74 | 2014-05-15 19:46:06 +0100 | [diff] [blame] | 34 | sys.path.insert(0, PY_SRC_DIR) |
Rich Lane | 3987804 | 2012-07-09 14:45:35 -0700 | [diff] [blame] | 35 | |
macauley | c454079 | 2015-08-03 09:40:05 +0800 | [diff] [blame] | 36 | if os.path.exists(os.path.join(ROOT_DIR, 'accton')): |
| 37 | PY_ACCTON_DIR = os.path.join(ROOT_DIR, 'accton') |
| 38 | sys.path.insert(0, PY_ACCTON_DIR) |
| 39 | |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 40 | import oftest |
| 41 | from oftest import config |
Rich Lane | e55abf7 | 2012-07-26 20:11:42 -0700 | [diff] [blame] | 42 | import oftest.ofutils |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 43 | import oftest.help_formatter |
Rich Lane | 3f7098c | 2013-03-12 10:28:32 -0700 | [diff] [blame] | 44 | import loxi |
Dan Talayco | ba3745c | 2010-07-21 21:51:08 -0700 | [diff] [blame] | 45 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 46 | ##@var DEBUG_LEVELS |
| 47 | # Map from strings to debugging levels |
| 48 | DEBUG_LEVELS = { |
| 49 | 'debug' : logging.DEBUG, |
| 50 | 'verbose' : logging.DEBUG, |
| 51 | 'info' : logging.INFO, |
| 52 | 'warning' : logging.WARNING, |
| 53 | 'warn' : logging.WARNING, |
| 54 | 'error' : logging.ERROR, |
| 55 | 'critical' : logging.CRITICAL |
| 56 | } |
| 57 | |
Stephen Finucane | 7424a74 | 2014-05-15 19:46:06 +0100 | [diff] [blame] | 58 | ##@var CONFIG_DEFAULT |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 59 | # The default configuration dictionary for OFT |
Stephen Finucane | 7424a74 | 2014-05-15 19:46:06 +0100 | [diff] [blame] | 60 | CONFIG_DEFAULT = { |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 61 | # Miscellaneous options |
| 62 | "list" : False, |
| 63 | "list_test_names" : False, |
| 64 | "allow_user" : False, |
| 65 | |
| 66 | # Test selection options |
Rich Lane | c76b09a | 2013-01-02 16:53:22 -0800 | [diff] [blame] | 67 | "test_spec" : "", |
Rich Lane | 1a08d23 | 2013-01-04 16:03:50 -0800 | [diff] [blame] | 68 | "test_file" : None, |
Rich Lane | 74b13d1 | 2013-05-03 17:58:50 -0700 | [diff] [blame] | 69 | "test_dir" : None, |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 70 | |
| 71 | # Switch connection options |
| 72 | "controller_host" : "0.0.0.0", # For passive bind |
Rich Lane | 4d1f3eb | 2013-10-03 13:45:57 -0700 | [diff] [blame] | 73 | "controller_port" : 6653, |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 74 | "switch_ip" : None, # If not none, actively connect to switch |
Rich Lane | 15f2632 | 2013-01-08 11:23:24 -0800 | [diff] [blame] | 75 | "platform" : "eth", |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 76 | "platform_args" : None, |
Stephen Finucane | 7424a74 | 2014-05-15 19:46:06 +0100 | [diff] [blame] | 77 | "platform_dir" : os.path.join(ROOT_DIR, "platforms"), |
Rich Lane | 2d6d482 | 2013-01-08 10:49:16 -0800 | [diff] [blame] | 78 | "interfaces" : [], |
Rich Lane | 9fd0568 | 2013-01-10 15:30:38 -0800 | [diff] [blame] | 79 | "openflow_version" : "1.0", |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 80 | |
| 81 | # Logging options |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 82 | "log_file" : "oft.log", |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame] | 83 | "log_dir" : None, |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 84 | "debug" : "verbose", |
Rich Lane | 9631f00 | 2014-03-21 18:05:16 -0700 | [diff] [blame] | 85 | "profile" : False, |
Stephen Finucane | 6219af7 | 2014-05-14 21:08:30 +0100 | [diff] [blame] | 86 | "profile_file" : "profile.out", |
Stephen Finucane | e016cf2 | 2014-04-16 22:04:11 +0100 | [diff] [blame] | 87 | "xunit" : False, |
| 88 | "xunit_dir" : "xunit", |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 89 | |
| 90 | # Test behavior options |
| 91 | "relax" : False, |
Dan Talayco | d7c80d1 | 2012-04-03 15:20:57 -0700 | [diff] [blame] | 92 | "test_params" : "None", |
Rich Lane | 9a84a4f | 2012-07-17 12:27:42 -0700 | [diff] [blame] | 93 | "fail_skipped" : False, |
Rich Lane | 48f6aed | 2014-03-23 15:51:02 -0700 | [diff] [blame] | 94 | "default_timeout" : 2.0, |
| 95 | "default_negative_timeout" : 0.01, |
Jeffrey Townsend | 5cb7ed3 | 2012-08-17 18:11:01 +0000 | [diff] [blame] | 96 | "minsize" : 0, |
Rich Lane | 8592bec | 2012-09-03 09:06:59 -0700 | [diff] [blame] | 97 | "random_seed" : None, |
Stephen Finucane | 92f7cf6 | 2014-03-13 15:08:11 +0000 | [diff] [blame] | 98 | "disable_ipv6" : False, |
Rich Lane | 62b4fb1 | 2015-04-02 18:04:10 -0700 | [diff] [blame] | 99 | "random_order" : False, |
macauley | c1b4bd7 | 2015-07-16 15:29:45 +0800 | [diff] [blame] | 100 | "dump_packet" : True, |
macauley | c2ad42c | 2015-07-17 15:59:15 +0800 | [diff] [blame] | 101 | "cicada_poject" : False, |
Flavio Castro | b01d0aa | 2016-07-20 16:14:48 -0700 | [diff] [blame^] | 102 | "force_ofdpa_restart": False, |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 103 | # Other configuration |
Rich Lane | 15f2632 | 2013-01-08 11:23:24 -0800 | [diff] [blame] | 104 | "port_map" : {}, |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 107 | def config_setup(): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 108 | """ |
| 109 | Set up the configuration including parsing the arguments |
| 110 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 111 | @return A pair (config, args) where config is an config |
| 112 | object and args is any additional arguments from the command line |
| 113 | """ |
| 114 | |
Rich Lane | 4113a58 | 2013-01-03 10:13:02 -0800 | [diff] [blame] | 115 | usage = "usage: %prog [options] (test|group)..." |
| 116 | |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 117 | description = """\ |
Rich Lane | 4113a58 | 2013-01-03 10:13:02 -0800 | [diff] [blame] | 118 | OFTest is a framework and set of tests for validating OpenFlow switches. |
| 119 | |
| 120 | The default configuration assumes that an OpenFlow 1.0 switch is attempting to |
Rich Lane | 4d1f3eb | 2013-10-03 13:45:57 -0700 | [diff] [blame] | 121 | connect to a controller on the machine running OFTest, port 6653. Additionally, |
Rich Lane | 4113a58 | 2013-01-03 10:13:02 -0800 | [diff] [blame] | 122 | the interfaces veth1, veth3, veth5, and veth7 should be connected to the switch's |
| 123 | dataplane. |
| 124 | |
| 125 | If no positional arguments are given then OFTest will run all tests that |
| 126 | depend only on standard OpenFlow 1.0. Otherwise each positional argument |
| 127 | is interpreted as either a test name or a test group name. The union of |
| 128 | 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] | 129 | --list option. Tests and groups can be subtracted from the result by |
| 130 | prefixing them with the '^' character. |
Rich Lane | 4113a58 | 2013-01-03 10:13:02 -0800 | [diff] [blame] | 131 | """ |
| 132 | |
Rich Lane | 2d6d482 | 2013-01-08 10:49:16 -0800 | [diff] [blame] | 133 | # Parse --interface |
| 134 | def check_interface(option, opt, value): |
| 135 | try: |
| 136 | ofport, interface = value.split('@', 1) |
| 137 | ofport = int(ofport) |
| 138 | except ValueError: |
| 139 | raise optparse.OptionValueError("incorrect interface syntax (got %s, expected 'ofport@interface')" % repr(value)) |
| 140 | return (ofport, interface) |
| 141 | |
| 142 | class Option(optparse.Option): |
| 143 | TYPES = optparse.Option.TYPES + ("interface",) |
| 144 | TYPE_CHECKER = copy.copy(optparse.Option.TYPE_CHECKER) |
| 145 | TYPE_CHECKER["interface"] = check_interface |
| 146 | |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 147 | parser = optparse.OptionParser(version="%prog 0.1", |
| 148 | usage=usage, |
| 149 | description=description, |
Rich Lane | 2d6d482 | 2013-01-08 10:49:16 -0800 | [diff] [blame] | 150 | formatter=oftest.help_formatter.HelpFormatter(), |
| 151 | option_class=Option) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 152 | |
| 153 | # Set up default values |
Stephen Finucane | 7424a74 | 2014-05-15 19:46:06 +0100 | [diff] [blame] | 154 | parser.set_defaults(**CONFIG_DEFAULT) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 155 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 156 | parser.add_option("--list", action="store_true", |
Brandon Heller | 824504e | 2010-04-01 12:21:37 -0700 | [diff] [blame] | 157 | help="List all tests and exit") |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 158 | parser.add_option("--list-test-names", action='store_true', |
| 159 | help="List test names matching the test spec and exit") |
Rich Lane | ee57ad0 | 2012-07-13 15:40:36 -0700 | [diff] [blame] | 160 | parser.add_option("--allow-user", action="store_true", |
| 161 | help="Proceed even if oftest is not run as root") |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 162 | |
| 163 | group = optparse.OptionGroup(parser, "Test selection options") |
| 164 | group.add_option("-T", "--test-spec", "--test-list", help="Tests to run, separated by commas") |
| 165 | group.add_option("-f", "--test-file", help="File of tests to run, one per line") |
| 166 | group.add_option("--test-dir", type="string", help="Directory containing tests") |
| 167 | parser.add_option_group(group) |
| 168 | |
| 169 | group = optparse.OptionGroup(parser, "Switch connection options") |
| 170 | group.add_option("-H", "--host", dest="controller_host", |
| 171 | help="IP address to listen on (default %default)") |
| 172 | group.add_option("-p", "--port", dest="controller_port", |
| 173 | type="int", help="Port number to listen on (default %default)") |
| 174 | group.add_option("-S", "--switch-ip", dest="switch_ip", |
| 175 | help="If set, actively connect to this switch by IP") |
| 176 | group.add_option("-P", "--platform", help="Platform module name (default %default)") |
| 177 | group.add_option("-a", "--platform-args", help="Custom arguments for the platform") |
| 178 | group.add_option("--platform-dir", type="string", help="Directory containing platform modules") |
Rich Lane | 2d6d482 | 2013-01-08 10:49:16 -0800 | [diff] [blame] | 179 | group.add_option("--interface", "-i", type="interface", dest="interfaces", metavar="INTERFACE", action="append", |
| 180 | help="Specify a OpenFlow port number and the dataplane interface to use. May be given multiple times. Example: 1@eth1") |
Rich Lane | 50cfa50 | 2013-04-25 13:45:08 -0700 | [diff] [blame] | 181 | group.add_option("--of-version", "-V", dest="openflow_version", choices=loxi.version_names.values(), |
Rich Lane | 9fd0568 | 2013-01-10 15:30:38 -0800 | [diff] [blame] | 182 | help="OpenFlow version to use") |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 183 | parser.add_option_group(group) |
| 184 | |
| 185 | group = optparse.OptionGroup(parser, "Logging options") |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame] | 186 | group.add_option("--log-file", help="Name of log file (default %default)") |
| 187 | group.add_option("--log-dir", help="Name of log directory") |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 188 | dbg_lvl_names = sorted(DEBUG_LEVELS.keys(), key=lambda x: DEBUG_LEVELS[x]) |
| 189 | group.add_option("--debug", choices=dbg_lvl_names, |
| 190 | help="Debug lvl: debug, info, warning, error, critical (default %default)") |
| 191 | group.add_option("-v", "--verbose", action="store_const", dest="debug", |
| 192 | const="verbose", help="Shortcut for --debug=verbose") |
| 193 | group.add_option("-q", "--quiet", action="store_const", dest="debug", |
| 194 | const="warning", help="Shortcut for --debug=warning") |
Stephen Finucane | 6219af7 | 2014-05-14 21:08:30 +0100 | [diff] [blame] | 195 | group.add_option("--profile", action="store_true", help="Enable Python profiling") |
| 196 | group.add_option("--profile-file", help="Output file for Python profiler") |
Stephen Finucane | e016cf2 | 2014-04-16 22:04:11 +0100 | [diff] [blame] | 197 | group.add_option("--xunit", action="store_true", help="Enable xUnit-formatted results") |
| 198 | group.add_option("--xunit-dir", help="Output directory for xUnit-formatted results") |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 199 | parser.add_option_group(group) |
| 200 | |
| 201 | group = optparse.OptionGroup(parser, "Test behavior options") |
| 202 | group.add_option("--relax", action="store_true", |
| 203 | help="Relax packet match checks allowing other packets") |
| 204 | test_params_help = """Set test parameters: key=val;... (see --list) |
| 205 | """ |
| 206 | group.add_option("-t", "--test-params", help=test_params_help) |
| 207 | group.add_option("--fail-skipped", action="store_true", |
Rich Lane | 9a84a4f | 2012-07-17 12:27:42 -0700 | [diff] [blame] | 208 | help="Return failure if any test was skipped") |
Rich Lane | 48f6aed | 2014-03-23 15:51:02 -0700 | [diff] [blame] | 209 | group.add_option("--default-timeout", type=float, |
Rich Lane | e55abf7 | 2012-07-26 20:11:42 -0700 | [diff] [blame] | 210 | help="Timeout in seconds for most operations") |
Rich Lane | 48f6aed | 2014-03-23 15:51:02 -0700 | [diff] [blame] | 211 | group.add_option("--default-negative-timeout", type=float, |
| 212 | help="Timeout in seconds for negative checks") |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 213 | group.add_option("--minsize", type="int", |
| 214 | help="Minimum allowable packet size on the dataplane.") |
| 215 | group.add_option("--random-seed", type="int", |
| 216 | help="Random number generator seed") |
Flavio Castro | b01d0aa | 2016-07-20 16:14:48 -0700 | [diff] [blame^] | 217 | group.add_option("--force-ofdpa-restart", |
| 218 | help="If set force ofdpa restart on user@switchIP") |
Stephen Finucane | 92f7cf6 | 2014-03-13 15:08:11 +0000 | [diff] [blame] | 219 | group.add_option("--disable-ipv6", action="store_true", |
| 220 | help="Disable IPv6 tests") |
Rich Lane | 62b4fb1 | 2015-04-02 18:04:10 -0700 | [diff] [blame] | 221 | group.add_option("--random-order", action="store_true", |
| 222 | help="Randomize order of tests") |
macauley | c1b4bd7 | 2015-07-16 15:29:45 +0800 | [diff] [blame] | 223 | group.add_option("--dump_packet", action="store_true", |
macauley | c2ad42c | 2015-07-17 15:59:15 +0800 | [diff] [blame] | 224 | help="Dump packet content on log when verify packet fail") |
| 225 | group.add_option("--cicada_poject", action="store_true", |
| 226 | help="True verify Cicada behavior, False verify AOS behaviro") |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 227 | parser.add_option_group(group) |
Jeffrey Townsend | 5cb7ed3 | 2012-08-17 18:11:01 +0000 | [diff] [blame] | 228 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 229 | # Might need this if other parsers want command line |
| 230 | # parser.allow_interspersed_args = False |
| 231 | (options, args) = parser.parse_args() |
| 232 | |
Rich Lane | 74b13d1 | 2013-05-03 17:58:50 -0700 | [diff] [blame] | 233 | # If --test-dir wasn't given, pick one based on the OpenFlow version |
| 234 | if options.test_dir == None: |
| 235 | if options.openflow_version == "1.0": |
Stephen Finucane | 7424a74 | 2014-05-15 19:46:06 +0100 | [diff] [blame] | 236 | options.test_dir = os.path.join(ROOT_DIR, "tests") |
Rich Lane | 74b13d1 | 2013-05-03 17:58:50 -0700 | [diff] [blame] | 237 | else: |
Stephen Finucane | 7424a74 | 2014-05-15 19:46:06 +0100 | [diff] [blame] | 238 | options.test_dir = os.path.join(ROOT_DIR, "tests-" + options.openflow_version) |
Rich Lane | 74b13d1 | 2013-05-03 17:58:50 -0700 | [diff] [blame] | 239 | |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 240 | # Convert options from a Namespace to a plain dictionary |
Stephen Finucane | 7424a74 | 2014-05-15 19:46:06 +0100 | [diff] [blame] | 241 | config = CONFIG_DEFAULT.copy() |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 242 | for key in config.keys(): |
| 243 | config[key] = getattr(options, key) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 244 | |
| 245 | return (config, args) |
| 246 | |
| 247 | def logging_setup(config): |
| 248 | """ |
| 249 | Set up logging based on config |
| 250 | """ |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame] | 251 | |
| 252 | logging.getLogger().setLevel(DEBUG_LEVELS[config["debug"]]) |
| 253 | |
| 254 | if config["log_dir"] != None: |
| 255 | if os.path.exists(config["log_dir"]): |
| 256 | import shutil |
| 257 | shutil.rmtree(config["log_dir"]) |
| 258 | os.makedirs(config["log_dir"]) |
| 259 | else: |
| 260 | if os.path.exists(config["log_file"]): |
| 261 | os.remove(config["log_file"]) |
| 262 | |
| 263 | oftest.open_logfile('main') |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 264 | |
Stephen Finucane | e016cf2 | 2014-04-16 22:04:11 +0100 | [diff] [blame] | 265 | def xunit_setup(config): |
| 266 | """ |
| 267 | Set up xUnit output based on config |
| 268 | """ |
| 269 | |
| 270 | if not config["xunit"]: |
| 271 | return |
| 272 | |
Stephen Finucane | 6219af7 | 2014-05-14 21:08:30 +0100 | [diff] [blame] | 273 | if os.path.exists(config["xunit_dir"]): |
| 274 | import shutil |
| 275 | shutil.rmtree(config["xunit_dir"]) |
| 276 | os.makedirs(config["xunit_dir"]) |
Stephen Finucane | e016cf2 | 2014-04-16 22:04:11 +0100 | [diff] [blame] | 277 | |
Rich Lane | 472aaea | 2013-08-27 09:27:38 -0700 | [diff] [blame] | 278 | def pcap_setup(config): |
| 279 | """ |
| 280 | Set up dataplane packet capturing based on config |
| 281 | """ |
| 282 | |
| 283 | if config["log_dir"] == None: |
| 284 | filename = os.path.splitext(config["log_file"])[0] + '.pcap' |
| 285 | oftest.dataplane_instance.start_pcap(filename) |
| 286 | else: |
| 287 | # start_pcap is called per-test in base_tests |
| 288 | pass |
| 289 | |
Stephen Finucane | 6219af7 | 2014-05-14 21:08:30 +0100 | [diff] [blame] | 290 | def profiler_setup(config): |
| 291 | """ |
| 292 | Set up profiler based on config |
| 293 | """ |
| 294 | |
| 295 | if not config["profile"]: |
| 296 | return |
| 297 | |
| 298 | import cProfile |
| 299 | profiler = cProfile.Profile() |
| 300 | profiler.enable() |
| 301 | |
| 302 | return profiler |
| 303 | |
| 304 | def profiler_teardown(profiler): |
| 305 | """ |
| 306 | Tear down profiler based on config |
| 307 | """ |
| 308 | |
| 309 | if not config["profile"]: |
| 310 | return |
| 311 | |
| 312 | profiler.disable() |
| 313 | profiler.dump_stats(config["profile_file"]) |
| 314 | |
Rich Lane | 472aaea | 2013-08-27 09:27:38 -0700 | [diff] [blame] | 315 | |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 316 | def load_test_modules(config): |
| 317 | """ |
| 318 | Load tests from the test_dir directory. |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 319 | |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 320 | Test cases are subclasses of unittest.TestCase |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 321 | |
Rich Lane | cc45b8e | 2013-01-02 15:55:02 -0800 | [diff] [blame] | 322 | Also updates the _groups member to include "standard" and |
| 323 | module test groups if appropriate. |
| 324 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 325 | @param config The oft configuration dictionary |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 326 | @returns A dictionary from test module names to tuples of |
| 327 | (module, dictionary from test names to test classes). |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 328 | """ |
| 329 | |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 330 | result = {} |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 331 | |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 332 | for root, dirs, filenames in os.walk(config["test_dir"]): |
| 333 | # Iterate over each python file |
| 334 | for filename in fnmatch.filter(filenames, '[!.]*.py'): |
| 335 | modname = os.path.splitext(os.path.basename(filename))[0] |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 336 | |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 337 | try: |
| 338 | if sys.modules.has_key(modname): |
| 339 | mod = sys.modules[modname] |
| 340 | else: |
| 341 | mod = imp.load_module(modname, *imp.find_module(modname, [root])) |
| 342 | except: |
| 343 | logging.warning("Could not import file " + filename) |
| 344 | raise |
Rich Lane | 520e415 | 2012-07-09 16:18:16 -0700 | [diff] [blame] | 345 | |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 346 | # Find all testcases defined in the module |
| 347 | tests = dict((k, v) for (k, v) in mod.__dict__.items() if type(v) == type and |
Rich Lane | 9cef274 | 2013-07-16 13:27:00 -0700 | [diff] [blame] | 348 | issubclass(v, unittest.TestCase) and |
| 349 | hasattr(v, "runTest")) |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 350 | if tests: |
Rich Lane | cc45b8e | 2013-01-02 15:55:02 -0800 | [diff] [blame] | 351 | for (testname, test) in tests.items(): |
| 352 | # Set default annotation values |
| 353 | if not hasattr(test, "_groups"): |
| 354 | test._groups = [] |
| 355 | if not hasattr(test, "_nonstandard"): |
| 356 | test._nonstandard = False |
| 357 | if not hasattr(test, "_disabled"): |
| 358 | test._disabled = False |
| 359 | |
| 360 | # Put test in its module's test group |
| 361 | if not test._disabled: |
| 362 | test._groups.append(modname) |
| 363 | |
| 364 | # Put test in the standard test group |
| 365 | if not test._disabled and not test._nonstandard: |
| 366 | test._groups.append("standard") |
| 367 | test._groups.append("all") # backwards compatibility |
| 368 | |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 369 | result[modname] = (mod, tests) |
Rich Lane | 520e415 | 2012-07-09 16:18:16 -0700 | [diff] [blame] | 370 | |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 371 | return result |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 372 | |
Rich Lane | 5a9a192 | 2013-01-11 14:29:30 -0800 | [diff] [blame] | 373 | def prune_tests(test_specs, test_modules, version): |
Rich Lane | 15f64de | 2012-10-04 21:25:57 -0700 | [diff] [blame] | 374 | """ |
Rich Lane | 5a9a192 | 2013-01-11 14:29:30 -0800 | [diff] [blame] | 375 | Return tests matching the given test-specs and OpenFlow version |
Rich Lane | c76b09a | 2013-01-02 16:53:22 -0800 | [diff] [blame] | 376 | @param test_specs A list of group names or test names. |
Rich Lane | 5a9a192 | 2013-01-11 14:29:30 -0800 | [diff] [blame] | 377 | @param version An OpenFlow version (e.g. "1.0") |
Rich Lane | 15f64de | 2012-10-04 21:25:57 -0700 | [diff] [blame] | 378 | @param test_modules Same format as the output of load_test_modules. |
| 379 | @returns Same format as the output of load_test_modules. |
| 380 | """ |
| 381 | result = {} |
Rich Lane | c76b09a | 2013-01-02 16:53:22 -0800 | [diff] [blame] | 382 | for e in test_specs: |
Rich Lane | 15f64de | 2012-10-04 21:25:57 -0700 | [diff] [blame] | 383 | matched = False |
Rich Lane | 948f030 | 2013-01-07 10:59:08 -0800 | [diff] [blame] | 384 | |
| 385 | if e.startswith('^'): |
| 386 | negated = True |
| 387 | e = e[1:] |
| 388 | else: |
| 389 | negated = False |
| 390 | |
Rich Lane | 15f64de | 2012-10-04 21:25:57 -0700 | [diff] [blame] | 391 | for (modname, (mod, tests)) in test_modules.items(): |
Rich Lane | cc45b8e | 2013-01-02 15:55:02 -0800 | [diff] [blame] | 392 | for (testname, test) in tests.items(): |
| 393 | if e in test._groups or e == "%s.%s" % (modname, testname): |
| 394 | result.setdefault(modname, (mod, {})) |
Rich Lane | 948f030 | 2013-01-07 10:59:08 -0800 | [diff] [blame] | 395 | if not negated: |
Rich Lane | 5a9a192 | 2013-01-11 14:29:30 -0800 | [diff] [blame] | 396 | if not hasattr(test, "_versions") or version in test._versions: |
| 397 | result[modname][1][testname] = test |
Rich Lane | 948f030 | 2013-01-07 10:59:08 -0800 | [diff] [blame] | 398 | else: |
| 399 | if modname in result and testname in result[modname][1]: |
| 400 | del result[modname][1][testname] |
| 401 | if not result[modname][1]: |
| 402 | del result[modname] |
Rich Lane | cc45b8e | 2013-01-02 15:55:02 -0800 | [diff] [blame] | 403 | matched = True |
Rich Lane | 948f030 | 2013-01-07 10:59:08 -0800 | [diff] [blame] | 404 | |
Rich Lane | 15f64de | 2012-10-04 21:25:57 -0700 | [diff] [blame] | 405 | if not matched: |
Rich Lane | cc45b8e | 2013-01-02 15:55:02 -0800 | [diff] [blame] | 406 | die("test-spec element %s did not match any tests" % e) |
Rich Lane | 948f030 | 2013-01-07 10:59:08 -0800 | [diff] [blame] | 407 | |
Rich Lane | 15f64de | 2012-10-04 21:25:57 -0700 | [diff] [blame] | 408 | return result |
| 409 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 410 | def die(msg, exit_val=1): |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 411 | logging.critical(msg) |
| 412 | sys.exit(exit_val) |
| 413 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 414 | # |
| 415 | # Main script |
| 416 | # |
| 417 | |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 418 | # Setup global configuration |
Rich Lane | 95f078b | 2013-01-06 13:24:58 -0800 | [diff] [blame] | 419 | (new_config, args) = config_setup() |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 420 | oftest.config.update(new_config) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 421 | |
Rich Lane | d7a85c4 | 2012-09-28 15:38:45 -0700 | [diff] [blame] | 422 | logging_setup(config) |
Stephen Finucane | e016cf2 | 2014-04-16 22:04:11 +0100 | [diff] [blame] | 423 | xunit_setup(config) |
Rich Lane | d7a85c4 | 2012-09-28 15:38:45 -0700 | [diff] [blame] | 424 | logging.info("++++++++ " + time.asctime() + " ++++++++") |
| 425 | |
Rich Lane | 9fd0568 | 2013-01-10 15:30:38 -0800 | [diff] [blame] | 426 | # Pick an OpenFlow protocol module based on the configured version |
Rich Lane | 50cfa50 | 2013-04-25 13:45:08 -0700 | [diff] [blame] | 427 | name_to_version = dict((v,k) for k, v in loxi.version_names.iteritems()) |
| 428 | sys.modules["ofp"] = loxi.protocol(name_to_version[config["openflow_version"]]) |
Rich Lane | 9fd0568 | 2013-01-10 15:30:38 -0800 | [diff] [blame] | 429 | |
| 430 | # HACK: testutils.py imports controller.py, which needs the ofp module |
| 431 | import oftest.testutils |
| 432 | |
Rich Lane | e284b6b | 2012-10-03 09:19:58 -0700 | [diff] [blame] | 433 | # Allow tests to import each other |
| 434 | sys.path.append(config["test_dir"]) |
| 435 | |
Rich Lane | c76b09a | 2013-01-02 16:53:22 -0800 | [diff] [blame] | 436 | test_specs = args |
| 437 | if config["test_spec"] != "": |
Stephen Finucane | 0158865 | 2014-05-15 20:03:12 +0100 | [diff] [blame] | 438 | logging.warning("The '--test-spec' option is deprecated.") |
Rich Lane | c76b09a | 2013-01-02 16:53:22 -0800 | [diff] [blame] | 439 | test_specs += config["test_spec"].split(',') |
Rich Lane | 1a08d23 | 2013-01-04 16:03:50 -0800 | [diff] [blame] | 440 | if config["test_file"] != None: |
| 441 | with open(config["test_file"], 'r') as f: |
| 442 | for line in f: |
| 443 | line, _, _ = line.partition('#') # remove comments |
| 444 | line = line.strip() |
| 445 | if line: |
| 446 | test_specs.append(line) |
Rich Lane | c76b09a | 2013-01-02 16:53:22 -0800 | [diff] [blame] | 447 | if test_specs == []: |
| 448 | test_specs = ["standard"] |
| 449 | |
Rich Lane | d8e4548 | 2013-01-02 17:36:21 -0800 | [diff] [blame] | 450 | test_modules = load_test_modules(config) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 451 | |
| 452 | # Check if test list is requested; display and exit if so |
| 453 | if config["list"]: |
Dan Talayco | 7f8dba8 | 2012-04-12 12:58:52 -0700 | [diff] [blame] | 454 | mod_count = 0 |
| 455 | test_count = 0 |
Rich Lane | 37f4211 | 2013-01-03 13:41:49 -0800 | [diff] [blame] | 456 | all_groups = set() |
Stephen Finucane | 0158865 | 2014-05-15 20:03:12 +0100 | [diff] [blame] | 457 | |
| 458 | print(""" |
| 459 | Tests are shown grouped by module. If a test is in any groups beyond |
| 460 | "standard" and its module's group then they are shown in parentheses. |
| 461 | |
Rich Lane | e811e7b | 2013-01-03 13:36:54 -0800 | [diff] [blame] | 462 | Tests marked with '*' are non-standard and may require vendor extensions or |
Stephen Finucane | 0158865 | 2014-05-15 20:03:12 +0100 | [diff] [blame] | 463 | special switch configuration. These are not part of the "standard" test group. |
| 464 | |
| 465 | Tests marked with '!' are disabled because they are experimental, |
| 466 | special-purpose, or are too long to be run normally. These are not part of |
| 467 | the "standard" test group or their module's test group. |
| 468 | |
| 469 | Tests marked (TP1) after name take --test-params including: |
| 470 | |
| 471 | 'vid=N;strip_vlan=bool;add_vlan=bool' |
| 472 | |
| 473 | Test List: |
| 474 | """) |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 475 | for (modname, (mod, tests)) in test_modules.items(): |
Dan Talayco | 7f8dba8 | 2012-04-12 12:58:52 -0700 | [diff] [blame] | 476 | mod_count += 1 |
Rich Lane | 80efd65 | 2013-01-02 14:05:20 -0800 | [diff] [blame] | 477 | desc = (mod.__doc__ or "No description").strip().split('\n')[0] |
Stephen Finucane | 0158865 | 2014-05-15 20:03:12 +0100 | [diff] [blame] | 478 | print(" Module %13s: %s" % (mod.__name__, desc)) |
| 479 | |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 480 | for (testname, test) in tests.items(): |
Stephen Finucane | 0158865 | 2014-05-15 20:03:12 +0100 | [diff] [blame] | 481 | desc = (test.__doc__ or "No description").strip().split('\n')[0] |
| 482 | |
Rich Lane | e811e7b | 2013-01-03 13:36:54 -0800 | [diff] [blame] | 483 | groups = set(test._groups) - set(["all", "standard", modname]) |
Rich Lane | 37f4211 | 2013-01-03 13:41:49 -0800 | [diff] [blame] | 484 | all_groups.update(test._groups) |
Rich Lane | e811e7b | 2013-01-03 13:36:54 -0800 | [diff] [blame] | 485 | if groups: |
| 486 | desc = "(%s) %s" % (",".join(groups), desc) |
Rich Lane | 5a9a192 | 2013-01-11 14:29:30 -0800 | [diff] [blame] | 487 | if hasattr(test, "_versions"): |
| 488 | desc = "(%s) %s" % (",".join(sorted(test._versions)), desc) |
Stephen Finucane | 0158865 | 2014-05-15 20:03:12 +0100 | [diff] [blame] | 489 | |
Rich Lane | e811e7b | 2013-01-03 13:36:54 -0800 | [diff] [blame] | 490 | start_str = " %s%s %s:" % (test._nonstandard and "*" or " ", |
| 491 | test._disabled and "!" or " ", |
| 492 | testname) |
Stephen Finucane | 0158865 | 2014-05-15 20:03:12 +0100 | [diff] [blame] | 493 | print(" %22s : %s" % (start_str, desc)) |
Dan Talayco | 7f8dba8 | 2012-04-12 12:58:52 -0700 | [diff] [blame] | 494 | test_count += 1 |
Dan Talayco | 79f3608 | 2010-03-11 16:53:53 -0800 | [diff] [blame] | 495 | print |
Stephen Finucane | 0158865 | 2014-05-15 20:03:12 +0100 | [diff] [blame] | 496 | print("'%d' modules shown with a total of '%d' tests\n" % |
| 497 | (mod_count, test_count)) |
| 498 | print("Test groups: %s" % (', '.join(sorted(all_groups)))) |
Rich Lane | 37f4211 | 2013-01-03 13:41:49 -0800 | [diff] [blame] | 499 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 500 | sys.exit(0) |
| 501 | |
Rich Lane | 5a9a192 | 2013-01-11 14:29:30 -0800 | [diff] [blame] | 502 | test_modules = prune_tests(test_specs, test_modules, config["openflow_version"]) |
Rich Lane | d8e4548 | 2013-01-02 17:36:21 -0800 | [diff] [blame] | 503 | |
Jeffrey Townsend | 6614f97 | 2012-08-16 13:27:18 -0700 | [diff] [blame] | 504 | # Check if test list is requested; display and exit if so |
| 505 | if config["list_test_names"]: |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 506 | for (modname, (mod, tests)) in test_modules.items(): |
Rich Lane | 943be67 | 2012-10-04 19:20:16 -0700 | [diff] [blame] | 507 | for (testname, test) in tests.items(): |
Stephen Finucane | 0158865 | 2014-05-15 20:03:12 +0100 | [diff] [blame] | 508 | print("%s.%s" % (modname, testname)) |
| 509 | |
Jeffrey Townsend | 6614f97 | 2012-08-16 13:27:18 -0700 | [diff] [blame] | 510 | sys.exit(0) |
| 511 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 512 | # Generate the test suite |
| 513 | #@todo Decide if multiple suites are ever needed |
| 514 | suite = unittest.TestSuite() |
| 515 | |
Rich Lane | 62b4fb1 | 2015-04-02 18:04:10 -0700 | [diff] [blame] | 516 | sorted_tests = [] |
Rich Lane | c72ef46 | 2015-04-02 17:52:44 -0700 | [diff] [blame] | 517 | for (modname, (mod, tests)) in sorted(test_modules.items()): |
| 518 | for (testname, test) in sorted(tests.items()): |
Rich Lane | 62b4fb1 | 2015-04-02 18:04:10 -0700 | [diff] [blame] | 519 | sorted_tests.append(test) |
| 520 | |
| 521 | if config["random_order"]: |
| 522 | random.shuffle(sorted_tests) |
| 523 | |
| 524 | for test in sorted_tests: |
| 525 | suite.addTest(test()) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 526 | |
Rich Lane | 51590f6 | 2012-10-09 15:06:29 -0700 | [diff] [blame] | 527 | # Allow platforms to import each other |
| 528 | sys.path.append(config["platform_dir"]) |
| 529 | |
Rich Lane | 8aebc5e | 2012-09-25 17:57:53 -0700 | [diff] [blame] | 530 | # Load the platform module |
| 531 | platform_name = config["platform"] |
| 532 | logging.info("Importing platform: " + platform_name) |
| 533 | platform_mod = None |
| 534 | try: |
Rich Lane | 483e154 | 2012-10-05 09:29:39 -0700 | [diff] [blame] | 535 | 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] | 536 | except: |
| 537 | logging.warn("Failed to import " + platform_name + " platform module") |
| 538 | raise |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 539 | |
| 540 | try: |
Rich Lane | 8aebc5e | 2012-09-25 17:57:53 -0700 | [diff] [blame] | 541 | platform_mod.platform_config_update(config) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 542 | except: |
| 543 | logging.warn("Could not run platform host configuration") |
Rich Lane | ef403e5 | 2012-07-09 14:59:43 -0700 | [diff] [blame] | 544 | raise |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 545 | |
| 546 | if not config["port_map"]: |
Rich Lane | 8aebc5e | 2012-09-25 17:57:53 -0700 | [diff] [blame] | 547 | die("Interface port map was not defined by the platform. Exiting.") |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 548 | |
| 549 | logging.debug("Configuration: " + str(config)) |
| 550 | logging.info("OF port map: " + str(config["port_map"])) |
| 551 | |
Rich Lane | e55abf7 | 2012-07-26 20:11:42 -0700 | [diff] [blame] | 552 | oftest.ofutils.default_timeout = config["default_timeout"] |
Rich Lane | 48f6aed | 2014-03-23 15:51:02 -0700 | [diff] [blame] | 553 | oftest.ofutils.default_negative_timeout = config["default_negative_timeout"] |
Rich Lane | da3b5ad | 2012-10-03 09:05:32 -0700 | [diff] [blame] | 554 | oftest.testutils.MINSIZE = config['minsize'] |
Rich Lane | e55abf7 | 2012-07-26 20:11:42 -0700 | [diff] [blame] | 555 | |
Rich Lane | ee57ad0 | 2012-07-13 15:40:36 -0700 | [diff] [blame] | 556 | if os.getuid() != 0 and not config["allow_user"]: |
Stephen Finucane | 0158865 | 2014-05-15 20:03:12 +0100 | [diff] [blame] | 557 | die("Super-user privileges required. Please re-run with sudo or as root.") |
Rich Lane | d1d9c28 | 2012-10-04 22:07:10 -0700 | [diff] [blame] | 558 | sys.exit(1) |
Brandon Heller | 446c143 | 2010-04-01 12:43:27 -0700 | [diff] [blame] | 559 | |
Rich Lane | 8592bec | 2012-09-03 09:06:59 -0700 | [diff] [blame] | 560 | if config["random_seed"] is not None: |
| 561 | logging.info("Random seed: %d" % config["random_seed"]) |
| 562 | random.seed(config["random_seed"]) |
Dan Talayco | ec6084d | 2013-03-14 10:38:22 -0700 | [diff] [blame] | 563 | else: |
| 564 | # Generate random seed and report to log file |
| 565 | seed = random.randrange(100000000) |
| 566 | logging.info("Autogen random seed: %d" % seed) |
| 567 | random.seed(seed) |
Rich Lane | 8592bec | 2012-09-03 09:06:59 -0700 | [diff] [blame] | 568 | |
Rich Lane | 5bd6cf9 | 2012-10-04 17:57:24 -0700 | [diff] [blame] | 569 | # Remove python's signal handler which raises KeyboardError. Exiting from an |
| 570 | # exception waits for all threads to terminate which might not happen. |
| 571 | signal.signal(signal.SIGINT, signal.SIG_DFL) |
Dan Talayco | ac25cf3 | 2010-07-20 14:08:28 -0700 | [diff] [blame] | 572 | |
| 573 | if __name__ == "__main__": |
Stephen Finucane | 6219af7 | 2014-05-14 21:08:30 +0100 | [diff] [blame] | 574 | profiler = profiler_setup(config) |
Rich Lane | 9631f00 | 2014-03-21 18:05:16 -0700 | [diff] [blame] | 575 | |
Rich Lane | 2c7812c | 2012-12-27 17:52:23 -0800 | [diff] [blame] | 576 | # Set up the dataplane |
| 577 | oftest.dataplane_instance = oftest.dataplane.DataPlane(config) |
Rich Lane | 472aaea | 2013-08-27 09:27:38 -0700 | [diff] [blame] | 578 | pcap_setup(config) |
Rich Lane | 2c7812c | 2012-12-27 17:52:23 -0800 | [diff] [blame] | 579 | for of_port, ifname in config["port_map"].items(): |
| 580 | oftest.dataplane_instance.port_add(ifname, of_port) |
| 581 | |
Dan Talayco | ac25cf3 | 2010-07-20 14:08:28 -0700 | [diff] [blame] | 582 | logging.info("*** TEST RUN START: " + time.asctime()) |
Stephen Finucane | e016cf2 | 2014-04-16 22:04:11 +0100 | [diff] [blame] | 583 | if config["xunit"]: |
Stephen Finucane | 1278255 | 2014-05-20 21:55:04 +0100 | [diff] [blame] | 584 | try: |
| 585 | import xmlrunner # fail-fast if module missing |
| 586 | except ImportError as ex: |
| 587 | oftest.dataplane_instance.kill() |
| 588 | profiler_teardown(profiler) |
| 589 | raise ex |
Stephen Finucane | e016cf2 | 2014-04-16 22:04:11 +0100 | [diff] [blame] | 590 | runner = xmlrunner.XMLTestRunner(output=config["xunit_dir"], |
| 591 | outsuffix="", |
| 592 | verbosity=2) |
| 593 | result = runner.run(suite) |
| 594 | else: |
| 595 | result = unittest.TextTestRunner(verbosity=2).run(suite) |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame] | 596 | oftest.open_logfile('main') |
Rich Lane | da3b5ad | 2012-10-03 09:05:32 -0700 | [diff] [blame] | 597 | if oftest.testutils.skipped_test_count > 0: |
Stephen Finucane | 0158865 | 2014-05-15 20:03:12 +0100 | [diff] [blame] | 598 | message = "Skipped %d test(s)" % oftest.testutils.skipped_test_count |
| 599 | logging.info(message) |
| 600 | logging.info("*** TEST RUN END : %s", time.asctime()) |
Rich Lane | 2c7812c | 2012-12-27 17:52:23 -0800 | [diff] [blame] | 601 | |
| 602 | # Shutdown the dataplane |
| 603 | oftest.dataplane_instance.kill() |
| 604 | oftest.dataplane_instance = None |
| 605 | |
Stephen Finucane | 6219af7 | 2014-05-14 21:08:30 +0100 | [diff] [blame] | 606 | profiler_teardown(profiler) |
Rich Lane | 9631f00 | 2014-03-21 18:05:16 -0700 | [diff] [blame] | 607 | |
Rich Lane | 50d42eb | 2012-07-16 11:57:03 -0700 | [diff] [blame] | 608 | if result.failures or result.errors: |
Shudong Zhou | d895fcb | 2012-08-01 19:09:55 -0700 | [diff] [blame] | 609 | # exit(1) hangs sometimes |
| 610 | os._exit(1) |
Rich Lane | da3b5ad | 2012-10-03 09:05:32 -0700 | [diff] [blame] | 611 | if oftest.testutils.skipped_test_count > 0 and config["fail_skipped"]: |
Shudong Zhou | d895fcb | 2012-08-01 19:09:55 -0700 | [diff] [blame] | 612 | os._exit(1) |