2
0
mirror of https://github.com/openvswitch/ovs synced 2025-08-22 09:58:01 +00:00
ovs/tests/test-json.py
Terry Wilson a7be68a4d7 Test the Python C JSON extension
The C JSON parser was added quite a while ago, but unless you
configure with --enable-shared and have the Python 2/3 development
libraries installed, and the resulting python-ovs module installed,
'make check' won't actually test it.

This patch changes Python-based tests to run from the
$builddir/python directory and makes the tests configurable to use
both JSON backends. There are some unicode failures in the C JSON
extension that I left unfixed in this patch to make it easy to
show run the new tests on broken code. The next patch in this set
works around the issue.

Signed-off-by: Terry Wilson <twilson@redhat.com>
Signed-off-by: Ben Pfaff <blp@ovn.org>
Acked-by: Lucas Alvares Gomes <lucasagomes@gmail.com>
2018-10-11 15:00:46 -07:00

115 lines
3.2 KiB
Python

# Copyright (c) 2009, 2010 Nicira, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function
import codecs
import getopt
import sys
from ovs.db import error
import ovs.json
import six
def print_json(json):
if isinstance(json, six.string_types):
print("error: %s" % json)
return False
else:
ovs.json.to_stream(json, sys.stdout)
sys.stdout.write("\n")
return True
def parse_multiple(stream):
buf = stream.read(4096)
ok = True
parser = None
while len(buf):
if parser is None and buf[0] in " \t\r\n":
buf = buf[1:]
else:
if parser is None:
parser = ovs.json.Parser()
n = parser.feed(buf)
buf = buf[n:]
if len(buf):
if not print_json(parser.finish()):
ok = False
parser = None
if len(buf) == 0:
buf = stream.read(4096)
if parser and not print_json(parser.finish()):
ok = False
return ok
def main(argv):
argv0 = argv[0]
# When this is used with Python 3, the program produces no output.
if six.PY2:
# Make stdout and stderr UTF-8, even if they are redirected to a file.
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
sys.stderr = codecs.getwriter("utf-8")(sys.stderr)
try:
options, args = getopt.gnu_getopt(argv[1:], 'j:',
['multiple', 'json-parser'])
except getopt.GetoptError as geo:
sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
sys.exit(1)
multiple = False
ORIG_PARSER = ovs.json.PARSER
ovs.json.PARSER = ovs.json.PARSER_PY
for key, value in options:
if key == '--multiple':
multiple = True
elif key in ('-j', '--json-parser'):
if value == "python":
ovs.json.PARSER = ovs.json.PARSER_PY
elif value in ('C', 'c'):
if ORIG_PARSER != ovs.json.PARSER_C:
raise error.Error("C parser selected, but not compiled")
else:
ovs.json.PARSER = ovs.json.PARSER_C
else:
sys.stderr.write("%s: unhandled option %s\n" % (argv0, key))
sys.exit(1)
if len(args) != 1:
sys.stderr.write("usage: %s [--multiple] INPUT.json\n" % argv0)
sys.exit(1)
input_file = args[0]
if input_file == "-":
stream = sys.stdin
else:
stream = open(input_file, "r")
if multiple:
ok = parse_multiple(stream)
else:
ok = print_json(ovs.json.from_stream(stream))
if not ok:
sys.exit(1)
if __name__ == '__main__':
main(sys.argv)