def waitForLinks(self, links, clix=None): for i in range(60): clinks = self.restGet("topology/links/json") count = 0 for link in links: for clink in clinks: match = True for k, v in link.iteritems(): if (k not in clink or clink[k] != v): match = False break if match: count += 1 break if count >= len(links): return log.info("Waiting for links (%d/%d found)" % (count, len(links))) time.sleep(1) log.error("Links never appeared") bigtest.Assert(False)
def doSerialLogin(self, console, username, usernamePrompt, passwordPrompt = None): p = console.pexpect() enteredUsername = False for i in xrange(300): n, b, a = console.expectReAlt([pexpect.TIMEOUT, usernamePrompt] + ([passwordPrompt] if passwordPrompt else []), timeout=2) if n == 0: # timeout if not b: # no output received # Send Control-D and Control-M in an attempt to exit from a shell or # other interactive process; don't send Control-C as that tends to # interrupt cleanup routines, leaving stale processes behind console.sendcontrol("d") console.sendcontrol("m") elif enteredUsername and passwordPrompt is None: console.sendline('\r\n') break elif n == 1: # username prompt # Send the username console.sendline(username) enteredUsername = True elif n == 2: # password prompt # We're done break else: bigtest.Assert(False) else: # Cause pexpect to raise a timeout exception if the password prompt is # still not seen if passwordPrompt != None: console.expectRe(passwordPrompt, timeout=0)
def waitForSwitchCluster(self, dpids, clix=None): self.waitForSwitches(dpids) # Set the time long enough (100s) to catch multiple lldp intervals log.info("WaitFSC: DPIDs: %s " % (dpids)) dpids = sorted(dpids) starttime = time.time() for i in range(300): if self.imageType() == "linux": clusters = self.restGet("topology/switchclusters/json") # Mininet sometimes generates spurious switchids # We need to check if there is a cluster that has all the given # dpids for cid in clusters: log.info("CL: %s dpid: %s" % (clusters[cid], dpids)) c = clusters[cid] count = 0 for s in dpids: if s in c: count = count + 1 if (count == len(dpids)): # Cluster with all the given dpids is formed dt = time.time() - starttime log.info( "Switch cluster set up with switches %s. It took %.2f seconds" % (clusters[cid], dt)) return log.info("Waiting for a cluster with all the given switches ...") time.sleep(1) bigtest.Assert(False)
def __init__(self, name=None, nodeNames=["node1", "node2", "node3"], fetchLogs=True, extraLogs=[]): Test.__init__(self, name, nodeNames, fetchLogs, extraLogs) bigtest.Assert(len(self.nodes_) == 3) self.mapNodes()
def getIPFromSerial(self, logLevel=logging.DEBUG): self.serialLogin(logLevel) cli = bigtest.cli.Cli(self.serialConsole(logLevel)) cli.gotoBashMode() output = cli.runCmd("ifconfig eth0") m = re.search(r"inet addr:([^\s]+)", output, re.M) bigtest.Assert(m and m.group(1) != "") return m.group(1)
def getMacAddrFromSerial(self, interface="eth0"): output = self.runSerialCmd("ifconfig %s" % interface) m = re.search(r"HWaddr ([^\s\n]+)", output, re.M) if m: return m.group(1).lower() else: print "Could not find MAC for interface %s"%interface bigtest.Assert(0)
def assert_no_flows(remove): if node1type == "linux": # remove flows via static flow pusher api (done when finishing test) if remove: urllib.urlopen( "http://%s:8080/wm/staticflowentrypusher/clear/all/json" % controller.ipAddress()) restout = rest('core/switch/%s/flow/json' % dpid) bigtest.Assert(restout[dpid] == [])
def assert_no_action_push(): sFlow = '{"switch": "%s", "name":"flow-mod-1", "cookie":"0", "priority":"32768", "ingress-port":"1","active":"true", "actions":""}' % dpid push_flow(sFlow) # verify in config and active flows if node1type == "linux": flows = controller.restGet('core/switch/%s/flow/json' % dpid) print "printing flows" + str(flows[dpid][0]['actions']) bigtest.Assert(flows[dpid][0]['actions'] == [])
def assert_all_action_push(): log("Test for flow-entry with all actions") sFlow = create_all_action_static_flow_post_data() push_flow(sFlow) # verify flow with all actions is active if node1type == "linux": flows = controller.restGet('core/switch/%s/flow/json' % dpid) bigtest.Assert(len(flows[dpid][0]['actions']) == len(actions))
def convertUnicodeKeyToString(ju): js = {} for i in ju: if str(i) in ["tz", "tzinfo", "microsecond"]: continue js[str(i)] = ju[i] # Make sure all of (year, month, day, hour, minute, second) are there # len() is ok because datetime.datetime() fails with unknown kwargs bigtest.Assert(len(js) == 6) return js
def add_cookie(self, server, header): header = string.strip(header) new_cookies = Cookie.SmartCookie() new_cookies.load(header) for cookie in new_cookies.values(): if not cookie.get('domain', None): cookie['domain'] = string.lower(server) bigtest.Assert(len(cookie['domain']) > 0) self.cookies.update(new_cookies) self._debug("added cookie: server=%s, header=%s" % (server, header))
def verifyPing(cli, hosta, hostb, controller_ipAddr=None, ignoreFirstPing=False, count = 1, timeout = 3, threshold=0): if ignoreFirstPing: cli.runCmd("%s ping -c%d -W%d %s" % (hosta, 1, 3, hostb)) x = cli.runCmd("%s ping -c%d -W%d %s" % (hosta, count, timeout, hostb)) m = re.search(r'(\d*)% packet loss', x) loss = int(m.group(1)) result = True if (loss > threshold): result = False bigtest.Assert(result)
def assert_action_push(): for i in range(len(actions)): if node1type == "linux": action_inp, action_outp = actions[i][0], actions[i][1] log("Test for flow-entry with action = '%s' -> '%s'", action_inp, action_outp) sFlow = create_single_action_static_flow_post_data(action_inp) push_flow(sFlow) # verify in config and active flows if node1type == "linux": flows = controller.restGet('core/switch/%s/flow/json' % dpid) bigtest.Assert( str(flows[dpid][0]['actions'][0]) == str(action_outp))
def waitForSwitches(self, dpids): log.info("Waiting for switches to connect to controller") for i in range(100): if self.imageType() == "linux": switches = self.restGet("core/controller/switches/json", "dpid") x = [ d for d in dpids if d not in switches or not switches[d]["dpid"] ] if not x: log.info("switches connected to controller") return i log.info("still waiting for switches: " + ", ".join(x)) time.sleep(1) bigtest.Assert(False)
def verifyArpCache(expected, actual, ignore=None, msg=None): expected = expected.copy() actual = actual.copy() if ignore is not None: for ip in ignore: if ip in expected: del expected[ip] if ip in actual: del actual[ip] if actual != expected: if msg is not None: print msg print "Expected:" pprint.pprint(expected) print "Actual:" pprint.pprint(actual) bigtest.Assert(False)
def verifyNoUdpPort(cli, hosta, hostb, port): cli.runCmd("%s rm /tmp/nc-u1.out; nc -l -u %s > /tmp/nc-u1.out&" % (hosta, port)) cli.runCmd("%s echo 'connected' | nc -w3 -u %s %s" % (hostb, hosta, port)) cli.runCmd("%s pkill nc" % hosta) x = cli.runCmd("%s cat /tmp/nc-u1.out" % hosta) bigtest.Assert('connected' not in x)
def verifyNoTcpPort(cli, hosta, hostb, port): cli.runCmd("%s nc -l %s &" % (hosta, port)) x = cli.runCmd("%s nc -w3 -z %s %s; echo $?" % (hostb, hosta, port)) cli.runCmd("%s pkill nc" % hosta) bigtest.Assert('1' in x)
def verifyNoPing(cli, hosta, hostb): x = cli.runCmd("%s ping -c1 -w1 %s" % (hosta, hostb)) bigtest.Assert("100% packet loss" in x)
# verify using staticflowentrypusher each switch has correct flowentry command = "http://%s:8080/wm/staticflowentrypusher/list/all/json" % controllerIp y = urllib.urlopen(command).read() parsedResult = json.loads(y) expectedSwitches = [ "00:00:00:00:00:00:00:1%c" % x for x in ['1', '2', '3', '4', '9', 'd', 'f'] ] foundSwitches = dict(parsedResult) for key in expectedSwitches: if foundSwitches.has_key(key): flowEntries = dict(parsedResult.get(key)) print "found switch %s with %s flows" % (key, len(flowEntries)) bigtest.Assert(len(flowEntries) == 4) else: print "missing switch %s" % key bigtest.Assert(False) # do iperf test to confirm things working (this is meaningful only if forwarding module is unloaded) mininetCli.runCmd("h16 iperf -s &") x = mininetCli.runCmd("h1 iperf -c h16 -t 2") bigtest.Assert(not "connect failed" in x) mininetCli.runCmd("h16 pkill iperf") # push another circuit from h1 to h9 command = "./circuitpusher.py --controller=%s:8080 --type ip --src %s --dst %s --add --name test-circuit-2" % ( controllerIp, "10.0.0.1", "10.0.0.9")
#!/usr/bin/env python import bigtest.controller import bigtest env = bigtest.controller.TwoNodeTest() log = bigtest.log.info controllerNode = env.node1() mininetNodeCli = env.node2().cli() mininetNodeCli.gotoBashMode() mininetNodeCli.runCmd("rm -f islands.py") with open("bigtest/islands.py", "r") as topofile: for line in topofile: mininetNodeCli.runCmd("echo \'%s\' >> islands.py" % (line[:-1], )) mininetNodeCli.gotoMininetMode( "--controller=remote --ip=%s --custom islands.py --topo=islands,2,2,1" % controllerNode.ipAddress()) x = mininetNodeCli.runCmd("pingall") x = mininetNodeCli.runCmd("pingall") bigtest.Assert("Results: 66%" in x) env.endTest()
controller1 = env.node1() cli1 = controller1.cli() controller2 = env.node2() cli2 = controller2.cli() mininettopo = mininettopo.replace("CONTROLLER1_IP", controller1.ipAddress()) mininettopo = mininettopo.replace("CONTROLLER2_IP", controller2.ipAddress()) putTopoOnNode(controller1, mininettopo) cli1.gotoBashMode() cli1.runCmd("sudo python topo.py") log("Sleeping to allow the network to converge") sleep(20) o = cli1.runCmd("pingall") bigtest.Assert(re.search(" 0% dropped", o, re.MULTILINE)) # ping to the h3 which is being moved to have a flowmod to h3 cli1.runCmd("h7 ping -c 1 h3") log("Detaching h3 from s1") cli1.runCmd("detach h3 s1") #Add this sleep as the transition from s1 to s2 could be #a move from non-broadcast domain to bradcast domain sleep(5) log("Attaching h3 to s2 (controller 2)") cli1.runCmd("attach h3 s2") n = cli1.runCmd("net") # ping from h3 to island/cluster of h5 so that it is not a silent move
log = bigtest.log.info controller1 = env.node1() cli1 = controller1.cli() net = controller1.mininet(mininet.topolib.TreeTopo(depth=2)) switch1 = "00:00:00:00:00:00:00:05" switch2 = "00:00:00:00:00:00:00:06" switch3 = "00:00:00:00:00:00:00:07" switches = [switch1, switch2, switch3] controller1.waitForSwitches(switches) net.pingAll() switchesConnected = controller1.restGet("core/controller/switches/json", "dpid") log("Testing switches") for switch in switches: bigtest.Assert(switch in switchesConnected) controller1.waitForSwitchCluster(switches) log("Testing links") links = controller1.restGet("/topology/links/json") bigtest.Assert({ "src-switch": str(switch1), "dst-switch": str(switch2), "dst-port": 3, "src-port": 1, "type": "internal" }) in links bigtest.Assert({ "src-switch": str(switch2), "dst-switch": str(switch1), "dst-port": 1,
# In theory this doesn't do anything c1.start() c2.start() print "*** Starting Switches" s1.start([c1]) s2.start([c2]) s3.start([c2]) s4.start([c1]) return net env = bigtest.controller.TwoNodeTest() log = bigtest.log.info controller1 = env.node1() cli1 = controller1.cli() controller2 = env.node2() cli2 = controller2.cli() net = MultiControllerNet(controller1.ipAddress(), controller2.ipAddress()) sleep(20) ## net.pingAll() returns percentage drop so the bigtest.Assert(is to make sure 0% dropped) o = net.pingAll() bigtest.Assert(o == 0) net.stop() env.endTest()
mininetNodeCli.runCmd("rm -f square.py") with open("bigtest/square.py", "r") as topofile: for line in topofile: mininetNodeCli.runCmd("echo \'%s\' >> square.py" % (line[:-1], )) # Startup the controller with the custom square topology mininetNodeCli.gotoMininetMode( "--controller=remote --ip=%s --custom square.py --topo=square" % controllerNode.ipAddress()) # Wait for network to converge (An attempt to mitigate unreliable results, see above) time.sleep(5) # Ping between the hosts to establish some temporary flows x = mininetNodeCli.runCmd("h5 ping -c1 h6") bigtest.Assert(" 0% packet loss" in x) # Verify that the traffic is using a direct route between switches 1,3 command = "http://%s:8080/wm/core/switch/all/flow/json" % controllerNode.ipAddress( ) x = urllib.urlopen(command).read() # Only switches 1,3 should contain flows bigtest.Assert(not "\"00:00:00:00:00:00:00:01\":[]" in x and not "\"00:00:00:00:00:00:00:03\":[]" in x and "\"00:00:00:00:00:00:00:02\":[]" in x and "\"00:00:00:00:00:00:00:04\":[]" in x) # Bring down the link between s1 and s3, triggering port down reconciliation x = mininetNodeCli.runCmd("link s1 s3 down") #Wait until any invalid flows are removed (Output Port 2 involves inter switch links, which should be deleted)
log = bigtest.log.info controller1 = env.node1() cli1 = controller1.cli() controller2 = env.node2() cli2 = controller2.cli() # Restart mininet with the same mac to simulate a real # switch disconnect/connect, verify pingall succeeds cli1.runCmd("show version") cli1.gotoBashMode() cli1.runCmd("uptime") dpids = ["00:00:00:00:00:00:00:%02x" % x for x in [9, 10, 11, 12, 13, 14, 15]] cli2.gotoMininetMode("--controller=remote --ip=%s --mac --topo=tree,3" % controller1.ipAddress()) controller1.waitForSwitchCluster(dpids) x = cli2.runCmd("pingall") bigtest.Assert("Results: 0%" in x) cli2.runCmd("exit") log("restart mininet and verify pingall") cli2.gotoMininetMode("--controller=remote --ip=%s --mac --topo=tree,3" % controller1.ipAddress()) controller1.waitForSwitchCluster(dpids) x = cli2.runCmd("pingall") bigtest.Assert("Results: 0%" in x) cli2.runCmd("exit") env.endTest()
hosti = "h%s" % i enableMininetHostICMPBroadcast(mininetCli, hosti) # cleanup all rules command = "http://%s:8080/wm/firewall/rules/json" % controllerIp x = urllib.urlopen(command).read() parsedResult = json.loads(x) for i in range(len(parsedResult)): params = "{\"ruleid\":\"%s\"}" % parsedResult[i]['ruleid'] command = "/wm/firewall/rules/json" url = "%s:8080" % controllerIp connection = httplib.HTTPConnection(url) connection.request("DELETE", command, params) x = connection.getresponse().read() bigtest.Assert("Rule deleted" in x) # sleep to time out previous flows in switches time.sleep(5) # Test REST rules, empty command = "http://%s:8080/wm/firewall/rules/json" % controllerIp x = urllib.urlopen(command).read() bigtest.Assert("[]" in x) # Test REST disable command = "http://%s:8080/wm/firewall/module/disable/json" % controllerIp x = urllib.urlopen(command).read() bigtest.Assert("stopped" in x) # sleep to time out previous flows in switches time.sleep(5)