using snapper::Exception;
using snapper::CodeLocation;
#include "client/commands.h"
+#include "client/errors.h"
#include "snapper/Log.h"
#include "snapper/XmlFile.h"
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;
if (s != nullptr) {
snapper_config = s;
}
+
+ s = getenv("SNAPPER_ZYPP_PLUGIN_DBUS_SESSION");
+ if (s != nullptr) {
+ bus = DBUS_BUS_SESSION;
+ }
}
};
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))
{
);
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);
}
}
userdata["important"] = important ? "yes" : "no";
try {
+ y2mil("setting snapshot data");
snapper::SMD modification_data;
modification_data.description = snapshot_description;
modification_data.cleanup = cleanup_algorithm;
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 {
);
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);
}
}
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);
}
}
map<string, string> SnapperZyppPlugin::get_userdata(const Message&) {
map<string, string> result;
+ // FIXME: implement this
return result;
}
}
int main() {
+ initDefaultLogger();
setLogQuery(&log_query);
if (getenv("DISABLE_SNAPPER_ZYPP_PLUGIN") != nullptr) {
y2mil("$DISABLE_SNAPPER_ZYPP_PLUGIN is set - disabling snapper-zypp-plugin");
--- /dev/null
+#!/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
--- /dev/null
+#!/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