summary: Check that execle doesn't regress
# This is blacklisted on debian because we first have to get the dpkg-vendor patches
systems: [-debian-8]
details: |
 The setup for this test is unorthodox because by the time the cgroup code is
 executed, the mounts are in place and /lib/udev/snap-device-helper from the core
 snap is used. Unfortunately, simple bind mounts over
 /snap/ubuntu-core/current/lib/udev don't work and the core snap must be
 unpacked, lib/udev/snap-device-helper modified to be tested, repacked and mounted.
 We unmount the core snap and move it aside to avoid both the original and the
 updated core snap from being mounted on the same mount point, which confuses
 the kernel.
prepare: |
    echo "This test is disabled because it causes failures for subsequent tests"
    echo "it seems to unmount ubuntu-core snap and not re-mount the original one correctly"
    exit 0
    cd /
    echo "Install hello-world"
    snap install hello-world
    systemctl stop snapd.service snapd.socket
    # all of this ls madness can go away when we have remote environment
    # variables
    echo "Unmount original core snap"
    umount $(ls -1d /snap/ubuntu-core/* | grep -v current | tail -1)
    mv $(ls -1 /var/lib/snapd/snaps/ubuntu-core_*.snap | tail -1) $(ls -1 /var/lib/snapd/snaps/ubuntu-core_*.snap | tail -1).orig
    echo "Create modified core snap for snap-device-helper"
    unsquashfs $(ls -1 /var/lib/snapd/snaps/ubuntu-core_*.snap.orig | tail -1)
    echo 'echo PATH=$PATH > /run/udev/spread-test.out' >> ./squashfs-root/lib/udev/snap-device-helper
    echo 'echo TESTVAR=$TESTVAR >> /run/udev/spread-test.out' >> ./squashfs-root/lib/udev/snap-device-helper
    mksquashfs ./squashfs-root $(ls -1 /var/lib/snapd/snaps/ubuntu-core_*.snap.orig | tail -1 | sed 's/.orig//') -comp xz -no-fragments
    if [ ! -e $(ls -1 /var/lib/snapd/snaps/ubuntu-core_*.snap | tail -1) ]; then exit 1; fi
    echo "Mount modified core snap"
    mount $(ls -1 /var/lib/snapd/snaps/ubuntu-core_*.snap | tail -1) $(ls -1d /snap/ubuntu-core/* | grep -v current | tail -1)
    systemctl start snapd.service snapd.socket
execute: |
    exit 0
    cd /
    echo "Add a udev tag so affected code branch is exercised"
    echo 'KERNEL=="uinput", TAG+="snap_hello-world_env"' > /etc/udev/rules.d/70-spread-test.rules
    udevadm control --reload-rules
    udevadm settle
    udevadm trigger
    udevadm settle
    PATH=/foo:$PATH TESTVAR=bar hello-world.env | grep PATH
    cat /run/udev/spread-test.out
    echo "Ensure user-specified PATH is not used"
    not grep 'PATH=/foo' /run/udev/spread-test.out
    echo "Ensure environment is clean"
    not grep 'TESTVAR=bar' /run/udev/spread-test.out
restore: |
    exit 0
    echo "Remove hello-world"
    snap remove --purge hello-world
    systemctl stop snapd.service snapd.socket
    echo "Unmount the modified core snap"
    # all of this ls madness can go away when we have remote environment
    # variables
    umount $(ls -1d /snap/ubuntu-core/* | grep -v current | tail -1)
    if [ "x"$(ls -1 /var/lib/snapd/snaps/ubuntu-core_*.snap.orig | tail -1) != "x" ]; then mv -f $(ls -1 /var/lib/snapd/snaps/ubuntu-core_*.snap.orig | tail -1) $(ls -1 /var/lib/snapd/snaps/ubuntu-core_*.snap.orig | tail -1 | sed 's/.orig//') ; fi
    echo "Mount the original core snap"
    mount $(ls -1 /var/lib/snapd/snaps/ubuntu-core_*.snap | tail -1) $(ls -1d /snap/ubuntu-core/* | grep -v current | tail -1)
    rm -rf /squashfs-root
    rm -f /run/udev/spread-test.out
    rm -f /etc/udev/rules.d/70-spread-test.rules
    udevadm control --reload-rules
    udevadm settle
    udevadm trigger
    udevadm settle
    systemctl start snapd.service snapd.socket
