From: Philippe Proulx Date: Wed, 9 Aug 2017 22:24:53 +0000 (-0400) Subject: Add Python plugin provider tests X-Git-Tag: v2.0.0-pre4~127 X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=a26f524a62622830e1f01e9e3ccc34ef3d93f8cf Add Python plugin provider tests This is a simple test for Python plugins. I'm using Python to load the Python plugin for simplicity, but the C API could load it as well. This tests both python-plugin-provider/python-plugin-provider.c and bindings/python/bt2/py_plugin.py. What happens is this: 1. test.sh executes Python which executes the test (test_python_plugin_provider.py). 2. test_python_plugin_provider.py imports the bt2 Python package. 3. The bt2 Python package imports _native_bt.so. 4. _native_bt.so is linked with libbabeltrace.so. 5. libbabeltrace.so, on construction, loads libbabeltrace-python-plugin-provider.so. 5. test_python_plugin_provider.py tries to load the Python plugin with `bt2.find_plugins()`. 6. `bt2.find_plugins()` calls plugin_create_all_from_file(), which, since the path points to a Python file, makes the Python plugin provider try to initialize the Python interpreter (already initialized by Python in step 1), import the `bt2.py_plugin` module, and load the Python file to create a bt_plugin object. Then the test makes sure that the returned Python plugin object has the expected properties and component classes. Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- diff --git a/configure.ac b/configure.ac index f5507622..62863c0d 100644 --- a/configure.ac +++ b/configure.ac @@ -691,6 +691,7 @@ AC_CONFIG_FILES([ tests/bindings/python/Makefile tests/bindings/python/bt2/Makefile tests/plugins/Makefile + tests/python-plugin-provider/Makefile extras/Makefile extras/valgrind/Makefile plugins/Makefile @@ -738,6 +739,10 @@ AS_IF([test "x$enable_python_bindings_tests" = xyes], [AC_CONFIG_FILES([tests/bindings/python/bt2/testall.sh], [chmod +x tests/bindings/python/bt2/testall.sh])] ) +AS_IF([test "x$enable_python_plugins" = "xyes"], + [AC_CONFIG_FILES([tests/python-plugin-provider/test.sh], [chmod +x tests/python-plugin-provider/test.sh])] +) + AC_OUTPUT # diff --git a/tests/Makefile.am b/tests/Makefile.am index 7ae72255..10471c34 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -7,3 +7,7 @@ EXTRA_DIST = $(srcdir)/ctf-traces/** \ if !ENABLE_BUILT_IN_PLUGINS SUBDIRS += plugins endif + +if ENABLE_PYTHON_PLUGINS +SUBDIRS += python-plugin-provider +endif diff --git a/tests/python-plugin-provider/.gitignore b/tests/python-plugin-provider/.gitignore new file mode 100644 index 00000000..3bdb38e3 --- /dev/null +++ b/tests/python-plugin-provider/.gitignore @@ -0,0 +1,3 @@ +htmlcov +.coverage +test.sh diff --git a/tests/python-plugin-provider/Makefile.am b/tests/python-plugin-provider/Makefile.am new file mode 100644 index 00000000..694329fd --- /dev/null +++ b/tests/python-plugin-provider/Makefile.am @@ -0,0 +1,12 @@ +check_SCRIPTS = test.sh +EXTRA_DIST = \ + $(check_SCRIPTS) \ + test_python_plugin_provider.py \ + bt_plugin_test_python_plugin_provider.py + +LOG_DRIVER_FLAGS='--merge' +LOG_DRIVER = env AM_TAP_AWK='$(AWK)' $(SHELL) $(top_srcdir)/config/tap-driver.sh + +if ENABLE_PYTHON_BINDINGS +TESTS = test.sh +endif diff --git a/tests/python-plugin-provider/bt_plugin_test_python_plugin_provider.py b/tests/python-plugin-provider/bt_plugin_test_python_plugin_provider.py new file mode 100644 index 00000000..83f5e3dc --- /dev/null +++ b/tests/python-plugin-provider/bt_plugin_test_python_plugin_provider.py @@ -0,0 +1,29 @@ +import bt2 + + +class MyIter(bt2._UserNotificationIterator): + pass + + +@bt2.plugin_component_class +class MySource(bt2._UserSourceComponent, + notification_iterator_class=MyIter): + pass + + +@bt2.plugin_component_class +class MyFilter(bt2._UserFilterComponent, + notification_iterator_class=MyIter): + pass + + +@bt2.plugin_component_class +class MySink(bt2._UserSinkComponent): + def _consume(self): + pass + + +bt2.register_plugin(__name__, 'sparkling', author='Philippe Proulx', + description='A delicious plugin.', + version=(1, 2, 3, 'EXTRA'), + license='MIT') diff --git a/tests/python-plugin-provider/test.sh.in b/tests/python-plugin-provider/test.sh.in new file mode 100644 index 00000000..d4e06a18 --- /dev/null +++ b/tests/python-plugin-provider/test.sh.in @@ -0,0 +1,13 @@ +#!/bin/sh + +export PYTHON_PLUGIN_PROVIDER_TEST_PLUGIN_PATH="@abs_top_srcdir@/tests/python-plugin-provider/bt_plugin_test_python_plugin_provider.py" +export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:@abs_top_builddir@/python-plugin-provider:@abs_top_builddir@/python-plugin-provider/.libs" +PYTHON_BUILD_DIR="@abs_top_builddir@/bindings/python" +BT2_NATIVE_LIBS_DIR="@abs_top_builddir@/bindings/python/bt2/.libs" +TESTS_UTILS_PYTHON_DIR="@abs_top_srcdir@/tests/utils/python" +TESTRUNNER_PY="@abs_top_srcdir@/tests/utils/python/testrunner.py" +THIS_DIR="@abs_top_srcdir@/tests/python-plugin-provider" + +PYTHONPATH="$PYTHON_BUILD_DIR:$BT2_NATIVE_LIBS_DIR:$TESTS_UTILS_PYTHON_DIR" \ + "@PYTHON@" "$TESTRUNNER_PY" "$THIS_DIR" +exit $? diff --git a/tests/python-plugin-provider/test_python_plugin_provider.py b/tests/python-plugin-provider/test_python_plugin_provider.py new file mode 100644 index 00000000..a1071c9c --- /dev/null +++ b/tests/python-plugin-provider/test_python_plugin_provider.py @@ -0,0 +1,24 @@ +import unittest +import bt2 +import os + + +class PythonPluginProviderTestCase(unittest.TestCase): + def test_python_plugin_provider(self): + pset = bt2.find_plugins(os.environ['PYTHON_PLUGIN_PROVIDER_TEST_PLUGIN_PATH']) + self.assertEqual(len(pset), 1) + plugin = pset[0] + self.assertEqual(plugin.name, 'sparkling') + self.assertEqual(plugin.author, 'Philippe Proulx') + self.assertEqual(plugin.description, 'A delicious plugin.') + self.assertEqual(plugin.version.major, 1) + self.assertEqual(plugin.version.minor, 2) + self.assertEqual(plugin.version.patch, 3) + self.assertEqual(plugin.version.extra, 'EXTRA') + self.assertEqual(plugin.license, 'MIT') + self.assertEqual(len(plugin.source_component_classes), 1) + self.assertEqual(len(plugin.filter_component_classes), 1) + self.assertEqual(len(plugin.sink_component_classes), 1) + self.assertEqual(plugin.source_component_classes['MySource'].name, 'MySource') + self.assertEqual(plugin.filter_component_classes['MyFilter'].name, 'MyFilter') + self.assertEqual(plugin.sink_component_classes['MySink'].name, 'MySink')