mininet / examples / test / test_baresshd.py @ 7c5d2771
History | View | Annotate | Download (2.02 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 mininet.clean import cleanup, sh |
||
10 | |||
11 | class testBareSSHD( unittest.TestCase ): |
||
12 | |||
13 | 7c5d2771 | cody burkard | opts = [ 'Welcome to h1', pexpect.EOF, pexpect.TIMEOUT ]
|
14 | a46fae06 | Brian O'Connor | |
15 | def connected( self ): |
||
16 | 01c0ef00 | Brian O'Connor | "Log into ssh server, check banner, then exit"
|
17 | 7c5d2771 | cody burkard | p = pexpect.spawn( 'ssh 10.0.0.1 -o StrictHostKeyChecking=no -i /tmp/ssh/test_rsa exit' )
|
18 | a46fae06 | Brian O'Connor | while True: |
19 | index = p.expect( self.opts )
|
||
20 | if index == 0: |
||
21 | return True |
||
22 | else:
|
||
23 | return False |
||
24 | |||
25 | def setUp( self ): |
||
26 | 01c0ef00 | Brian O'Connor | # verify that sshd is not running
|
27 | a46fae06 | Brian O'Connor | self.assertFalse( self.connected() ) |
28 | # create public key pair for testing
|
||
29 | bfb56004 | Brian O'Connor | sh( 'rm -rf /tmp/ssh' )
|
30 | a46fae06 | Brian O'Connor | sh( 'mkdir /tmp/ssh' )
|
31 | sh( "ssh-keygen -t rsa -P '' -f /tmp/ssh/test_rsa" )
|
||
32 | sh( 'cat /tmp/ssh/test_rsa.pub >> /tmp/ssh/authorized_keys' )
|
||
33 | 01c0ef00 | Brian O'Connor | # run example with custom sshd args
|
34 | a46fae06 | Brian O'Connor | cmd = ( 'python -m mininet.examples.baresshd '
|
35 | '-o AuthorizedKeysFile=/tmp/ssh/authorized_keys '
|
||
36 | '-o StrictModes=no' )
|
||
37 | 7c5d2771 | cody burkard | 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' ) |
||
48 | a46fae06 | Brian O'Connor | |
49 | def testSSH( self ): |
||
50 | 01c0ef00 | Brian O'Connor | "Simple test to verify that we can ssh into h1"
|
51 | a46fae06 | Brian O'Connor | result = False
|
52 | 01c0ef00 | Brian O'Connor | # try to connect up to 3 times; sshd can take a while to start
|
53 | 7c5d2771 | cody burkard | result = self.connected()
|
54 | a46fae06 | Brian O'Connor | self.assertTrue( result )
|
55 | |||
56 | def tearDown( self ): |
||
57 | # kill the ssh process
|
||
58 | sh( "ps aux | grep 'ssh.*Banner' | awk '{ print $2 }' | xargs kill" )
|
||
59 | cleanup() |
||
60 | # remove public key pair
|
||
61 | sh( 'rm -rf /tmp/ssh' )
|
||
62 | |||
63 | if __name__ == '__main__': |
||
64 | unittest.main() |