summary: Ensure that the openvswitch interface works.

details: |
    The openvswitch interface allows to task to the openvswitch socket (rw mode).

    A snap which defines a openvswitch plug must be shown in the interfaces list.
    The plug must not be autoconnected on install and, as usual, must be able to be
    reconnected.

    A snap declaring a plug on this interface must be able to do all the operations that
    are carried through the socket, in this test we exercise bridge and port creation,
    list and deletion.

# Openvswitch getting stuck during installation sporadically on ubuntu-14.04-64
# Openvswitch service fails to start on debian-sid
# Openvswitch service not available on the following systems
systems: [-ubuntu-core-*, -opensuse-*, -amazon-*, -arch-linux-*, -centos-*, -debian-sid-*, -ubuntu-14.04-64]

prepare: |
    echo "Given openvswitch is installed"
    tests.pkgs install openvswitch-switch

    # Ensure the openvswitch service is started which isn't the case by
    # default on all distributions
    if systemctl status openvswitch-switch.service | MATCH "Loaded:.*loaded"; then
        systemctl enable --now openvswitch-switch.service
    elif systemctl status openvswitch.service | MATCH "Loaded:.*loaded"; then
        systemctl enable --now openvswitch.service
    fi

    echo "And a snap declaring a plug on the openvswitch interface is installed"
    snap install --edge test-snapd-openvswitch-consumer

    echo "And a tap interface is defined"
    ip tuntap add tap1 mode tap

restore: |
    ovs-vsctl del-port br0 tap1 || true
    ovs-vsctl del-br br0 || true

    ip link delete tap1 || true

execute: |
    echo "The interface is disconnected by default"
    snap interfaces -i openvswitch | MATCH -- '^- +test-snapd-openvswitch-consumer:openvswitch'

    echo "When the plug is connected"
    snap connect test-snapd-openvswitch-consumer:openvswitch

    echo "Then the snap is able to create a bridge"
    test-snapd-openvswitch-consumer.ovs-vsctl add-br br0
    ovs-vsctl list-br | MATCH br0

    echo "And the snap is able to create a port"
    test-snapd-openvswitch-consumer.ovs-vsctl add-port br0 tap1
    ovs-vsctl list-ports br0 | MATCH tap1

    echo "And the snap is able to delete a port"
    test-snapd-openvswitch-consumer.ovs-vsctl del-port br0 tap1
    ovs-vsctl list-ports br0 | NOMATCH tap1

    echo "And the snap is able to delete a bridge"
    test-snapd-openvswitch-consumer.ovs-vsctl del-br br0
    ovs-vsctl list-br | NOMATCH br0

    if [ "$(snap debug confinement)" = partial ] ; then
        exit 0
    fi

    echo "When the plug is disconnected"
    snap disconnect test-snapd-openvswitch-consumer:openvswitch

    echo "Then the snap is not able to create a bridge"
    if test-snapd-openvswitch-consumer.ovs-vsctl add-br br0 2> bridge-creation.error; then
        echo "Expected permission error accessing openvswitch socket with disconnected plug"
        exit 1
    fi
    MATCH 'database connection failed \(Permission denied\)' < bridge-creation.error

    ovs-vsctl add-br br0

    echo "And the snap is not able to create a port"
    if test-snapd-openvswitch-consumer.ovs-vsctl add-port br0 tap1 2> port-creation.error; then
        echo "Expected permission error accessing openvswitch socket with disconnected plug"
        exit 1
    fi
    MATCH 'database connection failed \(Permission denied\)' < port-creation.error

    ovs-vsctl add-port br0 tap1

    echo "And the snap is not able to delete a port"
    if test-snapd-openvswitch-consumer.ovs-vsctl del-port br0 tap1 2> port-deletion.error; then
        echo "Expected permission error accessing openvswitch socket with disconnected plug"
        exit 1
    fi
    MATCH 'database connection failed \(Permission denied\)' < port-deletion.error

    echo "And the snap is not able to delete a bridge"
    if test-snapd-openvswitch-consumer.ovs-vsctl del-br br0 2> br-creation.error; then
        echo "Expected permission error accessing openvswitch socket with disconnected plug"
        exit 1
    fi
    MATCH 'database connection failed \(Permission denied\)' < br-creation.error
