Skip to content

Commit

Permalink
Test the Python C JSON extension
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
otherwiseguy authored and blp committed Oct 11, 2018
1 parent 39cc92a commit a7be68a
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 82 deletions.
11 changes: 11 additions & 0 deletions python/automake.mk
Expand Up @@ -92,6 +92,17 @@ python-sdist: $(srcdir)/python/ovs/version.py $(ovs_pyfiles) python/ovs/dirs.py

pypi-upload: $(srcdir)/python/ovs/version.py $(ovs_pyfiles) python/ovs/dirs.py
(cd python/ && $(PYTHON) setup.py sdist upload)

ALL_LOCAL += python-build
python-build:$(lib_LTLIBRARIES)
(cd $(srcdir)/python/ && $(PYTHON) setup.py build_py -d $(abs_top_builddir)/python && $(PYTHON) setup.py build_ext -b $(abs_top_builddir)/python -I$(abs_top_srcdir)/include:$(abs_top_builddir)/include -L$(abs_top_builddir)/lib/.libs)
if HAVE_PYTHON3
(cd $(srcdir)/python/ && $(PYTHON3) setup.py build_py -d $(abs_top_builddir)/python && $(PYTHON3) setup.py build_ext -b $(abs_top_builddir)/python -I$(abs_top_srcdir)/include:$(abs_top_builddir)/include -L$(abs_top_builddir)/lib/.libs)

CLEAN_LOCAL += python-clean
python-clean:
rm -rf $(abs_top_builddir)/python
endif
else
ovs-install-data-local:
@:
Expand Down
10 changes: 6 additions & 4 deletions python/ovs/json.py
Expand Up @@ -21,10 +21,13 @@

import six

PARSER_C = 'C'
PARSER_PY = 'PYTHON'
try:
import ovs._json
PARSER = PARSER_C
except ImportError:
pass
PARSER = PARSER_PY

__pychecker__ = 'no-stringiter'

Expand Down Expand Up @@ -91,10 +94,9 @@ class Parser(object):
MAX_HEIGHT = 1000

def __new__(cls, *args, **kwargs):
try:
if PARSER == PARSER_C:
return ovs._json.Parser(*args, **kwargs)
except NameError:
return super(Parser, cls).__new__(cls)
return super(Parser, cls).__new__(cls)

def __init__(self, check_trailer=False):
self.check_trailer = check_trailer
Expand Down
4 changes: 3 additions & 1 deletion tests/atlocal.in
Expand Up @@ -22,8 +22,10 @@ if test x"$PYTHON3" = x; then
export PYTHONCOERCECLOCALE
fi

PYTHONPATH=$abs_top_srcdir/python:$abs_top_builddir/tests:$PYTHONPATH
PYTHONPATH=$abs_top_builddir/python:$abs_top_builddir/tests:$PYTHONPATH
export PYTHONPATH
LD_LIBRARY_PATH=$abs_top_builddir/lib/.libs
export LD_LIBRARY_PATH

PYTHONIOENCODING=utf_8
export PYTHONIOENCODING
Expand Down
35 changes: 21 additions & 14 deletions tests/json.at
Expand Up @@ -9,15 +9,16 @@ m4_define([JSON_CHECK_POSITIVE_C],
AT_CLEANUP])

# JSON_CHECK_POSITIVE_PY(TITLE, INPUT, OUTPUT, TEST-JSON-ARGS,
# PYTHON-CHCEK, PYTHON-BIN)
# PYTHON-CHCEK, PYTHON-BIN, JSON-PARSER = python)
#
m4_define([JSON_CHECK_POSITIVE_PY],
[AT_SETUP([$1])
AT_KEYWORDS([json positive Python])
AT_SKIP_IF([test $5 = no])
AT_SKIP_IF([test $7 = C && ! $6 -c "import ovs._json" 2>/dev/null])
AT_CHECK([printf %s "AS_ESCAPE([$2])" > input])
AT_CAPTURE_FILE([input])
AT_CHECK([$6 $srcdir/test-json.py $4 input], [0], [stdout], [])
AT_CHECK([$6 $srcdir/test-json.py -j m4_default([$7], python) $4 input], [0], [stdout], [])
AT_CHECK([cat stdout], [0], [$3
])
AT_CLEANUP])
Expand All @@ -37,16 +38,17 @@ m4_define([JSON_CHECK_POSITIVE_UCS4PY],

m4_define([JSON_CHECK_POSITIVE],
[JSON_CHECK_POSITIVE_C([$1 - C], [$2], [$3], [$4])
JSON_CHECK_POSITIVE_PY([$1 - Python2], [$2], [$3], [$4],
[$HAVE_PYTHON2], [$PYTHON2])
JSON_CHECK_POSITIVE_PY([$1 - Python3], [$2], [$3], [$4],
[$HAVE_PYTHON3], [$PYTHON3])])
JSON_CHECK_POSITIVE_PY23([$1], [$2], [$3], [$4])])

