]>
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.
14 from __future__
import print_function
21 from qapi
.error
import QAPIError
22 from qapi
.schema
import QAPISchema
, QAPISchemaVisitor
24 if sys
.version_info
[0] < 3:
25 from cStringIO
import StringIO
27 from io
import StringIO
30 class QAPISchemaTestVisitor(QAPISchemaVisitor
):
32 def visit_module(self
, name
):
33 print('module %s' % name
)
35 def visit_include(self
, name
, info
):
36 print('include %s' % name
)
38 def visit_enum_type(self
, name
, info
, ifcond
, members
, prefix
):
39 print('enum %s' % name
)
41 print(' prefix %s' % prefix
)
43 print(' member %s' % m
.name
)
44 self
._print
_if
(m
.ifcond
, indent
=8)
45 self
._print
_if
(ifcond
)
47 def visit_array_type(self
, name
, info
, ifcond
, element_type
):
49 return # suppress built-in arrays
50 print('array %s %s' % (name
, element_type
.name
))
51 self
._print
_if
(ifcond
)
53 def visit_object_type(self
, name
, info
, ifcond
, base
, members
, variants
,
55 print('object %s' % name
)
57 print(' base %s' % base
.name
)
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
)
66 print(' feature %s' % f
.name
)
67 self
._print
_if
(f
.ifcond
, 8)
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
)
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
)
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
)
89 def _print_variants(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)
97 def _print_if(ifcond
, indent
=4):
99 print('%sif %s' % (' ' * indent
, ifcond
))
102 def test_frontend(fname
):
103 schema
= QAPISchema(fname
)
104 schema
.visit(QAPISchemaTestVisitor())
106 for doc
in schema
.docs
:
108 print('doc symbol=%s' % doc
.symbol
)
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
))
118 def test_and_diff(test_name
, dir_name
, update
):
119 sys
.stdout
= StringIO()
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
)
126 errstr
= str(err
) + '\n'
128 errstr
= errstr
.replace(dir_name
+ '/', '')
129 actual_err
= errstr
.splitlines(True)
133 actual_out
= sys
.stdout
.getvalue().splitlines(True)
135 sys
.stdout
= sys
.__stdout
__
137 mode
= 'r+' if update
else 'r'
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
),
149 if actual_out
== expected_out
and actual_err
== expected_err
:
152 print("%s %s" % (test_name
, 'UPDATE' if update
else 'FAIL'),
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
)
165 outfp
.writelines(actual_out
)
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
),
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()
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
)
198 if __name__
== '__main__':