From: Martin Vidner Date: Tue, 10 Dec 2019 12:50:00 +0000 (+0100) Subject: Test with TAP by comparing error strings X-Git-Tag: v0.8.7^2~10 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=30d0d550230dce65690372e0efd2ccb422667ef5;p=thirdparty%2Fsnapper.git Test with TAP by comparing error strings --- diff --git a/zypp-plugin/testsuite/1-happy.test b/zypp-plugin/testsuite/1-happy.test new file mode 100755 index 00000000..0059caea --- /dev/null +++ b/zypp-plugin/testsuite/1-happy.test @@ -0,0 +1,40 @@ +#!/bin/bash +set -e +set -u +#set -x + +. test-helper.sh + +mock_snapperd_setup || { echo "1..0 # SKIP"; exit; } + +# http://testanything.org/ +echo 1..4 + +TEST="1 - It wants to make pre and post snapshots" +rm -f mock-snapperd.log +test_pre_post_snapshots | runit ../../data/zypp-plugin.conf > /dev/null +if [ "${PIPESTATUS[1]}" != 0 ]; then + echo -n "not " +fi +echo "ok $TEST" + +TEST="2 - It tells snapperd so" +grep "Mock CreatePreSnapshot" mock-snapperd.log \ + && grep "Mock CreatePostSnapshot" mock-snapperd.log \ + || echo -n "not " +echo "ok $TEST" + +TEST="3 - It wants to make and delete pre snapshot" +rm -f mock-snapperd.log +test_pre_del_snapshots | runit ../../data/zypp-plugin.conf > /dev/null +if [ "${PIPESTATUS[1]}" != 0 ]; then + echo -n "not " +fi +echo "ok $TEST" + +TEST="4 - It tells snapperd so" +grep "Mock CreatePreSnapshot" mock-snapperd.log \ + && grep "Mock DeleteSnapshots" mock-snapperd.log \ + || echo -n "not " +rm -f mock-snapperd.log +echo "ok $TEST" diff --git a/zypp-plugin/testsuite/2-malformed-xml.test b/zypp-plugin/testsuite/2-malformed-xml.test new file mode 100755 index 00000000..e09f8557 --- /dev/null +++ b/zypp-plugin/testsuite/2-malformed-xml.test @@ -0,0 +1,17 @@ +#!/bin/bash +set -e +set -u +#set -x + +# http://testanything.org/ +echo 1..1 + +. test-helper.sh + +TEST="1 - It complains about malformed XML in config file" +STDERR=$(test_empty_messages | runit zypp-plugin-malformed.conf 2>&1 >/dev/null || :) +echo "$STDERR" +if [[ ! "$STDERR" =~ parser\ error ]]; then + echo -n "not " +fi +echo "ok $TEST" diff --git a/zypp-plugin/testsuite/3-invalid-xml.test b/zypp-plugin/testsuite/3-invalid-xml.test new file mode 100755 index 00000000..d22b376e --- /dev/null +++ b/zypp-plugin/testsuite/3-invalid-xml.test @@ -0,0 +1,17 @@ +#!/bin/bash +set -e +set -u +#set -x + +# http://testanything.org/ +echo 1..1 + +. test-helper.sh + +TEST="1 - It complains about invalid XML in config file" +STDERR=$(test_pre_post_snapshots | runit zypp-plugin-invalid.conf 2>&1 >/dev/null || :) +echo "$STDERR" +if [[ ! "$STDERR" =~ boost::regex_error ]]; then + echo -n "not " +fi +echo "ok $TEST" diff --git a/zypp-plugin/testsuite/4-badjson.test b/zypp-plugin/testsuite/4-badjson.test new file mode 100755 index 00000000..71333e8c --- /dev/null +++ b/zypp-plugin/testsuite/4-badjson.test @@ -0,0 +1,28 @@ +#!/bin/bash +set -e +set -u +#set -x + +. test-helper.sh + +mock_snapperd_setup || { echo "1..0 # SKIP"; exit; } + +# http://testanything.org/ +echo 1..1 + +test_bad_json() { + local JSON=("${1?JSON payload expected}") + + stomp_message PLUGINBEGIN "" "" + stomp_message COMMITBEGIN "" "${JSON[@]}" + stomp_message COMMITEND "" "" + stomp_message PLUGINEND "" "" +} + +TEST="1 - It complains about bad JSON" +STDERR=$(test_bad_json '{"well-formed": "but-invalid"}' | runit "" 2>&1 >/dev/null || :) +echo "$STDERR" +if [[ ! "$STDERR" =~ rapidjson.*Assertion.*failed ]]; then + echo -n "not " +fi +echo "ok $TEST" diff --git a/zypp-plugin/testsuite/99-shell.test b/zypp-plugin/testsuite/99-shell.test new file mode 100755 index 00000000..8c2eba64 --- /dev/null +++ b/zypp-plugin/testsuite/99-shell.test @@ -0,0 +1,14 @@ +#!/bin/bash +if ! type -P shellcheck >/dev/null; then + echo "1..0 # SKIP No shellcheck" + exit +fi +echo "1..1" + +TEST="1 - All shell scripts pass shellcheck" + +FILES=([0-9]*.test test-helper.sh) +if ! shellcheck --wiki-link-count=99 "${FILES[@]}"; then + echo -n "not " +fi +echo "ok $TEST" diff --git a/zypp-plugin/testsuite/Makefile.am b/zypp-plugin/testsuite/Makefile.am index f19c7dc9..3978f33e 100644 --- a/zypp-plugin/testsuite/Makefile.am +++ b/zypp-plugin/testsuite/Makefile.am @@ -1,13 +1,15 @@ if HAVE_ZYPP -LOG_COMPILER = dbus-run-session -AM_LOG_FLAGS = -- +TEST_LOG_DRIVER = dbus-run-session -- $(SHELL) tap-driver.sh -check_SCRIPTS = test1 test2-malformed-xml test3-invalid-xml test4-badjson -noinst_SCRIPTS = mock-snapperd +check_SCRIPTS = 1-happy.test 2-malformed-xml.test 3-invalid-xml.test 4-badjson.test 99-shell.test +noinst_SCRIPTS = mock-snapperd test-helper.sh noinst_DATA = zypp-plugin-malformed.conf zypp-plugin-invalid.conf +# Hack: these tests both use mock-snapperd which is not paralellized +# so serialize them +4-badjson.log: 1-happy.log + TESTS = $(check_SCRIPTS) -XFAIL_TESTS = test2-malformed-xml test3-invalid-xml test4-badjson -EXTRA_DIST = $(check_SCRIPTS) $(noinst_SCRIPTS) $(noinst_DATA) +EXTRA_DIST = $(check_SCRIPTS) $(noinst_SCRIPTS) $(noinst_DATA) tap-driver.sh endif diff --git a/zypp-plugin/testsuite/mock-snapperd b/zypp-plugin/testsuite/mock-snapperd index a10f3a34..92097114 100755 --- a/zypp-plugin/testsuite/mock-snapperd +++ b/zypp-plugin/testsuite/mock-snapperd @@ -7,7 +7,9 @@ SNAPPER_INTERFACE = "org.opensuse.Snapper" class MockSnapper < DBus::Object def report_mock(name, args) - puts format("Mock %-20s %s", name, args.inspect) + File.open("mock-snapperd.log", "a") do |f| + f.puts format("Mock %-20s %s", name, args.inspect) + end end dbus_interface SNAPPER_INTERFACE do @@ -60,6 +62,8 @@ main = DBus::Main.new main << bus begin main.run +rescue DBus::Connection::NameRequestError + warn "Cannot claim #{SNAPPER_SERVICE}, is it already running?" rescue SystemCallError # the test driver will kill the bus, that's OK end diff --git a/zypp-plugin/testsuite/test-helper.sh b/zypp-plugin/testsuite/test-helper.sh new file mode 100644 index 00000000..03d53254 --- /dev/null +++ b/zypp-plugin/testsuite/test-helper.sh @@ -0,0 +1,78 @@ +#!/bin/bash +# source this from the individual tests + +MYDIR=$(dirname "$0") + +runit() { + local CONFIG="${1:-../../data/zypp-plugin.conf}" + local STRACE="" + # STRACE="strace -efile" + + SNAPPER_ZYPP_PLUGIN_CONFIG="$MYDIR/$CONFIG" \ + SNAPPER_ZYPP_PLUGIN_SNAPPER_CONFIG=testsuite \ + SNAPPER_ZYPP_PLUGIN_DBUS_SESSION=1 \ + $STRACE \ + "$MYDIR"/../snapper-zypp-plugin +} + +# https://stomp.github.io/ +stomp_message() { + local COMMAND="$1" + local HEADERS="$2" + local BODY="$3" + if [ -n "$HEADERS" ]; then + HEADERS="$HEADERS"$'\n' + fi + printf '%s\n%s\n%s\0' "$COMMAND" "$HEADERS" "$BODY" +} + +json() { + local PACKAGE="${1?Package name expected}" + local JSON=('{' + ' "TransactionStepList": [' + ' {' + ' "type": "...",' + ' "stage": "...",' + ' "solvable": {' + " \"n\": \"${PACKAGE}\"" + ' }' + ' }' + ' ]' + '}') + echo "${JSON[@]}" +} + +test_empty_messages() { + stomp_message PLUGINBEGIN "" "" + stomp_message COMMITBEGIN "" "" + stomp_message COMMITEND "" "" + stomp_message PLUGINEND "" "" +} + +test_pre_post_snapshots() { + stomp_message PLUGINBEGIN "userdata: a=b,c=d" "" + stomp_message COMMITBEGIN "" "$(json mypkg)" + stomp_message COMMITEND "" "$(json mypkg)" + stomp_message PLUGINEND "" "" +} + +test_pre_del_snapshots() { + stomp_message PLUGINBEGIN "" "" + stomp_message COMMITBEGIN "" "$(json mypkg)" + stomp_message COMMITEND "" "" + stomp_message PLUGINEND "" "" +} + +mock_snapperd_setup() { + MOCKDEP=(ruby -e "require 'dbus'") + if ! "${MOCKDEP[@]}"; then + echo "Mock snapperd cannot be run, skipping test" + echo "('${MOCKDEP[*]}' failed)" + return 77 + fi + pkill -f mock-snapperd + "$MYDIR"/mock-snapperd & + sleep 1 + PID=$! + trap "kill \$PID" EXIT TERM INT +} diff --git a/zypp-plugin/testsuite/test1 b/zypp-plugin/testsuite/test1 deleted file mode 100755 index 20384e13..00000000 --- a/zypp-plugin/testsuite/test1 +++ /dev/null @@ -1,78 +0,0 @@ -#!/bin/bash -set -e -set -u -#set -x - -MYDIR=$(dirname $0) - -runit() { - local STRACE="" - # STRACE="strace -efile" - - SNAPPER_ZYPP_PLUGIN_CONFIG=$MYDIR/../../data/zypp-plugin.conf \ - SNAPPER_ZYPP_PLUGIN_SNAPPER_CONFIG=testsuite \ - SNAPPER_ZYPP_PLUGIN_DBUS_SESSION=1 \ - DEBUG=1 \ - $STRACE \ - $MYDIR/../snapper-zypp-plugin -} - -stomp_message() { - local COMMAND="$1" - local HEADERS="$2" - local BODY="$3" - printf '%s\n%s\n\n%s\0' "$COMMAND" "$HEADERS" "$BODY" -} - -JSON='{ - "TransactionStepList": [ - { - "type": "...", - "stage": "...", - "solvable": { - "n": "mypackage" - } - } - ] -}' - -test_pre_post() { - stomp_message PLUGINBEGIN "" "" - stomp_message COMMITBEGIN "" "$JSON" - stomp_message COMMITEND "" "$JSON" - stomp_message PLUGINEND "" "" -} - -test_pre_del() { - stomp_message PLUGINBEGIN "" "" - stomp_message COMMITBEGIN "" "$JSON" - stomp_message COMMITEND "" "" - stomp_message PLUGINEND "" "" -} - -setup() { - MOCKDEP=(ruby -rdbus -e 1) - if ! "${MOCKDEP[@]}"; then - echo "Mock snapperd cannot be run, skipping test" - echo "('${MOCKDEP[@]}' failed)" - exit 77 - fi - $MYDIR/mock-snapperd & - sleep 1 - PID=$! - trap "kill \$PID" EXIT TERM INT -} -teardown() { - : -} - -# FIXME: run this automatically, and declare the ruby-dbus dependency -# FIXME: add tests for unhappy paths -# FIXME: is exit code 0 really a success? -# FIXME: try with coverage testing - -setup -# test1 | tee /dev/stderr | runit >&2 -test_pre_post | runit > /dev/null -test_pre_del | runit > /dev/null -teardown diff --git a/zypp-plugin/testsuite/test2-malformed-xml b/zypp-plugin/testsuite/test2-malformed-xml deleted file mode 100755 index 0bdf3124..00000000 --- a/zypp-plugin/testsuite/test2-malformed-xml +++ /dev/null @@ -1,43 +0,0 @@ -#!/bin/bash -set -e -set -u -#set -x - -MYDIR=$(dirname $0) - -runit() { - local STRACE="" - # STRACE="strace -efile" - - SNAPPER_ZYPP_PLUGIN_CONFIG=$MYDIR/zypp-plugin-malformed.conf \ - SNAPPER_ZYPP_PLUGIN_SNAPPER_CONFIG=testsuite \ - SNAPPER_ZYPP_PLUGIN_DBUS_SESSION=1 \ - DEBUG=1 \ - $STRACE \ - $MYDIR/../snapper-zypp-plugin -} - -stomp_message() { - local COMMAND="$1" - local HEADERS="$2" - local BODY="$3" - printf '%s\n%s\n\n%s\0' "$COMMAND" "$HEADERS" "$BODY" -} - -test_empty_messages() { - stomp_message PLUGINBEGIN "" "" - stomp_message COMMITBEGIN "" "" - stomp_message COMMITEND "" "" - stomp_message PLUGINEND "" "" -} - -setup() { - : -} -teardown() { - : -} - -setup -test_empty_messages | runit > /dev/null -teardown diff --git a/zypp-plugin/testsuite/test3-invalid-xml b/zypp-plugin/testsuite/test3-invalid-xml deleted file mode 100755 index 2af036c0..00000000 --- a/zypp-plugin/testsuite/test3-invalid-xml +++ /dev/null @@ -1,56 +0,0 @@ -#!/bin/bash -set -e -set -u -#set -x - -MYDIR=$(dirname $0) - -runit() { - local STRACE="" - # STRACE="strace -efile" - - SNAPPER_ZYPP_PLUGIN_CONFIG=$MYDIR/zypp-plugin-invalid.conf \ - SNAPPER_ZYPP_PLUGIN_SNAPPER_CONFIG=testsuite \ - SNAPPER_ZYPP_PLUGIN_DBUS_SESSION=1 \ - DEBUG=1 \ - $STRACE \ - $MYDIR/../snapper-zypp-plugin -} - -stomp_message() { - local COMMAND="$1" - local HEADERS="$2" - local BODY="$3" - printf '%s\n%s\n\n%s\0' "$COMMAND" "$HEADERS" "$BODY" -} - - -JSON='{ - "TransactionStepList": [ - { - "type": "...", - "stage": "...", - "solvable": { - "n": "mypackage" - } - } - ] -}' - -test_pre_post() { - stomp_message PLUGINBEGIN "" "" - stomp_message COMMITBEGIN "" "$JSON" - stomp_message COMMITEND "" "$JSON" - stomp_message PLUGINEND "" "" -} - -setup() { - : -} -teardown() { - : -} - -setup -test_pre_post | runit > /dev/null -teardown diff --git a/zypp-plugin/testsuite/test4-badjson b/zypp-plugin/testsuite/test4-badjson deleted file mode 100755 index ab73aa8a..00000000 --- a/zypp-plugin/testsuite/test4-badjson +++ /dev/null @@ -1,70 +0,0 @@ -#!/bin/bash -set -e -set -u -#set -x - -MYDIR=$(dirname $0) - -runit() { - local STRACE="" - # STRACE="strace -efile" - - SNAPPER_ZYPP_PLUGIN_CONFIG=$MYDIR/../../data/zypp-plugin.conf \ - SNAPPER_ZYPP_PLUGIN_SNAPPER_CONFIG=testsuite \ - SNAPPER_ZYPP_PLUGIN_DBUS_SESSION=1 \ - DEBUG=1 \ - $STRACE \ - $MYDIR/../snapper-zypp-plugin -} - -stomp_message() { - local COMMAND="$1" - local HEADERS="$2" - local BODY="$3" - printf '%s\n%s\n\n%s\0' "$COMMAND" "$HEADERS" "$BODY" -} - -JSON='{ - "well-formed": "but-invalid" -}' - -test_pre_post() { - stomp_message PLUGINBEGIN "" "" - stomp_message COMMITBEGIN "" "$JSON" - stomp_message COMMITEND "" "$JSON" - stomp_message PLUGINEND "" "" -} - -test_pre_del() { - stomp_message PLUGINBEGIN "" "" - stomp_message COMMITBEGIN "" "$JSON" - stomp_message COMMITEND "" "" - stomp_message PLUGINEND "" "" -} - -setup() { - MOCKDEP=(ruby -rdbus -e 1) - if ! "${MOCKDEP[@]}"; then - echo "Mock snapperd cannot be run, skipping test" - echo "('${MOCKDEP[@]}' failed)" - exit 77 - fi - $MYDIR/mock-snapperd & - sleep 1 - PID=$! - trap "kill \$PID" EXIT TERM INT -} -teardown() { - : -} - -# FIXME: run this automatically, and declare the ruby-dbus dependency -# FIXME: add tests for unhappy paths -# FIXME: is exit code 0 really a success? -# FIXME: try with coverage testing - -setup -# test1 | tee /dev/stderr | runit >&2 -test_pre_post | runit > /dev/null -test_pre_del | runit > /dev/null -teardown