second try
[vsorcdistro/.git] / mininet / examples / test / test_bind.py
1 #!/usr/bin/env python
2
3 """
4 Tests for bind.py
5 """
6
7 import unittest
8 from mininet.util import pexpect
9
10 class testBind( unittest.TestCase ):
11
12     prompt = 'mininet>'
13
14     def setUp( self ):
15         self.net = pexpect.spawn( 'python -m mininet.examples.bind' )
16         self.net.expect( "Private Directories: \[([\w\s,'/]+)\]" )
17         self.directories = []
18         # parse directories from mn output
19         for d in self.net.match.group(1).split(', '):
20             self.directories.append( d.strip("'") )
21         self.net.expect( self.prompt )
22         self.assertTrue( len( self.directories ) > 0 )
23
24     def testCreateFile( self ):
25         "Create a file, a.txt, in the first private directory and verify"
26         fileName = 'a.txt'
27         directory = self.directories[ 0 ]
28         path = directory + '/' + fileName
29         self.net.sendline( 'h1 touch %s; ls %s' % ( path, directory ) )
30         index = self.net.expect( [ fileName, self.prompt ] )
31         self.assertTrue( index == 0 )
32         self.net.expect( self.prompt )
33         self.net.sendline( 'h1 rm %s' % path )
34         self.net.expect( self.prompt )
35
36     def testIsolation( self ):
37         "Create a file in two hosts and verify that contents are different"
38         fileName = 'b.txt'
39         directory = self.directories[ 0 ]
40         path = directory + '/' + fileName
41         contents = { 'h1' : '1', 'h2' : '2' }
42         # Verify file doesn't exist, then write private copy of file
43         for host in contents:
44             value = contents[ host ]
45             self.net.sendline( '%s cat %s' % ( host, path ) )
46             self.net.expect( 'No such file' )
47             self.net.expect( self.prompt )
48             self.net.sendline( '%s echo %s > %s' % ( host, value, path ) )
49             self.net.expect( self.prompt )
50         # Verify file contents
51         for host in contents:
52             value = contents[ host ]
53             self.net.sendline( '%s cat %s' % ( host, path ) )
54             self.net.expect( value )
55             self.net.expect( self.prompt )
56             self.net.sendline( '%s rm %s' % ( host, path ) )
57             self.net.expect( self.prompt )
58
59     # TODO: need more tests
60
61     def tearDown( self ):
62         self.net.sendline( 'exit' )
63         self.net.wait()
64
65 if __name__ == '__main__':
66     unittest.main()