summary: Create ubuntu core image, install snapd and test hotplug feature

systems: [ubuntu-16.04-64, ubuntu-18.04-64]

prepare: |
    snap pack "$TESTSLIB"/snaps/serial-port-hotplug
    tests.nested copy serial-port-hotplug_1.0_all.snap

    if nested_is_core_18_system; then
        tests.nested exec "sudo snap install --devmode jq-core18"
        tests.nested exec "sudo snap alias jq-core18.jq jq"
    else
        tests.nested exec "sudo snap install --devmode jq"
    fi

restore: |
    rm -f /tmp/serialport{0,1}

debug: |
    set +e
    tests.nested exec "snap connections --all"
    tests.nested exec "snap list"
    tests.nested exec "dmesg"
    tests.nested exec 'sudo jq -r ".data[\"hotplug-slots\"]" /var/lib/snapd/state.json'
    set -e

execute: |
    #shellcheck source=tests/lib/nested.sh
    . "$TESTSLIB/nested.sh"
    #shellcheck source=tests/lib/hotplug.sh
    . "$TESTSLIB/hotplug.sh"

    if [ "$NESTED_BUILD_SNAPD_FROM_CURRENT" != "true" ] && [ "$NESTED_CORE_REFRESH_CHANNEL" != "$NESTED_CORE_CHANNEL" ]; then
        echo "Refresh core snap to desired channel"
        nested_refresh_to_new_core "$NESTED_CORE_REFRESH_CHANNEL"
    fi

    if tests.nested exec "udevadm info -e" | MATCH "ID_MODEL=QEMU_USB_SERIAL"; then
        echo "USB serial already registered, exiting..."
        exit 1
    fi

    echo "Enabling hotplug"
    tests.nested exec "sudo snap set core experimental.hotplug=true"

    echo "Plugging the device"
    hotplug_add_dev1

    # precondition checks to make sure qemu setup is correct
    tests.nested exec "retry --wait 1 -n 5 sh -c 'udevadm info -e | MATCH ID_MODEL=QEMU_USB_SERIAL'"

    tests.nested exec "ls /dev/tty*" | MATCH "ttyUSB0"

    echo "Checking that qemuusbserial hotplug slot is present"
    check_slot_present qemuusbserial
    check_slot_not_gone qemuusbserial
    check_slot_device_path qemuusbserial "/dev/ttyUSB0"

    echo "Unplugging the device with no connections on the slot"
    hotplug_del_dev1

    # precondition check to make sure qemu event was triggered correctly
    tests.nested exec "retry --wait 1 -n 5 sh -c 'udevadm info -e | NOMATCH ID_MODEL=QEMU_USB_SERIAL'"
    if tests.nested exec "udevadm info -e" | MATCH "ID_MODEL=QEMU_USB_SERIAL"; then
        echo "USB serial should not be registered anymore, exiting..."
        exit 1
    fi

    echo "Checking that hotplug slot is no longer present"
    check_slot_not_present qemuusbserial
    check_slot_not_present_in_state qemuusbserial

    echo "Plugging the device back"
    hotplug_add_dev1

    echo "Checking that qemuusbserial hotplug slot is present again"
    check_slot_present qemuusbserial

    echo "Installing test snap with serial port plug"
    tests.nested exec "sudo snap install --dangerous serial-port-hotplug_1.0_all.snap"

    echo "Connecting hotplug slot of the first device"
    tests.nested exec "sudo snap connect serial-port-hotplug:serial-port :qemuusbserial"
    check_slot_connected qemuusbserial

    echo "Veryfing serial-port permissions of the snap"
    verify_apparmor_profile "/dev/ttyUSB0"

    echo "Unplugging the device"
    hotplug_del_dev1

    echo "Checking that qemuusbserial hotplug slot is no longer present, but is remembered"
    check_slot_not_present qemuusbserial
    check_slot_gone qemuusbserial

    echo "Plugging device back, checking that the slot and connection are restored"
    hotplug_add_dev1
    check_slot_connected qemuusbserial
    check_slot_not_gone qemuusbserial

    echo "Unplugging the device again"
    hotplug_del_dev1

    echo "Checking that qemuusbserial hotplug slot is not present, but is still remembered"
    check_slot_not_present qemuusbserial
    check_slot_gone qemuusbserial

    echo "Plugging second device creates a slot with new name and it's expected on /dev/ttyUSB0"
    hotplug_add_dev2
    check_slot_present qemuusbserial-1
    check_slot_not_gone qemuusbserial-1
    check_slot_device_path qemuusbserial-1 "/dev/ttyUSB0"

    echo "Plugging first device back while second device is present, checking that connection is restored"
    hotplug_add_dev1
    check_slot_connected qemuusbserial
    check_slot_not_gone qemuusbserial

    echo "Veryfing serial-port permissions of the snap, the first device is now expected on ttyUSB1"
    check_slot_device_path qemuusbserial "/dev/ttyUSB1"
    verify_apparmor_profile "/dev/ttyUSB1"

    echo "Restarting snapd should restore both hotplug slots since devices are still present"
    tests.nested exec "sudo systemctl stop snapd.service snapd.socket"
    tests.nested exec "sudo systemctl start snapd.service snapd.socket"
    check_slot_connected qemuusbserial
    check_slot_not_gone qemuusbserial
    check_slot_present qemuusbserial-1

    echo "Unplugging first device while snapd is stopped and then starting snapd remembers the slot internally due to connection"
    tests.nested exec "sudo systemctl stop snapd.service snapd.socket"
    hotplug_del_dev1
    tests.nested exec "sudo systemctl start snapd.service snapd.socket"
    check_slot_not_present qemuusbserial
    check_slot_gone qemuusbserial

    echo "Plugging first device back restores the slot and connection"
    hotplug_add_dev1
    check_slot_connected qemuusbserial
    check_slot_not_gone qemuusbserial

    echo "Disconnecting first slot and then unplugging the device removes the slot completely"
    # manual snap disconnect doesn't implement retry and errors out if there are conflicting changes, so wait for hotplug changes to complete
    wait_for_all_changes
    tests.nested exec "sudo snap disconnect serial-port-hotplug:serial-port :qemuusbserial"
    check_slot_present qemuusbserial
    hotplug_del_dev1
    check_slot_not_present qemuusbserial
    check_slot_not_present_in_state qemuusbserial

    echo "Unplugging second device removes the slot completely"
    hotplug_del_dev2
    check_slot_not_present qemuusbserial-1
    check_slot_not_present_in_state qemuusbserial-1

    echo "Plugging device back creates the slot again"
    hotplug_add_dev1
    check_slot_present qemuusbserial
    check_slot_not_gone qemuusbserial
    check_slot_device_path qemuusbserial "/dev/ttyUSB0"

    echo "Connecting hotplug slot of the first device again"
    tests.nested exec "sudo snap connect serial-port-hotplug:serial-port :qemuusbserial"
    check_slot_connected qemuusbserial

    echo "Hotplug slot stays after removing the snap"
    tests.nested exec "sudo snap remove serial-port-hotplug"
    check_slot_present qemuusbserial
    check_slot_not_gone qemuusbserial
