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