Statistics
| Branch: | Tag: | Revision:

mininet / examples / test / test_baresshd.py @ 7c5d2771

History | View | Annotate | Download (2.02 KB)

1
#!/usr/bin/env python
2

    
3
"""
4
Tests for baresshd.py
5
"""
6

    
7
import unittest
8
import pexpect
9
from mininet.clean import cleanup, sh
10

    
11
class testBareSSHD( unittest.TestCase ):
12

    
13
    opts = [ 'Welcome to h1', pexpect.EOF, pexpect.TIMEOUT ]
14

    
15
    def connected( self ):
16
        "Log into ssh server, check banner, then exit"
17
        p = pexpect.spawn( 'ssh 10.0.0.1 -o StrictHostKeyChecking=no -i /tmp/ssh/test_rsa exit' )
18
        while True:
19
            index = p.expect( self.opts )
20
            if index == 0:
21
                return True
22
            else:
23
                return False
24

    
25
    def setUp( self ):
26
        # verify that sshd is not running
27
        self.assertFalse( self.connected() )
28
        # create public key pair for testing
29
        sh( 'rm -rf /tmp/ssh' )
30
        sh( 'mkdir /tmp/ssh' )
31
        sh( "ssh-keygen -t rsa -P '' -f /tmp/ssh/test_rsa" )
32
        sh( 'cat /tmp/ssh/test_rsa.pub >> /tmp/ssh/authorized_keys' )
33
        # run example with custom sshd args
34
        cmd = ( 'python -m mininet.examples.baresshd '
35
                '-o AuthorizedKeysFile=/tmp/ssh/authorized_keys '
36
                '-o StrictModes=no' )
37
        p = pexpect.spawn( cmd )
38
        runOpts = [ 'You may now ssh into h1 at 10.0.0.1',
39
                    'after 5 seconds, h1 is not listening on port 22', 
40
                    pexpect.EOF, pexpect.TIMEOUT ]
41
        while True:
42
            index = p.expect( runOpts )
43
            if index == 0:
44
                break
45
            else:
46
                self.tearDown()
47
                self.fail( 'sshd failed to start in host h1' )
48

    
49
    def testSSH( self ):
50
        "Simple test to verify that we can ssh into h1"
51
        result = False
52
        # try to connect up to 3 times; sshd can take a while to start
53
        result = self.connected()
54
        self.assertTrue( result )
55

    
56
    def tearDown( self ):
57
        # kill the ssh process
58
        sh( "ps aux | grep 'ssh.*Banner' | awk '{ print $2 }' | xargs kill" )
59
        cleanup()
60
        # remove public key pair
61
        sh( 'rm -rf /tmp/ssh' )
62

    
63
if __name__ == '__main__':
64
    unittest.main()