tests: add autopilot-introspection interface test #3484

Merged
merged 17 commits into from Aug 30, 2017
View
@@ -0,0 +1,34 @@
+#!/bin/bash
+
+start_dbus_unit(){
+ local executable="$1"
+
+ dbus-launch > dbus.env
+ export $(cat dbus.env)
+ if [[ "$SPREAD_SYSTEM" == ubuntu-14.04-* ]]; then
+ cat <<EOF > /etc/init/dbus-provider.conf
+env DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS"
+env DBUS_SESSION_BUS_PID="$DBUS_SESSION_BUS_PID"
+script
+ $executable
+end script
+EOF
+ initctl reload-configuration
+ start dbus-provider
+ else
+ systemd-run --unit dbus-provider \
+ --setenv=DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS \
+ --setenv=DBUS_SESSION_BUS_PID=$DBUS_SESSION_BUS_PID \
+ $executable
+ fi
+}
+
+stop_dbus_unit(){
+ rm -f dbus.env
+ if [[ "$SPREAD_SYSTEM" == ubuntu-14.04-* ]]; then
+ stop dbus-provider
+ rm -f /etc/init/dbus-provider.conf
+ else
+ systemctl stop dbus-provider
+ fi
+}
View
@@ -363,12 +363,14 @@ pkg_dependencies_ubuntu_classic(){
echo "
cups
dbus-x11
+ gnome-keyring
jq
man
printer-driver-cups-pdf
python3-yaml
upower
weston
+ xdg-utils
"
case "$SPREAD_SYSTEM" in
@@ -439,6 +441,7 @@ pkg_dependencies_opensuse(){
netcat-openbsd
osc
rng-tools
+ xdg-utils
"
}
@@ -460,7 +463,7 @@ pkg_dependencies(){
;;
*)
;;
- esac
+ esac
}
install_pkg_dependencies(){
@@ -0,0 +1,22 @@
+#!/usr/bin/env python
+
+import dbus
+import os
+import sys
+
+def _get_obj():
+ return dbus.SessionBus().get_object("com.canonical.Autopilot.Introspection", "/com/canonical/Autopilot/Introspection")
+
+def get_version():
+ obj = _get_obj()
+ print(obj.GetVersion(dbus_interface="com.canonical.Autopilot.Introspection"))
+
+def get_state():
+ obj = _get_obj()
+ print(obj.GetState(dbus_interface="com.canonical.Autopilot.Introspection"))
+
+if __name__ == "__main__":
+ if len(sys.argv) > 1 and sys.argv[1] == "GetState":
+ sys.exit(get_state())
+ else:
+ sys.exit(get_version())
@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+
+from gi.repository import GLib
+import dbus
+import dbus.service
+
+from dbus.mainloop.glib import DBusGMainLoop
+
+DBusGMainLoop(set_as_default=True)
+
+class DBusProvider(dbus.service.Object):
+ def __init__(self):
+ bus = dbus.SessionBus()
+ bus_name = dbus.service.BusName("com.canonical.Autopilot.Introspection", bus=bus)
+ dbus.service.Object.__init__(self, bus_name, "/com/canonical/Autopilot/Introspection")
+
+ @dbus.service.method(dbus_interface="com.canonical.Autopilot.Introspection",
+ out_signature="s")
+ def GetVersion(self):
+ return "my-ap-version"
+
+ @dbus.service.method(dbus_interface="com.canonical.Autopilot.Introspection",
+ out_signature="s")
+ def GetState(self):
+ return "my-ap-state"
+
+if __name__ == "__main__":
+ DBusProvider()
+ loop = GLib.MainLoop()
+ loop.run()
@@ -0,0 +1,28 @@
+name: test-snapd-autopilot-consumer
+version: 1.0
+summary: Basic autopilot consumer snap
+description: A basic snap declaring an autopilot plug
+confinement: strict
+grade: stable
+
+apps:
+ provider:
+ command: wrapper
+ slots: [autopilot-test]
+ consumer:
+ command: consumer
+ plugs: [autopilot-introspection]
+
+slots:
+ autopilot-test:
+ interface: dbus
+ bus: session
+ name: com.canonical.Autopilot.Introspection
+
+parts:
+ deps:
+ plugin: python
+ stage-packages: [python3-gi, python3-dbus, gir1.2-glib-2.0]
+ copy:
+ plugin: dump
+ source: .
@@ -0,0 +1,3 @@
+export GI_TYPELIB_PATH=$SNAP/usr/lib/girepository-1.0:$(ls -d $SNAP/usr/lib/*/girepository-1.0)
+
+$SNAP/usr/bin/python3 $SNAP/provider.py
@niemeyer

niemeyer Jul 11, 2017

Contributor

Is the wrapper necessary? Can't it leverage "environment:" in snapcraft.yaml?

@fgimenez

fgimenez Jul 13, 2017

Contributor

AFAIK we can't include $(...) calls in snapcraft.yaml's env definitions, pls correct me if I'm wrong. The glob in the path stands for the target architecture, in a local installation of test-snapd-dbus-provider it expands to /snap/test-snapd-dbus-provider/6/usr/lib/x86_64-linux-gnu/girepository-1.0/

@niemeyer

niemeyer Jul 17, 2017

Contributor

I see, okay. Would be good to solve these cases over time, but not a problem for this PR.

@@ -0,0 +1,85 @@
+summary: Ensure that the autopilot-introspection interface works
+
+details: |
+ The autopilot-intrspection interface allows an application to be introspected
+ and export its ui status over DBus.
+
+ The test uses an snap that declares a plug on autopilot-intrsopection, it
+ needs to request a dbus name on start so that its state can be queried.
+
+systems: [-ubuntu-core-16-*]
+
+prepare: |
+ . "$TESTSLIB/dirs.sh"
+
+ echo "Given a snap declaring an autopilot-intrspection plug in installed"
+ snap install --edge test-snapd-autopilot-consumer
+
+ echo "And the provider dbus loop is started"
+ . "$TESTSLIB/dbus.sh"
+ start_dbus_unit $SNAP_MOUNT_DIR/bin/test-snapd-autopilot-consumer.provider
+
+restore: |
+ rm -f *.error
+ . "$TESTSLIB/dbus.sh"
+ stop_dbus_unit
+
+execute: |
+ . "$TESTSLIB/dirs.sh"
+
+ dbus_send(){
+ local method="$1"
+ echo $(dbus-send --print-reply --dest=com.canonical.Autopilot.Introspection /com/canonical/Autopilot/Introspection com.canonical.Autopilot.Introspection.${method})
+ }
+
+ CONNECTED_PATTERN=":autopilot-introspection +test-snapd-autopilot-consumer"
+ DISCONNECTED_PATTERN="^\- +test-snapd-autopilot-consumer:autopilot-introspection"
+
+ export $(cat dbus.env)
+
+ echo "Then the plug is disconnected by default"
+ snap interfaces | MATCH "$DISCONNECTED_PATTERN"
+
+ echo "When the plug is connected"
+ snap connect test-snapd-autopilot-consumer:autopilot-introspection
+ snap interfaces | MATCH "$CONNECTED_PATTERN"
+
+ echo "Then the dbus name is properly reserved and the snap app version can be introspected"
+
+ for i in $(seq 10); do
+ if ! dbus_send GetVersion | MATCH "my-ap-version"; then
+ sleep 1
+ else
+ break
+ fi
+ done
+ $SNAP_MOUNT_DIR/bin/test-snapd-autopilot-consumer.consumer GetVersion | MATCH "my-ap-version"
+
+ echo "And the snap app state can be intrsopected"
+ $SNAP_MOUNT_DIR/bin/test-snapd-autopilot-consumer.consumer GetState | MATCH "my-ap-state"
+
+ if [ "$(snap debug confinement)" = none ]; then
+ exit 0
+ fi
+
+ echo "================================="
+
+ if [ "$(snap debug confinement)" = strict ] ; then
+ echo "When the plug is disconnected"
+ snap disconnect test-snapd-autopilot-consumer:autopilot-introspection
+ snap interfaces | MATCH "$DISCONNECTED_PATTERN"
+
+ echo "Then the snap version is not introspectable"
+ if $SNAP_MOUNT_DIR/bin/test-snapd-autopilot-consumer.consumer GetVersion 2>${PWD}/getversion.error ; then
+ echo "Expected permission error trying to introspect version with disconnected plug"
+ exit 1
+ fi
+ MATCH "Permission denied" < getversion.error
+
+ echo "And the snap state is not introspectable"
+ if $SNAP_MOUNT_DIR/bin/test-snapd-autopilot-consumer.consumer GetState 2>${PWD}/getstate.error; then
+ echo "Expected permission error trying to introspect state with disconnected plug"
+ exit 1
+ fi
+ MATCH "Permission denied" < getstate.error
+ fi
@@ -15,9 +15,6 @@ systems: [-ubuntu-core-16-*]
prepare: |
. "$TESTSLIB/dirs.sh"
- . "$TESTSLIB/pkgdb.sh"
-
- distro_install_package dbus-x11
echo "Give a snap declaring a dbus slot in installed"
snap install --edge test-snapd-dbus-provider
@@ -26,42 +23,19 @@ prepare: |
snap install --edge test-snapd-dbus-consumer
echo "And the provider dbus loop is started"
- dbus-launch > dbus.env
- export $(cat dbus.env | xargs)
- if [[ "$SPREAD_SYSTEM" == ubuntu-14.04-* ]]; then
- cat <<EOF > /etc/init/dbus-provider.conf
- env DISPLAY="$DISPLAY"
- env DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS"
- env DBUS_SESSION_BUS_PID="$DBUS_SESSION_BUS_PID"
- script
- $SNAP_MOUNT_DIR/bin/test-snapd-dbus-provider.provider
- end script
- EOF
- initctl reload-configuration
- start dbus-provider
- else
- systemd-run --unit dbus-provider \
- --setenv=DISPLAY=$DISPLAY \
- --setenv=DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS \
- --setenv=DBUS_SESSION_BUS_PID=$DBUS_SESSION_BUS_PID \
- $SNAP_MOUNT_DIR/bin/test-snapd-dbus-provider.provider
- fi
+ . "$TESTSLIB/dbus.sh"
+ start_dbus_unit $SNAP_MOUNT_DIR/bin/test-snapd-dbus-provider.provider
restore: |
- . "$TESTSLIB/pkgdb.sh"
- rm -f call.error dbus.env
- if [[ "$SPREAD_SYSTEM" == ubuntu-14.04-* ]]; then
- stop dbus-provider
- rm -f /etc/init/dbus-provider.conf
- else
- systemctl stop dbus-provider
- fi
- distro_purge_package dbus-x11
+ rm -f call.error
+ . "$TESTSLIB/dbus.sh"
+ stop_dbus_unit
execute: |
CONNECTED_PATTERN="test-snapd-dbus-provider:dbus-test +test-snapd-dbus-consumer"
DISCONNECTED_PATTERN="^\- +test-snapd-dbus-consumer:dbus-test"
- export $(cat dbus.env | xargs)
+
+ export $(cat dbus.env)
echo "Then the dbus name is properly reserved by the provider and the method is accessible"
while ! dbus-send --print-reply --dest=com.dbustest.HelloWorld /com/dbustest/HelloWorld com.dbustest.HelloWorld.SayHello | MATCH "hello world"; do
@@ -6,12 +6,10 @@ systems: [ ubuntu-1* ]
prepare: |
. $TESTSLIB/pkgdb.sh
echo "Ensure we have a working gnome-keyring"
- distro_install_package gnome-keyring dbus-x11
snap install --edge test-snapd-password-manager-consumer
restore: |
. $TESTSLIB/pkgdb.sh
- distro_auto_remove_packages gnome-keyring dbus-x11
kill $(cat dbus-launch.pid)
rm -f dbus-launch.pid
@@ -14,16 +14,11 @@ restore: |
rm -f dbus.env
umount -f /usr/bin/xdg-open || true
umount -f $SNAP_MOUNT_DIR/core/current/usr/bin/xdg-open || true
- distro_purge_package dbus-x11 xdg-utils
execute: |
. "$TESTSLIB/pkgdb.sh"
. "$TESTSLIB/dirs.sh"
- # Install necessary pacakges to get dbus-launch helper
- distro_install_package dbus-x11 xdg-utils
-
- # launch dbus session bus
dbus-launch > dbus.env
export $(cat dbus.env | xargs)