]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
Functional test of snapper-zypp-plugin
authorMartin Vidner <mvidner@suse.cz>
Wed, 4 Dec 2019 15:03:09 +0000 (16:03 +0100)
committerMartin Vidner <mvidner@suse.cz>
Fri, 13 Dec 2019 15:31:47 +0000 (16:31 +0100)
zypp-plugin/snapper_zypp_plugin.cc
zypp-plugin/testsuite/mock-snapperd [new file with mode: 0755]
zypp-plugin/testsuite/test1 [new file with mode: 0755]

index d4bcef460e7246335e30b9047a8b9aff10c5344c..e7c09c522f1bf03023bf4846a6f9a45c23c72d86 100644 (file)
@@ -20,6 +20,7 @@ using namespace std;
 using snapper::Exception;
 using snapper::CodeLocation;
 #include "client/commands.h"
+#include "client/errors.h"
 #include "snapper/Log.h"
 #include "snapper/XmlFile.h"
 
@@ -43,10 +44,12 @@ class ProgramOptions {
 public:
     string plugin_config;
     string snapper_config;
+    DBusBusType bus;
 
     ProgramOptions()
     : plugin_config("/etc/snapper/zypp-plugin.conf")
     , snapper_config("root")
+    , bus(DBUS_BUS_SYSTEM)
     {
        const char * s;
 
@@ -59,6 +62,11 @@ public:
        if (s != nullptr) {
            snapper_config = s;
        }
+
+       s = getenv("SNAPPER_ZYPP_PLUGIN_DBUS_SESSION");
+       if (s != nullptr) {
+            bus = DBUS_BUS_SESSION;
+        }
     }
 };
 
@@ -124,7 +132,7 @@ public:
 
     SnapperZyppPlugin(const ProgramOptions& opts)
     : snapper_cfg(opts.snapper_config)
-    , dbus_conn(DBUS_BUS_SYSTEM)
+    , dbus_conn(opts.bus)
     , pre_snapshot_num(0)
     , solvable_matchers(SolvableMatcher::load_config(opts.plugin_config))
     {
@@ -162,8 +170,11 @@ public:
                 );
                y2deb("created pre snapshot " << pre_snapshot_num);
            }
+            catch (const DBus::ErrorException& ex) {
+               SN_CAUGHT(ex);
+               y2err(error_description(ex));
+           }
            catch (const Exception& ex) {
-               // assumes a logging setup
                SN_CAUGHT(ex);
            }
        }
@@ -186,6 +197,7 @@ public:
                userdata["important"] = important ? "yes" : "no";
 
                try {
+                   y2mil("setting snapshot data");
                    snapper::SMD modification_data;
                    modification_data.description = snapshot_description;
                    modification_data.cleanup = cleanup_algorithm;
@@ -195,9 +207,11 @@ public:
                         pre_snapshot_num, modification_data
                     );
                }
+               catch (const DBus::ErrorException& ex) {
+                   SN_CAUGHT(ex);
+                   y2err(error_description(ex));
+               }
                catch (const Exception& ex) {
-                   y2err("setting snapshot data failed:");
-                   // assumes a logging setup
                    SN_CAUGHT(ex);
                }
                try {
@@ -208,9 +222,11 @@ public:
                     );
                    y2deb("created post snapshot " << post_snapshot_num);
                }
+               catch (const DBus::ErrorException& ex) {
+                   SN_CAUGHT(ex);
+                   y2err(error_description(ex));
+               }
                catch (const Exception& ex) {
-                   y2err("creating snapshot failed:");
-                   // assumes a logging setup
                    SN_CAUGHT(ex);
                }
            }
@@ -222,9 +238,11 @@ public:
                    command_delete_snapshots(dbus_conn, snapper_cfg, nums, verbose);
                    y2deb("deleted pre snapshot " << pre_snapshot_num);
                }
+               catch (const DBus::ErrorException& ex) {
+                   SN_CAUGHT(ex);
+                   y2err(error_description(ex));
+               }
                catch (const Exception& ex) {
-                   y2err("deleting snapshot failed:");
-                   // assumes a logging setup
                    SN_CAUGHT(ex);
                }
            }
@@ -258,6 +276,7 @@ const string SnapperZyppPlugin::cleanup_algorithm = "number";
 
 map<string, string> SnapperZyppPlugin::get_userdata(const Message&) {
     map<string, string> result;
+    // FIXME: implement this
     return result;
 }
 
@@ -314,6 +333,7 @@ log_query(LogLevel level, const string& component)
 }
 
 int main() {
+    initDefaultLogger();
     setLogQuery(&log_query);
     if (getenv("DISABLE_SNAPPER_ZYPP_PLUGIN") != nullptr) {
        y2mil("$DISABLE_SNAPPER_ZYPP_PLUGIN is set - disabling snapper-zypp-plugin");
diff --git a/zypp-plugin/testsuite/mock-snapperd b/zypp-plugin/testsuite/mock-snapperd
new file mode 100755 (executable)
index 0000000..a10f3a3
--- /dev/null
@@ -0,0 +1,65 @@
+#!/usr/bin/ruby
+require "dbus"
+
+SNAPPER_SERVICE = "org.opensuse.Snapper"
+SNAPPER_OBJECT = "/org/opensuse/Snapper"
+SNAPPER_INTERFACE = "org.opensuse.Snapper"
+
+class MockSnapper < DBus::Object
+  def report_mock(name, args)
+    puts format("Mock %-20s %s", name, args.inspect)
+  end
+
+  dbus_interface SNAPPER_INTERFACE do
+    dbus_method :CreatePreSnapshot,
+                "in config_name:s, " \
+                "in description:s, " \
+                "in cleanup:s, " \
+                "in userdata:a{ss}, " \
+                "out num:u" do |*args|
+      report_mock :CreatePreSnapshot, args
+      99
+    end
+
+    dbus_method :CreatePostSnapshot,
+                "in config_name:s, " \
+                "in pre_num:u, " \
+                "in description:s, " \
+                "in cleanup:s, " \
+                "in userdata:a{ss}, " \
+                "out num:u" do |*args|
+      report_mock :CreatePostSnapshot, args
+      999
+    end
+
+    dbus_method :DeleteSnapshots,
+                "in config_name:s, " \
+                "in nums:au" do |*args|
+      report_mock :DeleteSnapshots, args
+      nil
+    end
+
+    dbus_method :SetSnapshot,
+                "in config_name:s, " \
+                "in num:u, " \
+                "in description:s, " \
+                "in cleanup:s, " \
+                "in userdata:a{ss}" do |*args|
+      report_mock :SetSnapshot, args
+      nil
+    end
+  end
+end
+
+bus = DBus::SessionBus.instance
+service = bus.request_service(SNAPPER_SERVICE)
+object = MockSnapper.new(SNAPPER_OBJECT)
+service.export(object)
+
+main = DBus::Main.new
+main << bus
+begin
+  main.run
+rescue SystemCallError
+  # the test driver will kill the bus, that's OK
+end
diff --git a/zypp-plugin/testsuite/test1 b/zypp-plugin/testsuite/test1
new file mode 100755 (executable)
index 0000000..b54262f
--- /dev/null
@@ -0,0 +1,72 @@
+#!/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() {
+    $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