test-qapi.py 6.3 KB

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