second try
[vsorcdistro/.git] / mininet / examples / test / test_popen.py
1 #!/usr/bin/env python
2
3 """
4 Test for popen.py and popenpoll.py
5 """
6
7 import unittest
8 from mininet.util import pexpect
9
10 class testPopen( unittest.TestCase ):
11
12     def pingTest( self, name ):
13         "Verify that there are no dropped packets for each host"
14         p = pexpect.spawn( 'python -m %s' % name )
15         opts = [ "<(h\d+)>: PING ",
16                  "<(h\d+)>: (\d+) packets transmitted, (\d+) received",
17                  pexpect.EOF ]
18         pings = {}
19         while True:
20             index = p.expect( opts )
21             if index == 0:
22                 name = p.match.group(1)
23                 pings[ name ] = 0
24             elif index == 1:
25                 name = p.match.group(1)
26                 transmitted = p.match.group(2)
27                 received = p.match.group(3)
28                 # verify no dropped packets
29                 self.assertEqual( received, transmitted )
30                 pings[ name ] += 1
31             else:
32                 break
33         self.assertTrue( len(pings) > 0 )
34         # verify that each host has gotten results
35         for count in pings.values():
36             self.assertEqual( count, 1 )
37
38     def testPopen( self ):
39         self.pingTest( 'mininet.examples.popen' )
40
41     def testPopenPoll( self ):
42         self.pingTest( 'mininet.examples.popenpoll' )
43
44 if __name__ == '__main__':
45     unittest.main()