From b97d789f23b512b8ed46ddf34c79cad7a5c1de48 Mon Sep 17 00:00:00 2001 From: Gabriel Mougard Date: Mon, 26 Feb 2024 17:51:58 +0100 Subject: [PATCH] tests/storage-vm: Test that we avoid deleting existing target directories not created by LXD when unmounting disks Signed-off-by: Gabriel Mougard --- tests/storage-vm | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/storage-vm b/tests/storage-vm index e11d0dc14..4577211e1 100755 --- a/tests/storage-vm +++ b/tests/storage-vm @@ -242,6 +242,49 @@ for poolDriver in $poolDriverList; do echo "==> Deleting VM" lxc delete -f v1 + if hasNeededAPIExtension disk_state_created; then + # Check that we don't remove an existing target directory when unmounting a disk device if the original dir hasn't been created by LXD + echo "==> Avoid deleting existing target directories not created by LXD when unmounting disks." + mkdir -p "/tmp/lxd-test-${poolName}/empty-dir" + lxc launch "${TEST_IMG:-ubuntu-minimal-daily:22.04}" v1 --vm -s "${poolName}" + waitInstanceReady v1 + + ! lxc exec v1 -- mkdir /opt || false # The directory already exists and is not created by LXD. + lxc config device add v1 empty-dir disk source="/tmp/lxd-test-${poolName}/empty-dir" path=/opt + # Check that no `volatile..last_state.created` has been set. + if [ -n "$(lxc config get v1 volatile.empty-dir.last_state.created)"]; then + echo "==> volatile..last_state.created should not be set" + false + fi + + lxc config device remove v1 empty-dir + lxc exec v1 -- stat /opt # /opt is not removed as it was not created by LXD. + + lxc exec v1 -- mkdir -p /tmp/empty-dir # Create a new directory to check that it is not removed when unmounting the disk. + lxc config device add v1 empty-dir disk source="/tmp/lxd-test-${poolName}/empty-dir" path=/tmp/empty-dir + # Check that no `volatile..last_state.created` has been set. + if [ -n "$(lxc config get v1 volatile.empty-dir.last_state.created)"]; then + echo "==> volatile..last_state.created should not be set" + false + fi + + lxc config device remove v1 empty-dir + lxc exec v1 -- stat /tmp/empty-dir + + lxc config device add v1 empty-dir disk source="/tmp/lxd-test-${poolName}/empty-dir" path=/tmp/new-dir + # Check that the volatile..last_state.created is set. + targetPath=$(lxc config get v1 volatile.empty-dir.last_state.created) + if [ "${targetPath}" != "/tmp/new-dir" ]; then + echo "==> volatile..last_state.created is not set to the correct value" + false + fi + + lxc config device remove v1 empty-dir + ! lxc exec v1 -- stat /tmp/new-dir || false # /tmp/new-dir is removed as it was created by LXD. + + lxc delete -f v1 + rmdir "/tmp/lxd-test-${poolName}/empty-dir" + fi # Create directory with a file for directory disk tests. mkdir "/tmp/lxd-test-${poolName}"