test-qapi.py 6.5 KB

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