Statistics
| Branch: | Tag: | Revision:

mininet / examples / test / test_baresshd.py @ 01c0ef00

History | View | Annotate | Download (1.85 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.log import setLogLevel
11
from mininet.clean import cleanup, sh
12

    
13
class testBareSSHD( unittest.TestCase ):
14

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

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

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

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

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

    
61
if __name__ == '__main__':
62
    setLogLevel( 'warning' )
63
    unittest.main()