]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
Test with TAP by comparing error strings
authorMartin Vidner <mvidner@suse.cz>
Tue, 10 Dec 2019 12:50:00 +0000 (13:50 +0100)
committerMartin Vidner <mvidner@suse.cz>
Tue, 17 Dec 2019 10:22:26 +0000 (11:22 +0100)
12 files changed:
zypp-plugin/testsuite/1-happy.test [new file with mode: 0755]
zypp-plugin/testsuite/2-malformed-xml.test [new file with mode: 0755]
zypp-plugin/testsuite/3-invalid-xml.test [new file with mode: 0755]
zypp-plugin/testsuite/4-badjson.test [new file with mode: 0755]
zypp-plugin/testsuite/99-shell.test [new file with mode: 0755]
zypp-plugin/testsuite/Makefile.am
zypp-plugin/testsuite/mock-snapperd
zypp-plugin/testsuite/test-helper.sh [new file with mode: 0644]
zypp-plugin/testsuite/test1 [deleted file]
zypp-plugin/testsuite/test2-malformed-xml [deleted file]
zypp-plugin/testsuite/test3-invalid-xml [deleted file]
zypp-plugin/testsuite/test4-badjson [deleted file]

diff --git a/zypp-plugin/testsuite/1-happy.test b/zypp-plugin/testsuite/1-happy.test
new file mode 100755 (executable)
index 0000000..0059cae
--- /dev/null
@@ -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 (executable)
index 0000000..e09f855
--- /dev/null
@@ -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 (executable)
index 0000000..d22b376
--- /dev/null
@@ -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 (executable)
index 0000000..71333e8
--- /dev/null
@@ -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 (executable)
index 0000000..8c2eba6
--- /dev/null
@@ -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"
index f19c7dc95824ad95f17de5af4ffb5eb2e4120aa6..3978f33efb4b3298d3afecb99865ae92f04068c6 100644 (file)
@@ -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
index a10f3a3481225f2b8510c091b77ec61373bbd1f0..920971143f8ffb424758d67d751c5e5f45b2977f 100755 (executable)
@@ -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 (file)
index 0000000..03d5325
--- /dev/null
@@ -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 (executable)
index 20384e1..0000000
+++ /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 (executable)
index 0bdf312..0000000
+++ /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 (executable)
index 2af036c..0000000
+++ /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 (executable)
index ab73aa8..0000000
+++ /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