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 |
| 18 | port_count : (Optional) Number of ports in dataplane |
| 19 | base_of_port : (Optional) Base OpenFlow port number in dataplane |
| 20 | base_if_index : (Optional) Base OS network interface for dataplane |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 21 | test_dir : (TBD) Directory to search for test files (default .) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 22 | test_spec : (TBD) Specification of test(s) to run |
| 23 | log_file : Filename for test logging |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 24 | list : Boolean: List all tests and exit |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 25 | debug : String giving debug level (info, warning, error...) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 26 | </pre> |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 27 | |
| 28 | See config_defaults below for the default values. |
| 29 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 30 | The following are stored in the config dictionary, but are not currently |
| 31 | configurable through the command line. |
| 32 | |
| 33 | <pre> |
| 34 | dbg_level : logging module value of debug level |
| 35 | port_map : Map of dataplane OpenFlow port to OS interface names |
| 36 | test_mod_map : Dictionary indexed by module names and whose value |
| 37 | is the module reference |
| 38 | all_tests : Dictionary indexed by module reference and whose |
| 39 | value is a list of functions in that module |
| 40 | </pre> |
| 41 | |
Dan Talayco | c24aaae | 2010-07-08 14:05:24 -0700 | [diff] [blame] | 42 | Each test may be assigned a priority by setting test_prio["TestName"] in |
| 43 | the respective module. For now, the only use of this is to avoid |
| 44 | automatic inclusion of tests into the default list. This is done by |
| 45 | setting the test_prio value less than 0. Eventually we may add ordering |
| 46 | of test execution by test priority. |
| 47 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 48 | To add a test to the system, either: edit an existing test case file (like |
| 49 | basic.py) to add a test class which inherits from unittest.TestCase (directly |
| 50 | or indirectly); or add a new file which includes a function definition |
| 51 | test_set_init(config). Preferably the file is in the same directory as existing |
| 52 | tests, though you can specify the directory on the command line. The file |
| 53 | should not be called "all" as that's reserved for the test-spec. |
| 54 | |
| 55 | If you add a new file, the test_set_init function should record the port |
| 56 | map object from the configuration along with whatever other configuration |
| 57 | information it may need. |
| 58 | |
| 59 | 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] | 60 | below. If you want this to be a command line parameter, edit config_setup |
| 61 | to add the option and default value to the parser. Then edit config_get |
| 62 | to make sure the option value gets copied into the configuration |
| 63 | structure (which then gets passed to everyone else). |
| 64 | |
| 65 | By convention, oft attempts to import the contents of a file by the |
| 66 | name of $platform.py into the local namespace. |
| 67 | |
| 68 | IMPORTANT: That file should define a function platform_config_update which |
| 69 | takes a configuration dictionary as an argument and updates it for the |
| 70 | current run. In particular, it should set up config["port_map"] with |
| 71 | the proper map from OF port numbers to OF interface names. |
| 72 | |
| 73 | You can add your own platform, say gp104, by adding a file gp104.py |
| 74 | that defines the function platform_config_update and then use the |
| 75 | parameter --platform=gp104 on the command line. |
| 76 | |
| 77 | If platform_config_update does not set config["port_map"], an attempt |
| 78 | is made to generate a default map via the function default_port_map_setup. |
| 79 | This will use "local" and "remote" for platform names if available |
| 80 | and generate a sequential map based on the values of base_of_port and |
| 81 | base_if_index in the configuration structure. |
| 82 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 83 | The current model for test sets is basic.py. The current convention is |
| 84 | that the test set should implement a function test_set_init which takes |
| 85 | an oft configuration dictionary and returns a unittest.TestSuite object. |
| 86 | Future test sets should do the same thing. |
| 87 | |
Dan Talayco | 52f6444 | 2010-03-03 15:32:41 -0800 | [diff] [blame] | 88 | Default setup: |
| 89 | |
| 90 | The default setup runs locally using veth pairs. To exercise this, |
| 91 | checkout and build an openflow userspace datapath. Then start it on |
| 92 | the local host: |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 93 | <pre> |
Dan Talayco | 52f6444 | 2010-03-03 15:32:41 -0800 | [diff] [blame] | 94 | sudo ~/openflow/regress/bin/veth_setup.pl |
| 95 | sudo ofdatapath -i veth0,veth2,veth4,veth6 punix:/tmp/ofd & |
| 96 | sudo ofprotocol unix:/tmp/ofd tcp:127.0.0.1 --fail=closed --max-backoff=1 & |
| 97 | |
| 98 | Next, run oft: |
| 99 | sudo ./oft --debug=info |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 100 | </pre> |
Dan Talayco | 52f6444 | 2010-03-03 15:32:41 -0800 | [diff] [blame] | 101 | |
| 102 | Examine oft.log if things don't work. |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 103 | |
Dan Talayco | 1a88c12 | 2010-03-07 22:00:20 -0800 | [diff] [blame] | 104 | @todo Support per-component debug levels (esp controller vs dataplane) |
| 105 | @todo Consider moving oft up a level |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 106 | |
Dan Talayco | 1a88c12 | 2010-03-07 22:00:20 -0800 | [diff] [blame] | 107 | Current test case setup: |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 108 | Files in this or sub directories (or later, directory specified on |
| 109 | command line) that contain a function test_set_init are considered test |
| 110 | files. |
| 111 | The function test_set_init examines the test_spec config variable |
| 112 | and generates a suite of tests. |
| 113 | Support a command line option --test_mod so that all tests in that |
| 114 | module will be run. |
| 115 | Support all to specify all tests from the module. |
| 116 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 117 | """ |
| 118 | |
| 119 | import sys |
| 120 | from optparse import OptionParser |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 121 | from subprocess import Popen,PIPE |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 122 | import logging |
| 123 | import unittest |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 124 | import time |
Brandon Heller | 446c143 | 2010-04-01 12:43:27 -0700 | [diff] [blame] | 125 | import os |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 126 | |
Dan Talayco | 02eca0b | 2010-04-15 16:09:43 -0700 | [diff] [blame] | 127 | try: |
| 128 | import scapy.all as scapy |
| 129 | except: |
| 130 | try: |
| 131 | import scapy as scapy |
| 132 | except: |
| 133 | sys.exit("Need to install scapy for packet parsing") |
| 134 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 135 | ##@var DEBUG_LEVELS |
| 136 | # Map from strings to debugging levels |
| 137 | DEBUG_LEVELS = { |
| 138 | 'debug' : logging.DEBUG, |
| 139 | 'verbose' : logging.DEBUG, |
| 140 | 'info' : logging.INFO, |
| 141 | 'warning' : logging.WARNING, |
| 142 | 'warn' : logging.WARNING, |
| 143 | 'error' : logging.ERROR, |
| 144 | 'critical' : logging.CRITICAL |
| 145 | } |
| 146 | |
| 147 | _debug_default = "warning" |
| 148 | _debug_level_default = DEBUG_LEVELS[_debug_default] |
| 149 | |
| 150 | ##@var config_default |
| 151 | # The default configuration dictionary for OFT |
| 152 | config_default = { |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 153 | "param" : None, |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 154 | "platform" : "local", |
| 155 | "controller_host" : "127.0.0.1", |
| 156 | "controller_port" : 6633, |
| 157 | "port_count" : 4, |
| 158 | "base_of_port" : 1, |
| 159 | "base_if_index" : 1, |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 160 | "test_spec" : "all", |
| 161 | "test_dir" : ".", |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 162 | "log_file" : "oft.log", |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 163 | "list" : False, |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 164 | "debug" : _debug_default, |
| 165 | "dbg_level" : _debug_level_default, |
Dan Talayco | ac25cf3 | 2010-07-20 14:08:28 -0700 | [diff] [blame] | 166 | "port_map" : {}, |
| 167 | "test_params" : "None" |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Dan Talayco | c24aaae | 2010-07-08 14:05:24 -0700 | [diff] [blame] | 170 | # Default test priority |
| 171 | TEST_PRIO_DEFAULT=100 |
| 172 | |
Dan Talayco | 1a88c12 | 2010-03-07 22:00:20 -0800 | [diff] [blame] | 173 | #@todo Set up a dict of config params so easier to manage: |
| 174 | # <param> <cmdline flags> <default value> <help> <optional parser> |
| 175 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 176 | # Map options to config structure |
| 177 | def config_get(opts): |
| 178 | "Convert options class to OFT configuration dictionary" |
| 179 | cfg = config_default.copy() |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 180 | for key in cfg.keys(): |
| 181 | cfg[key] = eval("opts." + key) |
| 182 | |
| 183 | # Special case checks |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 184 | if opts.debug not in DEBUG_LEVELS.keys(): |
| 185 | print "Warning: Bad value specified for debug level; using default" |
| 186 | opts.debug = _debug_default |
Dan Talayco | 02eca0b | 2010-04-15 16:09:43 -0700 | [diff] [blame] | 187 | if opts.verbose: |
| 188 | cfg["debug"] = "verbose" |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 189 | cfg["dbg_level"] = DEBUG_LEVELS[cfg["debug"]] |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 190 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 191 | return cfg |
| 192 | |
| 193 | def config_setup(cfg_dflt): |
| 194 | """ |
| 195 | Set up the configuration including parsing the arguments |
| 196 | |
| 197 | @param cfg_dflt The default configuration dictionary |
| 198 | @return A pair (config, args) where config is an config |
| 199 | object and args is any additional arguments from the command line |
| 200 | """ |
| 201 | |
| 202 | parser = OptionParser(version="%prog 0.1") |
| 203 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 204 | #@todo parse port map as option? |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 205 | # Set up default values |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 206 | for key in cfg_dflt.keys(): |
| 207 | eval("parser.set_defaults("+key+"=cfg_dflt['"+key+"'])") |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 208 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 209 | #@todo Add options via dictionary |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 210 | plat_help = """Set the platform type. Valid values include: |
| 211 | local: User space virtual ethernet pair setup |
| 212 | remote: Remote embedded Broadcom based switch |
Dan Talayco | 673e085 | 2010-03-06 23:09:23 -0800 | [diff] [blame] | 213 | Create a new_plat.py file and use --platform=new_plat on the command line |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 214 | """ |
| 215 | parser.add_option("-P", "--platform", help=plat_help) |
| 216 | parser.add_option("-H", "--host", dest="controller_host", |
| 217 | help="The IP/name of the test controller host") |
| 218 | parser.add_option("-p", "--port", dest="controller_port", |
| 219 | type="int", help="Port number of the test controller") |
Dan Talayco | 673e085 | 2010-03-06 23:09:23 -0800 | [diff] [blame] | 220 | test_list_help = """Indicate tests to run. Valid entries are "all" (the |
| 221 | default) or a comma separated list of: |
| 222 | module Run all tests in the named module |
| 223 | testcase Run tests in all modules with the name testcase |
| 224 | module.testcase Run the specific test case |
| 225 | """ |
| 226 | parser.add_option("--test-spec", "--test-list", help=test_list_help) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 227 | parser.add_option("--log-file", |
| 228 | help="Name of log file, empty string to log to console") |
| 229 | parser.add_option("--debug", |
| 230 | help="Debug lvl: debug, info, warning, error, critical") |
Dan Talayco | 02eca0b | 2010-04-15 16:09:43 -0700 | [diff] [blame] | 231 | parser.add_option("--port-count", type="int", |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 232 | help="Number of ports to use (optional)") |
Dan Talayco | 02eca0b | 2010-04-15 16:09:43 -0700 | [diff] [blame] | 233 | parser.add_option("--base-of-port", type="int", |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 234 | help="Base OpenFlow port number (optional)") |
Dan Talayco | 02eca0b | 2010-04-15 16:09:43 -0700 | [diff] [blame] | 235 | parser.add_option("--base-if-index", type="int", |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 236 | help="Base interface index number (optional)") |
| 237 | parser.add_option("--list", action="store_true", |
Brandon Heller | 824504e | 2010-04-01 12:21:37 -0700 | [diff] [blame] | 238 | help="List all tests and exit") |
Dan Talayco | 02eca0b | 2010-04-15 16:09:43 -0700 | [diff] [blame] | 239 | parser.add_option("--verbose", action="store_true", |
| 240 | help="Short cut for --debug=verbose") |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 241 | parser.add_option("--param", type="int", |
| 242 | help="Parameter sent to test (for debugging)") |
Dan Talayco | ac25cf3 | 2010-07-20 14:08:28 -0700 | [diff] [blame] | 243 | parser.add_option("-t", "--test-params", |
| 244 | help="Set test parameters: key=val;... See --list") |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 245 | # Might need this if other parsers want command line |
| 246 | # parser.allow_interspersed_args = False |
| 247 | (options, args) = parser.parse_args() |
| 248 | |
| 249 | config = config_get(options) |
| 250 | |
| 251 | return (config, args) |
| 252 | |
| 253 | def logging_setup(config): |
| 254 | """ |
| 255 | Set up logging based on config |
| 256 | """ |
| 257 | _format = "%(asctime)s %(name)-10s: %(levelname)-8s: %(message)s" |
| 258 | _datefmt = "%H:%M:%S" |
Dan Talayco | 88fc880 | 2010-03-07 11:37:52 -0800 | [diff] [blame] | 259 | logging.basicConfig(filename=config["log_file"], |
| 260 | level=config["dbg_level"], |
| 261 | format=_format, datefmt=_datefmt) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 262 | |
| 263 | def default_port_map_setup(config): |
| 264 | """ |
| 265 | Setup the OF port mapping based on config |
| 266 | @param config The OFT configuration structure |
| 267 | @return Port map dictionary |
| 268 | """ |
| 269 | if (config["base_of_port"] is None) or not config["port_count"]: |
| 270 | return None |
| 271 | port_map = {} |
| 272 | if config["platform"] == "local": |
| 273 | # For local, use every other veth port |
| 274 | for idx in range(config["port_count"]): |
| 275 | port_map[config["base_of_port"] + idx] = "veth" + \ |
| 276 | str(config["base_if_index"] + (2 * idx)) |
| 277 | elif config["platform"] == "remote": |
| 278 | # For remote, use eth ports |
| 279 | for idx in range(config["port_count"]): |
| 280 | port_map[config["base_of_port"] + idx] = "eth" + \ |
| 281 | str(config["base_if_index"] + idx) |
| 282 | else: |
| 283 | return None |
| 284 | |
| 285 | logging.info("Built default port map") |
| 286 | return port_map |
| 287 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 288 | def test_list_generate(config): |
| 289 | """Generate the list of all known tests indexed by module name |
| 290 | |
| 291 | Conventions: Test files must implement the function test_set_init |
| 292 | |
Dan Talayco | 1a88c12 | 2010-03-07 22:00:20 -0800 | [diff] [blame] | 293 | Test cases are classes that implement runTest |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 294 | |
| 295 | @param config The oft configuration dictionary |
| 296 | @returns An array of triples (mod-name, module, [tests]) where |
| 297 | mod-name is the string (filename) of the module, module is the |
| 298 | value returned from __import__'ing the module and [tests] is an |
| 299 | array of strings giving the test cases from the module. |
| 300 | """ |
| 301 | |
| 302 | # Find and import test files |
| 303 | p1 = Popen(["find", config["test_dir"], "-type","f"], stdout = PIPE) |
| 304 | p2 = Popen(["xargs", "grep", "-l", "-e", "^def test_set_init"], |
| 305 | stdin=p1.stdout, stdout=PIPE) |
| 306 | |
| 307 | all_tests = {} |
| 308 | mod_name_map = {} |
| 309 | # There's an extra empty entry at the end of the list |
| 310 | filelist = p2.communicate()[0].split("\n")[:-1] |
| 311 | for file in filelist: |
Dan Talayco | ac25cf3 | 2010-07-20 14:08:28 -0700 | [diff] [blame] | 312 | if file[-1:] == '~' or file[0] == '#': |
Dan Talayco | de2a639 | 2010-03-10 13:56:51 -0800 | [diff] [blame] | 313 | continue |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 314 | modfile = file.lstrip('./')[:-3] |
| 315 | |
| 316 | try: |
| 317 | mod = __import__(modfile) |
| 318 | except: |
| 319 | logging.warning("Could not import file " + file) |
| 320 | continue |
| 321 | mod_name_map[modfile] = mod |
| 322 | added_fn = False |
| 323 | for fn in dir(mod): |
| 324 | if 'runTest' in dir(eval("mod." + fn)): |
| 325 | if not added_fn: |
| 326 | mod_name_map[modfile] = mod |
| 327 | all_tests[mod] = [] |
| 328 | added_fn = True |
| 329 | all_tests[mod].append(fn) |
| 330 | config["all_tests"] = all_tests |
| 331 | config["mod_name_map"] = mod_name_map |
| 332 | |
| 333 | def die(msg, exit_val=1): |
| 334 | print msg |
| 335 | logging.critical(msg) |
| 336 | sys.exit(exit_val) |
| 337 | |
| 338 | def add_test(suite, mod, name): |
| 339 | logging.info("Adding test " + mod.__name__ + "." + name) |
| 340 | suite.addTest(eval("mod." + name)()) |
| 341 | |
Dan Talayco | 79f3608 | 2010-03-11 16:53:53 -0800 | [diff] [blame] | 342 | def _space_to(n, str): |
| 343 | """ |
| 344 | Generate a string of spaces to achieve width n given string str |
| 345 | If length of str >= n, return one space |
| 346 | """ |
| 347 | spaces = n - len(str) |
| 348 | if spaces > 0: |
| 349 | return " " * spaces |
| 350 | return " " |
| 351 | |
Dan Talayco | c24aaae | 2010-07-08 14:05:24 -0700 | [diff] [blame] | 352 | def test_prio_get(mod, test): |
| 353 | """ |
| 354 | Return the priority of a test |
| 355 | If set in the test_prio variable for the module, return |
| 356 | that value. Otherwise return 100 (default) |
| 357 | """ |
| 358 | if 'test_prio' in dir(mod): |
| 359 | if test in mod.test_prio.keys(): |
| 360 | return mod.test_prio[test] |
| 361 | return TEST_PRIO_DEFAULT |
| 362 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 363 | # |
| 364 | # Main script |
| 365 | # |
| 366 | |
| 367 | # Get configuration, set up logging, import platform from file |
| 368 | (config, args) = config_setup(config_default) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 369 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 370 | test_list_generate(config) |
| 371 | |
| 372 | # Check if test list is requested; display and exit if so |
| 373 | if config["list"]: |
Dan Talayco | 79f3608 | 2010-03-11 16:53:53 -0800 | [diff] [blame] | 374 | did_print = False |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 375 | print "\nTest List:" |
| 376 | for mod in config["all_tests"].keys(): |
Dan Talayco | 79f3608 | 2010-03-11 16:53:53 -0800 | [diff] [blame] | 377 | if config["test_spec"] != "all" and \ |
| 378 | config["test_spec"] != mod.__name__: |
| 379 | continue |
| 380 | did_print = True |
| 381 | desc = mod.__doc__.strip() |
| 382 | desc = desc.split('\n')[0] |
| 383 | start_str = " Module " + mod.__name__ + ": " |
| 384 | print start_str + _space_to(22, start_str) + desc |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 385 | for test in config["all_tests"][mod]: |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 386 | try: |
| 387 | desc = eval('mod.' + test + '.__doc__.strip()') |
| 388 | desc = desc.split('\n')[0] |
| 389 | except: |
| 390 | desc = "No description" |
Dan Talayco | c24aaae | 2010-07-08 14:05:24 -0700 | [diff] [blame] | 391 | if test_prio_get(mod, test) < 0: |
| 392 | start_str = " * " + test + ":" |
| 393 | else: |
| 394 | start_str = " " + test + ":" |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 395 | if len(start_str) > 22: |
| 396 | desc = "\n" + _space_to(22, "") + desc |
Dan Talayco | 79f3608 | 2010-03-11 16:53:53 -0800 | [diff] [blame] | 397 | print start_str + _space_to(22, start_str) + desc |
| 398 | print |
| 399 | if not did_print: |
| 400 | print "No tests found for " + config["test_spec"] |
Dan Talayco | c24aaae | 2010-07-08 14:05:24 -0700 | [diff] [blame] | 401 | else: |
Dan Talayco | 7aa0b81 | 2010-07-20 14:51:41 -0700 | [diff] [blame^] | 402 | print "Tests preceded by * are not run by default" |
| 403 | print "Tests marked (TP1) after name take --test-params including:" |
Dan Talayco | ac25cf3 | 2010-07-20 14:08:28 -0700 | [diff] [blame] | 404 | print " 'vid=N;strip_vlan=bool;add_vlan=bool'" |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 405 | sys.exit(0) |
| 406 | |
| 407 | logging_setup(config) |
| 408 | logging.info("++++++++ " + time.asctime() + " ++++++++") |
| 409 | |
| 410 | # Generate the test suite |
| 411 | #@todo Decide if multiple suites are ever needed |
| 412 | suite = unittest.TestSuite() |
| 413 | |
Dan Talayco | c24aaae | 2010-07-08 14:05:24 -0700 | [diff] [blame] | 414 | #@todo Allow specification of priority to override prio check |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 415 | if config["test_spec"] == "all": |
| 416 | for mod in config["all_tests"].keys(): |
| 417 | for test in config["all_tests"][mod]: |
Dan Talayco | c24aaae | 2010-07-08 14:05:24 -0700 | [diff] [blame] | 418 | # For now, a way to avoid tests |
| 419 | if test_prio_get(mod, test) >= 0: |
| 420 | add_test(suite, mod, test) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 421 | |
| 422 | else: |
| 423 | for ts_entry in config["test_spec"].split(","): |
| 424 | parts = ts_entry.split(".") |
| 425 | |
| 426 | if len(parts) == 1: # Either a module or test name |
| 427 | if ts_entry in config["mod_name_map"].keys(): |
| 428 | mod = config["mod_name_map"][ts_entry] |
| 429 | for test in config["all_tests"][mod]: |
| 430 | add_test(suite, mod, test) |
| 431 | else: # Search for matching tests |
| 432 | test_found = False |
| 433 | for mod in config["all_tests"].keys(): |
| 434 | if ts_entry in config["all_tests"][mod]: |
| 435 | add_test(suite, mod, ts_entry) |
| 436 | test_found = True |
| 437 | if not test_found: |
| 438 | die("Could not find module or test: " + ts_entry) |
| 439 | |
| 440 | elif len(parts) == 2: # module.test |
| 441 | if parts[0] not in config["mod_name_map"]: |
| 442 | die("Unknown module in test spec: " + ts_entry) |
| 443 | mod = config["mod_name_map"][parts[0]] |
| 444 | if parts[1] in config["all_tests"][mod]: |
| 445 | add_test(suite, mod, parts[1]) |
| 446 | else: |
| 447 | die("No known test matches: " + ts_entry) |
| 448 | |
| 449 | else: |
| 450 | die("Bad test spec: " + ts_entry) |
| 451 | |
| 452 | # Check if platform specified |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 453 | if config["platform"]: |
| 454 | _imp_string = "from " + config["platform"] + " import *" |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 455 | logging.info("Importing platform: " + _imp_string) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 456 | try: |
| 457 | exec(_imp_string) |
| 458 | except: |
| 459 | logging.warn("Failed to import " + config["platform"] + " file") |
| 460 | |
| 461 | try: |
| 462 | platform_config_update(config) |
| 463 | except: |
| 464 | logging.warn("Could not run platform host configuration") |
| 465 | |
| 466 | if not config["port_map"]: |
| 467 | # Try to set up default port mapping if not done by platform |
| 468 | config["port_map"] = default_port_map_setup(config) |
| 469 | |
| 470 | if not config["port_map"]: |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 471 | die("Interface port map is not defined. Exiting") |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 472 | |
| 473 | logging.debug("Configuration: " + str(config)) |
| 474 | logging.info("OF port map: " + str(config["port_map"])) |
| 475 | |
| 476 | # Init the test sets |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 477 | for (modname,mod) in config["mod_name_map"].items(): |
| 478 | try: |
| 479 | mod.test_set_init(config) |
| 480 | except: |
| 481 | logging.warning("Could not run test_set_init for " + modname) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 482 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 483 | if config["dbg_level"] == logging.CRITICAL: |
| 484 | _verb = 0 |
| 485 | elif config["dbg_level"] >= logging.WARNING: |
| 486 | _verb = 1 |
| 487 | else: |
| 488 | _verb = 2 |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 489 | |
Brandon Heller | 446c143 | 2010-04-01 12:43:27 -0700 | [diff] [blame] | 490 | if os.getuid() != 0: |
| 491 | print "ERROR: Super-user privileges required. Please re-run with " \ |
| 492 | "sudo or as root." |
| 493 | exit(1) |
| 494 | |
Dan Talayco | ac25cf3 | 2010-07-20 14:08:28 -0700 | [diff] [blame] | 495 | |
| 496 | if __name__ == "__main__": |
| 497 | logging.info("*** TEST RUN START: " + time.asctime()) |
| 498 | unittest.TextTestRunner(verbosity=_verb).run(suite) |
| 499 | logging.info("*** TEST RUN END : " + time.asctime()) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 500 | |