]> git.proxmox.com Git - mirror_qemu.git/blame - tests/qapi-schema/test-qapi.py
qapi: Speed up frontend tests
[mirror_qemu.git] / tests / qapi-schema / test-qapi.py
CommitLineData
f01338cc 1#!/usr/bin/env python
98626572
MA
2#
3# QAPI parser test harness
4#
5# Copyright (c) 2013 Red Hat Inc.
6#
7# Authors:
8# Markus Armbruster <armbru@redhat.com>
9#
10# This work is licensed under the terms of the GNU GPL, version 2 or later.
11# See the COPYING file in the top-level directory.
12#
13
ef9d9108 14from __future__ import print_function
f01338cc
MA
15import argparse
16import difflib
17import os
98626572 18import sys
f01338cc
MA
19if sys.version_info[0] < 3:
20 from cStringIO import StringIO
21else:
22 from io import StringIO
181feaf3 23from qapi.common import QAPIError, QAPISchema, QAPISchemaVisitor
98626572 24
156402e5
MA
25
26class QAPISchemaTestVisitor(QAPISchemaVisitor):
cf40a0a5
MA
27
28 def visit_module(self, name):
29 print('module %s' % name)
30
31 def visit_include(self, name, info):
32 print('include %s' % name)
33
1962bd39 34 def visit_enum_type(self, name, info, ifcond, members, prefix):
1e381b65 35 print('enum %s' % name)
156402e5 36 if prefix:
ef9d9108 37 print(' prefix %s' % prefix)
1e381b65
MAL
38 for m in members:
39 print(' member %s' % m.name)
6cc32b0e 40 self._print_if(m.ifcond, indent=8)
fbf09a2f 41 self._print_if(ifcond)
156402e5 42
ca0ac758
MA
43 def visit_array_type(self, name, info, ifcond, element_type):
44 if not info:
45 return # suppress built-in arrays
46 print('array %s %s' % (name, element_type.name))
47 self._print_if(ifcond)
48
6a8c0b51
KW
49 def visit_object_type(self, name, info, ifcond, base, members, variants,
50 features):
ef9d9108 51 print('object %s' % name)
156402e5 52 if base:
ef9d9108 53 print(' base %s' % base.name)
156402e5 54 for m in members:
b736e25a
MA
55 print(' member %s: %s optional=%s'
56 % (m.name, m.type.name, m.optional))
ccadd6bc 57 self._print_if(m.ifcond, 8)
156402e5 58 self._print_variants(variants)
fbf09a2f 59 self._print_if(ifcond)
8aa3a33e
KW
60 if features:
61 for f in features:
62 print(' feature %s' % f.name)
63 self._print_if(f.ifcond, 8)
156402e5 64
fbf09a2f 65 def visit_alternate_type(self, name, info, ifcond, variants):
ef9d9108 66 print('alternate %s' % name)
156402e5 67 self._print_variants(variants)
fbf09a2f 68 self._print_if(ifcond)
156402e5 69
fbf09a2f 70 def visit_command(self, name, info, ifcond, arg_type, ret_type, gen,
d6fe3d02 71 success_response, boxed, allow_oob, allow_preconfig):
b736e25a
MA
72 print('command %s %s -> %s'
73 % (name, arg_type and arg_type.name,
74 ret_type and ret_type.name))
75 print(' gen=%s success_response=%s boxed=%s oob=%s preconfig=%s'
76 % (gen, success_response, boxed, allow_oob, allow_preconfig))
fbf09a2f 77 self._print_if(ifcond)
156402e5 78
fbf09a2f 79 def visit_event(self, name, info, ifcond, arg_type, boxed):
ef9d9108
DB
80 print('event %s %s' % (name, arg_type and arg_type.name))
81 print(' boxed=%s' % boxed)
fbf09a2f 82 self._print_if(ifcond)
156402e5
MA
83
84 @staticmethod
85 def _print_variants(variants):
86 if variants:
ef9d9108 87 print(' tag %s' % variants.tag_member.name)
156402e5 88 for v in variants.variants:
ef9d9108 89 print(' case %s: %s' % (v.name, v.type.name))
a2724280 90 QAPISchemaTestVisitor._print_if(v.ifcond, indent=8)
156402e5 91
fbf09a2f
MAL
92 @staticmethod
93 def _print_if(ifcond, indent=4):
94 if ifcond:
95 print('%sif %s' % (' ' * indent, ifcond))
96
181feaf3 97
f01338cc
MA
98def test_frontend(fname):
99 schema = QAPISchema(fname)
100 schema.visit(QAPISchemaTestVisitor())
101
102 for doc in schema.docs:
103 if doc.symbol:
104 print('doc symbol=%s' % doc.symbol)
105 else:
106 print('doc freeform')
107 print(' body=\n%s' % doc.body.text)
108 for arg, section in doc.args.items():
109 print(' arg=%s\n%s' % (arg, section.text))
110 for section in doc.sections:
111 print(' section=%s\n%s' % (section.name, section.text))
112
113
114def test_and_diff(test_name, dir_name, update):
115 sys.stdout = StringIO()
116 try:
117 test_frontend(os.path.join(dir_name, test_name + '.json'))
118 except QAPIError as err:
119 if err.info.fname is None:
120 print("%s" % err, file=sys.stderr)
121 return 2
122 errstr = str(err) + '\n'
123 if dir_name:
124 errstr = errstr.replace(dir_name + '/', '')
125 actual_err = errstr.splitlines(True)
818c3318 126 else:
f01338cc
MA
127 actual_err = []
128 finally:
129 actual_out = sys.stdout.getvalue().splitlines(True)
130 sys.stdout.close()
131 sys.stdout = sys.__stdout__
132
133 mode = 'r+' if update else 'r'
134 try:
135 outfp = open(os.path.join(dir_name, test_name + '.out'), mode)
136 errfp = open(os.path.join(dir_name, test_name + '.err'), mode)
137 expected_out = outfp.readlines()
138 expected_err = errfp.readlines()
139 except IOError as err:
140 print("%s: can't open '%s': %s"
141 % (sys.argv[0], err.filename, err.strerror),
142 file=sys.stderr)
143 return 2
144
145 if actual_out == expected_out and actual_err == expected_err:
146 return 0
147
148 print("%s %s" % (test_name, 'UPDATE' if update else 'FAIL'),
149 file=sys.stderr)
150 out_diff = difflib.unified_diff(expected_out, actual_out, outfp.name)
151 err_diff = difflib.unified_diff(expected_err, actual_err, errfp.name)
152 sys.stdout.writelines(out_diff)
153 sys.stdout.writelines(err_diff)
154
155 if not update:
156 return 1
157
158 try:
159 outfp.truncate(0)
160 outfp.seek(0)
161 outfp.writelines(actual_out)
162 errfp.truncate(0)
163 errfp.seek(0)
164 errfp.writelines(actual_err)
165 except IOError as err:
166 print("%s: can't write '%s': %s"
167 % (sys.argv[0], err.filename, err.strerror),
168 file=sys.stderr)
169 return 2
170
171 return 0
172
173
174def main(argv):
175 parser = argparse.ArgumentParser(
176 description='QAPI schema tester')
177 parser.add_argument('-d', '--dir', action='store', default='',
178 help="directory containing tests")
179 parser.add_argument('-u', '--update', action='store_true',
180 help="update expected test results")
181 parser.add_argument('tests', nargs='*', metavar='TEST', action='store')
182 args = parser.parse_args()
183
184 status = 0
185 for t in args.tests:
186 (dir_name, base_name) = os.path.split(t)
187 dir_name = dir_name or args.dir
188 test_name = os.path.splitext(base_name)[0]
189 status |= test_and_diff(test_name, dir_name, args.update)
190
191 exit(status)
192
193
194if __name__ == '__main__':
195 main(sys.argv)
196 exit(0)