m4_define([JSON_CHECK_POSITIVE_PY23],
[JSON_CHECK_POSITIVE_PY([$1 - Python2], [$2], [$3], [$4],
[JSON_CHECK_POSITIVE_PY([$1 - Python2(pyjson)], [$2], [$3], [$4],
[$HAVE_PYTHON2], [$PYTHON2])
JSON_CHECK_POSITIVE_PY([$1 - Python3], [$2], [$3], [$4],
[$HAVE_PYTHON3], [$PYTHON3])])
JSON_CHECK_POSITIVE_PY([$1 - Python2(cjson)], [$2], [$3], [$4],
[$HAVE_PYTHON2], [$PYTHON2], C)
JSON_CHECK_POSITIVE_PY([$1 - Python3(pyjson)], [$2], [$3], [$4],
[$HAVE_PYTHON3], [$PYTHON3])
JSON_CHECK_POSITIVE_PY([$1 - Python3(cjson)], [$2], [$3], [$4],
[$HAVE_PYTHON3], [$PYTHON3], C)])

m4_define([JSON_CHECK_NEGATIVE_C],
[AT_SETUP([$1])
Expand All @@ -59,25 +61,30 @@ m4_define([JSON_CHECK_NEGATIVE_C],
AT_CLEANUP])

# JSON_CHECK_NEGATIVE_PY(TITLE, INPUT, OUTPUT, TEST-JSON-ARGS,
# PYTHON-CHCEK, PYTHON-BIN)
# PYTHON-CHCEK, PYTHON-BIN, JSON_PARSER = python)
#
m4_define([JSON_CHECK_NEGATIVE_PY],
[AT_SETUP([$1])
AT_KEYWORDS([json negative Python])
AT_SKIP_IF([test $5 = no])
AT_SKIP_IF([test $7 = C && ! $6 -c "import ovs._json" 2>/dev/null])
AT_CHECK([printf %s "AS_ESCAPE([$2])" > input])
AT_CAPTURE_FILE([input])
AT_CHECK([$6 $srcdir/test-json.py $4 input], [1], [stdout], [])
AT_CHECK([$6 $srcdir/test-json.py -j m4_default([$7], python) $4 input], [1], [stdout], [])
AT_CHECK([[sed 's/^error: [^:]*:/error:/' < stdout]], [0], [$3
])
AT_CLEANUP])

m4_define([JSON_CHECK_NEGATIVE],
[JSON_CHECK_NEGATIVE_C([$1 - C], [$2], [$3], [$4])
JSON_CHECK_NEGATIVE_PY([$1 - Python2], [$2], [$3], [$4],
JSON_CHECK_NEGATIVE_PY([$1 - Python2(pyjson)], [$2], [$3], [$4],
[$HAVE_PYTHON2], [$PYTHON2])
JSON_CHECK_NEGATIVE_PY([$1 - Python3], [$2], [$3], [$4],
[$HAVE_PYTHON3], [$PYTHON3])])
JSON_CHECK_NEGATIVE_PY([$1 - Python2(cjson)], [$2], [$3], [$4],
[$HAVE_PYTHON2], [$PYTHON2], C)
JSON_CHECK_NEGATIVE_PY([$1 - Python3(pyjson)], [$2], [$3], [$4],
[$HAVE_PYTHON3], [$PYTHON3])
JSON_CHECK_NEGATIVE_PY([$1 - Python3(cjson)], [$2], [$3], [$4],
[$HAVE_PYTHON3], [$PYTHON3], C)])

AT_BANNER([JSON -- arrays])

Expand Down

0 comments on commit a7be68a

Please sign in to comment.