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 |
| 11 | ygenerally called config. The keys have the following |
| 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 | |
| 42 | To add a test to the system, either: edit an existing test case file (like |
| 43 | basic.py) to add a test class which inherits from unittest.TestCase (directly |
| 44 | or indirectly); or add a new file which includes a function definition |
| 45 | test_set_init(config). Preferably the file is in the same directory as existing |
| 46 | tests, though you can specify the directory on the command line. The file |
| 47 | should not be called "all" as that's reserved for the test-spec. |
| 48 | |
| 49 | If you add a new file, the test_set_init function should record the port |
| 50 | map object from the configuration along with whatever other configuration |
| 51 | information it may need. |
| 52 | |
| 53 | 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] | 54 | below. If you want this to be a command line parameter, edit config_setup |
| 55 | to add the option and default value to the parser. Then edit config_get |
| 56 | to make sure the option value gets copied into the configuration |
| 57 | structure (which then gets passed to everyone else). |
| 58 | |
| 59 | By convention, oft attempts to import the contents of a file by the |
| 60 | name of $platform.py into the local namespace. |
| 61 | |
| 62 | IMPORTANT: That file should define a function platform_config_update which |
| 63 | takes a configuration dictionary as an argument and updates it for the |
| 64 | current run. In particular, it should set up config["port_map"] with |
| 65 | the proper map from OF port numbers to OF interface names. |
| 66 | |
| 67 | You can add your own platform, say gp104, by adding a file gp104.py |
| 68 | that defines the function platform_config_update and then use the |
| 69 | parameter --platform=gp104 on the command line. |
| 70 | |
| 71 | If platform_config_update does not set config["port_map"], an attempt |
| 72 | is made to generate a default map via the function default_port_map_setup. |
| 73 | This will use "local" and "remote" for platform names if available |
| 74 | and generate a sequential map based on the values of base_of_port and |
| 75 | base_if_index in the configuration structure. |
| 76 | |
| 77 | @todo Determine and implement conventions for test_spec. |
| 78 | |
| 79 | The current model for test sets is basic.py. The current convention is |
| 80 | that the test set should implement a function test_set_init which takes |
| 81 | an oft configuration dictionary and returns a unittest.TestSuite object. |
| 82 | Future test sets should do the same thing. |
| 83 | |
Dan Talayco | 52f6444 | 2010-03-03 15:32:41 -0800 | [diff] [blame] | 84 | Default setup: |
| 85 | |
| 86 | The default setup runs locally using veth pairs. To exercise this, |
| 87 | checkout and build an openflow userspace datapath. Then start it on |
| 88 | the local host: |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame^] | 89 | <pre> |
Dan Talayco | 52f6444 | 2010-03-03 15:32:41 -0800 | [diff] [blame] | 90 | sudo ~/openflow/regress/bin/veth_setup.pl |
| 91 | sudo ofdatapath -i veth0,veth2,veth4,veth6 punix:/tmp/ofd & |
| 92 | sudo ofprotocol unix:/tmp/ofd tcp:127.0.0.1 --fail=closed --max-backoff=1 & |
| 93 | |
| 94 | Next, run oft: |
| 95 | sudo ./oft --debug=info |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame^] | 96 | </pre> |
Dan Talayco | 52f6444 | 2010-03-03 15:32:41 -0800 | [diff] [blame] | 97 | |
| 98 | Examine oft.log if things don't work. |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame^] | 99 | |
| 100 | @todo Generate test catalog; support list, selection and grouping |
| 101 | |
| 102 | Proposed test case setup: |
| 103 | Files in this or sub directories (or later, directory specified on |
| 104 | command line) that contain a function test_set_init are considered test |
| 105 | files. |
| 106 | The function test_set_init examines the test_spec config variable |
| 107 | and generates a suite of tests. |
| 108 | Support a command line option --test_mod so that all tests in that |
| 109 | module will be run. |
| 110 | Support all to specify all tests from the module. |
| 111 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 112 | """ |
| 113 | |
| 114 | import sys |
| 115 | from optparse import OptionParser |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame^] | 116 | from subprocess import Popen,PIPE |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 117 | import logging |
| 118 | import unittest |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame^] | 119 | import time |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 120 | |
| 121 | ##@var DEBUG_LEVELS |
| 122 | # Map from strings to debugging levels |
| 123 | DEBUG_LEVELS = { |
| 124 | 'debug' : logging.DEBUG, |
| 125 | 'verbose' : logging.DEBUG, |
| 126 | 'info' : logging.INFO, |
| 127 | 'warning' : logging.WARNING, |
| 128 | 'warn' : logging.WARNING, |
| 129 | 'error' : logging.ERROR, |
| 130 | 'critical' : logging.CRITICAL |
| 131 | } |
| 132 | |
| 133 | _debug_default = "warning" |
| 134 | _debug_level_default = DEBUG_LEVELS[_debug_default] |
| 135 | |
| 136 | ##@var config_default |
| 137 | # The default configuration dictionary for OFT |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame^] | 138 | #@todo Set up a dict of config params so easier to manage |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 139 | config_default = { |
| 140 | "platform" : "local", |
| 141 | "controller_host" : "127.0.0.1", |
| 142 | "controller_port" : 6633, |
| 143 | "port_count" : 4, |
| 144 | "base_of_port" : 1, |
| 145 | "base_if_index" : 1, |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame^] | 146 | "test_spec" : "all", |
| 147 | "test_dir" : ".", |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 148 | "log_file" : "oft.log", |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame^] | 149 | "list" : False, |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 150 | "debug" : _debug_default, |
| 151 | "dbg_level" : _debug_level_default, |
| 152 | "port_map" : {} |
| 153 | } |
| 154 | |
| 155 | # Map options to config structure |
| 156 | def config_get(opts): |
| 157 | "Convert options class to OFT configuration dictionary" |
| 158 | cfg = config_default.copy() |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame^] | 159 | for key in cfg.keys(): |
| 160 | cfg[key] = eval("opts." + key) |
| 161 | |
| 162 | # Special case checks |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 163 | if opts.debug not in DEBUG_LEVELS.keys(): |
| 164 | print "Warning: Bad value specified for debug level; using default" |
| 165 | opts.debug = _debug_default |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 166 | cfg["dbg_level"] = DEBUG_LEVELS[cfg["debug"]] |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame^] | 167 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 168 | return cfg |
| 169 | |
| 170 | def config_setup(cfg_dflt): |
| 171 | """ |
| 172 | Set up the configuration including parsing the arguments |
| 173 | |
| 174 | @param cfg_dflt The default configuration dictionary |
| 175 | @return A pair (config, args) where config is an config |
| 176 | object and args is any additional arguments from the command line |
| 177 | """ |
| 178 | |
| 179 | parser = OptionParser(version="%prog 0.1") |
| 180 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame^] | 181 | #@todo parse port map as option? |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 182 | # Set up default values |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame^] | 183 | for key in cfg_dflt.keys(): |
| 184 | eval("parser.set_defaults("+key+"=cfg_dflt['"+key+"'])") |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 185 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame^] | 186 | #@todo Add options via dictionary |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 187 | plat_help = """Set the platform type. Valid values include: |
| 188 | local: User space virtual ethernet pair setup |
| 189 | remote: Remote embedded Broadcom based switch |
| 190 | """ |
| 191 | parser.add_option("-P", "--platform", help=plat_help) |
| 192 | parser.add_option("-H", "--host", dest="controller_host", |
| 193 | help="The IP/name of the test controller host") |
| 194 | parser.add_option("-p", "--port", dest="controller_port", |
| 195 | type="int", help="Port number of the test controller") |
| 196 | parser.add_option("--test-spec", "--test-list", |
| 197 | help="Indicate tests to run (TBD)") |
| 198 | parser.add_option("--log-file", |
| 199 | help="Name of log file, empty string to log to console") |
| 200 | parser.add_option("--debug", |
| 201 | help="Debug lvl: debug, info, warning, error, critical") |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame^] | 202 | parser.add_option("--port-count", |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 203 | help="Number of ports to use (optional)") |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame^] | 204 | parser.add_option("--base-of-port", |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 205 | help="Base OpenFlow port number (optional)") |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame^] | 206 | parser.add_option("--base-if-index", |
| 207 | help="Base interface index number (optional)") |
| 208 | parser.add_option("--list", action="store_true", |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 209 | help="Base interface index number (optional)") |
| 210 | # Might need this if other parsers want command line |
| 211 | # parser.allow_interspersed_args = False |
| 212 | (options, args) = parser.parse_args() |
| 213 | |
| 214 | config = config_get(options) |
| 215 | |
| 216 | return (config, args) |
| 217 | |
| 218 | def logging_setup(config): |
| 219 | """ |
| 220 | Set up logging based on config |
| 221 | """ |
| 222 | _format = "%(asctime)s %(name)-10s: %(levelname)-8s: %(message)s" |
| 223 | _datefmt = "%H:%M:%S" |
| 224 | if config["log_file"]: |
| 225 | logging.basicConfig(filename=config["log_file"], |
| 226 | level=config["dbg_level"], |
| 227 | format=_format, datefmt=_datefmt) |
| 228 | #@todo Handle "no log file" |
| 229 | |
| 230 | def default_port_map_setup(config): |
| 231 | """ |
| 232 | Setup the OF port mapping based on config |
| 233 | @param config The OFT configuration structure |
| 234 | @return Port map dictionary |
| 235 | """ |
| 236 | if (config["base_of_port"] is None) or not config["port_count"]: |
| 237 | return None |
| 238 | port_map = {} |
| 239 | if config["platform"] == "local": |
| 240 | # For local, use every other veth port |
| 241 | for idx in range(config["port_count"]): |
| 242 | port_map[config["base_of_port"] + idx] = "veth" + \ |
| 243 | str(config["base_if_index"] + (2 * idx)) |
| 244 | elif config["platform"] == "remote": |
| 245 | # For remote, use eth ports |
| 246 | for idx in range(config["port_count"]): |
| 247 | port_map[config["base_of_port"] + idx] = "eth" + \ |
| 248 | str(config["base_if_index"] + idx) |
| 249 | else: |
| 250 | return None |
| 251 | |
| 252 | logging.info("Built default port map") |
| 253 | return port_map |
| 254 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame^] | 255 | def test_list_generate(config): |
| 256 | """Generate the list of all known tests indexed by module name |
| 257 | |
| 258 | Conventions: Test files must implement the function test_set_init |
| 259 | |
| 260 | Test cases are classes that implement testRun |
| 261 | |
| 262 | @param config The oft configuration dictionary |
| 263 | @returns An array of triples (mod-name, module, [tests]) where |
| 264 | mod-name is the string (filename) of the module, module is the |
| 265 | value returned from __import__'ing the module and [tests] is an |
| 266 | array of strings giving the test cases from the module. |
| 267 | """ |
| 268 | |
| 269 | # Find and import test files |
| 270 | p1 = Popen(["find", config["test_dir"], "-type","f"], stdout = PIPE) |
| 271 | p2 = Popen(["xargs", "grep", "-l", "-e", "^def test_set_init"], |
| 272 | stdin=p1.stdout, stdout=PIPE) |
| 273 | |
| 274 | all_tests = {} |
| 275 | mod_name_map = {} |
| 276 | # There's an extra empty entry at the end of the list |
| 277 | filelist = p2.communicate()[0].split("\n")[:-1] |
| 278 | for file in filelist: |
| 279 | modfile = file.lstrip('./')[:-3] |
| 280 | |
| 281 | try: |
| 282 | mod = __import__(modfile) |
| 283 | except: |
| 284 | logging.warning("Could not import file " + file) |
| 285 | continue |
| 286 | mod_name_map[modfile] = mod |
| 287 | added_fn = False |
| 288 | for fn in dir(mod): |
| 289 | if 'runTest' in dir(eval("mod." + fn)): |
| 290 | if not added_fn: |
| 291 | mod_name_map[modfile] = mod |
| 292 | all_tests[mod] = [] |
| 293 | added_fn = True |
| 294 | all_tests[mod].append(fn) |
| 295 | config["all_tests"] = all_tests |
| 296 | config["mod_name_map"] = mod_name_map |
| 297 | |
| 298 | def die(msg, exit_val=1): |
| 299 | print msg |
| 300 | logging.critical(msg) |
| 301 | sys.exit(exit_val) |
| 302 | |
| 303 | def add_test(suite, mod, name): |
| 304 | logging.info("Adding test " + mod.__name__ + "." + name) |
| 305 | suite.addTest(eval("mod." + name)()) |
| 306 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 307 | # |
| 308 | # Main script |
| 309 | # |
| 310 | |
| 311 | # Get configuration, set up logging, import platform from file |
| 312 | (config, args) = config_setup(config_default) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 313 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame^] | 314 | test_list_generate(config) |
| 315 | |
| 316 | # Check if test list is requested; display and exit if so |
| 317 | if config["list"]: |
| 318 | print "\nTest List:" |
| 319 | for mod in config["all_tests"].keys(): |
| 320 | print " Module: " + mod.__name__ |
| 321 | for test in config["all_tests"][mod]: |
| 322 | print " " + test |
| 323 | sys.exit(0) |
| 324 | |
| 325 | logging_setup(config) |
| 326 | logging.info("++++++++ " + time.asctime() + " ++++++++") |
| 327 | |
| 328 | # Generate the test suite |
| 329 | #@todo Decide if multiple suites are ever needed |
| 330 | suite = unittest.TestSuite() |
| 331 | |
| 332 | if config["test_spec"] == "all": |
| 333 | for mod in config["all_tests"].keys(): |
| 334 | for test in config["all_tests"][mod]: |
| 335 | add_test(suite, mod, test) |
| 336 | |
| 337 | else: |
| 338 | for ts_entry in config["test_spec"].split(","): |
| 339 | parts = ts_entry.split(".") |
| 340 | |
| 341 | if len(parts) == 1: # Either a module or test name |
| 342 | if ts_entry in config["mod_name_map"].keys(): |
| 343 | mod = config["mod_name_map"][ts_entry] |
| 344 | for test in config["all_tests"][mod]: |
| 345 | add_test(suite, mod, test) |
| 346 | else: # Search for matching tests |
| 347 | test_found = False |
| 348 | for mod in config["all_tests"].keys(): |
| 349 | if ts_entry in config["all_tests"][mod]: |
| 350 | add_test(suite, mod, ts_entry) |
| 351 | test_found = True |
| 352 | if not test_found: |
| 353 | die("Could not find module or test: " + ts_entry) |
| 354 | |
| 355 | elif len(parts) == 2: # module.test |
| 356 | if parts[0] not in config["mod_name_map"]: |
| 357 | die("Unknown module in test spec: " + ts_entry) |
| 358 | mod = config["mod_name_map"][parts[0]] |
| 359 | if parts[1] in config["all_tests"][mod]: |
| 360 | add_test(suite, mod, parts[1]) |
| 361 | else: |
| 362 | die("No known test matches: " + ts_entry) |
| 363 | |
| 364 | else: |
| 365 | die("Bad test spec: " + ts_entry) |
| 366 | |
| 367 | # Check if platform specified |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 368 | if config["platform"]: |
| 369 | _imp_string = "from " + config["platform"] + " import *" |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame^] | 370 | logging.info("Importing platform: " + _imp_string) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 371 | try: |
| 372 | exec(_imp_string) |
| 373 | except: |
| 374 | logging.warn("Failed to import " + config["platform"] + " file") |
| 375 | |
| 376 | try: |
| 377 | platform_config_update(config) |
| 378 | except: |
| 379 | logging.warn("Could not run platform host configuration") |
| 380 | |
| 381 | if not config["port_map"]: |
| 382 | # Try to set up default port mapping if not done by platform |
| 383 | config["port_map"] = default_port_map_setup(config) |
| 384 | |
| 385 | if not config["port_map"]: |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame^] | 386 | die("Interface port map is not defined. Exiting") |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 387 | |
| 388 | logging.debug("Configuration: " + str(config)) |
| 389 | logging.info("OF port map: " + str(config["port_map"])) |
| 390 | |
| 391 | # Init the test sets |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame^] | 392 | for (modname,mod) in config["mod_name_map"].items(): |
| 393 | try: |
| 394 | mod.test_set_init(config) |
| 395 | except: |
| 396 | logging.warning("Could not run test_set_init for " + modname) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 397 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame^] | 398 | if config["dbg_level"] == logging.CRITICAL: |
| 399 | _verb = 0 |
| 400 | elif config["dbg_level"] >= logging.WARNING: |
| 401 | _verb = 1 |
| 402 | else: |
| 403 | _verb = 2 |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 404 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame^] | 405 | logging.info("*** TEST RUN START: " + time.asctime()) |
| 406 | unittest.TextTestRunner(verbosity=_verb).run(suite) |
| 407 | logging.info("*** TEST RUN END : " + time.asctime()) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 408 | |