tests: new test for interface network status #4221

Merged
merged 16 commits into from Dec 11, 2017
@@ -0,0 +1,31 @@
+#!/usr/bin/env python3
+
+import dbus
+import os
+import sys
+
+INTERFACE = 'com.ubuntu.connectivity1.NetworkingStatus'
+PATH = '/com/ubuntu/connectivity1/NetworkingStatus'
+
+
+def _get_obj():
+ return dbus.SystemBus().get_object(INTERFACE, PATH)
+
+
+def get_version():
+ obj = _get_obj()
+ return obj.GetVersion(dbus_interface=INTERFACE)
+
+
+def get_state():
+ obj = _get_obj()
+ return obj.GetState(dbus_interface=INTERFACE)
+
+
+if __name__ == "__main__":
+ if len(sys.argv) > 1 and sys.argv[1] == "GetState":
+ res = get_state()
+ else:
+ res = get_version()
+
+ print(res)
@@ -0,0 +1,33 @@
+#!/usr/bin/env python3
+
+from gi.repository import GLib
+import dbus
+import dbus.service
+
+from dbus.mainloop.glib import DBusGMainLoop
+
+INTERFACE = 'com.ubuntu.connectivity1.NetworkingStatus'
+PATH = '/com/ubuntu/connectivity1/NetworkingStatus'
+
+DBusGMainLoop(set_as_default=True)
+
+
+class DBusProvider(dbus.service.Object):
+ def __init__(self):
+ bus = dbus.SystemBus()
+ bus_name = dbus.service.BusName(INTERFACE, bus=bus)
+ dbus.service.Object.__init__(self, bus_name, PATH)
+
+ @dbus.service.method(dbus_interface=INTERFACE, out_signature="s")
+ def GetVersion(self):
+ return "my-ap-version"
+
+ @dbus.service.method(dbus_interface=INTERFACE, out_signature="s")
+ def GetState(self):
+ return "my-ap-state"
+
+
+if __name__ == "__main__":
+ DBusProvider()
+ loop = GLib.MainLoop()
+ loop.run()
@@ -0,0 +1,26 @@
+name: test-snapd-network-status-provider
+version: 1.0
+summary: Basic network status provider snap
+description: A basic snap declaring an network status slot
+confinement: strict
+grade: stable
+
+apps:
+ provider:
+ command: wrapper
+ slots: [network-status-test]
+ consumer:
+ command: consumer
+ plugs: [network-status]
+
+slots:
+ network-status-test:
+ interface: network-status
+
+parts:
+ deps:
+ plugin: python
+ stage-packages: [python3-gi, python3-dbus, gir1.2-glib-2.0]
+ copy:
+ plugin: dump
+ source: .
@stolowski

stolowski Nov 17, 2017

Contributor

Newline.

@@ -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
@@ -0,0 +1,67 @@
+summary: Ensure that the network-status interface works.
+
+details: |
+ The network-status interface allows a snap to monitor network connection status.
+
+ A snap which defines the network-status plug must be shown in the interfaces list.
+ The plug is autoconnected on install and, as usual, must be able to be disconnect/reconnect.
+
+ The snap is also declaring a plug on this interface must be able to ask for its status.
+
+# dbus-launch not supported in ubuntu-core
+systems: [-ubuntu-core-16-*]
+
+prepare: |
+ . "$TESTSLIB/dbus.sh"
+ . "$TESTSLIB/dirs.sh"
+
+ echo "Given a snap declaring a plug on the network-status interface is installed"
+ snap install test-snapd-network-status-provider
+
+ echo "And the provider dbus loop is started"
+ start_dbus_unit $SNAP_MOUNT_DIR/bin/test-snapd-network-status-provider.provider
+
+restore: |
+ rm -f getstate.error
+
+ . "$TESTSLIB/dbus.sh"
+ stop_dbus_unit
+
+execute: |
+ CONNECTED_PATTERN="^test-snapd-network-status-provider:network-status-test +test-snapd-network-status-provider:network-status"
+ DISCONNECTED_PATTERN="^- +test-snapd-network-status-provider:network-status$"
+
+ echo "The interface is connected by default"
@zyga

zyga Nov 29, 2017

Contributor

This conflicts with the description in details above.

+ snap interfaces | MATCH "$CONNECTED_PATTERN"
+
+ echo "Then wait until the dbus name is properly reserved"
+ for i in $(seq 10); do
@stolowski

stolowski Dec 11, 2017

Contributor

If this loop doesn't match the version in 10 runs (which equals to 10 seconds, roughly) the test will carry on without an error. I'm not sure how likely that is to happen, if at all?

+ if ! test-snapd-network-status-provider.consumer GetVersion | MATCH "my-ap-version"; then
+ sleep 1
+ else
+ break
+ fi
+ done
+
+ echo "Check the network-status is working from the consumer app in the snap"
+ test-snapd-network-status-provider.consumer GetState | MATCH "my-ap-state"
+ test-snapd-network-status-provider.consumer GetVersion | MATCH "my-ap-version"
@stolowski

stolowski Dec 11, 2017

Contributor

Ah, ok, you can ignore the above comment as we will fail here eventually if it didn't match in the loop. That's ok.

+
+ if [ "$(snap debug confinement)" = partial ] ; then
+ exit 0
+ fi
+
+ echo "When the plug is disconnected"
+ snap disconnect test-snapd-network-status-provider:network-status test-snapd-network-status-provider:network-status-test
+ snap interfaces | MATCH "$DISCONNECTED_PATTERN"
+
+ echo "And the snap state cannot be accessed"
+ if test-snapd-network-status-provider.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
+
+ echo "When the plug is re-connected the interfaces show the connection"
+ snap connect test-snapd-network-status-provider:network-status test-snapd-network-status-provider:network-status-test
+ snap interfaces | MATCH "$CONNECTED_PATTERN"