Skip to content

Commit 7c5d277

Browse files
committed
wait for sshd to start in example
1 parent 2ceb579 commit 7c5d277

3 files changed

Lines changed: 27 additions & 17 deletions

File tree

examples/baresshd.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
import sys
66
from mininet.node import Host
7-
from mininet.util import ensureRoot
7+
from mininet.util import ensureRoot, waitListening
88

99
ensureRoot()
10+
timeout = 5
1011

1112
print "*** Creating nodes"
1213
h1 = Host( 'h1' )
@@ -33,5 +34,10 @@
3334
if len( sys.argv ) > 1:
3435
cmd += ' ' + ' '.join( sys.argv[ 1: ] )
3536
h1.cmd( cmd )
37+
listening = waitListening( server=h1, port=22, timeout=timeout )
3638

37-
print "*** You may now ssh into", h1.name, "at", h1.IP()
39+
if listening:
40+
print "*** You may now ssh into", h1.name, "at", h1.IP()
41+
else:
42+
print ( "*** Warning: after %s seconds, %s is not listening on port 22"
43+
% ( timeout, h1.name ) )

examples/test/test_baresshd.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,18 @@
66

77
import unittest
88
import pexpect
9-
from time import sleep
109
from mininet.clean import cleanup, sh
1110

1211
class testBareSSHD( unittest.TestCase ):
1312

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

1615
def connected( self ):
1716
"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' )
1918
while True:
2019
index = p.expect( self.opts )
2120
if index == 0:
22-
p.sendline( 'yes' )
23-
elif index == 1:
2421
return True
2522
else:
2623
return False
@@ -37,18 +34,23 @@ def setUp( self ):
3734
cmd = ( 'python -m mininet.examples.baresshd '
3835
'-o AuthorizedKeysFile=/tmp/ssh/authorized_keys '
3936
'-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' )
4148

4249
def testSSH( self ):
4350
"Simple test to verify that we can ssh into h1"
4451
result = False
4552
# 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()
5254
self.assertTrue( result )
5355

5456
def tearDown( self ):

mininet/util.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,8 @@ def ensureRoot():
543543
return
544544

545545
def waitListening( client=None, server='127.0.0.1', port=80, timeout=None ):
546-
"Wait until server is listening on port"
546+
"""Wait until server is listening on port.
547+
returns True if server is listening"""
547548
run = ( client.cmd if client else
548549
partial( quietRun, shell=True ) )
549550
if not run( 'which telnet' ):
@@ -554,12 +555,13 @@ def waitListening( client=None, server='127.0.0.1', port=80, timeout=None ):
554555
time = 0
555556
while 'Connected' not in run( cmd ):
556557
if timeout:
558+
print time
557559
if time >= timeout:
558560
error( 'could not connect to %s on port %d\n'
559561
% ( server, port ) )
560-
break
562+
return False
561563
output('waiting for', server,
562564
'to listen on port', port, '\n')
563565
sleep( .5 )
564566
time += .5
565-
567+
return True

0 commit comments

Comments
 (0)