def testStartupOptions(self): cmd = "python {0} ".format(resource_file("ravel.py")) p = pexpect.spawn(cmd + "--help") p.expect("Usage") p.sendeof() time.sleep(1) p = pexpect.spawn(cmd + "--topo=single,3") p.expect("ravel>") p.sendeof() time.sleep(1) p = pexpect.spawn(cmd + "--topo=single,3 --onlydb") p.expect("ravel>") p.sendline("m") p.sendline("net") p.expect("no CLI available") p.sendline("exit") p.sendeof() time.sleep(1) p = pexpect.spawn(cmd + "--topo single,3 --noctl") p.expect("Unable to contact the remote controller") p.expect("ravel>") p.sendline("exit") p.sendeof()
def do_load(self, line): """Start one or more applications with orchestration Usage: load [app1] [app2] ... (priority: low -> high)""" ordering = [app.lower() for app in line.split()] for app in ordering: if app.lower() not in self.env.apps: print "Unrecognized app", app return # load unloaded apps loads = [app for app in ordering if app not in self.env.loaded] for app in loads: logger.debug("loading unloaded app %s", app) self.env.load_app(app) # unload unlisted apps: if it's loaded but not a shortcut or core app unlisted = [ app for app in self.env.loaded if app not in ordering and app not in self.env.coreapps and app in self.env.apps ] # for app in unlisted: # logger.info("unloading unlisted app %s", app) # self.env.unload_app(app) # processing in ascending order of priority ordering.reverse() sql = "" for app in [x for x in ordering if x != "routing"]: vtable = "{0}_violation".format(app) sql += ptable_template.format(app) sql += runrule_template.format(app, vtable) if "routing" in ordering: sql += routing for app1, app2 in pairwise(ordering): sql += orderrule_template.format(app1, app2) sql += clock_template.format(ordering[-1]) self.ordering = [x for x in reversed(ordering)] self.sql = sql log = resource_file("orch_log.sql") f = open(log, 'w') f.write(self.sql) f.close() logger.debug("logged orchestration protocol to %s", log) try: self.db.cursor.execute(self.sql) except Exception, e: print e
def do_load(self, line): """Start one or more applications with orchestration Usage: load [app1] [app2] ... (priority: low -> high)""" ordering = [app.lower() for app in line.split()] for app in ordering: if app.lower() not in self.env.apps: print "Unrecognized app", app return # load unloaded apps loads = [app for app in ordering if app not in self.env.loaded] for app in loads: logger.debug("loading unloaded app %s", app) self.env.load_app(app) # unload unlisted apps: if it's loaded but not a shortcut or core app unlisted = [app for app in self.env.loaded if app not in ordering and app not in self.env.coreapps and app in self.env.apps] for app in unlisted: logger.info("unloading unlisted app %s", app) self.env.unload_app(app) # processing in ascending order of priority ordering.reverse() sql = "" for app in [x for x in ordering if x != "routing"]: vtable = "{0}_violation".format(app) sql += ptable_template.format(app) sql += runrule_template.format(app, vtable) if "routing" in ordering: sql += routing for app1, app2 in pairwise(ordering): sql += orderrule_template.format(app1, app2) sql += clock_template.format(ordering[-1]) self.ordering = [x for x in reversed(ordering)] self.sql = sql log = resource_file("orch_log.sql") f = open(log, 'w') f.write(self.sql) f.close() logger.debug("logged orchestration protocol to %s", log) try: self.db.cursor.execute(self.sql) except Exception, e: print e
def testDemo(self): p = pexpect.spawn("python {0} --topo=linear,4".format( resource_file("ravel.py"))) p.expect("ravel>") p.sendline("orch load routing fw") p.expect("ravel>") p.sendline("fw addhost h4") p.expect("Success") p.sendline("fw addhost h2") p.expect("Success") p.sendline("fw addflow h4 h3") p.expect("Success") p.sendline("rt addflow h4 h3 1") p.expect("Success") p.sendline("orch run") p.sendline("p select count(*) from rm") p.expect("1") p.sendline("p select count(*) from cf") p.expect("2") p.sendline("m h4 ping -c 1 h3") p.expect(" 0% packet loss") p.sendline("rt addflow h1 h2 1") p.expect("Success") p.sendline("p select count(*) from rm") p.expect("2") p.sendline("p select count(*) from fw_violation") p.expect("1") p.sendline("orch run") p.sendline("p select count(*) from fw_violation") p.expect("0") p.sendline("p select count(*) from rm") p.expect("1") p.sendline("p select count(*) from cf") p.expect("2") p.sendline("m h1 ping -c 1 h2") p.expect("100% packet loss") p.sendline("exit") p.sendeof() time.sleep(1)
def testDemo(self): p = pexpect.spawn("python {0} --topo=linear,4".format(resource_file("ravel.py"))) p.expect("ravel>") p.sendline("orch load routing fw") p.expect("ravel>") p.sendline("fw addhost h4") p.expect("Success") p.sendline("fw addhost h2") p.expect("Success") p.sendline("fw addflow h4 h3") p.expect("Success") p.sendline("rt addflow h4 h3 1") p.expect("Success") p.sendline("orch run") p.sendline("p select count(*) from rm") p.expect("1") p.sendline("p select count(*) from cf") p.expect("2") p.sendline("m h4 ping -c 1 h3") p.expect(" 0% packet loss") p.sendline("rt addflow h1 h2 1") p.expect("Success") p.sendline("p select count(*) from rm") p.expect("2") p.sendline("p select count(*) from fw_violation") p.expect("1") p.sendline("orch run") p.sendline("p select count(*) from fw_violation") p.expect("0") p.sendline("p select count(*) from rm") p.expect("1") p.sendline("p select count(*) from cf") p.expect("2") p.sendline("m h1 ping -c 1 h2") p.expect("100% packet loss") p.sendline("exit") p.sendeof() time.sleep(1)
class testCommands(unittest.TestCase): ravelCmd = "python {0} --topo single,3".format(resource_file("ravel.py")) def testStartupOptions(self): cmd = "python {0} ".format(resource_file("ravel.py")) p = pexpect.spawn(cmd + "--help") p.expect("Usage") p.sendeof() time.sleep(1) p = pexpect.spawn(cmd + "--topo=single,3") p.expect("ravel>") p.sendeof() time.sleep(1) p = pexpect.spawn(cmd + "--topo=single,3 --onlydb") p.expect("ravel>") p.sendline("m") p.sendline("net") p.expect("no CLI available") p.sendline("exit") p.sendeof() time.sleep(1) p = pexpect.spawn(cmd + "--topo single,3 --noctl") p.expect("Unable to contact the remote controller") p.expect("ravel>") p.sendline("exit") p.sendeof() def testCommands(self): p = pexpect.spawn(self.ravelCmd) p.expect("ravel>") p.sendline("exit") p.expect(pexpect.EOF) def tearDown(self): # kill pox if it's still running os.system("sudo killall -9 python2.7 > /dev/null 2>&1")
def testStartup(self): cmd = "python {0} ".format(resource_file("ravel.py")) p = pexpect.spawn(cmd + "--help") p.expect("Usage") p.sendeof() time.sleep(1) p = pexpect.spawn(cmd + "--topo=single,2") p.expect("ravel>") p.sendline("exit") p.sendeof() time.sleep(1) p = pexpect.spawn(cmd + "--topo=single,2 --onlydb") p.expect("ravel>") p.sendline("exit") p.sendeof() time.sleep(1) p = pexpect.spawn(cmd + "--topo=single,2 --verbosity=debug") p.expect("DEBUG") p.sendline("exit") p.sendeof() time.sleep(1)
except Exception, e: print e sql += runrule_template.format(app, vtable) if "routing" in ordering: sql += routing for app1, app2 in pairwise(ordering): sql += orderrule_template.format(app1, app2) sql += clock_template.format(ordering[-1]) self.ordering = [x for x in reversed(ordering)] self.sql = sql log = resource_file("orch_log.sql") f = open(log, 'w') f.write(self.sql) f.close() logger.debug("logged orchestration protocol to %s", log) try: self.db.cursor.execute(self.sql) except Exception, e: print e def do_unload(self, line): """Stop one or more applications Usage: unload [app1] [app2] ..."""
""" The Ravel backend PostgreSQL database """ import psycopg2 from ravel.log import logger from ravel.util import resource_file ISOLEVEL = psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT BASE_SQL = resource_file("ravel/sql/base.sql") FLOW_SQL = resource_file("ravel/sql/flows.sql") NOFLOW_SQL = resource_file("ravel/sql/noflows.sql") TOPO_SQL = resource_file("ravel/sql/topo.sql") class RavelDb(): """A representation of Ravel's backend PostgreSQL database.""" def __init__(self, name, user, base, passwd=None, reconnect=False): """name: the name of the database to connect to user: the username to use to connect base: a file containing the SQL implementation for Ravel's base passwd: the password to connect to the database reconnect: true to connect to an existing database setup, false to load a new instance of Ravel's base into the database""" self.name = name self.user = user self.passwd = passwd self.base = base self.cleaned = not reconnect
class testCommands(unittest.TestCase): ravelCmd = "python {0} --topo single,3".format(resource_file("ravel.py")) # Part 1 - Startup Options def testStartup(self): cmd = "python {0} ".format(resource_file("ravel.py")) p = pexpect.spawn(cmd + "--help") p.expect("Usage") p.sendeof() time.sleep(1) p = pexpect.spawn(cmd + "--topo=single,2") p.expect("ravel>") p.sendline("exit") p.sendeof() time.sleep(1) p = pexpect.spawn(cmd + "--topo=single,2 --onlydb") p.expect("ravel>") p.sendline("exit") p.sendeof() time.sleep(1) p = pexpect.spawn(cmd + "--topo=single,2 --verbosity=debug") p.expect("DEBUG") p.sendline("exit") p.sendeof() time.sleep(1) # Part 2 - Ravel Commands def testCommands(self): p = pexpect.spawn(self.ravelCmd) p.expect("ravel>") p.sendline("help") p.expect("Commands") p.sendline("p SELECT * FROM hosts;") p.expect("hid") p.sendline("stat") p.expect("app path") p.sendline("apps") p.expect("offline") p.sendline("exit") p.sendeof() time.sleep(1) # Part 3 - Orchestratioon def testOrchestration(self): p = pexpect.spawn(self.ravelCmd) p.expect("ravel>") p.sendline("orch load routing") p.sendline("rt addflow h1 h2") p.expect("Success") p.sendline("p select count(*) from rm") p.expect("1") p.sendline("p select count(*) from cf") p.expect("0") p.sendline("orch run") p.sendline("p select count(*) from rm") p.expect("1") p.sendline("p select count(*) from cf") p.expect("1") p.sendline("m h1 ping -c 1 h2") p.expect(" 0% packet loss") p.sendline("rt delflow h1 h2") p.expect("Success") p.sendline("orch run") p.sendline("p select count(*) from rm") p.expect("0") p.sendline("p select count(*) from cf") p.expect("0") p.sendline("orch auto on") p.sendline("rt addflow h1 h2") p.expect("Success") p.sendline("p select count(*) from rm") p.sendline("p select count(*) from cf") p.expect("1") p.sendline("m h1 ping -c 1 h2") p.expect(" 0% packet loss") p.sendline("exit") p.sendeof() time.sleep(1) # Part 4 - App Sub-shells def testApplications(self): p = pexpect.spawn(self.ravelCmd) p.expect("ravel>") p.sendline("orch load sample") p.sendline("sample echo Hello World") p.expect("SampleConsole says: Hello World") p.sendline("sample") p.expect("sample>") p.sendline("echo Hello World") p.expect("SampleConsole says: Hello World") p.sendline("exit") p.expect("ravel>") p.sendline("sample") p.sendline("help echo") p.expect("echo arguments") p.sendline("exit") p.expect("ravel>") p.sendline("help sample echo") p.expect("echo arguments") p.sendline("help sample") p.expect("sample commands") p.sendline("orch load routing") p.sendline("rt addflow h1 h2") p.expect("Success") p.sendline("orch run") p.sendline("m h1 ping -c 1 h2") p.expect(" 0% packet loss") p.sendline("rt delflow h1 h2") p.expect("Success") p.sendline("orch run") p.sendline("m h1 ping -c 1 h2") p.expect("100% packet loss") p.sendline("orch auto on") p.sendline("rt addflow h1 h2") p.expect("Success") p.sendline("m h1 ping -c 1 h2") p.expect(" 0% packet loss") p.sendline("rt delflow h1 h2") p.expect("Success") p.sendline("m h1 ping -c 1 h2") p.expect("100% packet loss") p.sendline("time rt addflow h1 h2") p.expect("Time:") p.sendline("profile rt delflow h1 h2") p.expect("db_select") p.sendline("profile rt addflow h1 h2") p.expect("db_select") p.sendline("orch load fw") p.sendline("fw addflow h1 h2") p.sendline("p select count(*) from fw_policy_acl") p.expect("1") p.sendline("fw addhost h1") p.sendline("p select count(*) from fw_policy_user") p.expect("1") p.sendline("exit") p.sendeof() time.sleep(1) # Part 5 - Orchestration Demo def testDemo(self): p = pexpect.spawn("python {0} --topo=linear,4".format( resource_file("ravel.py"))) p.expect("ravel>") p.sendline("orch load routing fw") p.expect("ravel>") p.sendline("fw addhost h4") p.expect("Success") p.sendline("fw addhost h2") p.expect("Success") p.sendline("fw addflow h4 h3") p.expect("Success") p.sendline("rt addflow h4 h3 1") p.expect("Success") p.sendline("orch run") p.sendline("p select count(*) from rm") p.expect("1") p.sendline("p select count(*) from cf") p.expect("2") p.sendline("m h4 ping -c 1 h3") p.expect(" 0% packet loss") p.sendline("rt addflow h1 h2 1") p.expect("Success") p.sendline("p select count(*) from rm") p.expect("2") p.sendline("p select count(*) from fw_violation") p.expect("1") p.sendline("orch run") p.sendline("p select count(*) from fw_violation") p.expect("0") p.sendline("p select count(*) from rm") p.expect("1") p.sendline("p select count(*) from cf") p.expect("2") p.sendline("m h1 ping -c 1 h2") p.expect("100% packet loss") p.sendline("exit") p.sendeof() time.sleep(1) def tearDown(self): # kill pox if it's still running os.system("sudo killall -9 python2.7 > /dev/null 2>&1")