summaryrefslogtreecommitdiff
path: root/systest.py
blob: bdcb3b6cce75af6d4c34569e4e49d092f70ff0c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Copyright 2011  Lars Wirzenius
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


'''System test classes.

This is like ``unittest``, except for testing running systems.

An extension is that the test runner will set up a member, ``settings``,
which acts as a dictionary, and has run-time settings for things like
the hostname of the target system.

'''


import logging
import re
import subprocess
import unittest


class TestCase(unittest.TestCase):

    '''Base class for system tests.
    
    This extends ``unittest.TestCase`` with some things that are 
    relevant to system tests.
    
    '''

    def runcmd(self, argv, stdin='', *args, **kwargs):
        logging.debug('systest.TestCase.runcmd: argv=%s' % repr(argv))
        p = subprocess.Popen(argv, 
                             stdin=subprocess.PIPE, 
                             stdout=subprocess.PIPE, 
                             stderr=subprocess.PIPE)
        out, err = p.communicate(stdin)
        return p.returncode, out, err

    def hostcmd(self, argv, *args, **kwargs):
        '''Run external command on test host.'''
        returncode, out, err = self.runcmd(argv, *args, **kwargs)
        if returncode:
            msg = 'host command failed: %s\n%s' % (' '.join(argv), err)
            raise AssertionError(msg)
        return out

    def targetcmd(self, argv, *args, **kwargs):
        '''Run command on test target.'''
        full_argv = (['ssh', '-l', self.settings['user'], 
                      '-p', str(self.settings['ssh-port']),
                      self.settings['target']] + 
                     argv)
        returncode, out, err = self.runcmd(full_argv, *args, **kwargs)
        if returncode:
            msg = 'target command failed: %s\n%s' % (' '.join(argv), err)
            raise AssertionError(msg)
        return out

    def assertMatches(self, pat, text, msg=None):
        self.assert_(re.match(pat, text),
                     msg=('pattern %s does not match %s' % (pat, text)) or msg)

    def find_open_ports(self):
        '''Return list of open ports.
        
        Each port is shown as '22/tcp', where '22' is the port number,
        and 'tcp' indicates the TCP protocol. See nmap documentation for
        more info on ports.
        
        '''

        out = self.hostcmd(['nmap', self.settings['target']])
        return [line.split()[0]
                 for line in out.splitlines()
                 if ' open ' in line]