Statistics
| Branch: | Tag: | Revision:

mininet / examples / test / test_baresshd.py @ c5da46f1

History | View | Annotate | Download (1.79 KB)

1
#!/usr/bin/env python
2

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

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

    
12
class testBareSSHD( unittest.TestCase ):
13

    
14
    opts = [ '\(yes/no\)\?', 'Welcome to h1', 'refused', pexpect.EOF, pexpect.TIMEOUT ]
15

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

    
28
    def setUp( self ):
29
        # verify that sshd is not running
30
        self.assertFalse( self.connected() )
31
        # create public key pair for testing
32
        sh( 'mkdir /tmp/ssh' )
33
        sh( "ssh-keygen -t rsa -P '' -f /tmp/ssh/test_rsa" )
34
        sh( 'cat /tmp/ssh/test_rsa.pub >> /tmp/ssh/authorized_keys' )
35
        # run example with custom sshd args
36
        cmd = ( 'python -m mininet.examples.baresshd '
37
                '-o AuthorizedKeysFile=/tmp/ssh/authorized_keys '
38
                '-o StrictModes=no' )
39
        sh( cmd )
40

    
41
    def testSSH( self ):
42
        "Simple test to verify that we can ssh into h1"
43
        result = False
44
        # try to connect up to 3 times; sshd can take a while to start
45
        for _ in range( 3 ):
46
            result = self.connected()
47
            if result:
48
                break
49
            else:
50
                sleep( 1 )
51
        self.assertTrue( result )
52

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

    
60
if __name__ == '__main__':
61
    unittest.main()