Add Python plugin provider tests
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Wed, 9 Aug 2017 22:24:53 +0000 (18:24 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 18 Aug 2017 18:46:41 +0000 (14:46 -0400)
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 <eeppeliteloop@gmail.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
configure.ac
tests/Makefile.am
tests/python-plugin-provider/.gitignore [new file with mode: 0644]
tests/python-plugin-provider/Makefile.am [new file with mode: 0644]
tests/python-plugin-provider/bt_plugin_test_python_plugin_provider.py [new file with mode: 0644]
tests/python-plugin-provider/test.sh.in [new file with mode: 0644]
tests/python-plugin-provider/test_python_plugin_provider.py [new file with mode: 0644]

index ad89358d3f8f21f477038f73e750025e414815e6..aee4f5b9c7ae2ee883416c8ed131d5128e676890 100644 (file)
@@ -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
 
 #
index 7ae72255db6aac3cd796d213eff49df73f8e0420..10471c3418cab175d25835e929e76968ae934306 100644 (file)
@@ -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 (file)
index 0000000..3bdb38e
--- /dev/null
@@ -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 (file)
index 0000000..694329f
--- /dev/null
@@ -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 (file)
index 0000000..83f5e3d
--- /dev/null
@@ -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 (file)
index 0000000..d4e06a1
--- /dev/null
@@ -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 (file)
index 0000000..a1071c9
--- /dev/null
@@ -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')
This page took 0.030397 seconds and 4 git commands to generate.