blob: ee26bdeadc12a4a14d4c27649e6ff32fe5e8a39a [file] [log] [blame]
kelvin8ec71442015-01-15 16:57:00 -08001"""
2Driver for blank dataplane VMs. Created for SDNIP test.
3"""
timlindbergef8d55d2013-09-27 12:50:13 -07004import pexpect
kelvin8ec71442015-01-15 16:57:00 -08005import sys
kelvin8ec71442015-01-15 16:57:00 -08006sys.path.append( "../" )
timlindbergef8d55d2013-09-27 12:50:13 -07007from drivers.common.clidriver import CLI
8
timlindbergef8d55d2013-09-27 12:50:13 -07009
kelvin8ec71442015-01-15 16:57:00 -080010class DPCliDriver( CLI ):
timlindbergef8d55d2013-09-27 12:50:13 -070011
kelvin8ec71442015-01-15 16:57:00 -080012 def __init__( self ):
13 super( CLI, self ).__init__()
14
15 def connect( self, **connectargs ):
timlindbergef8d55d2013-09-27 12:50:13 -070016 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080017 vars( self )[ key ] = connectargs[ key ]
timlindbergef8d55d2013-09-27 12:50:13 -070018
kelvin8ec71442015-01-15 16:57:00 -080019 self.name = self.options[ 'name' ]
20 self.handle = super( DPCliDriver, self ).connect( user_name=self.user_name,
21 ip_address=self.ip_address,
22 port=self.port,
23 pwd=self.pwd )
timlindbergef8d55d2013-09-27 12:50:13 -070024
25 if self.handle:
26 return self.handle
kelvin8ec71442015-01-15 16:57:00 -080027 else:
28 main.log.info( "NO HANDLE" )
timlindbergef8d55d2013-09-27 12:50:13 -070029 return main.FALSE
30
kelvin8ec71442015-01-15 16:57:00 -080031 def create_interfaces( self, net, number, start ):
32 """
SeanCorcoranf580d102013-09-27 15:13:21 -070033 Creates a number,specified by 'number,' of subinterfaces on eth0. Ip addresses start at 'net'.'start'.1.1 with a 24 bit netmask. Addresses increment sequentially in the third quad,
34 therefore all interfaces are in different subnets on the same machine. When the third quad reaches 255, it is reset to 1 and the second quad is incremented.
35 Every single ip address is placed in a file in /tmp titled 'ip_table{net}.txt'
36 The file is used by 'pingall_interfaces()' as a fping argument
37 This method returns true if all interfaces are created without a hitch, and false if a single interface has issues
kelvin8ec71442015-01-15 16:57:00 -080038 """
39 self.handle.sendline( "" )
40 self.handle.expect( "\$" )
SeanCorcoranf580d102013-09-27 15:13:21 -070041
kelvin8ec71442015-01-15 16:57:00 -080042 self.handle.sendline( "rm /tmp/local_ip.txt" )
43 self.handle.expect( "\$" )
44 self.handle.sendline( "touch /tmp/local_ip.txt" )
45 self.handle.expect( "\$" )
timlindbergef8d55d2013-09-27 12:50:13 -070046
kelvin8ec71442015-01-15 16:57:00 -080047 main.log.info( "Creating interfaces" )
timlindbergef8d55d2013-09-27 12:50:13 -070048 k = 0
49 intf = 0
50 while number != 0:
kelvin8ec71442015-01-15 16:57:00 -080051 k = k + 1
timlindbergef8d55d2013-09-27 12:50:13 -070052 if k == 256:
53 k = 1
54 start = start + 1
55 number = number - 1
56 intf = intf + 1
kelvin8ec71442015-01-15 16:57:00 -080057 ip = net + "." + str( start ) + "." + str( k ) + ".1"
58 self.handle.sendline(
59 "sudo ifconfig eth0:" + str(
60 intf ) + " " + ip + " netmask 255.255.255.0" )
timlindbergef8d55d2013-09-27 12:50:13 -070061
kelvin8ec71442015-01-15 16:57:00 -080062 i = self.handle.expect( [
63 "\$",
64 "password",
65 pexpect.TIMEOUT,
66 pexpect.EOF ],
67 timeout=120 )
SeanCorcoranf580d102013-09-27 15:13:21 -070068
kelvin8ec71442015-01-15 16:57:00 -080069 if i == 0:
70 self.handle.sendline(
71 "echo " + str( ip ) + " >> /tmp/local_ip.txt" )
72 self.handle.expect( "\$" )
73 elif i == 1:
74 main.log.info( "Sending sudo password" )
75 self.handle.sendline( self.pwd )
76 self.handle.expect( "\$" )
77 else:
78 main.log.error( "INTERFACES NOT CREATED" )
79 return main.FALSE
SeanCorcoranf580d102013-09-27 15:13:21 -070080
kelvin8ec71442015-01-15 16:57:00 -080081 def pingall_interfaces( self, netsrc, netstrt, netdst, destlogin, destip ):
82 """
83 Copies the /tmp/ip_table{ net }.txt file from the machine you wish to ping, then runs fping with a source address of { netsrc }.{ netstrt }.1.1 on the copied file.
SeanCorcoranf580d102013-09-27 15:13:21 -070084 Check every single response for reachable or unreachable. If all are reachable, function returns true. If a SINGLE host is unreachable, then the function stops and returns false
85 If fping is not installed, this function will install fping then run the same command
kelvin8ec71442015-01-15 16:57:00 -080086 """
87 self.handle.sendline( "" )
88 self.handle.expect( "\$" )
SeanCorcoranf580d102013-09-27 15:13:21 -070089
Jon Hallfebb1c72015-03-05 13:30:09 -080090 self.handle.sendline( "scp " + str( destlogin ) + "@" +
91 str( destip ) + ":/tmp/local_ip.txt /tmp/ip_table" +
92 str( netsrc ) + ".txt" )
93
94 i = self.handle.expect( [
95 "100%",
kelvin8ec71442015-01-15 16:57:00 -080096 "password",
97 pexpect.TIMEOUT ],
Jon Hallfebb1c72015-03-05 13:30:09 -080098 timeout=30 )
kelvin8ec71442015-01-15 16:57:00 -080099
SeanCorcoranf580d102013-09-27 15:13:21 -0700100 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800101 main.log.info( "Copied ping file successfully" )
SeanCorcoranf580d102013-09-27 15:13:21 -0700102 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800103 self.handle.sendline( self.pwd )
104 self.handle.expect( "100%" )
105 main.log.info( "Copied ping file successfully" )
SeanCorcoranf580d102013-09-27 15:13:21 -0700106 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800107 main.log.error( "COULD NOT COPY PING FILE FROM " + str( destip ) )
SeanCorcoranf580d102013-09-27 15:13:21 -0700108 result = main.FALSE
109 return result
SeanCorcoranf580d102013-09-27 15:13:21 -0700110
kelvin8ec71442015-01-15 16:57:00 -0800111 self.handle.sendline( "" )
112 self.handle.expect( "\$" )
113
114 main.log.info( "Pinging interfaces on the " + str( netdst ) +
Jon Hallfebb1c72015-03-05 13:30:09 -0800115 " network from " + str( netsrc ) + "." +
116 str( netstrt ) + ".1.1" )
kelvin8ec71442015-01-15 16:57:00 -0800117 self.handle.sendline( "sudo fping -S " + str( netsrc ) + "." +
Jon Hallfebb1c72015-03-05 13:30:09 -0800118 str( netstrt ) + ".1.1 -f /tmp/ip_table" +
119 str( netdst ) + ".txt" )
timlindbergef8d55d2013-09-27 12:50:13 -0700120 while 1:
kelvin8ec71442015-01-15 16:57:00 -0800121 i = self.handle.expect( [
Jon Hallfebb1c72015-03-05 13:30:09 -0800122 "reachable",
kelvin8ec71442015-01-15 16:57:00 -0800123 "unreachable",
124 "\$",
125 "password",
126 pexpect.TIMEOUT,
127 "not installed" ],
Jon Hallfebb1c72015-03-05 13:30:09 -0800128 timeout=45 )
timlindbergef8d55d2013-09-27 12:50:13 -0700129 if i == 0:
130 result = main.TRUE
131 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800132 main.log.error( "An interface was unreachable" )
timlindbergef8d55d2013-09-27 12:50:13 -0700133 result = main.FALSE
134 return result
135 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800136 main.log.info( "All interfaces reachable" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800137 result = main.FALSE
timlindbergef8d55d2013-09-27 12:50:13 -0700138 return result
139 elif i == 3:
kelvin8ec71442015-01-15 16:57:00 -0800140 self.handle.sendline( self.pwd )
timlindbergef8d55d2013-09-27 12:50:13 -0700141 elif i == 4:
kelvin8ec71442015-01-15 16:57:00 -0800142 main.log.error( "Unable to fping" )
timlindbergef8d55d2013-09-27 12:50:13 -0700143 result = main.FALSE
144 return result
145 elif i == 5:
kelvin8ec71442015-01-15 16:57:00 -0800146 main.log.info( "fping not installed, installing fping" )
147 self.handle.sendline( "sudo apt-get install fping" )
148 i = self.handle.expect(
149 [ "password",
150 "\$",
151 pexpect.TIMEOUT ],
152 timeout=60 )
timlindbergef8d55d2013-09-27 12:50:13 -0700153 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800154 self.handle.sendline( self.pwd )
155 self.handle.expect( "\$", timeout=30 )
156 main.log.info( "fping installed, now pinging interfaces" )
157 self.handle.sendline(
158 "sudo fping -S " + str(
159 netsrc ) + "." + str(
160 netstrt ) + ".1.1 -f /tmp/ip_table" + str(
161 netdst ) + ".txt" )
timlindbergef8d55d2013-09-27 12:50:13 -0700162 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800163 main.log.info( "fping installed, now pinging interfaces" )
164 self.handle.sendline(
165 "sudo fping -S " + str(
166 netsrc ) + "." + str(
167 netstrt ) + ".1.1 -f /tmp/ip_table" + str(
168 netdst ) + ".txt" )
timlindbergef8d55d2013-09-27 12:50:13 -0700169 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800170 main.log.error( "Could not install fping" )
timlindbergef8d55d2013-09-27 12:50:13 -0700171 result = main.FALSE
172 return result
173
kelvin8ec71442015-01-15 16:57:00 -0800174 def disconnect( self ):
timlindbergef8d55d2013-09-27 12:50:13 -0700175 response = ''
176 try:
kelvin8ec71442015-01-15 16:57:00 -0800177 self.handle.sendline( "exit" )
178 self.handle.expect( "closed" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800179 except Exception:
180 main.log.exception( "Connection failed to the host" )
timlindbergef8d55d2013-09-27 12:50:13 -0700181 response = main.FALSE
182 return response
183