Revision 706229da

View differences:

examples/intfOptions.py
1
#!/usr/bin/python
2

  
3
'''
4
example of using various TCIntf options.
5
reconfigures a single interface using intf.config()
6
to use different traffic control commands to test
7
bandwidth, loss, and delay
8
'''
9

  
10
from mininet.net import Mininet
11
from mininet.log import setLogLevel, info
12
from mininet.link import TCLink
13

  
14
def intfOptions():
15
    "run various traffic control commands on a single interface"
16
    net = Mininet()
17
    net.addController( 'c0' )
18
    h1 = net.addHost( 'h1' )
19
    h2 = net.addHost( 'h2' )
20
    s1 = net.addSwitch( 's1' )
21
    link1 = net.addLink( h1, s1, cls=TCLink )
22
    link2 = net.addLink( h2, s1 )
23
    net.start()
24
    
25
    # flush out latency from reactive forwarding delay
26
    net.pingAll()
27

  
28
    info( '\n*** Configuring one intf with bandwidth of 5 Mb\n' )
29
    link1.intf1.config( bw=5 )
30
    info( '\n*** Running iperf to test\n' )
31
    net.iperf()
32

  
33
    info( '\n*** Configuring one intf with loss of 50%\n' )
34
    link1.intf1.config( loss=50 )
35
    info( '\n*** Running 20 pings to test\n' )
36
    info( net.hosts[ 0 ].cmd( 'ping -c 20 -i .2 10.0.0.2' ) )
37
    
38
    
39
    info( '\n*** Configuring one intf with delay of 15ms\n' )
40
    link1.intf1.config( delay='15ms' )
41
    info( '\n*** Run a ping to confirm delay' )
42
    net.pingPairFull()
43
    
44
    info( '\n*** Done testing\n' )
45
    net.stop()
46

  
47
if __name__ == '__main__':
48
    setLogLevel( 'info' )
49
    intfOptions()
examples/test/test_intfOptions.py
1
#!/usr/bin/env python
2

  
3
"""
4
Test for intfOptions.py
5
"""
6

  
7
import unittest
8
import pexpect
9
import sys
10

  
11
class testIntfOptions( unittest.TestCase ):
12

  
13
    def testIntfOptions( self ):
14
        "verify that intf.config is correctly limiting traffic"
15
        p = pexpect.spawn( 'python -m mininet.examples.intfOptions ' )
16
        tolerance = .8
17
        opts = [ "Results: \['([\d\.]+) .bits/sec",
18
                 "(\d+) packets transmitted, (\d+) received, (\d+)% packet loss, time (\d+)ms",
19
                 "h(\d+)->h(\d+): (\d)/(\d), rtt min/avg/max/mdev ([\d\.]+)/([\d\.]+)/([\d\.]+)/([\d\.]+) ms",
20
                 pexpect.EOF ]
21
        while True:
22
            index = p.expect( opts, timeout=600 )
23
            if index == 0:
24
                bw = float( p.match.group( 1 ) )
25
                self.assertGreaterEqual( bw, float( 5 * tolerance ) )
26
                self.assertLessEqual( bw, float( 5 + 5 * ( 1 - tolerance ) ) )
27
            elif index == 1:
28
                loss = int( p.match.group( 3 ) )
29
                msg = ( "testing packet loss at 50%\n",
30
                        "this test will sometimes fail\n",
31
                        "ran 20 pings accross network\n",
32
                        "packet loss is %d%%\n\n"
33
                        % loss )
34
                self.assertGreaterEqual( loss, 50 * .8, msg )
35
                self.assertLessEqual( loss,  50 + 50 * ( 1 - tolerance ), msg )
36
            elif index == 2:
37
                delay = float( p.match.group( 6 ) )
38
                self.assertGreaterEqual( delay, 15 * .8 )
39
                self.assertLessEqual( delay,  15 + 15 * ( 1 - tolerance ) )
40
            else:
41
                break
42

  
43

  
44
if __name__ == '__main__':
45
    unittest.main()

Also available in: Unified diff