second try
[vsorcdistro/.git] / mininet / examples / test / test_multiping.py
1 #!/usr/bin/env python
2
3 """
4 Test for multiping.py
5 """
6
7 import unittest
8 from mininet.util import pexpect
9 from collections import defaultdict
10
11 class testMultiPing( unittest.TestCase ):
12
13     def testMultiPing( self ):
14         """Verify that each target is pinged at least once, and
15            that pings to 'real' targets are successful and unknown targets fail"""
16         p = pexpect.spawn( 'python -m mininet.examples.multiping' )
17         opts = [ "Host (h\d+) \(([\d.]+)\) will be pinging ips: ([\d\. ]+)",
18                  "(h\d+): ([\d.]+) -> ([\d.]+) \d packets transmitted, (\d) received",
19                  pexpect.EOF ]
20         pings = defaultdict( list )
21         while True:
22             index = p.expect( opts )
23             if index == 0:
24                 name = p.match.group(1)
25                 ip = p.match.group(2)
26                 targets = p.match.group(3).split()
27                 pings[ name ] += targets
28             elif index == 1:
29                 name = p.match.group(1)
30                 ip = p.match.group(2)
31                 target = p.match.group(3)
32                 received = int( p.match.group(4) )
33                 if target == '10.0.0.200':
34                     self.assertEqual( received, 0, p.match.group(0) + '\n' +
35                                       target + ' received %d != 0 packets' % received )
36                 else:
37                     self.assertEqual( received, 1, p.match.group(0) + '\n' +
38                                       target + ' received %d != 1 packets' % received )
39                 try:
40                     pings[ name ].remove( target )
41                 except:
42                     pass
43             else:
44                 break
45         self.assertTrue( len( pings ) > 0, 'too few pings' )
46         for t in pings.values():
47             self.assertEqual( len( t ), 0, 'missed ping target(s): %s' % t )
48
49 if __name__ == '__main__':
50     unittest.main()