blob: 81743d31d0a64dd835aae724c372e1b8d5dac1ca [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,
Jon Hallefbd9792015-03-05 16:11:36 -080021 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 """
Jon Hallefbd9792015-03-05 16:11:36 -080033 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
kelvin8ec71442015-01-15 16:57:00 -080044 """
45 self.handle.sendline( "" )
46 self.handle.expect( "\$" )
SeanCorcoranf580d102013-09-27 15:13:21 -070047
kelvin8ec71442015-01-15 16:57:00 -080048 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( "\$" )
timlindbergef8d55d2013-09-27 12:50:13 -070052
kelvin8ec71442015-01-15 16:57:00 -080053 main.log.info( "Creating interfaces" )
timlindbergef8d55d2013-09-27 12:50:13 -070054 k = 0
55 intf = 0
56 while number != 0:
kelvin8ec71442015-01-15 16:57:00 -080057 k = k + 1
timlindbergef8d55d2013-09-27 12:50:13 -070058 if k == 256:
59 k = 1
60 start = start + 1
61 number = number - 1
62 intf = intf + 1
kelvin8ec71442015-01-15 16:57:00 -080063 ip = net + "." + str( start ) + "." + str( k ) + ".1"
64 self.handle.sendline(
65 "sudo ifconfig eth0:" + str(
66 intf ) + " " + ip + " netmask 255.255.255.0" )
timlindbergef8d55d2013-09-27 12:50:13 -070067
kelvin8ec71442015-01-15 16:57:00 -080068 i = self.handle.expect( [
Jon Hallefbd9792015-03-05 16:11:36 -080069 "\$",
kelvin8ec71442015-01-15 16:57:00 -080070 "password",
71 pexpect.TIMEOUT,
72 pexpect.EOF ],
Jon Hallefbd9792015-03-05 16:11:36 -080073 timeout=120 )
SeanCorcoranf580d102013-09-27 15:13:21 -070074
kelvin8ec71442015-01-15 16:57:00 -080075 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
SeanCorcoranf580d102013-09-27 15:13:21 -070086
kelvin8ec71442015-01-15 16:57:00 -080087 def pingall_interfaces( self, netsrc, netstrt, netdst, destlogin, destip ):
88 """
Jon Hallefbd9792015-03-05 16:11:36 -080089 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
kelvin8ec71442015-01-15 16:57:00 -080096 """
97 self.handle.sendline( "" )
98 self.handle.expect( "\$" )
SeanCorcoranf580d102013-09-27 15:13:21 -070099
Jon Hallfebb1c72015-03-05 13:30:09 -0800100 self.handle.sendline( "scp " + str( destlogin ) + "@" +
Jon Hallefbd9792015-03-05 16:11:36 -0800101 str( destip ) +
102 ":/tmp/local_ip.txt /tmp/ip_table" +
Jon Hallfebb1c72015-03-05 13:30:09 -0800103 str( netsrc ) + ".txt" )
104
105 i = self.handle.expect( [
106 "100%",
kelvin8ec71442015-01-15 16:57:00 -0800107 "password",
108 pexpect.TIMEOUT ],
Jon Hallfebb1c72015-03-05 13:30:09 -0800109 timeout=30 )
kelvin8ec71442015-01-15 16:57:00 -0800110
SeanCorcoranf580d102013-09-27 15:13:21 -0700111 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800112 main.log.info( "Copied ping file successfully" )
SeanCorcoranf580d102013-09-27 15:13:21 -0700113 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800114 self.handle.sendline( self.pwd )
115 self.handle.expect( "100%" )
116 main.log.info( "Copied ping file successfully" )
SeanCorcoranf580d102013-09-27 15:13:21 -0700117 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800118 main.log.error( "COULD NOT COPY PING FILE FROM " + str( destip ) )
SeanCorcoranf580d102013-09-27 15:13:21 -0700119 result = main.FALSE
120 return result
SeanCorcoranf580d102013-09-27 15:13:21 -0700121
kelvin8ec71442015-01-15 16:57:00 -0800122 self.handle.sendline( "" )
123 self.handle.expect( "\$" )
124
125 main.log.info( "Pinging interfaces on the " + str( netdst ) +
Jon Hallfebb1c72015-03-05 13:30:09 -0800126 " network from " + str( netsrc ) + "." +
127 str( netstrt ) + ".1.1" )
kelvin8ec71442015-01-15 16:57:00 -0800128 self.handle.sendline( "sudo fping -S " + str( netsrc ) + "." +
Jon Hallfebb1c72015-03-05 13:30:09 -0800129 str( netstrt ) + ".1.1 -f /tmp/ip_table" +
130 str( netdst ) + ".txt" )
timlindbergef8d55d2013-09-27 12:50:13 -0700131 while 1:
kelvin8ec71442015-01-15 16:57:00 -0800132 i = self.handle.expect( [
Jon Hallfebb1c72015-03-05 13:30:09 -0800133 "reachable",
kelvin8ec71442015-01-15 16:57:00 -0800134 "unreachable",
135 "\$",
136 "password",
137 pexpect.TIMEOUT,
138 "not installed" ],
Jon Hallfebb1c72015-03-05 13:30:09 -0800139 timeout=45 )
timlindbergef8d55d2013-09-27 12:50:13 -0700140 if i == 0:
141 result = main.TRUE
142 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800143 main.log.error( "An interface was unreachable" )
timlindbergef8d55d2013-09-27 12:50:13 -0700144 result = main.FALSE
145 return result
146 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800147 main.log.info( "All interfaces reachable" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800148 result = main.FALSE
timlindbergef8d55d2013-09-27 12:50:13 -0700149 return result
150 elif i == 3:
kelvin8ec71442015-01-15 16:57:00 -0800151 self.handle.sendline( self.pwd )
timlindbergef8d55d2013-09-27 12:50:13 -0700152 elif i == 4:
kelvin8ec71442015-01-15 16:57:00 -0800153 main.log.error( "Unable to fping" )
timlindbergef8d55d2013-09-27 12:50:13 -0700154 result = main.FALSE
155 return result
156 elif i == 5:
kelvin8ec71442015-01-15 16:57:00 -0800157 main.log.info( "fping not installed, installing fping" )
158 self.handle.sendline( "sudo apt-get install fping" )
Jon Hallefbd9792015-03-05 16:11:36 -0800159 i = self.handle.expect( [ "password",
160 "\$",
161 pexpect.TIMEOUT ],
162 timeout=60 )
timlindbergef8d55d2013-09-27 12:50:13 -0700163 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800164 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" )
timlindbergef8d55d2013-09-27 12:50:13 -0700172 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800173 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" )
timlindbergef8d55d2013-09-27 12:50:13 -0700179 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800180 main.log.error( "Could not install fping" )
timlindbergef8d55d2013-09-27 12:50:13 -0700181 result = main.FALSE
182 return result
183
kelvin8ec71442015-01-15 16:57:00 -0800184 def disconnect( self ):
timlindbergef8d55d2013-09-27 12:50:13 -0700185 response = ''
186 try:
kelvin8ec71442015-01-15 16:57:00 -0800187 self.handle.sendline( "exit" )
188 self.handle.expect( "closed" )
Jon Hallefbd9792015-03-05 16:11:36 -0800189 except pexpect.ExceptionPexpect:
Jon Hallfebb1c72015-03-05 13:30:09 -0800190 main.log.exception( "Connection failed to the host" )
timlindbergef8d55d2013-09-27 12:50:13 -0700191 response = main.FALSE
192 return response
193