blob: 23214b8914a779bba83ac33556bab5b7edb2c65f [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
timlindbergef8d55d2013-09-27 12:50:13 -07005from drivers.common.clidriver import CLI
6
timlindbergef8d55d2013-09-27 12:50:13 -07007
kelvin8ec71442015-01-15 16:57:00 -08008class DPCliDriver( CLI ):
timlindbergef8d55d2013-09-27 12:50:13 -07009
kelvin8ec71442015-01-15 16:57:00 -080010 def __init__( self ):
11 super( CLI, self ).__init__()
12
13 def connect( self, **connectargs ):
timlindbergef8d55d2013-09-27 12:50:13 -070014 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080015 vars( self )[ key ] = connectargs[ key ]
timlindbergef8d55d2013-09-27 12:50:13 -070016
kelvin8ec71442015-01-15 16:57:00 -080017 self.name = self.options[ 'name' ]
18 self.handle = super( DPCliDriver, self ).connect( user_name=self.user_name,
Jon Hallefbd9792015-03-05 16:11:36 -080019 ip_address=self.ip_address,
20 port=self.port,
21 pwd=self.pwd )
timlindbergef8d55d2013-09-27 12:50:13 -070022
23 if self.handle:
24 return self.handle
kelvin8ec71442015-01-15 16:57:00 -080025 else:
26 main.log.info( "NO HANDLE" )
timlindbergef8d55d2013-09-27 12:50:13 -070027 return main.FALSE
28
kelvin8ec71442015-01-15 16:57:00 -080029 def create_interfaces( self, net, number, start ):
30 """
Jon Hallefbd9792015-03-05 16:11:36 -080031 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
kelvin8ec71442015-01-15 16:57:00 -080042 """
43 self.handle.sendline( "" )
44 self.handle.expect( "\$" )
SeanCorcoranf580d102013-09-27 15:13:21 -070045
kelvin8ec71442015-01-15 16:57:00 -080046 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( "\$" )
timlindbergef8d55d2013-09-27 12:50:13 -070050
kelvin8ec71442015-01-15 16:57:00 -080051 main.log.info( "Creating interfaces" )
timlindbergef8d55d2013-09-27 12:50:13 -070052 k = 0
53 intf = 0
54 while number != 0:
kelvin8ec71442015-01-15 16:57:00 -080055 k = k + 1
timlindbergef8d55d2013-09-27 12:50:13 -070056 if k == 256:
57 k = 1
58 start = start + 1
59 number = number - 1
60 intf = intf + 1
kelvin8ec71442015-01-15 16:57:00 -080061 ip = net + "." + str( start ) + "." + str( k ) + ".1"
62 self.handle.sendline(
63 "sudo ifconfig eth0:" + str(
64 intf ) + " " + ip + " netmask 255.255.255.0" )
timlindbergef8d55d2013-09-27 12:50:13 -070065
kelvin8ec71442015-01-15 16:57:00 -080066 i = self.handle.expect( [
Jon Hallefbd9792015-03-05 16:11:36 -080067 "\$",
kelvin8ec71442015-01-15 16:57:00 -080068 "password",
69 pexpect.TIMEOUT,
70 pexpect.EOF ],
Jon Hallefbd9792015-03-05 16:11:36 -080071 timeout=120 )
SeanCorcoranf580d102013-09-27 15:13:21 -070072
kelvin8ec71442015-01-15 16:57:00 -080073 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
SeanCorcoranf580d102013-09-27 15:13:21 -070084
kelvin8ec71442015-01-15 16:57:00 -080085 def pingall_interfaces( self, netsrc, netstrt, netdst, destlogin, destip ):
86 """
Jon Hallefbd9792015-03-05 16:11:36 -080087 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
kelvin8ec71442015-01-15 16:57:00 -080094 """
95 self.handle.sendline( "" )
96 self.handle.expect( "\$" )
SeanCorcoranf580d102013-09-27 15:13:21 -070097
Jon Hallfebb1c72015-03-05 13:30:09 -080098 self.handle.sendline( "scp " + str( destlogin ) + "@" +
Jon Hallefbd9792015-03-05 16:11:36 -080099 str( destip ) +
100 ":/tmp/local_ip.txt /tmp/ip_table" +
Jon Hallfebb1c72015-03-05 13:30:09 -0800101 str( netsrc ) + ".txt" )
102
103 i = self.handle.expect( [
104 "100%",
kelvin8ec71442015-01-15 16:57:00 -0800105 "password",
106 pexpect.TIMEOUT ],
Jon Hallfebb1c72015-03-05 13:30:09 -0800107 timeout=30 )
kelvin8ec71442015-01-15 16:57:00 -0800108
SeanCorcoranf580d102013-09-27 15:13:21 -0700109 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800110 main.log.info( "Copied ping file successfully" )
SeanCorcoranf580d102013-09-27 15:13:21 -0700111 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800112 self.handle.sendline( self.pwd )
113 self.handle.expect( "100%" )
114 main.log.info( "Copied ping file successfully" )
SeanCorcoranf580d102013-09-27 15:13:21 -0700115 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800116 main.log.error( "COULD NOT COPY PING FILE FROM " + str( destip ) )
SeanCorcoranf580d102013-09-27 15:13:21 -0700117 result = main.FALSE
118 return result
SeanCorcoranf580d102013-09-27 15:13:21 -0700119
kelvin8ec71442015-01-15 16:57:00 -0800120 self.handle.sendline( "" )
121 self.handle.expect( "\$" )
122
123 main.log.info( "Pinging interfaces on the " + str( netdst ) +
Jon Hallfebb1c72015-03-05 13:30:09 -0800124 " network from " + str( netsrc ) + "." +
125 str( netstrt ) + ".1.1" )
kelvin8ec71442015-01-15 16:57:00 -0800126 self.handle.sendline( "sudo fping -S " + str( netsrc ) + "." +
Jon Hallfebb1c72015-03-05 13:30:09 -0800127 str( netstrt ) + ".1.1 -f /tmp/ip_table" +
128 str( netdst ) + ".txt" )
timlindbergef8d55d2013-09-27 12:50:13 -0700129 while 1:
kelvin8ec71442015-01-15 16:57:00 -0800130 i = self.handle.expect( [
Jon Hallfebb1c72015-03-05 13:30:09 -0800131 "reachable",
kelvin8ec71442015-01-15 16:57:00 -0800132 "unreachable",
133 "\$",
134 "password",
135 pexpect.TIMEOUT,
136 "not installed" ],
Jon Hallfebb1c72015-03-05 13:30:09 -0800137 timeout=45 )
timlindbergef8d55d2013-09-27 12:50:13 -0700138 if i == 0:
139 result = main.TRUE
140 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800141 main.log.error( "An interface was unreachable" )
timlindbergef8d55d2013-09-27 12:50:13 -0700142 result = main.FALSE
143 return result
144 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800145 main.log.info( "All interfaces reachable" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800146 result = main.FALSE
timlindbergef8d55d2013-09-27 12:50:13 -0700147 return result
148 elif i == 3:
kelvin8ec71442015-01-15 16:57:00 -0800149 self.handle.sendline( self.pwd )
timlindbergef8d55d2013-09-27 12:50:13 -0700150 elif i == 4:
kelvin8ec71442015-01-15 16:57:00 -0800151 main.log.error( "Unable to fping" )
timlindbergef8d55d2013-09-27 12:50:13 -0700152 result = main.FALSE
153 return result
154 elif i == 5:
kelvin8ec71442015-01-15 16:57:00 -0800155 main.log.info( "fping not installed, installing fping" )
156 self.handle.sendline( "sudo apt-get install fping" )
Jon Hallefbd9792015-03-05 16:11:36 -0800157 i = self.handle.expect( [ "password",
158 "\$",
159 pexpect.TIMEOUT ],
160 timeout=60 )
timlindbergef8d55d2013-09-27 12:50:13 -0700161 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800162 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" )
timlindbergef8d55d2013-09-27 12:50:13 -0700170 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800171 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" )
timlindbergef8d55d2013-09-27 12:50:13 -0700177 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800178 main.log.error( "Could not install fping" )
timlindbergef8d55d2013-09-27 12:50:13 -0700179 result = main.FALSE
180 return result
181
kelvin8ec71442015-01-15 16:57:00 -0800182 def disconnect( self ):
timlindbergef8d55d2013-09-27 12:50:13 -0700183 response = ''
184 try:
kelvin8ec71442015-01-15 16:57:00 -0800185 self.handle.sendline( "exit" )
186 self.handle.expect( "closed" )
Jon Hallefbd9792015-03-05 16:11:36 -0800187 except pexpect.ExceptionPexpect:
Jon Hallfebb1c72015-03-05 13:30:09 -0800188 main.log.exception( "Connection failed to the host" )
timlindbergef8d55d2013-09-27 12:50:13 -0700189 response = main.FALSE
190 return response
191