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