summaryrefslogtreecommitdiff
path: root/morphlib/tester.py
blob: 569199d7c43fa00ef83f16f91f2a914a8ec2d89f (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Copyright (C) 2011-2012  Codethink Limited
# 
# 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; version 2 of the License.
# 
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


import imp
import os
import re
import select
import subprocess
import sys
import time


class Timeout(Exception):

    def __str__(self):
        buf, regexp = self.args
        return 'Cannot find %s in %s' % (repr(regexp), repr(buf))


class System(object):

    '''Abstract interface to a system.
    
    The interface allows starting and stopping the system, and interacting
    with its via a serial console.
    
    '''

    def start(self):
        '''Start the system.'''
        raise NotImplementedError()

    def stop(self):
        '''Stop the system.'''
        raise NotImplementedError()

    def waitfor(self, regexp, timeout):
        '''Wait system to output a match for ``regexp`` on the serial console.
        
        If timeout is exceeded, raise Timeout.
        
        '''
        raise NotImplementedError()

    def send(self, text):
        '''Send TEXT via the serial console to the system.'''
        raise NotImplementedError()


class KvmSystem(System):

    '''A system running under KVM.'''

    def __init__(self, image_filename, verbose=False, timeout=None):
        self.image_filename = image_filename
        self.verbose = verbose
        self.timeout = timeout
        self.p = None
        self.stdin_buf = ''
        self.stdout_buf = ''
        
    def start(self):
        self.p = subprocess.Popen(['kvm', '-nographic', self.image_filename],
                                  stdin=subprocess.PIPE,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE)

    def stop(self):
        self.p.terminate()
        self.p.wait()

    def _io(self, timeout):
        r, w, x = select.select([self.p.stdout], [self.p.stdin], [], timeout)
        if self.p.stdout in r:
            byte = self.p.stdout.read(1)
            if byte:
                if self.verbose:
                    sys.stdout.write(byte)
                    sys.stdout.flush()
                self.stdout_buf += byte
        elif self.p.stdin in w:
            if self.stdin_buf:
                byte = self.stdin_buf[0]
                self.p.stdin.write(byte)
                self.p.stdin.flush()
                self.stdin_buf = self.stdin_buf[1:]
        else:
            if self.verbose:
                sys.stdout.write('_')
                sys.stdout.flush()

    def waitfor(self, regexp, timeout=None):
        pat = re.compile(regexp, re.DOTALL | re.MULTILINE)
        started = time.time()
        timeout = timeout if timeout is not None else self.timeout
        remaining = timeout
        while not pat.search(self.stdout_buf) and remaining > 0:
            self._io(remaining)
            remaining = (started + timeout) - time.time()
        m = pat.search(self.stdout_buf)
        if not m:
            raise Timeout(self.stdout_buf, regexp)
        self.stdout_buf = self.stdout_buf[m.end():]

    def send(self, text):
        self.stdin_buf += text


class TestStory(object):

    '''Execute a test story.'''
    
    def __init__(self, system, steps, msg):
        self.system = system
        self.steps = steps
        self.msg = msg
        
    def run(self):
        self.system.start()
        for t in self.steps:
            if len(t) == 2:
                send, expect = t
                timeout = None
            else:
                assert len(t) == 3
                send, expect, timeout = t
            self.msg('Sending: %s' % repr(send))
            self.msg('Expecting: %s' % repr(expect))
            self.system.send(send)
            self.system.waitfor(expect, timeout=timeout)
        self.system.stop()


def load_module(filename):
    for t in imp.get_suffixes():
        suffix, mode, kind = t
        if filename.endswith(suffix):
            module_name = os.path.basename(filename[:-len(suffix)])
            with open(filename, mode) as f:
                return imp.load_module(module_name, f, filename, t)
    raise Exception("Unknown module: %s" % filename)