kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1 | """ |
| 2 | Driver for blank dataplane VMs. Created for SDNIP test. |
| 3 | """ |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 4 | import pexpect |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 5 | import sys |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 6 | sys.path.append( "../" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 7 | from drivers.common.clidriver import CLI |
| 8 | |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 9 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 10 | class DPCliDriver( CLI ): |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 11 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 12 | def __init__( self ): |
| 13 | super( CLI, self ).__init__() |
| 14 | |
| 15 | def connect( self, **connectargs ): |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 16 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 17 | vars( self )[ key ] = connectargs[ key ] |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 18 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 19 | self.name = self.options[ 'name' ] |
| 20 | self.handle = super( DPCliDriver, self ).connect( user_name=self.user_name, |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 21 | ip_address=self.ip_address, |
| 22 | port=self.port, |
| 23 | pwd=self.pwd ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 24 | |
| 25 | if self.handle: |
| 26 | return self.handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 27 | else: |
| 28 | main.log.info( "NO HANDLE" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 29 | return main.FALSE |
| 30 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 31 | def create_interfaces( self, net, number, start ): |
| 32 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 33 | Creates a number,specified by 'number,' of subinterfaces on eth0. |
| 34 | Ip addresses start at 'net'.'start'.1.1 with a 24 bit netmask. |
| 35 | Addresses increment sequentially in the third quad, therefore all |
| 36 | interfaces are in different subnets on the same machine. When the |
| 37 | third quad reaches 255, it is reset to 1 and the second quad is |
| 38 | incremented. Every single ip address is placed in a file in /tmp |
| 39 | titled 'ip_table{net}.txt'. The file is used by 'pingall_interfaces()' |
| 40 | as a fping argument |
| 41 | |
| 42 | This method returns true if all interfaces are created without a hitch, |
| 43 | and false if a single interface has issues |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 44 | """ |
| 45 | self.handle.sendline( "" ) |
| 46 | self.handle.expect( "\$" ) |
SeanCorcoran | f580d10 | 2013-09-27 15:13:21 -0700 | [diff] [blame] | 47 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 48 | self.handle.sendline( "rm /tmp/local_ip.txt" ) |
| 49 | self.handle.expect( "\$" ) |
| 50 | self.handle.sendline( "touch /tmp/local_ip.txt" ) |
| 51 | self.handle.expect( "\$" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 52 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 53 | main.log.info( "Creating interfaces" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 54 | k = 0 |
| 55 | intf = 0 |
| 56 | while number != 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 57 | k = k + 1 |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 58 | if k == 256: |
| 59 | k = 1 |
| 60 | start = start + 1 |
| 61 | number = number - 1 |
| 62 | intf = intf + 1 |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 63 | ip = net + "." + str( start ) + "." + str( k ) + ".1" |
| 64 | self.handle.sendline( |
| 65 | "sudo ifconfig eth0:" + str( |
| 66 | intf ) + " " + ip + " netmask 255.255.255.0" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 67 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 68 | i = self.handle.expect( [ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 69 | "\$", |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 70 | "password", |
| 71 | pexpect.TIMEOUT, |
| 72 | pexpect.EOF ], |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 73 | timeout=120 ) |
SeanCorcoran | f580d10 | 2013-09-27 15:13:21 -0700 | [diff] [blame] | 74 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 75 | if i == 0: |
| 76 | self.handle.sendline( |
| 77 | "echo " + str( ip ) + " >> /tmp/local_ip.txt" ) |
| 78 | self.handle.expect( "\$" ) |
| 79 | elif i == 1: |
| 80 | main.log.info( "Sending sudo password" ) |
| 81 | self.handle.sendline( self.pwd ) |
| 82 | self.handle.expect( "\$" ) |
| 83 | else: |
| 84 | main.log.error( "INTERFACES NOT CREATED" ) |
| 85 | return main.FALSE |
SeanCorcoran | f580d10 | 2013-09-27 15:13:21 -0700 | [diff] [blame] | 86 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 87 | def pingall_interfaces( self, netsrc, netstrt, netdst, destlogin, destip ): |
| 88 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 89 | Copies the /tmp/ip_table{ net }.txt file from the machine you wish to |
| 90 | ping, then runs fping with a source address of |
| 91 | { netsrc }.{ netstrt }.1.1 on the copied file. |
| 92 | Check every single response for reachable or unreachable. If all are |
| 93 | reachable, function returns true. If a SINGLE host is unreachable, |
| 94 | then the function stops and returns false. If fping is not installed, |
| 95 | this function will install fping then run the same command |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 96 | """ |
| 97 | self.handle.sendline( "" ) |
| 98 | self.handle.expect( "\$" ) |
SeanCorcoran | f580d10 | 2013-09-27 15:13:21 -0700 | [diff] [blame] | 99 | |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 100 | self.handle.sendline( "scp " + str( destlogin ) + "@" + |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 101 | str( destip ) + |
| 102 | ":/tmp/local_ip.txt /tmp/ip_table" + |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 103 | str( netsrc ) + ".txt" ) |
| 104 | |
| 105 | i = self.handle.expect( [ |
| 106 | "100%", |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 107 | "password", |
| 108 | pexpect.TIMEOUT ], |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 109 | timeout=30 ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 110 | |
SeanCorcoran | f580d10 | 2013-09-27 15:13:21 -0700 | [diff] [blame] | 111 | if i == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 112 | main.log.info( "Copied ping file successfully" ) |
SeanCorcoran | f580d10 | 2013-09-27 15:13:21 -0700 | [diff] [blame] | 113 | elif i == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 114 | self.handle.sendline( self.pwd ) |
| 115 | self.handle.expect( "100%" ) |
| 116 | main.log.info( "Copied ping file successfully" ) |
SeanCorcoran | f580d10 | 2013-09-27 15:13:21 -0700 | [diff] [blame] | 117 | elif i == 2: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 118 | main.log.error( "COULD NOT COPY PING FILE FROM " + str( destip ) ) |
SeanCorcoran | f580d10 | 2013-09-27 15:13:21 -0700 | [diff] [blame] | 119 | result = main.FALSE |
| 120 | return result |
SeanCorcoran | f580d10 | 2013-09-27 15:13:21 -0700 | [diff] [blame] | 121 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 122 | self.handle.sendline( "" ) |
| 123 | self.handle.expect( "\$" ) |
| 124 | |
| 125 | main.log.info( "Pinging interfaces on the " + str( netdst ) + |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 126 | " network from " + str( netsrc ) + "." + |
| 127 | str( netstrt ) + ".1.1" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 128 | self.handle.sendline( "sudo fping -S " + str( netsrc ) + "." + |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 129 | str( netstrt ) + ".1.1 -f /tmp/ip_table" + |
| 130 | str( netdst ) + ".txt" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 131 | while 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 132 | i = self.handle.expect( [ |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 133 | "reachable", |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 134 | "unreachable", |
| 135 | "\$", |
| 136 | "password", |
| 137 | pexpect.TIMEOUT, |
| 138 | "not installed" ], |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 139 | timeout=45 ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 140 | if i == 0: |
| 141 | result = main.TRUE |
| 142 | elif i == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 143 | main.log.error( "An interface was unreachable" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 144 | result = main.FALSE |
| 145 | return result |
| 146 | elif i == 2: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 147 | main.log.info( "All interfaces reachable" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 148 | result = main.FALSE |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 149 | return result |
| 150 | elif i == 3: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 151 | self.handle.sendline( self.pwd ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 152 | elif i == 4: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 153 | main.log.error( "Unable to fping" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 154 | result = main.FALSE |
| 155 | return result |
| 156 | elif i == 5: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 157 | main.log.info( "fping not installed, installing fping" ) |
| 158 | self.handle.sendline( "sudo apt-get install fping" ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 159 | i = self.handle.expect( [ "password", |
| 160 | "\$", |
| 161 | pexpect.TIMEOUT ], |
| 162 | timeout=60 ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 163 | if i == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 164 | self.handle.sendline( self.pwd ) |
| 165 | self.handle.expect( "\$", timeout=30 ) |
| 166 | main.log.info( "fping installed, now pinging interfaces" ) |
| 167 | self.handle.sendline( |
| 168 | "sudo fping -S " + str( |
| 169 | netsrc ) + "." + str( |
| 170 | netstrt ) + ".1.1 -f /tmp/ip_table" + str( |
| 171 | netdst ) + ".txt" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 172 | elif i == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 173 | main.log.info( "fping installed, now pinging interfaces" ) |
| 174 | self.handle.sendline( |
| 175 | "sudo fping -S " + str( |
| 176 | netsrc ) + "." + str( |
| 177 | netstrt ) + ".1.1 -f /tmp/ip_table" + str( |
| 178 | netdst ) + ".txt" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 179 | elif i == 2: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 180 | main.log.error( "Could not install fping" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 181 | result = main.FALSE |
| 182 | return result |
| 183 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 184 | def disconnect( self ): |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 185 | response = '' |
| 186 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 187 | self.handle.sendline( "exit" ) |
| 188 | self.handle.expect( "closed" ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 189 | except pexpect.ExceptionPexpect: |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 190 | main.log.exception( "Connection failed to the host" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 191 | response = main.FALSE |
| 192 | return response |
| 193 | |