def testNoCode(self): from switchyard.syinit import start_framework o = self._makeOptions(verbose=True, tests=['stest3'], usercode=None) with self.assertLogs(level='DEBUG') as cm: start_framework(o) self.assertIn("In test mode, but not user code supplied", cm.output[-1]) del(start_framework)
def testReal3(self): from switchyard.syinit import start_framework prevplat = sys.platform setattr(sys, "platform", "test") o = self._makeOptions(app=None, intf=None, exclude=None, verbose=True, debug=True, tests=[], fwconfig=['none'], usercode='ucode14') mdlmock = Mock(return_value={'happy', 'joy'}) netmock = Mock(return_value=Mock()) import switchyard.syinit setattr(switchyard.syinit, "LLNetReal", netmock) setattr(switchyard.syinit, "make_device_list", mdlmock) with redirectio() as xio: with self.assertLogs(level='DEBUG') as cm: start_framework(o) self.assertIn("expect errors", cm.output[0]) setattr(sys, "platform", prevplat) del (start_framework)
def testFailAppCode(self): from switchyard.syinit import start_framework o = self._makeOptions(app='doesntexist',tests=['stest3'], usercode='ucode13') with redirectio() as xio: with self.assertLogs(level='DEBUG') as cm: start_framework(o) self.assertIn("No module named 'doesntexist'", xio.contents) del(start_framework)
def testFailUserCode(self): from switchyard.syinit import start_framework o = self._makeOptions(app='appcode13',tests=['stest3'], usercode='doesntexist') with redirectio() as xio: with self.assertLogs(level='DEBUG') as cm: with self.assertRaises(ImportError): start_framework(o) del(start_framework)
def testCompileWithCode(self): from switchyard.syinit import start_framework o = self._makeOptions(debug=True, app=None, compile=['stest3'], usercode='ucode13') with redirectio() as xio: with self.assertLogs(level='DEBUG') as cm: start_framework(o) self.assertIn("specified user code to run with compile flag", cm.output[0]) self.assertIn("Doing sanity check", cm.output[-1]) del(start_framework)
def testSockemu(self): from switchyard.syinit import start_framework o = self._makeOptions(app='appcode13', tests=['stest3'], usercode='ucode13') with redirectio() as xio: with self.assertLogs(level='DEBUG') as cm: start_framework(o) self.assertIn("All tests passed", xio.contents) self.assertIn("Client socket application received message", xio.contents) self.assertIn("Preventing host from receiving traffic on", cm.output[5]) self.assertIn("Selecting only", cm.output[6]) del(start_framework)
def testReal2(self): from switchyard.syinit import start_framework o = self._makeOptions(app=None, tests=[], usercode='ucode13') mrmock = Mock(return_value=True) mdlmock = Mock(side_effect=[[],['fakedev']]) netmock = Mock(return_value=Mock()) import switchyard.syinit setattr(switchyard.syinit, "main_real", mrmock) setattr(switchyard.syinit, "_assemble_device_list", mdlmock) setattr(switchyard.syinit, "LLNetReal", netmock) with redirectio() as xio: with self.assertLogs(level='DEBUG') as cm: start_framework(o) self.assertIn("CRITICAL:root:Here are all the interfaces I see on your system: fakedev", cm.output[-1]) del(start_framework)
def testReal(self): from switchyard.syinit import start_framework o = self._makeOptions(app='appcode13', tests=[], usercode='ucode13') mrmock = Mock(return_value=True) mdlmock = Mock(return_value=['fakedev']) netmock = Mock(return_value=Mock()) import switchyard.syinit setattr(switchyard.syinit, "main_real", mrmock) setattr(switchyard.syinit, "make_device_list", mdlmock) setattr(switchyard.syinit, "LLNetReal", netmock) with redirectio() as xio: with self.assertLogs(level='DEBUG') as cm: start_framework(o) self.assertIn("WARNING:root:You're running in real mode, but not as root", cm.output[-1]) del(start_framework)
def testReal3(self): from switchyard.syinit import start_framework prevplat = sys.platform setattr(sys, "platform", "test") o = self._makeOptions(app=None, intf=None, exclude=None, verbose=True, debug=True, tests=[], fwconfig=['none'], usercode='ucode14') mdlmock = Mock(return_value={'happy', 'joy'}) netmock = Mock(return_value=Mock()) import switchyard.syinit setattr(switchyard.syinit, "LLNetReal", netmock) setattr(switchyard.syinit, "_assemble_device_list", mdlmock) with redirectio() as xio: with self.assertLogs(level='DEBUG') as cm: start_framework(o) self.assertIn("expect errors", cm.output[0]) setattr(sys, "platform", prevplat) del(start_framework)
def main(): version_check() progname = "swyard" parser = argparse.ArgumentParser(prog=progname) parser.add_argument( "-i", "--include", metavar='INCLUDE_INTF', help="Specify interface names to include/use for data plane traffic " "(default: all non-loopback interfaces).", dest="intf", action='append') parser.add_argument( "-x", "--exclude", metavar='EXCLUDE_INTF', help="Specify interface names to exclude in {}. " "All other non-loopback interfaces will be used.".format(progname), dest="exclude", action='append') parser.add_argument('usercode', metavar="YOURCODE", type=str, nargs='?', help='User switch/router code to execute.') parser.add_argument( "-g", "--codearg", metavar="YOURCODE_ARGS", type=str, default='', help='Arguments to pass to your code (if multiple args, they need to be ' ' quoted in the shell).') parser.add_argument( "-c", "--compile", help="Compile test scenario to binary format for distribution.", dest="compile", action="append", metavar="TEST_SCENARIO") parser.add_argument("-t", "--test", help="Run {} in testing mode, using the given test " "scenario file.".format(progname), metavar="TESTSCENARIO", dest="tests", action="append") parser.add_argument( "--dryrun", help="Get everything ready to go, but don't actually do anything.", action='store_true', dest='dryrun', default=False) parser.add_argument( "-v", "--verbose", help="Turn on verbose output, including full packet dumps in test " "results. Can be specified multiple times to increase verbosity.", dest="verbose", action="count", default=0) parser.add_argument("-d", "--debug", help="Turn on debug logging output.", dest="debug", action="store_true", default=False) parser.add_argument( "-l", "--logfile", help="Specify the name of a file to send" " log entries to (default is to send log to stdout/stderr).", dest="logfile", default=None, type=str) parser.add_argument("--nopdb", help="Don't enter pdb on crash.", dest="nopdb", action="store_true", default=False) parser.add_argument( "-f", "--firewall", help="Specify host firewall rules (for real/live mode only).", dest="fwconfig", action="append") parser.add_argument( "-a", "--app", help="Specify application layer (socket-based) program to start.", dest="app", default=None, metavar="SOCKET_APP") parser.add_argument( "-e", "--nohandle", help="Don't trap exceptions. Use of this option is helpful if you want" " to use Switchyard with a different symbolic debugger than pdb.", dest="nohandle", action="store_true", default=False) parser.add_argument( "--cli", help="Enter switchyard simulation command-line (EXPERIMENTAL!)", dest="cli", action="store_true", default=False) parser.add_argument("--topology", help="Specify topology to use for simulation" " (only used if --cli is specified).", dest="topology", type=str, default=None) args = parser.parse_args() if args.usercode is None and not args.compile: parser.print_usage() return -1 start_framework(args)
def main(): version_check() progname = "swyard" parser = argparse.ArgumentParser(prog=progname) parser.add_argument("-i", "--include", metavar='INCLUDE_INTF', help="Specify interface names to include/use for data plane traffic " "(default: all non-loopback interfaces).", dest="intf", action='append') parser.add_argument("-x", "--exclude", metavar='EXCLUDE_INTF', help="Specify interface names to exclude in {}. " "All other non-loopback interfaces will be used.".format(progname), dest="exclude", action='append') parser.add_argument('usercode', metavar="YOURCODE", type=str, nargs='?', help='User switch/router code to execute.') parser.add_argument("-g", "--codearg", metavar="YOURCODE_ARGS", type=str, default='', help='Arguments to pass to your code (if multiple args, they need to be ' ' quoted in the shell).') parser.add_argument("-c", "--compile", help="Compile test scenario to binary format for distribution.", dest="compile", action="append", metavar="TEST_SCENARIO") parser.add_argument("-t", "--test", help="Run {} in testing mode, using the given test " "scenario file.".format(progname), metavar="TESTSCENARIO", dest="tests", action="append") parser.add_argument("--dryrun", help="Get everything ready to go, but don't actually do anything.", action='store_true', dest='dryrun', default=False) parser.add_argument("-v", "--verbose", help="Turn on verbose output, including full packet dumps in test " "results. Can be specified multiple times to increase verbosity.", dest="verbose", action="count", default=0) parser.add_argument("-d", "--debug", help="Turn on debug logging output.", dest="debug", action="store_true", default=False) parser.add_argument("-l", "--logfile", help="Specify the name of a file to send" " log entries to (default is to send log to stdout/stderr).", dest="logfile", default=None, type=str) parser.add_argument("--nopdb", help="Don't enter pdb on crash.", dest="nopdb", action="store_true", default=False) parser.add_argument("-f", "--firewall", help="Specify host firewall rules (for real/live mode only).", dest="fwconfig", action="append") parser.add_argument("-a", "--app", help="Specify application layer (socket-based) program to start.", dest="app", default=None, metavar="SOCKET_APP") parser.add_argument("-e", "--nohandle", help="Don't trap exceptions. Use of this option is helpful if you want" " to use Switchyard with a different symbolic debugger than pdb.", dest="nohandle", action="store_true", default=False) parser.add_argument("--cli", help="Enter switchyard simulation command-line (EXPERIMENTAL!)", dest="cli", action="store_true", default=False) parser.add_argument("--topology", help="Specify topology to use for simulation" " (only used if --cli is specified).", dest="topology", type=str, default=None) parser.add_argument("--listif", help="List available interfaces (then exit)", dest="listif", action="store_true", default=False) args = parser.parse_args() if (args.usercode is None and not args.compile) and not args.listif: parser.print_usage() return -1 start_framework(args)