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