Statistics
| Branch: | Tag: | Revision:

mininet / examples / test / test_baresshd.py @ bfb56004

History | View | Annotate | Download (1.82 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( 'rm -rf /tmp/ssh' )
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
    unittest.main()