admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | ''' |
| 3 | Created on 26-Oct-2012 |
| 4 | |
| 5 | @author: Anil Kumar (anilkumar.s@paxterrasolutions.com) |
| 6 | |
| 7 | |
| 8 | TestON is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 2 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | TestON is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | |
| 22 | MininetCliDriver is the basic driver which will handle the Mininet functions |
| 23 | ''' |
| 24 | |
| 25 | import pexpect |
| 26 | import struct |
| 27 | import fcntl |
| 28 | import os |
| 29 | import signal |
| 30 | import re |
| 31 | import sys |
| 32 | import core.teston |
| 33 | sys.path.append("../") |
| 34 | from drivers.common.cli.emulatordriver import Emulator |
| 35 | from drivers.common.clidriver import CLI |
| 36 | |
| 37 | class MininetCliDriver(Emulator): |
| 38 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 39 | MininetCliDriver is the basic driver which will handle the Mininet functions |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 40 | ''' |
| 41 | def __init__(self): |
| 42 | super(Emulator, self).__init__() |
| 43 | self.handle = self |
| 44 | self.wrapped = sys.modules[__name__] |
| 45 | self.flag = 0 |
| 46 | |
| 47 | def connect(self, **connectargs): |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 48 | ''' |
| 49 | Here the main is the TestON instance after creating all the log handles. |
| 50 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 51 | for key in connectargs: |
| 52 | vars(self)[key] = connectargs[key] |
| 53 | |
| 54 | self.name = self.options['name'] |
| 55 | self.handle = super(MininetCliDriver, self).connect(user_name = self.user_name, ip_address = self.ip_address,port = None, pwd = self.pwd) |
| 56 | |
| 57 | self.ssh_handle = self.handle |
| 58 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 59 | if self.handle : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 60 | main.log.info(self.name+": Clearing any residual state or processes") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 61 | self.handle.sendline("sudo mn -c") |
admin | f939f8b | 2014-04-03 17:22:56 -0700 | [diff] [blame] | 62 | i=self.handle.expect(['password\sfor\s','Cleanup\scomplete',pexpect.EOF,pexpect.TIMEOUT],120) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 63 | if i==0: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 64 | main.log.info(self.name+": Sending sudo password") |
admin | f939f8b | 2014-04-03 17:22:56 -0700 | [diff] [blame] | 65 | self.handle.sendline(self.pwd) |
| 66 | i=self.handle.expect(['%s:'%(self.user),'\$',pexpect.EOF,pexpect.TIMEOUT],120) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 67 | if i==1: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 68 | main.log.info(self.name+": Clean") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 69 | elif i==2: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 70 | main.log.error(self.name+": Connection terminated") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 71 | elif i==3: #timeout |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 72 | main.log.error(self.name+": Something while cleaning MN took too long... " ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 73 | |
| 74 | #cmdString = "sudo mn --topo "+self.options['topo']+","+self.options['topocount']+" --mac --switch "+self.options['switch']+" --controller "+self.options['controller'] |
| 75 | #cmdString = "sudo mn --custom ~/mininet/custom/topo-2sw-2host.py --controller remote --ip 192.168.56.102 --port 6633 --topo mytopo" |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 76 | main.log.info(self.name+": building fresh mininet") |
admin | beea003 | 2014-01-23 14:54:13 -0800 | [diff] [blame] | 77 | #### for reactive/PARP enabled tests |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 78 | cmdString = "sudo mn " + self.options['arg1'] + " " + self.options['arg2'] + " --mac --controller " + self.options['controller'] |
admin | beea003 | 2014-01-23 14:54:13 -0800 | [diff] [blame] | 79 | #### for proactive flow with static ARP entries |
| 80 | #cmdString = "sudo mn " + self.options['arg1'] + " " + self.options['arg2'] + " --mac --arp --controller " + self.options['controller'] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 81 | #resultCommand = self.execute(cmd=cmdString,prompt='mininet>',timeout=120) |
| 82 | self.handle.sendline(cmdString) |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 83 | self.handle.expect(["sudo mn",pexpect.EOF,pexpect.TIMEOUT]) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 84 | while 1: |
| 85 | i=self.handle.expect(['mininet>','\*\*\*','Exception',pexpect.EOF,pexpect.TIMEOUT],300) |
| 86 | if i==0: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 87 | main.log.info(self.name+": mininet built") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 88 | return main.TRUE |
| 89 | if i==1: |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 90 | self.handle.expect(["\n",pexpect.EOF,pexpect.TIMEOUT]) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 91 | main.log.info(self.handle.before) |
| 92 | elif i==2: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 93 | main.log.error(self.name+": Launching mininet failed...") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 94 | return main.FALSE |
| 95 | elif i==3: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 96 | main.log.error(self.name+": Connection timeout") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 97 | return main.FALSE |
| 98 | elif i==4: #timeout |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 99 | main.log.error(self.name+": Something took too long... " ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 100 | return main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 101 | #if utilities.assert_matches(expect=patterns,actual=resultCommand,onpass="Network is being launched",onfail="Network launching is being failed "): |
| 102 | return main.TRUE |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 103 | else:#if no handle |
| 104 | main.log.error(self.name+": Connection failed to the host "+self.user_name+"@"+self.ip_address) |
| 105 | main.log.error(self.name+": Failed to connect to the Mininet") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 106 | return main.FALSE |
| 107 | |
| 108 | def pingall(self): |
| 109 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 110 | Verifies the reachability of the hosts using pingall command. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 111 | ''' |
| 112 | if self.handle : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 113 | main.log.info(self.name+": Checking reachabilty to the hosts using pingall") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 114 | response = self.execute(cmd="pingall",prompt="mininet>",timeout=10) |
| 115 | pattern = 'Results\:\s0\%\sdropped\s\(0\/\d+\slost\)\s*$' |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 116 | #if utilities.assert_matches(expect=pattern,actual=response,onpass="All hosts are reaching",onfail="Unable to reach all the hosts"): |
| 117 | if re.search(pattern,response): |
| 118 | main.log.info(self.name+": All hosts are reachable") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 119 | return main.TRUE |
| 120 | else: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 121 | main.log.error(self.name+": Unable to reach all the hosts") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 122 | return main.FALSE |
| 123 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 124 | main.log.error(self.name+": Connection failed to the host") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 125 | return main.FALSE |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 126 | |
| 127 | def fpingHost(self,**pingParams): |
| 128 | ''' |
| 129 | Uses the fping package for faster pinging... |
| 130 | *requires fping to be installed on machine running mininet |
| 131 | ''' |
| 132 | args = utilities.parse_args(["SRC","TARGET"],**pingParams) |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 133 | command = args["SRC"] + " fping -i 100 -t 20 -C 1 -q "+args["TARGET"] |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 134 | self.handle.sendline(command) |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 135 | self.handle.expect([args["TARGET"],pexpect.EOF,pexpect.TIMEOUT]) |
| 136 | self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT]) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 137 | response = self.handle.before |
| 138 | if re.search(":\s-" ,response): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 139 | main.log.info(self.name+": Ping fail") |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 140 | return main.FALSE |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 141 | elif re.search(":\s\d{1,2}\.\d\d", response): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 142 | main.log.info(self.name+": Ping good!") |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 143 | return main.TRUE |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 144 | main.log.info(self.name+": Install fping on mininet machine... ") |
| 145 | main.log.info(self.name+": \n---\n"+response) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 146 | return main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 147 | |
| 148 | def pingHost(self,**pingParams): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 149 | ''' |
| 150 | Ping from one mininet host to another |
| 151 | Currently the only supported Params: SRC and TARGET |
| 152 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 153 | args = utilities.parse_args(["SRC","TARGET"],**pingParams) |
| 154 | #command = args["SRC"] + " ping -" + args["CONTROLLER"] + " " +args ["TARGET"] |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 155 | command = args["SRC"] + " ping "+args ["TARGET"]+" -c 1 -i 1" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 156 | response = self.execute(cmd=command,prompt="mininet",timeout=10 ) |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 157 | main.log.info(self.name+": Ping Response: "+ response ) |
| 158 | #if utilities.assert_matches(expect=',\s0\%\spacket\sloss',actual=response,onpass="No Packet loss",onfail="Host is not reachable"): |
| 159 | if re.search(',\s0\%\spacket\sloss',response): |
| 160 | main.log.info(self.name+": NO PACKET LOSS, HOST IS REACHABLE") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 161 | main.last_result = main.TRUE |
| 162 | return main.TRUE |
| 163 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 164 | main.log.error(self.name+": PACKET LOST, HOST IS NOT REACHABLE") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 165 | main.last_result = main.FALSE |
| 166 | return main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 167 | |
| 168 | def checkIP(self,host): |
| 169 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 170 | Verifies the host's ip configured or not. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 171 | ''' |
| 172 | if self.handle : |
| 173 | response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10) |
| 174 | |
| 175 | pattern = "inet\s(addr|Mask):([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2})" |
| 176 | #pattern = "inet\saddr:10.0.0.6" |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 177 | #if utilities.assert_matches(expect=pattern,actual=response,onpass="Host Ip configured properly",onfail="Host IP not found") : |
| 178 | if re.search(pattern,response): |
| 179 | main.log.info(self.name+": Host Ip configured properly") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 180 | return main.TRUE |
| 181 | else: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 182 | main.log.error(self.name+": Host IP not found") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 183 | return main.FALSE |
| 184 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 185 | main.log.error(self.name+": Connection failed to the host") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 186 | |
| 187 | def verifySSH(self,**connectargs): |
| 188 | response = self.execute(cmd="h1 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10) |
| 189 | response = self.execute(cmd="h4 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10) |
| 190 | for key in connectargs: |
| 191 | vars(self)[key] = connectargs[key] |
| 192 | response = self.execute(cmd="xterm h1 h4 ",prompt="mininet>",timeout=10) |
| 193 | import time |
| 194 | time.sleep(20) |
| 195 | if self.flag == 0: |
| 196 | self.flag = 1 |
| 197 | return main.FALSE |
| 198 | else : |
| 199 | return main.TRUE |
| 200 | |
| 201 | def getMacAddress(self,host): |
| 202 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 203 | Verifies the host's ip configured or not. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 204 | ''' |
| 205 | if self.handle : |
| 206 | response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10) |
| 207 | |
Ahmed El-Hassany | f720e20 | 2014-04-04 16:11:36 -0700 | [diff] [blame] | 208 | pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})' |
| 209 | mac_address_search = re.search(pattern, response, re.I) |
| 210 | mac_address = mac_address_search.group().split(" ")[1] |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 211 | main.log.info(self.name+": Mac-Address of Host "+ host + " is " + mac_address) |
Ahmed El-Hassany | f720e20 | 2014-04-04 16:11:36 -0700 | [diff] [blame] | 212 | return mac_address |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 213 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 214 | main.log.error(self.name+": Connection failed to the host") |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 215 | |
| 216 | def getInterfaceMACAddress(self,host, interface): |
| 217 | ''' |
| 218 | Return the IP address of the interface on the given host |
| 219 | ''' |
| 220 | if self.handle : |
| 221 | response = self.execute(cmd=host+" ifconfig " + interface, |
| 222 | prompt="mininet>",timeout=10) |
| 223 | |
| 224 | pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})' |
| 225 | mac_address_search = re.search(pattern, response, re.I) |
| 226 | if mac_address_search is None: |
| 227 | main.log.info("No mac address found in %s" % response) |
| 228 | return main.FALSE |
| 229 | mac_address = mac_address_search.group().split(" ")[1] |
| 230 | main.log.info("Mac-Address of "+ host + ":"+ interface + " is " + mac_address) |
| 231 | return mac_address |
| 232 | else: |
| 233 | main.log.error("Connection failed to the host") |
| 234 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 235 | def getIPAddress(self,host): |
| 236 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 237 | Verifies the host's ip configured or not. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 238 | ''' |
| 239 | if self.handle : |
| 240 | response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10) |
| 241 | |
| 242 | pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)" |
| 243 | ip_address_search = re.search(pattern, response) |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 244 | main.log.info(self.name+": IP-Address of Host "+host +" is "+ip_address_search.group(1)) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 245 | return ip_address_search.group(1) |
| 246 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 247 | main.log.error(self.name+": Connection failed to the host") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 248 | |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 249 | def getSwitchDPID(self,switch): |
| 250 | ''' |
| 251 | return the datapath ID of the switch |
| 252 | ''' |
| 253 | if self.handle : |
| 254 | cmd = "py %s.dpid" % switch |
| 255 | response = self.execute(cmd=cmd,prompt="mininet>",timeout=10) |
| 256 | pattern = r'^(?P<dpid>\d)+' |
| 257 | result = re.search(pattern, response, re.MULTILINE) |
| 258 | if result is None: |
| 259 | main.log.info("Couldn't find DPID for switch '', found: %s" % (switch, response)) |
| 260 | return main.FALSE |
| 261 | return int(result.group('dpid')) |
| 262 | else: |
| 263 | main.log.error("Connection failed to the host") |
| 264 | |
| 265 | def getInterfaces(self, node): |
| 266 | ''' |
| 267 | return information dict about interfaces connected to the node |
| 268 | ''' |
| 269 | if self.handle : |
| 270 | cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,isUp=%s" % (i.name, i.MAC(), i.IP(), i.isUp())' |
| 271 | cmd += ' for i in %s.intfs.values()])' % node |
| 272 | response = self.execute(cmd=cmd,prompt="mininet>",timeout=10) |
| 273 | return response |
| 274 | else: |
| 275 | main.log.error("Connection failed to the node") |
| 276 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 277 | def dump(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 278 | main.log.info(self.name+": Dump node info") |
Ahmed El-Hassany | d1f7170 | 2014-04-04 16:12:45 -0700 | [diff] [blame] | 279 | response = self.execute(cmd = 'dump',prompt = 'mininet>',timeout = 10) |
| 280 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 281 | |
| 282 | def intfs(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 283 | main.log.info(self.name+": List interfaces") |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 284 | response = self.execute(cmd = 'intfs',prompt = 'mininet>',timeout = 10) |
| 285 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 286 | |
| 287 | def net(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 288 | main.log.info(self.name+": List network connections") |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 289 | response = self.execute(cmd = 'net',prompt = 'mininet>',timeout = 10) |
| 290 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 291 | |
| 292 | def iperf(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 293 | main.log.info(self.name+": Simple iperf TCP test between two (optionally specified) hosts") |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 294 | response = self.execute(cmd = 'iperf',prompt = 'mininet>',timeout = 10) |
| 295 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 296 | |
| 297 | def iperfudp(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 298 | main.log.info(self.name+": Simple iperf TCP test between two (optionally specified) hosts") |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 299 | response = self.execute(cmd = 'iperfudp',prompt = 'mininet>',timeout = 10) |
| 300 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 301 | |
| 302 | def nodes(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 303 | main.log.info(self.name+": List all nodes.") |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 304 | response = self.execute(cmd = 'nodes',prompt = 'mininet>',timeout = 10) |
| 305 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 306 | |
| 307 | def pingpair(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 308 | main.log.info(self.name+": Ping between first two hosts") |
| 309 | response = self.execute(cmd = 'pingpair',prompt = 'mininet>',timeout = 20) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 310 | |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 311 | #if utilities.assert_matches(expect='0% packet loss',actual=response,onpass="No Packet loss",onfail="Hosts not reachable"): |
| 312 | if re.search(',\s0\%\spacket\sloss',response): |
| 313 | main.log.info(self.name+": Ping between two hosts SUCCESSFUL") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 314 | main.last_result = main.TRUE |
| 315 | return main.TRUE |
| 316 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 317 | main.log.error(self.name+": PACKET LOST, HOSTS NOT REACHABLE") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 318 | main.last_result = main.FALSE |
| 319 | return main.FALSE |
| 320 | |
| 321 | def link(self,**linkargs): |
| 322 | ''' |
| 323 | Bring link(s) between two nodes up or down |
| 324 | ''' |
| 325 | main.log.info('Bring link(s) between two nodes up or down') |
| 326 | args = utilities.parse_args(["END1","END2","OPTION"],**linkargs) |
| 327 | end1 = args["END1"] if args["END1"] != None else "" |
| 328 | end2 = args["END2"] if args["END2"] != None else "" |
| 329 | option = args["OPTION"] if args["OPTION"] != None else "" |
| 330 | command = "link "+str(end1) + " " + str(end2)+ " " + str(option) |
| 331 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 332 | return main.TRUE |
| 333 | |
| 334 | |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 335 | def yank(self,**yankargs): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 336 | ''' |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 337 | yank a mininet switch interface to a host |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 338 | ''' |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 339 | main.log.info('Yank the switch interface attached to a host') |
| 340 | args = utilities.parse_args(["SW","INTF"],**yankargs) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 341 | sw = args["SW"] if args["SW"] !=None else "" |
| 342 | intf = args["INTF"] if args["INTF"] != None else "" |
| 343 | command = "py "+ str(sw) + '.detach("' + str(intf) + '")' |
| 344 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 345 | return main.TRUE |
| 346 | |
| 347 | def plug(self, **plugargs): |
| 348 | ''' |
| 349 | plug the yanked mininet switch interface to a switch |
| 350 | ''' |
| 351 | main.log.info('Plug the switch interface attached to a switch') |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 352 | args = utilities.parse_args(["SW","INTF"],**plugargs) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 353 | sw = args["SW"] if args["SW"] !=None else "" |
| 354 | intf = args["INTF"] if args["INTF"] != None else "" |
| 355 | command = "py "+ str(sw) + '.attach("' + str(intf) + '")' |
| 356 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 357 | return main.TRUE |
| 358 | |
| 359 | |
| 360 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 361 | def dpctl(self,**dpctlargs): |
| 362 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 363 | Run dpctl command on all switches. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 364 | ''' |
| 365 | main.log.info('Run dpctl command on all switches') |
| 366 | args = utilities.parse_args(["CMD","ARGS"],**dpctlargs) |
| 367 | cmd = args["CMD"] if args["CMD"] != None else "" |
| 368 | cmdargs = args["ARGS"] if args["ARGS"] != None else "" |
| 369 | command = "dpctl "+cmd + " " + str(cmdargs) |
| 370 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 371 | return main.TRUE |
| 372 | |
| 373 | |
| 374 | def get_version(self): |
| 375 | file_input = path+'/lib/Mininet/INSTALL' |
| 376 | version = super(Mininet, self).get_version() |
| 377 | pattern = 'Mininet\s\w\.\w\.\w\w*' |
| 378 | for line in open(file_input,'r').readlines(): |
| 379 | result = re.match(pattern, line) |
| 380 | if result: |
| 381 | version = result.group(0) |
| 382 | return version |
| 383 | |
| 384 | def get_sw_controller(self,sw): |
| 385 | command = "sh ovs-vsctl get-controller "+str(sw) |
| 386 | main.log.info(self.execute(cmd=command,prompt="mininet>",timeout=10)) |
| 387 | |
| 388 | def assign_sw_controller(self,**kwargs): |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 389 | ''' |
| 390 | count is only needed if there is more than 1 controller |
| 391 | ''' |
| 392 | args = utilities.parse_args(["COUNT"],**kwargs) |
| 393 | count = args["COUNT"] if args!={} else 1 |
| 394 | |
| 395 | argstring = "SW" |
| 396 | for j in range(count): |
| 397 | argstring = argstring + ",IP" + str(j+1) + ",PORT" + str(j+1) |
| 398 | args = utilities.parse_args(argstring.split(","),**kwargs) |
| 399 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 400 | sw = args["SW"] if args["SW"] != None else "" |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 401 | ptcpA = int(args["PORT1"])+int(sw) if args["PORT1"] != None else "" |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 402 | ptcpB = "ptcp:"+str(ptcpA) if ptcpA != "" else "" |
| 403 | |
| 404 | command = "sh ovs-vsctl set-controller s" + str(sw) + " " + ptcpB + " " |
| 405 | for j in range(count): |
| 406 | i=j+1 |
| 407 | args = utilities.parse_args(["IP"+str(i),"PORT"+str(i)],**kwargs) |
| 408 | ip = args["IP"+str(i)] if args["IP"+str(i)] != None else "" |
| 409 | port = args["PORT" + str(i)] if args["PORT" + str(i)] != None else "" |
| 410 | tcp = "tcp:" + str(ip) + ":" + str(port) + " " if ip != "" else "" |
| 411 | command = command + tcp |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 412 | self.execute(cmd=command,prompt="mininet>",timeout=5) |
| 413 | |
| 414 | def disconnect(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 415 | main.log.info(self.name+": Disconnecting mininet...") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 416 | response = '' |
| 417 | if self.handle: |
| 418 | response = self.execute(cmd="exit",prompt="(.*)",timeout=120) |
| 419 | response = self.execute(cmd="exit",prompt="(.*)",timeout=120) |
| 420 | |
| 421 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 422 | main.log.error(self.name+": Connection failed to the host") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 423 | response = main.FALSE |
| 424 | return response |
| 425 | |
| 426 | def ctrl_none(self): |
| 427 | #self.execute(cmd="sh ~/ONOS/scripts/test-ctrl-none.sh", prompt="mininet",timeout=20) |
| 428 | self.handle.sendline() |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 429 | self.handle.expect(["mininet>",pexpect.EOF,pexpect.TIMEOUT]) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 430 | self.handle.sendline("sh ~/ONOS/scripts/test-ctrl-none.sh") |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 431 | self.handle.expect(["test-ctrl-none",pexpect.EOF,pexpect.TIMEOUT]) |
| 432 | self.handle.expect(["mininet>",pexpect.EOF,pexpect.TIMEOUT], 20) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 433 | |
| 434 | def ctrl_all(self): |
| 435 | self.execute(cmd="sh ~/ONOS/scripts/test-ctrl-add-ext.sh", prompt="mininet",timeout=20) |
| 436 | |
| 437 | def ctrl_divide(self): |
| 438 | self.execute(cmd="sh ~/ONOS/scripts/ctrl-divide.sh ", prompt="mininet",timeout=20) |
| 439 | |
| 440 | def ctrl_local(self): |
| 441 | self.execute(cmd="sh ~/ONOS/scripts/test-ctrl-local.sh ", prompt="mininet",timeout=20) |
| 442 | |
| 443 | def ctrl_one(self, ip): |
| 444 | self.execute(cmd="sh ~/ONOS/scripts/ctrl-one.sh "+ip, prompt="mininet",timeout=20) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 445 | |
| 446 | def arping(self, src, dest, destmac): |
| 447 | self.handle.sendline('') |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 448 | self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT]) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 449 | |
| 450 | self.handle.sendline(src + ' arping ' + dest) |
| 451 | try: |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 452 | self.handle.expect([destmac,pexpect.EOF,pexpect.TIMEOUT]) |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 453 | main.log.info(self.name+": ARP successful") |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 454 | self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT]) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 455 | return main.TRUE |
| 456 | except: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 457 | main.log.warn(self.name+": ARP FAILURE") |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 458 | self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT]) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 459 | return main.FALSE |
| 460 | |
| 461 | def decToHex(num): |
| 462 | return hex(num).split('x')[1] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 463 | |
| 464 | if __name__ != "__main__": |
| 465 | import sys |
| 466 | sys.modules[__name__] = MininetCliDriver() |