Statistics
| Branch: | Tag: | Revision:

mininet / examples / test / test_baresshd.py @ c5da46f1

History | View | Annotate | Download (1.79 KB)

1 a46fae06 Brian O'Connor
#!/usr/bin/env python
2
3 01c0ef00 Brian O'Connor
"""
4
Tests for baresshd.py
5
"""
6 a46fae06 Brian O'Connor
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 01c0ef00 Brian O'Connor
        "Log into ssh server, check banner, then exit"
18
        p = pexpect.spawn( 'ssh 10.0.0.1 -i /tmp/ssh/test_rsa exit' )
19 a46fae06 Brian O'Connor
        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 01c0ef00 Brian O'Connor
        # verify that sshd is not running
30 a46fae06 Brian O'Connor
        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 01c0ef00 Brian O'Connor
        # run example with custom sshd args
36 a46fae06 Brian O'Connor
        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 01c0ef00 Brian O'Connor
        "Simple test to verify that we can ssh into h1"
43 a46fae06 Brian O'Connor
        result = False
44 01c0ef00 Brian O'Connor
        # try to connect up to 3 times; sshd can take a while to start
45 a46fae06 Brian O'Connor
        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()