Statistics
| Branch: | Tag: | Revision:

mininet / mininet / test / test_walkthrough.py @ 17dbc7e0

History | View | Annotate | Download (11.7 KB)

1
#!/usr/bin/env python
2

    
3
"""
4
Tests for the Mininet Walkthrough
5

6
TODO: missing xterm test
7
"""
8

    
9
import unittest
10
import pexpect
11
import os
12
from mininet.util import quietRun
13

    
14
class testWalkthrough( unittest.TestCase ):
15

    
16
    prompt = 'mininet>'
17

    
18
    # PART 1
19
    def testHelp( self ):
20
        "Check the usage message"
21
        p = pexpect.spawn( 'mn -h' )
22
        index = p.expect( [ 'Usage: mn', pexpect.EOF ] )
23
        self.assertEqual( index, 0 )
24

    
25
    def testWireshark( self ):
26
        "Use tshark to test the of dissector"
27
        tshark = pexpect.spawn( 'tshark -i lo -R of' )
28
        tshark.expect( 'Capturing on lo' )
29
        mn = pexpect.spawn( 'mn --test pingall' )
30
        mn.expect( '0% dropped' )
31
        tshark.expect( 'OFP 74 Hello' )
32
        tshark.sendintr()
33

    
34
    def testBasic( self ):
35
        "Test basic CLI commands (help, nodes, net, dump)"
36
        p = pexpect.spawn( 'mn' )
37
        p.expect( self.prompt )
38
        # help command
39
        p.sendline( 'help' )
40
        index = p.expect( [ 'commands', self.prompt ] )
41
        self.assertEqual( index, 0, 'No output for "help" command')
42
        # nodes command
43
        p.sendline( 'nodes' )
44
        p.expect( '([chs]\d ?){4}' )
45
        nodes = p.match.group( 0 ).split()
46
        self.assertEqual( len( nodes ), 4, 'No nodes in "nodes" command')
47
        p.expect( self.prompt )
48
        # net command
49
        p.sendline( 'net' )
50
        expected = [ x for x in nodes ]
51
        while len( expected ) > 0:
52
            index = p.expect( expected )
53
            node = p.match.group( 0 )
54
            expected.remove( node )
55
            p.expect( '\n' )
56
        self.assertEqual( len( expected ), 0, '"nodes" and "net" differ')
57
        p.expect( self.prompt )
58
        # dump command
59
        p.sendline( 'dump' )
60
        expected = [ '<\w+ (%s)' % n for n in nodes ]
61
        actual = []
62
        for _ in nodes:
63
            index = p.expect( expected )
64
            node = p.match.group( 1 )
65
            actual.append( node )
66
            p.expect( '\n' )
67
        self.assertEqual( actual.sort(), nodes.sort(), '"nodes" and "dump" differ' ) 
68
        p.expect( self.prompt )
69
        p.sendline( 'exit' )
70
        p.wait()
71

    
72
    def testHostCommands( self ):
73
        "Test ifconfig and ps on h1 and s1"
74
        p = pexpect.spawn( 'mn' )
75
        p.expect( self.prompt )
76
        interfaces = [ 'h1-eth0', 's1-eth1', '[^-]eth0', 'lo', self.prompt ]
77
        # h1 ifconfig
78
        p.sendline( 'h1 ifconfig -a' )
79
        ifcount = 0
80
        while True:
81
            index = p.expect( interfaces )
82
            if index == 0 or index == 3:
83
                ifcount += 1
84
            elif index == 1:
85
                self.fail( 's1 interface displayed in "h1 ifconfig"' )
86
            elif index == 2:
87
                self.fail( 'eth0 displayed in "h1 ifconfig"' )
88
            else:
89
                break
90
        self.assertEqual( ifcount, 2, 'Missing interfaces on h1')
91
        # s1 ifconfig
92
        p.sendline( 's1 ifconfig -a' )
93
        ifcount = 0
94
        while True:
95
            index = p.expect( interfaces )
96
            if index == 0:
97
                self.fail( 'h1 interface displayed in "s1 ifconfig"' )
98
            elif index == 1 or index == 2 or index == 3:
99
                ifcount += 1
100
            else:
101
                break
102
        self.assertEqual( ifcount, 3, 'Missing interfaces on s1')
103
        # h1 ps
104
        p.sendline( 'h1 ps -a' )
105
        p.expect( self.prompt )
106
        h1Output = p.before
107
        # s1 ps
108
        p.sendline( 's1 ps -a' )
109
        p.expect( self.prompt )
110
        s1Output = p.before
111
        # strip command from ps output
112
        h1Output = h1Output.split( '\n', 1 )[ 1 ]
113
        s1Output = s1Output.split( '\n', 1 )[ 1 ]
114
        self.assertEqual( h1Output, s1Output, 'h1 and s1 "ps" output differs')
115
        p.sendline( 'exit' )
116
        p.wait()
117

    
118
    def testConnectivity( self ):
119
        "Test ping and pingall"
120
        p = pexpect.spawn( 'mn' )
121
        p.expect( self.prompt )
