tests: add CLI query tests
authorSimon Marchi <simon.marchi@efficios.com>
Tue, 22 Oct 2019 21:55:25 +0000 (17:55 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 20 Nov 2019 16:07:57 +0000 (11:07 -0500)
Add a few simple tests for the query CLI command, using a custom Python
component class.

Change-Id: Ic2b2d3f9f5e0f460f596bd1e4403c250ec2daad5
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/2245
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
tests/Makefile.am
tests/cli/query/test_query [new file with mode: 0755]
tests/data/cli/params/bt_plugin_params.py
tests/data/cli/query/bt_plugin_query.py [new file with mode: 0644]
tests/utils/python/cli_params_to_string.py [new file with mode: 0644]

index 079e79227f4dd6c9bdc36b88d316256e79f57bfd..d341c3cdbffccdb89c6a17264bde1c221d451e3a 100644 (file)
@@ -42,6 +42,7 @@ dist_check_SCRIPTS = \
        cli/convert/test_auto_source_discovery_log_level \
        cli/convert/test_convert_args \
        cli/params/test_params \
+       cli/query/test_query \
        cli/test_exit_status \
        cli/test_help \
        cli/test_intersection \
@@ -126,6 +127,7 @@ TESTS_CLI += \
        cli/convert/test_auto_source_discovery_log_level \
        cli/convert/test_auto_source_discovery_params \
        cli/params/test_params \
+       cli/query/test_query
        cli/test_exit_status
 
 TESTS_PLUGINS += plugins/flt.utils.trimmer/test_trimming \
diff --git a/tests/cli/query/test_query b/tests/cli/query/test_query
new file mode 100755 (executable)
index 0000000..1be9fa2
--- /dev/null
@@ -0,0 +1,98 @@
+#!/bin/bash
+#
+# Copyright (C) 2019 Simon Marchi <simon.marchi@efficios.com>
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License, version 2 only, as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+# more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program; if not, write to the Free Software Foundation, Inc., 51
+# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+if [ "x${BT_TESTS_SRCDIR:-}" != "x" ]; then
+       UTILSSH="$BT_TESTS_SRCDIR/utils/utils.sh"
+else
+       UTILSSH="$(dirname "$0")/../../utils/utils.sh"
+fi
+
+# shellcheck source=../../utils/utils.sh
+SH_TAP=1 source "$UTILSSH"
+
+NUM_TESTS=11
+
+plan_tests $NUM_TESTS
+
+data_dir="${BT_TESTS_DATADIR}/cli/query"
+plugin_dir="${data_dir}"
+
+stdout_expected_file=$(mktemp -t test_cli_query_stdout_xpected.XXXXXX)
+stdout_file=$(mktemp -t test_cli_query_stdout.XXXXXX)
+stderr_file=$(mktemp -t test_cli_query_stderr.XXXXXX)
+
+expect_success() {
+       local expected_str="$1"
+       shift 1
+       local args=("$@")
+
+       echo "$expected_str" > "$stdout_expected_file"
+
+       bt_diff_cli "$stdout_expected_file" /dev/null \
+               --plugin-path "$plugin_dir" \
+               query "src.query.SourceWithQueryThatPrintsParams" \
+               "${args[@]}"
+       ok "$?" "${args[*]}"
+}
+
+expect_failure() {
+       local expected_str="$1"
+       shift 1
+       local args=("$@")
+       local test_name="${args[*]}"
+
+       echo -n > "$stdout_expected_file"
+
+       bt_cli "$stdout_file" "$stderr_file" \
+               --plugin-path "$plugin_dir" \
+               query \
+               "${args[@]}"
+       isnt "$?" 0 "${test_name}: exit code is not 0"
+
+       bt_diff /dev/null "$stdout_file"
+       ok "$?" "${test_name}: nothing output on stout"
+
+       # Ensure that a CLI error stack is printed (and that babeltrace doesn't
+       # abort before that).
+       grep --silent "^ERROR: " "${stderr_file}"
+       ok $? "${test_name}: babeltrace produces an error stack"
+
+       grep --silent "${expected_str}" "${stderr_file}"
+       ok "$?" "${test_name}: expect \`${expected_str}\` error message on stderr"
+}
+
+expect_success 'the-object:{}' \
+       'the-object'
+expect_success "the-object:{a=2}" \
+       'the-object' -p 'a=2'
+
+# Check that -p parameters are processed in order.
+expect_success "the-object:{a=3, ben=kin, voyons=donc}" \
+       'the-object' -p 'a=2,ben=kin' -p 'voyons=donc,a=3'
+
+# Failure inside the component class' query method.
+expect_failure "ValueError: catastrophic failure" \
+       'src.query.SourceWithQueryThatPrintsParams' 'please-fail' '-p' 'a=2'
+
+# Non-existent component class.
+expect_failure 'Cannot find component class: plugin-name="query", comp-cls-name="NonExistentSource", comp-cls-type=1' \
+       'src.query.NonExistentSource' 'the-object' '-p' 'a=2'
+
+
+rm -f "$stdout_expected_file"
+rm -f "$stdout_file"
+rm -f "$stderr_file"
index 63c655b0e74be0fd1ca1edbcdb22c7fbed1138a6..873dfa84caae550a2a602d9f80355fec013067bb 100644 (file)
 #
 
 import bt2
-
-
-def to_string(p):
-    # Print BT values in a predictable way (the order of map entries) and with
-    # additional information (u suffix to differentiate unsigned integers from
-    # signed integers).
-
-    if type(p) is bt2._ArrayValueConst:
-        s = '[{}]'.format(', '.join([to_string(x) for x in p]))
-    elif type(p) is bt2._MapValueConst:
-        s = '{{{}}}'.format(
-            ', '.join([k + '=' + to_string(p[k]) for k in sorted(p.keys())])
-        )
-    elif type(p) is bt2._UnsignedIntegerValueConst:
-        s = str(p) + 'u'
-    elif (
-        type(p)
-        in (
-            bt2._StringValueConst,
-            bt2._SignedIntegerValueConst,
-            bt2._RealValueConst,
-            bt2._BoolValueConst,
-        )
-        or p is None
-    ):
-        s = str(p)
-
-    else:
-        raise TypeError('Unexpected type', type(p))
-
-    return s
+from cli_params_to_string import to_string
 
 
 @bt2.plugin_component_class
diff --git a/tests/data/cli/query/bt_plugin_query.py b/tests/data/cli/query/bt_plugin_query.py
new file mode 100644 (file)
index 0000000..65dcfa8
--- /dev/null
@@ -0,0 +1,35 @@
+#
+# Copyright (C) 2019 EfficiOS Inc.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; only version 2
+# of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#
+
+import bt2
+from cli_params_to_string import to_string
+
+
+@bt2.plugin_component_class
+class SourceWithQueryThatPrintsParams(
+    bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator
+):
+    @classmethod
+    def _user_query(cls, executor, obj, params, method_obj):
+        if obj == 'please-fail':
+            raise ValueError('catastrophic failure')
+
+        return obj + ':' + to_string(params)
+
+
+bt2.register_plugin(__name__, "query")
diff --git a/tests/utils/python/cli_params_to_string.py b/tests/utils/python/cli_params_to_string.py
new file mode 100644 (file)
index 0000000..a3d5ff8
--- /dev/null
@@ -0,0 +1,50 @@
+#
+# Copyright (C) 2019 EfficiOS Inc.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; only version 2
+# of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#
+
+import bt2
+
+
+def to_string(p):
+    # Print BT values in a predictable way (the order of map entries) and with
+    # additional information (u suffix to differentiate unsigned integers from
+    # signed integers).
+
+    if type(p) is bt2._ArrayValueConst:
+        s = '[{}]'.format(', '.join([to_string(x) for x in p]))
+    elif type(p) is bt2._MapValueConst:
+        s = '{{{}}}'.format(
+            ', '.join([k + '=' + to_string(p[k]) for k in sorted(p.keys())])
+        )
+    elif type(p) is bt2._UnsignedIntegerValueConst:
+        s = str(p) + 'u'
+    elif (
+        type(p)
+        in (
+            bt2._StringValueConst,
+            bt2._SignedIntegerValueConst,
+            bt2._RealValueConst,
+            bt2._BoolValueConst,
+        )
+        or p is None
+    ):
+        s = str(p)
+
+    else:
+        raise TypeError('Unexpected type', type(p))
+
+    return s
This page took 0.028871 seconds and 4 git commands to generate.