2
0

test-qapi.py 7.0 KB

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