summary: Test that snapctl mount will mount in a re-plug case

details: |
  Test that snapctl mount will mount a hot-pluged device if such device is
  removed, then reinserted, and snapctl mount is called again.

systems: [-ubuntu-1*]

restore: |
  rm -f mount-usb-drive_1.0_all.snap

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

  version="$(nested_get_version)"
  sed -i "s/##BASE##/core$version/" mount-usb-drive/meta/snap.yaml
  snap pack mount-usb-drive
  remote.push mount-usb-drive_1.0_all.snap

  img_file=$PWD/ext4.img
  truncate -s 10M "$img_file"
  mkfs.ext4 "$img_file"
  # No need to run e2fsck
  tune2fs -c0 -i0 "$img_file"

  remote.exec sudo snap install --dangerous mount-usb-drive_1.0_all.snap
  remote.exec sudo snap connect mount-usb-drive:block-devices
  remote.exec sudo snap connect mount-usb-drive:mnt

  # Plug USB drive
  qemu_dev_id=usbdrive
  nested_add_usb_drive "$qemu_dev_id" "$img_file"
  # Check snapctl mount on the drive
  remote.exec "retry --wait 1 -n 5 sh -c 'sudo mount-usb-drive.test'"

  # Unplug drive
  nested_del_device "$qemu_dev_id"
  not remote.exec sudo mount-usb-drive.test

  # Re-plug, running snapct mount should start the mount
  nested_add_usb_drive "$qemu_dev_id" "$img_file"
  remote.exec "retry --wait 1 -n 5 sh -c 'sudo mount-usb-drive.test'"

  # Clean-up
  nested_del_device "$qemu_dev_id"
