test-qapi.py 6.4 KB

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