Revision 7c5d2771 examples/test/test_baresshd.py

View differences:

examples/test/test_baresshd.py
6 6

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

  
12 11
class testBareSSHD( unittest.TestCase ):
13 12

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

  
16 15
    def connected( self ):
17 16
        "Log into ssh server, check banner, then exit"
18
        p = pexpect.spawn( 'ssh 10.0.0.1 -i /tmp/ssh/test_rsa exit' )
17
        p = pexpect.spawn( 'ssh 10.0.0.1 -o StrictHostKeyChecking=no -i /tmp/ssh/test_rsa exit' )
19 18
        while True:
20 19
            index = p.expect( self.opts )
21 20
            if index == 0:
22
                p.sendline( 'yes' )
23
            elif index == 1:
24 21
                return True
25 22
            else:
26 23
                return False
......
37 34
        cmd = ( 'python -m mininet.examples.baresshd '
38 35
                '-o AuthorizedKeysFile=/tmp/ssh/authorized_keys '
39 36
                '-o StrictModes=no' )
40
        sh( cmd )
37
        p = pexpect.spawn( cmd )
38
        runOpts = [ 'You may now ssh into h1 at 10.0.0.1',
39
                    'after 5 seconds, h1 is not listening on port 22', 
40
                    pexpect.EOF, pexpect.TIMEOUT ]
41
        while True:
42
            index = p.expect( runOpts )
43
            if index == 0:
44
                break
45
            else:
46
                self.tearDown()
47
                self.fail( 'sshd failed to start in host h1' )
41 48

  
42 49
    def testSSH( self ):
43 50
        "Simple test to verify that we can ssh into h1"
44 51
        result = False
45 52
        # 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 )
53
        result = self.connected()
52 54
        self.assertTrue( result )
53 55

  
54 56
    def tearDown( self ):

Also available in: Unified diff