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