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