122
        p.sendline( 'h1 ping -c 1 h2' )
123
        p.expect( '1 packets transmitted, 1 received' )
124
        p.expect( self.prompt )
125
        p.sendline( 'pingall' )
126
        p.expect( '0% dropped' )
127
        p.expect( self.prompt )
128
        p.sendline( 'exit' )
129
        p.wait()
130

    
131
    def testSimpleHTTP( self ):
132
        "Start an HTTP server on h1 and wget from h2"
133
        p = pexpect.spawn( 'mn' )
134
        p.expect( self.prompt )
135
        p.sendline( 'h1 python -m SimpleHTTPServer 80 &' )
136
        p.expect( self.prompt )
137
        p.sendline( ' h2 wget -O - h1' )
138
        p.expect( '200 OK' )
139
        p.expect( self.prompt )
140
        p.sendline( 'h1 kill %python' )
141
        p.expect( self.prompt )
142
        p.sendline( 'exit' )
143
        p.wait()
144

    
145
    # PART 2
146
    def testRegressionRun( self ):
147
        "Test pingpair (0% drop) and iperf (bw > 0) regression tests"
148
        # test pingpair
149
        p = pexpect.spawn( 'mn --test pingpair' )
150
        p.expect( '0% dropped' )
151
        p.expect( pexpect.EOF )
152
        # test iperf
153
        p = pexpect.spawn( 'mn --test iperf' )
154
        p.expect( "Results: \['([\d\.]+) .bits/sec'," )
155
        bw = float( p.match.group( 1 ) )
156
        self.assertTrue( bw > 0 )
157
        p.expect( pexpect.EOF )
158

    
159
    def testTopoChange( self ):
160
        "Test pingall on single,3 and linear,4 topos"
161
        # testing single,3
162
        p = pexpect.spawn( 'mn --test pingall --topo single,3' )
163
        p.expect( '(\d+)/(\d+) received')
164
        received = int( p.match.group( 1 ) )
165
        sent = int( p.match.group( 2 ) )
166
        self.assertEqual( sent, 6, 'Wrong number of pings sent in single,3' )
167
        self.assertEqual( sent, received, 'Dropped packets in single,3')
168
        p.expect( pexpect.EOF )
169
        # testing linear,4
170
        p = pexpect.spawn( 'mn --test pingall --topo linear,4' )
171
        p.expect( '(\d+)/(\d+) received')
172
        received = int( p.match.group( 1 ) )
173
        sent = int( p.match.group( 2 ) )
174
        self.assertEqual( sent, 12, 'Wrong number of pings sent in linear,4' )
175
        self.assertEqual( sent, received, 'Dropped packets in linear,4')
176
        p.expect( pexpect.EOF )
177

    
178
    def testLinkChange( self ):
179
        "Test TCLink bw and delay"
180
        p = pexpect.spawn( 'mn --link tc,bw=10,delay=10ms' )
181
        # test bw
182
        p.expect( self.prompt )
183
        p.sendline( 'iperf' )
184
        p.expect( "Results: \['([\d\.]+) Mbits/sec'," )
185
        bw = float( p.match.group( 1 ) )
186
        self.assertTrue( bw < 10.1, 'Bandwidth > 10 Mb/s')
187
        self.assertTrue( bw > 9.0, 'Bandwidth < 9 Mb/s')
188
        p.expect( self.prompt )
189
        # test delay
190
        p.sendline( 'h1 ping -c 4 h2' )
191
        p.expect( 'rtt min/avg/max/mdev = ([\d\.]+)/([\d\.]+)/([\d\.]+)/([\d\.]+) ms' )
192
        delay = float( p.match.group( 2 ) )
193
        self.assertTrue( delay > 40, 'Delay < 40ms' )
194
        self.assertTrue( delay < 45, 'Delay > 40ms' )
195
        p.expect( self.prompt )
196
        p.sendline( 'exit' )
197
        p.wait()
198

    
199
    def testVerbosity( self ):
200
        "Test debug and output verbosity"
201
        # test output
202
        p = pexpect.spawn( 'mn -v output' )
203
        p.expect( self.prompt )
204
        self.assertEqual( len( p.before ), 0, 'Too much output for "output"' )
205
        p.sendline( 'exit' )
206
        p.wait()
207
        # test debug
208
        p = pexpect.spawn( 'mn -v debug --test none' )
209
        p.expect( pexpect.EOF )
210
        lines = p.before.split( '\n' )
211
        self.assertTrue( len( lines ) > 100, "Debug output is too short" )
212

    
213
    def testCustomTopo( self ):
214
        "Start Mininet using a custom topo, then run pingall"
215
        custom = os.path.dirname( os.path.realpath( __file__ ) )
216
        custom = os.path.join( custom, '../../custom/topo-2sw-2host.py' )
217
        custom = os.path.normpath( custom )
218
        p = pexpect.spawn( 'mn --custom %s --topo mytopo --test pingall' % custom )
219
        p.expect( '0% dropped' )
220
        p.expect( pexpect.EOF )
221

    
222
    def testStaticMAC( self ):
223
        "Verify that MACs are set to easy to read numbers"
