]>
Commit | Line | Data |
---|---|---|
f345cfd0 SH |
1 | # Common utilities and Python wrappers for qemu-iotests |
2 | # | |
3 | # Copyright (C) 2012 IBM Corp. | |
4 | # | |
5 | # This program is free software; you can redistribute it and/or modify | |
6 | # it under the terms of the GNU General Public License as published by | |
7 | # the Free Software Foundation; either version 2 of the License, or | |
8 | # (at your option) any later version. | |
9 | # | |
10 | # This program is distributed in the hope that it will be useful, | |
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | # GNU General Public License for more details. | |
14 | # | |
15 | # You should have received a copy of the GNU General Public License | |
16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | # | |
18 | ||
19 | import os | |
20 | import re | |
21 | import subprocess | |
4f450568 | 22 | import string |
f345cfd0 SH |
23 | import unittest |
24 | import sys; sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'QMP')) | |
25 | import qmp | |
26 | ||
27 | __all__ = ['imgfmt', 'imgproto', 'test_dir' 'qemu_img', 'qemu_io', | |
28 | 'VM', 'QMPTestCase', 'notrun', 'main'] | |
29 | ||
30 | # This will not work if arguments or path contain spaces but is necessary if we | |
31 | # want to support the override options that ./check supports. | |
c68b039a PB |
32 | qemu_img_args = os.environ.get('QEMU_IMG', 'qemu-img').strip().split(' ') |
33 | qemu_io_args = os.environ.get('QEMU_IO', 'qemu-io').strip().split(' ') | |
34 | qemu_args = os.environ.get('QEMU', 'qemu').strip().split(' ') | |
f345cfd0 SH |
35 | |
36 | imgfmt = os.environ.get('IMGFMT', 'raw') | |
37 | imgproto = os.environ.get('IMGPROTO', 'file') | |
38 | test_dir = os.environ.get('TEST_DIR', '/var/tmp') | |
39 | ||
40 | def qemu_img(*args): | |
41 | '''Run qemu-img and return the exit code''' | |
42 | devnull = open('/dev/null', 'r+') | |
43 | return subprocess.call(qemu_img_args + list(args), stdin=devnull, stdout=devnull) | |
44 | ||
d2ef210c | 45 | def qemu_img_verbose(*args): |
993d46ce | 46 | '''Run qemu-img without suppressing its output and return the exit code''' |
d2ef210c KW |
47 | return subprocess.call(qemu_img_args + list(args)) |
48 | ||
f345cfd0 SH |
49 | def qemu_io(*args): |
50 | '''Run qemu-io and return the stdout data''' | |
51 | args = qemu_io_args + list(args) | |
52 | return subprocess.Popen(args, stdout=subprocess.PIPE).communicate()[0] | |
53 | ||
54 | class VM(object): | |
55 | '''A QEMU VM''' | |
56 | ||
57 | def __init__(self): | |
58 | self._monitor_path = os.path.join(test_dir, 'qemu-mon.%d' % os.getpid()) | |
59 | self._qemu_log_path = os.path.join(test_dir, 'qemu-log.%d' % os.getpid()) | |
60 | self._args = qemu_args + ['-chardev', | |
61 | 'socket,id=mon,path=' + self._monitor_path, | |
0fd05e8d PB |
62 | '-mon', 'chardev=mon,mode=control', |
63 | '-qtest', 'stdio', '-machine', 'accel=qtest', | |
64 | '-display', 'none', '-vga', 'none'] | |
f345cfd0 SH |
65 | self._num_drives = 0 |
66 | ||
67 | def add_drive(self, path, opts=''): | |
68 | '''Add a virtio-blk drive to the VM''' | |
69 | options = ['if=virtio', | |
70 | 'format=%s' % imgfmt, | |
71 | 'cache=none', | |
72 | 'file=%s' % path, | |
73 | 'id=drive%d' % self._num_drives] | |
74 | if opts: | |
75 | options.append(opts) | |
76 | ||
77 | self._args.append('-drive') | |
78 | self._args.append(','.join(options)) | |
79 | self._num_drives += 1 | |
80 | return self | |
81 | ||
23e956bf CB |
82 | def add_fd(self, fd, fdset, opaque, opts=''): |
83 | '''Pass a file descriptor to the VM''' | |
84 | options = ['fd=%d' % fd, | |
85 | 'set=%d' % fdset, | |
86 | 'opaque=%s' % opaque] | |
87 | if opts: | |
88 | options.append(opts) | |
89 | ||
90 | self._args.append('-add-fd') | |
91 | self._args.append(','.join(options)) | |
92 | return self | |
93 | ||
f345cfd0 SH |
94 | def launch(self): |
95 | '''Launch the VM and establish a QMP connection''' | |
96 | devnull = open('/dev/null', 'rb') | |
97 | qemulog = open(self._qemu_log_path, 'wb') | |
98 | try: | |
99 | self._qmp = qmp.QEMUMonitorProtocol(self._monitor_path, server=True) | |
100 | self._popen = subprocess.Popen(self._args, stdin=devnull, stdout=qemulog, | |
101 | stderr=subprocess.STDOUT) | |
102 | self._qmp.accept() | |
103 | except: | |
104 | os.remove(self._monitor_path) | |
105 | raise | |
106 | ||
107 | def shutdown(self): | |
108 | '''Terminate the VM and clean up''' | |
863a5d04 PB |
109 | if not self._popen is None: |
110 | self._qmp.cmd('quit') | |
111 | self._popen.wait() | |
112 | os.remove(self._monitor_path) | |
113 | os.remove(self._qemu_log_path) | |
114 | self._popen = None | |
f345cfd0 | 115 | |
4f450568 | 116 | underscore_to_dash = string.maketrans('_', '-') |
f345cfd0 SH |
117 | def qmp(self, cmd, **args): |
118 | '''Invoke a QMP command and return the result dict''' | |
4f450568 PB |
119 | qmp_args = dict() |
120 | for k in args.keys(): | |
121 | qmp_args[k.translate(self.underscore_to_dash)] = args[k] | |
122 | ||
123 | return self._qmp.cmd(cmd, args=qmp_args) | |
f345cfd0 | 124 | |
9dfa9f59 PB |
125 | def get_qmp_event(self, wait=False): |
126 | '''Poll for one queued QMP events and return it''' | |
127 | return self._qmp.pull_event(wait=wait) | |
128 | ||
f345cfd0 SH |
129 | def get_qmp_events(self, wait=False): |
130 | '''Poll for queued QMP events and return a list of dicts''' | |
131 | events = self._qmp.get_events(wait=wait) | |
132 | self._qmp.clear_events() | |
133 | return events | |
134 | ||
135 | index_re = re.compile(r'([^\[]+)\[([^\]]+)\]') | |
136 | ||
137 | class QMPTestCase(unittest.TestCase): | |
138 | '''Abstract base class for QMP test cases''' | |
139 | ||
140 | def dictpath(self, d, path): | |
141 | '''Traverse a path in a nested dict''' | |
142 | for component in path.split('/'): | |
143 | m = index_re.match(component) | |
144 | if m: | |
145 | component, idx = m.groups() | |
146 | idx = int(idx) | |
147 | ||
148 | if not isinstance(d, dict) or component not in d: | |
149 | self.fail('failed path traversal for "%s" in "%s"' % (path, str(d))) | |
150 | d = d[component] | |
151 | ||
152 | if m: | |
153 | if not isinstance(d, list): | |
154 | self.fail('path component "%s" in "%s" is not a list in "%s"' % (component, path, str(d))) | |
155 | try: | |
156 | d = d[idx] | |
157 | except IndexError: | |
158 | self.fail('invalid index "%s" in path "%s" in "%s"' % (idx, path, str(d))) | |
159 | return d | |
160 | ||
90f0b711 PB |
161 | def assert_qmp_absent(self, d, path): |
162 | try: | |
163 | result = self.dictpath(d, path) | |
164 | except AssertionError: | |
165 | return | |
166 | self.fail('path "%s" has value "%s"' % (path, str(result))) | |
167 | ||
f345cfd0 SH |
168 | def assert_qmp(self, d, path, value): |
169 | '''Assert that the value for a specific path in a QMP dict matches''' | |
170 | result = self.dictpath(d, path) | |
171 | self.assertEqual(result, value, 'values not equal "%s" and "%s"' % (str(result), str(value))) | |
172 | ||
173 | def notrun(reason): | |
174 | '''Skip this test suite''' | |
175 | # Each test in qemu-iotests has a number ("seq") | |
176 | seq = os.path.basename(sys.argv[0]) | |
177 | ||
178 | open('%s.notrun' % seq, 'wb').write(reason + '\n') | |
179 | print '%s not run: %s' % (seq, reason) | |
180 | sys.exit(0) | |
181 | ||
182 | def main(supported_fmts=[]): | |
183 | '''Run tests''' | |
184 | ||
185 | if supported_fmts and (imgfmt not in supported_fmts): | |
186 | notrun('not suitable for this image format: %s' % imgfmt) | |
187 | ||
188 | # We need to filter out the time taken from the output so that qemu-iotest | |
189 | # can reliably diff the results against master output. | |
190 | import StringIO | |
191 | output = StringIO.StringIO() | |
192 | ||
193 | class MyTestRunner(unittest.TextTestRunner): | |
194 | def __init__(self, stream=output, descriptions=True, verbosity=1): | |
195 | unittest.TextTestRunner.__init__(self, stream, descriptions, verbosity) | |
196 | ||
197 | # unittest.main() will use sys.exit() so expect a SystemExit exception | |
198 | try: | |
199 | unittest.main(testRunner=MyTestRunner) | |
200 | finally: | |
d2ef210c | 201 | sys.stderr.write(re.sub(r'Ran (\d+) tests? in [\d.]+s', r'Ran \1 tests', output.getvalue())) |