]>
git.proxmox.com Git - mirror_qemu.git/blob - tests/qapi-schema/test-qapi.py
3 # QAPI parser test harness
5 # Copyright (c) 2013 Red Hat Inc.
8 # Markus Armbruster <armbru@redhat.com>
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.
19 from io
import StringIO
21 from qapi
.error
import QAPIError
22 from qapi
.schema
import QAPISchema
, QAPISchemaVisitor
25 class QAPISchemaTestVisitor(QAPISchemaVisitor
):
27 def visit_module(self
, name
):
28 print('module %s' % name
)
30 def visit_include(self
, name
, info
):
31 print('include %s' % name
)
33 def visit_enum_type(self
, name
, info
, ifcond
, features
, members
, prefix
):
34 print('enum %s' % name
)
36 print(' prefix %s' % prefix
)
38 print(' member %s' % m
.name
)
39 self
._print
_if
(m
.ifcond
, indent
=8)
40 self
._print
_if
(ifcond
)
41 self
._print
_features
(features
)
43 def visit_array_type(self
, name
, info
, ifcond
, element_type
):
45 return # suppress built-in arrays
46 print('array %s %s' % (name
, element_type
.name
))
47 self
._print
_if
(ifcond
)
49 def visit_object_type(self
, name
, info
, ifcond
, features
,
50 base
, members
, variants
):
51 print('object %s' % name
)
53 print(' base %s' % base
.name
)
55 print(' member %s: %s optional=%s'
56 % (m
.name
, m
.type.name
, m
.optional
))
57 self
._print
_if
(m
.ifcond
, 8)
58 self
._print
_features
(m
.features
, indent
=8)
59 self
._print
_variants
(variants
)
60 self
._print
_if
(ifcond
)
61 self
._print
_features
(features
)
63 def visit_alternate_type(self
, name
, info
, ifcond
, features
, variants
):
64 print('alternate %s' % name
)
65 self
._print
_variants
(variants
)
66 self
._print
_if
(ifcond
)
67 self
._print
_features
(features
)
69 def visit_command(self
, name
, info
, ifcond
, features
,
70 arg_type
, ret_type
, gen
, success_response
, boxed
,
71 allow_oob
, allow_preconfig
, coroutine
):
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%s'
76 % (gen
, success_response
, boxed
, allow_oob
, allow_preconfig
,
77 " coroutine=True" if coroutine
else ""))
78 self
._print
_if
(ifcond
)
79 self
._print
_features
(features
)
81 def visit_event(self
, name
, info
, ifcond
, features
, arg_type
, boxed
):
82 print('event %s %s' % (name
, arg_type
and arg_type
.name
))
83 print(' boxed=%s' % boxed
)
84 self
._print
_if
(ifcond
)
85 self
._print
_features
(features
)
88 def _print_variants(variants
):
90 print(' tag %s' % variants
.tag_member
.name
)
91 for v
in variants
.variants
:
92 print(' case %s: %s' % (v
.name
, v
.type.name
))
93 QAPISchemaTestVisitor
._print
_if
(v
.ifcond
, indent
=8)
96 def _print_if(ifcond
, indent
=4):
97 # TODO Drop this hack after replacing OrderedDict by plain
98 # dict (requires Python 3.7)
99 def _massage(subcond
):
100 if isinstance(subcond
, str):
102 if isinstance(subcond
, list):
103 return [_massage(val
) for val
in subcond
]
104 return {key
: _massage(val
) for key
, val
in subcond
.items()}
106 if ifcond
.is_present():
107 print('%sif %s' % (' ' * indent
, _massage(ifcond
.ifcond
)))
110 def _print_features(cls
, features
, indent
=4):
113 print('%sfeature %s' % (' ' * indent
, f
.name
))
114 cls
._print
_if
(f
.ifcond
, indent
+ 4)
117 def test_frontend(fname
):
118 schema
= QAPISchema(fname
)
119 schema
.visit(QAPISchemaTestVisitor())
121 for doc
in schema
.docs
:
123 print('doc symbol=%s' % doc
.symbol
)
125 print('doc freeform')
126 print(' body=\n%s' % doc
.body
.text
)
127 for arg
, section
in doc
.args
.items():
128 print(' arg=%s\n%s' % (arg
, section
.text
))
129 for feat
, section
in doc
.features
.items():
130 print(' feature=%s\n%s' % (feat
, section
.text
))
131 for section
in doc
.sections
:
132 print(' section=%s\n%s' % (section
.name
, section
.text
))
135 def test_and_diff(test_name
, dir_name
, update
):
136 sys
.stdout
= StringIO()
138 test_frontend(os
.path
.join(dir_name
, test_name
+ '.json'))
139 except QAPIError
as err
:
140 errstr
= str(err
) + '\n'
142 errstr
= errstr
.replace(dir_name
+ '/', '')
143 actual_err
= errstr
.splitlines(True)
147 actual_out
= sys
.stdout
.getvalue().splitlines(True)
149 sys
.stdout
= sys
.__stdout
__
151 mode
= 'r+' if update
else 'r'
153 outfp
= open(os
.path
.join(dir_name
, test_name
+ '.out'), mode
)
154 errfp
= open(os
.path
.join(dir_name
, test_name
+ '.err'), mode
)
155 expected_out
= outfp
.readlines()
156 expected_err
= errfp
.readlines()
157 except IOError as err
:
158 print("%s: can't open '%s': %s"
159 % (sys
.argv
[0], err
.filename
, err
.strerror
),
163 if actual_out
== expected_out
and actual_err
== expected_err
:
166 print("%s %s" % (test_name
, 'UPDATE' if update
else 'FAIL'),
168 out_diff
= difflib
.unified_diff(expected_out
, actual_out
, outfp
.name
)
169 err_diff
= difflib
.unified_diff(expected_err
, actual_err
, errfp
.name
)
170 sys
.stdout
.writelines(out_diff
)
171 sys
.stdout
.writelines(err_diff
)
179 outfp
.writelines(actual_out
)
182 errfp
.writelines(actual_err
)
183 except IOError as err
:
184 print("%s: can't write '%s': %s"
185 % (sys
.argv
[0], err
.filename
, err
.strerror
),
193 parser
= argparse
.ArgumentParser(
194 description
='QAPI schema tester')
195 parser
.add_argument('-d', '--dir', action
='store', default
='',
196 help="directory containing tests")
197 parser
.add_argument('-u', '--update', action
='store_true',
198 help="update expected test results")
199 parser
.add_argument('tests', nargs
='*', metavar
='TEST', action
='store')
200 args
= parser
.parse_args()
204 (dir_name
, base_name
) = os
.path
.split(t
)
205 dir_name
= dir_name
or args
.dir
206 test_name
= os
.path
.splitext(base_name
)[0]
207 status |
= test_and_diff(test_name
, dir_name
, args
.update
)
212 if __name__
== '__main__':