2020-01-30 17:32:24 +01:00
|
|
|
#!/usr/bin/env python3
|
2013-07-27 17:41:53 +02:00
|
|
|
#
|
|
|
|
# QAPI parser test harness
|
|
|
|
#
|
|
|
|
# Copyright (c) 2013 Red Hat Inc.
|
|
|
|
#
|
|
|
|
# Authors:
|
|
|
|
# Markus Armbruster <armbru@redhat.com>
|
|
|
|
#
|
|
|
|
# This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
# See the COPYING file in the top-level directory.
|
|
|
|
#
|
|
|
|
|
2019-10-18 09:43:44 +02:00
|
|
|
|
2019-10-18 09:43:42 +02:00
|
|
|
import argparse
|
|
|
|
import difflib
|
|
|
|
import os
|
2013-07-27 17:41:53 +02:00
|
|
|
import sys
|
2020-03-04 16:59:30 +01:00
|
|
|
from io import StringIO
|
2019-10-18 09:43:44 +02:00
|
|
|
|
|
|
|
from qapi.error import QAPIError
|
|
|
|
from qapi.schema import QAPISchema, QAPISchemaVisitor
|
|
|
|
|
2015-09-16 13:06:08 +02:00
|
|
|
|
|
|
|
class QAPISchemaTestVisitor(QAPISchemaVisitor):
|
2018-02-11 10:35:55 +01:00
|
|
|
|
|
|
|
def visit_module(self, name):
|
|
|
|
print('module %s' % name)
|
|
|
|
|
|
|
|
def visit_include(self, name, info):
|
|
|
|
print('include %s' % name)
|
|
|
|
|
2020-03-17 12:54:37 +01:00
|
|
|
def visit_enum_type(self, name, info, ifcond, features, members, prefix):
|
2018-12-13 13:37:05 +01:00
|
|
|
print('enum %s' % name)
|
2015-09-16 13:06:08 +02:00
|
|
|
if prefix:
|
2018-01-16 14:42:04 +01:00
|
|
|
print(' prefix %s' % prefix)
|
2018-12-13 13:37:05 +01:00
|
|
|
for m in members:
|
|
|
|
print(' member %s' % m.name)
|
2018-12-13 13:37:11 +01:00
|
|
|
self._print_if(m.ifcond, indent=8)
|
2018-07-03 17:56:38 +02:00
|
|
|
self._print_if(ifcond)
|
2020-03-17 12:54:37 +01:00
|
|
|
self._print_features(features)
|
2015-09-16 13:06:08 +02:00
|
|
|
|
2019-03-01 16:40:45 +01:00
|
|
|
def visit_array_type(self, name, info, ifcond, element_type):
|
|
|
|
if not info:
|
|
|
|
return # suppress built-in arrays
|
|
|
|
print('array %s %s' % (name, element_type.name))
|
|
|
|
self._print_if(ifcond)
|
|
|
|
|
2020-03-17 12:54:38 +01:00
|
|
|
def visit_object_type(self, name, info, ifcond, features,
|
|
|
|
base, members, variants):
|
2018-01-16 14:42:04 +01:00
|
|
|
print('object %s' % name)
|
2015-09-16 13:06:08 +02:00
|
|
|
if base:
|
2018-01-16 14:42:04 +01:00
|
|
|
print(' base %s' % base.name)
|
2015-09-16 13:06:08 +02:00
|
|
|
for m in members:
|
2018-06-21 10:35:51 +02:00
|
|
|
print(' member %s: %s optional=%s'
|
|
|
|
% (m.name, m.type.name, m.optional))
|
2018-12-13 13:37:15 +01:00
|
|
|
self._print_if(m.ifcond, 8)
|
2020-03-17 12:54:45 +01:00
|
|
|
self._print_features(m.features, indent=8)
|
2015-09-16 13:06:08 +02:00
|
|
|
self._print_variants(variants)
|
2018-07-03 17:56:38 +02:00
|
|
|
self._print_if(ifcond)
|
2019-10-18 10:14:52 +02:00
|
|
|
self._print_features(features)
|
2015-09-16 13:06:08 +02:00
|
|
|
|
2020-03-17 12:54:37 +01:00
|
|
|
def visit_alternate_type(self, name, info, ifcond, features, variants):
|
2018-01-16 14:42:04 +01:00
|
|
|
print('alternate %s' % name)
|
2015-09-16 13:06:08 +02:00
|
|
|
self._print_variants(variants)
|
2018-07-03 17:56:38 +02:00
|
|
|
self._print_if(ifcond)
|
2020-03-17 12:54:37 +01:00
|
|
|
self._print_features(features)
|
2015-09-16 13:06:08 +02:00
|
|
|
|
2020-03-17 12:54:38 +01:00
|
|
|
def visit_command(self, name, info, ifcond, features,
|
|
|
|
arg_type, ret_type, gen, success_response, boxed,
|
2020-10-05 17:58:49 +02:00
|
|
|
allow_oob, allow_preconfig, coroutine):
|
2018-06-21 10:35:51 +02:00
|
|
|
print('command %s %s -> %s'
|
|
|
|
% (name, arg_type and arg_type.name,
|
|
|
|
ret_type and ret_type.name))
|
2020-10-05 17:58:49 +02:00
|
|
|
print(' gen=%s success_response=%s boxed=%s oob=%s preconfig=%s%s'
|
|
|
|
% (gen, success_response, boxed, allow_oob, allow_preconfig,
|
|
|
|
" coroutine=True" if coroutine else ""))
|
2018-07-03 17:56:38 +02:00
|
|
|
self._print_if(ifcond)
|
2019-10-18 10:14:52 +02:00
|
|
|
self._print_features(features)
|
2015-09-16 13:06:08 +02:00
|
|
|
|
2020-03-17 12:54:37 +01:00
|
|
|
def visit_event(self, name, info, ifcond, features, arg_type, boxed):
|
2018-01-16 14:42:04 +01:00
|
|
|
print('event %s %s' % (name, arg_type and arg_type.name))
|
2019-10-18 10:14:50 +02:00
|
|
|
print(' boxed=%s' % boxed)
|
2018-07-03 17:56:38 +02:00
|
|
|
self._print_if(ifcond)
|
2020-03-17 12:54:37 +01:00
|
|
|
self._print_features(features)
|
2015-09-16 13:06:08 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _print_variants(variants):
|
|
|
|
if variants:
|
2018-01-16 14:42:04 +01:00
|
|
|
print(' tag %s' % variants.tag_member.name)
|
2015-09-16 13:06:08 +02:00
|
|
|
for v in variants.variants:
|
2018-01-16 14:42:04 +01:00
|
|
|
print(' case %s: %s' % (v.name, v.type.name))
|
2018-12-13 13:37:17 +01:00
|
|
|
QAPISchemaTestVisitor._print_if(v.ifcond, indent=8)
|
2015-09-16 13:06:08 +02:00
|
|
|
|
2018-07-03 17:56:38 +02:00
|
|
|
@staticmethod
|
|
|
|
def _print_if(ifcond, indent=4):
|
|
|
|
if ifcond:
|
|
|
|
print('%sif %s' % (' ' * indent, ifcond))
|
|
|
|
|
2019-10-18 10:14:52 +02:00
|
|
|
@classmethod
|
2020-03-17 12:54:45 +01:00
|
|
|
def _print_features(cls, features, indent=4):
|
2019-10-18 10:14:52 +02:00
|
|
|
if features:
|
|
|
|
for f in features:
|
2020-03-17 12:54:45 +01:00
|
|
|
print('%sfeature %s' % (' ' * indent, f.name))
|
|
|
|
cls._print_if(f.ifcond, indent + 4)
|
2019-10-18 10:14:52 +02:00
|
|
|
|
2018-02-11 10:35:51 +01:00
|
|
|
|
2019-10-18 09:43:42 +02:00
|
|
|
def test_frontend(fname):
|
|
|
|
schema = QAPISchema(fname)
|
|
|
|
schema.visit(QAPISchemaTestVisitor())
|
|
|
|
|
|
|
|
for doc in schema.docs:
|
|
|
|
if doc.symbol:
|
|
|
|
print('doc symbol=%s' % doc.symbol)
|
|
|
|
else:
|
|
|
|
print('doc freeform')
|
|
|
|
print(' body=\n%s' % doc.body.text)
|
|
|
|
for arg, section in doc.args.items():
|
|
|
|
print(' arg=%s\n%s' % (arg, section.text))
|
2019-10-24 13:02:22 +02:00
|
|
|
for feat, section in doc.features.items():
|
|
|
|
print(' feature=%s\n%s' % (feat, section.text))
|
2019-10-18 09:43:42 +02:00
|
|
|
for section in doc.sections:
|
|
|
|
print(' section=%s\n%s' % (section.name, section.text))
|
|
|
|
|
|
|
|
|
|
|
|
def test_and_diff(test_name, dir_name, update):
|
|
|
|
sys.stdout = StringIO()
|
|
|
|
try:
|
|
|
|
test_frontend(os.path.join(dir_name, test_name + '.json'))
|
|
|
|
except QAPIError as err:
|
|
|
|
errstr = str(err) + '\n'
|
|
|
|
if dir_name:
|
|
|
|
errstr = errstr.replace(dir_name + '/', '')
|
|
|
|
actual_err = errstr.splitlines(True)
|
2017-03-20 14:11:53 +01:00
|
|
|
else:
|
2019-10-18 09:43:42 +02:00
|
|
|
actual_err = []
|
|
|
|
finally:
|
|
|
|
actual_out = sys.stdout.getvalue().splitlines(True)
|
|
|
|
sys.stdout.close()
|
|
|
|
sys.stdout = sys.__stdout__
|
|
|
|
|
|
|
|
mode = 'r+' if update else 'r'
|
|
|
|
try:
|
|
|
|
outfp = open(os.path.join(dir_name, test_name + '.out'), mode)
|
|
|
|
errfp = open(os.path.join(dir_name, test_name + '.err'), mode)
|
|
|
|
expected_out = outfp.readlines()
|
|
|
|
expected_err = errfp.readlines()
|
|
|
|
except IOError as err:
|
|
|
|
print("%s: can't open '%s': %s"
|
|
|
|
% (sys.argv[0], err.filename, err.strerror),
|
|
|
|
file=sys.stderr)
|
|
|
|
return 2
|
|
|
|
|
|
|
|
if actual_out == expected_out and actual_err == expected_err:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
print("%s %s" % (test_name, 'UPDATE' if update else 'FAIL'),
|
|
|
|
file=sys.stderr)
|
|
|
|
out_diff = difflib.unified_diff(expected_out, actual_out, outfp.name)
|
|
|
|
err_diff = difflib.unified_diff(expected_err, actual_err, errfp.name)
|
|
|
|
sys.stdout.writelines(out_diff)
|
|
|
|
sys.stdout.writelines(err_diff)
|
|
|
|
|
|
|
|
if not update:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
try:
|
|
|
|
outfp.truncate(0)
|
|
|
|
outfp.seek(0)
|
|
|
|
outfp.writelines(actual_out)
|
|
|
|
errfp.truncate(0)
|
|
|
|
errfp.seek(0)
|
|
|
|
errfp.writelines(actual_err)
|
|
|
|
except IOError as err:
|
|
|
|
print("%s: can't write '%s': %s"
|
|
|
|
% (sys.argv[0], err.filename, err.strerror),
|
|
|
|
file=sys.stderr)
|
|
|
|
return 2
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='QAPI schema tester')
|
|
|
|
parser.add_argument('-d', '--dir', action='store', default='',
|
|
|
|
help="directory containing tests")
|
|
|
|
parser.add_argument('-u', '--update', action='store_true',
|
|
|
|
help="update expected test results")
|
|
|
|
parser.add_argument('tests', nargs='*', metavar='TEST', action='store')
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
status = 0
|
|
|
|
for t in args.tests:
|
|
|
|
(dir_name, base_name) = os.path.split(t)
|
|
|
|
dir_name = dir_name or args.dir
|
|
|
|
test_name = os.path.splitext(base_name)[0]
|
|
|
|
status |= test_and_diff(test_name, dir_name, args.update)
|
|
|
|
|
|
|
|
exit(status)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main(sys.argv)
|
|
|
|
exit(0)
|