From: Martin Vidner Date: Wed, 4 Dec 2019 15:03:09 +0000 (+0100) Subject: Functional test of snapper-zypp-plugin X-Git-Tag: v0.8.7^2~23 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=52e768d937576c75da5df4f3ddc0b6ae99ae14ea;p=thirdparty%2Fsnapper.git Functional test of snapper-zypp-plugin --- diff --git a/zypp-plugin/snapper_zypp_plugin.cc b/zypp-plugin/snapper_zypp_plugin.cc index d4bcef46..e7c09c52 100644 --- a/zypp-plugin/snapper_zypp_plugin.cc +++ b/zypp-plugin/snapper_zypp_plugin.cc @@ -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 SnapperZyppPlugin::get_userdata(const Message&) { map 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 index 00000000..a10f3a34 --- /dev/null +++ b/zypp-plugin/testsuite/mock-snapperd @@ -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 index 00000000..b54262fb --- /dev/null +++ b/zypp-plugin/testsuite/test1 @@ -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