Revision 01c0ef00 examples/test/test_baresshd.py

View differences:

examples/test/test_baresshd.py
1 1
#!/usr/bin/env python
2 2

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

  
5 7
import unittest
6 8
import pexpect
......
9 11
from mininet.clean import cleanup, sh
10 12

  
11 13
class testBareSSHD( unittest.TestCase ):
12
    "Test ping with single switch topology (common code)."
13 14

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

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

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

  
39 42
    def testSSH( self ):
43
        "Simple test to verify that we can ssh into h1"
40 44
        result = False
41
        # try to connect up to 3 times
45
        # try to connect up to 3 times; sshd can take a while to start
42 46
        for _ in range( 3 ):
43 47
            result = self.connected()
44 48
            if result:
......
54 58
        # remove public key pair
55 59
        sh( 'rm -rf /tmp/ssh' )
56 60

  
57

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

Also available in: Unified diff