224
        p = pexpect.spawn( 'mn --mac' )
225
        p.expect( self.prompt )
226
        for i in range( 1, 3 ):
227
            p.sendline( 'h%d ifconfig' % i )
228
            p.expect( 'HWaddr 00:00:00:00:00:0%d' % i )
229
            p.expect( self.prompt )
230

    
231
    def testSwitches( self ):
232
        "Run iperf test using user and ovsk switches"
233
        switches = [ 'user', 'ovsk' ]
234
        for sw in switches:
235
            p = pexpect.spawn( 'mn --switch %s --test iperf' % sw )
236
            p.expect( "Results: \['([\d\.]+) .bits/sec'," )
237
            bw = float( p.match.group( 1 ) )
238
            self.assertTrue( bw > 0 )
239
            p.expect( pexpect.EOF )
240

    
241
    def testBenchmark( self ):
242
        "Run benchmark and verify that it takes less than 2 seconds"
243
        p = pexpect.spawn( 'mn --test none' )
244
        p.expect( 'completed in ([\d\.]+) seconds' )
245
        time = float( p.match.group( 1 ) )
246
        self.assertTrue( time < 2, 'Benchmark takes more than 2 seconds' )
247

    
248
    def testOwnNamespace( self ):
249
        "Test running user switch in its own namespace"
250
        p = pexpect.spawn( 'mn --innamespace --switch user' )
251
        p.expect( self.prompt )
252
        interfaces = [ 'h1-eth0', 's1-eth1', '[^-]eth0', 'lo', self.prompt ]
253
        p.sendline( 's1 ifconfig -a' )
254
        ifcount = 0
255
        while True:
256
            index = p.expect( interfaces )
257
            if index == 1 or index == 3:
258
                ifcount += 1
259
            elif index == 0:
260
                self.fail( 'h1 interface displayed in "s1 ifconfig"' )
261
            elif index == 2:
262
                self.fail( 'eth0 displayed in "s1 ifconfig"' )
263
            else:
264
                break
265
        self.assertEqual( ifcount, 2, 'Missing interfaces on s1' )
266
        # verify that all hosts a reachable
267
        p.sendline( 'pingall' )
268
        p.expect( '(\d+)% dropped' )
269
        dropped = int( p.match.group( 1 ) )
270
        self.assertEqual( dropped, 0, 'pingall failed')
271
        p.expect( self.prompt )
272
        p.sendline( 'exit' )
273
        p.wait()
274

    
275
    # PART 3
276
    def testPythonInterpreter( self ):
277
        "Test py and px by checking IP for h1 and adding h3"
278
        p = pexpect.spawn( 'mn' )
279
        p.expect( self.prompt )
280
        # test host IP
281
        p.sendline( 'py h1.IP()' )
282
        p.expect( '10.0.0.1' )
283
        p.expect( self.prompt )
284
        # test adding host
285
        p.sendline( "px net.addHost('h3')" )
286
        p.expect( self.prompt )
287
        p.sendline( "px net.addLink(s1, h3)" )
288
        p.expect( self.prompt )
289
        p.sendline( 'net' )
290
        p.expect( 'h3' )
291
        p.expect( self.prompt )
292
        p.sendline( 'py h3.MAC()' )
293
        p.expect( '([a-f0-9]{2}:?){6}' )
294
        p.expect( self.prompt )
295
        p.sendline( 'exit' )
296
        p.wait()
297

    
298
    def testLink( self ):
299
        "Test link CLI command using ping"
300
        p = pexpect.spawn( 'mn' )
301
        p.expect( self.prompt )
302
        p.sendline( 'link s1 h1 down' )
303
        p.expect( self.prompt )
304
        p.sendline( 'h1 ping -c 1 h2' )
305
        p.expect( 'unreachable' )
306
        p.expect( self.prompt )
307
        p.sendline( 'link s1 h1 up' )
308
        p.expect( self.prompt )
309
        p.sendline( 'h1 ping -c 1 h2' )
310
        p.expect( '0% packet loss' )
311
        p.expect( self.prompt )
312
        p.sendline( 'exit' )
313
        p.wait()
314

    
315
    @unittest.skipUnless( os.path.exists( '/tmp/pox' ) or
316
                          '1 received' in quietRun( 'ping -c 1 github.com' ),
317
                          'Github is not reachable; cannot download Pox' )
318
    def testRemoteController( self ):
319
        "Test Mininet using Pox controller"
320
        if not os.path.exists( '/tmp/pox' ):
321
            p = pexpect.spawn( 'git clone https://github.com/noxrepo/pox.git /tmp/pox' )
322
            p.expect( pexpect.EOF )
323
        pox = pexpect.spawn( '/tmp/pox/pox.py forwarding.l2_learning' )
324
        net = pexpect.spawn( 'mn --controller=remote,ip=127.0.0.1,port=6633 --test pingall' )
325
        net.expect( '0% dropped' )
326
        net.expect( pexpect.EOF )
327
        pox.sendintr()
328
        pox.wait()
329

    
330
if __name__ == '__main__':
331
    unittest.main()