]>
git.proxmox.com Git - qemu.git/blob - tests/qemu-iotests/iotests.py
1 # Common utilities and Python wrappers for qemu-iotests
3 # Copyright (C) 2012 IBM Corp.
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.
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.
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/>.
24 import sys
; sys
.path
.append(os
.path
.join(os
.path
.dirname(__file__
), '..', '..', 'QMP'))
27 __all__
= ['imgfmt', 'imgproto', 'test_dir' 'qemu_img', 'qemu_io',
28 'VM', 'QMPTestCase', 'notrun', 'main']
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.
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(' ')
36 imgfmt
= os
.environ
.get('IMGFMT', 'raw')
37 imgproto
= os
.environ
.get('IMGPROTO', 'file')
38 test_dir
= os
.environ
.get('TEST_DIR', '/var/tmp')
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
)
46 '''Run qemu-io and return the stdout data'''
47 args
= qemu_io_args
+ list(args
)
48 return subprocess
.Popen(args
, stdout
=subprocess
.PIPE
).communicate()[0]
54 self
._monitor
_path
= os
.path
.join(test_dir
, 'qemu-mon.%d' % os
.getpid())
55 self
._qemu
_log
_path
= os
.path
.join(test_dir
, 'qemu-log.%d' % os
.getpid())
56 self
._args
= qemu_args
+ ['-chardev',
57 'socket,id=mon,path=' + self
._monitor
_path
,
58 '-mon', 'chardev=mon,mode=control',
59 '-qtest', 'stdio', '-machine', 'accel=qtest',
60 '-display', 'none', '-vga', 'none']
63 def add_drive(self
, path
, opts
=''):
64 '''Add a virtio-blk drive to the VM'''
65 options
= ['if=virtio',
69 'id=drive%d' % self
._num
_drives
]
73 self
._args
.append('-drive')
74 self
._args
.append(','.join(options
))
79 '''Launch the VM and establish a QMP connection'''
80 devnull
= open('/dev/null', 'rb')
81 qemulog
= open(self
._qemu
_log
_path
, 'wb')
83 self
._qmp
= qmp
.QEMUMonitorProtocol(self
._monitor
_path
, server
=True)
84 self
._popen
= subprocess
.Popen(self
._args
, stdin
=devnull
, stdout
=qemulog
,
85 stderr
=subprocess
.STDOUT
)
88 os
.remove(self
._monitor
_path
)
92 '''Terminate the VM and clean up'''
93 if not self
._popen
is None:
96 os
.remove(self
._monitor
_path
)
97 os
.remove(self
._qemu
_log
_path
)
100 underscore_to_dash
= string
.maketrans('_', '-')
101 def qmp(self
, cmd
, **args
):
102 '''Invoke a QMP command and return the result dict'''
104 for k
in args
.keys():
105 qmp_args
[k
.translate(self
.underscore_to_dash
)] = args
[k
]
107 return self
._qmp
.cmd(cmd
, args
=qmp_args
)
109 def get_qmp_events(self
, wait
=False):
110 '''Poll for queued QMP events and return a list of dicts'''
111 events
= self
._qmp
.get_events(wait
=wait
)
112 self
._qmp
.clear_events()
115 index_re
= re
.compile(r
'([^\[]+)\[([^\]]+)\]')
117 class QMPTestCase(unittest
.TestCase
):
118 '''Abstract base class for QMP test cases'''
120 def dictpath(self
, d
, path
):
121 '''Traverse a path in a nested dict'''
122 for component
in path
.split('/'):
123 m
= index_re
.match(component
)
125 component
, idx
= m
.groups()
128 if not isinstance(d
, dict) or component
not in d
:
129 self
.fail('failed path traversal for "%s" in "%s"' % (path
, str(d
)))
133 if not isinstance(d
, list):
134 self
.fail('path component "%s" in "%s" is not a list in "%s"' % (component
, path
, str(d
)))
138 self
.fail('invalid index "%s" in path "%s" in "%s"' % (idx
, path
, str(d
)))
141 def assert_qmp_absent(self
, d
, path
):
143 result
= self
.dictpath(d
, path
)
144 except AssertionError:
146 self
.fail('path "%s" has value "%s"' % (path
, str(result
)))
148 def assert_qmp(self
, d
, path
, value
):
149 '''Assert that the value for a specific path in a QMP dict matches'''
150 result
= self
.dictpath(d
, path
)
151 self
.assertEqual(result
, value
, 'values not equal "%s" and "%s"' % (str(result
), str(value
)))
154 '''Skip this test suite'''
155 # Each test in qemu-iotests has a number ("seq")
156 seq
= os
.path
.basename(sys
.argv
[0])
158 open('%s.notrun' % seq
, 'wb').write(reason
+ '\n')
159 print '%s not run: %s' % (seq
, reason
)
162 def main(supported_fmts
=[]):
165 if supported_fmts
and (imgfmt
not in supported_fmts
):
166 notrun('not suitable for this image format: %s' % imgfmt
)
168 # We need to filter out the time taken from the output so that qemu-iotest
169 # can reliably diff the results against master output.
171 output
= StringIO
.StringIO()
173 class MyTestRunner(unittest
.TextTestRunner
):
174 def __init__(self
, stream
=output
, descriptions
=True, verbosity
=1):
175 unittest
.TextTestRunner
.__init
__(self
, stream
, descriptions
, verbosity
)
177 # unittest.main() will use sys.exit() so expect a SystemExit exception
179 unittest
.main(testRunner
=MyTestRunner
)
181 sys
.stderr
.write(re
.sub(r
'Ran (\d+) test[s] in [\d.]+s', r
'Ran \1 tests', output
.getvalue()))