test-qapi.py 6.5 KB

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