[lxc-devel] [lxd/stable-2.0] Backport the test functions refactoring

albertodonato on Github lxc-bot at linuxcontainers.org
Wed Oct 4 08:08:25 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20171004/9570137b/attachment.bin>
-------------- next part --------------
From 2038cf162e40de3e4ea5a0c47ee436c974dfb029 Mon Sep 17 00:00:00 2001
From: Alberto Donato <alberto.donato at canonical.com>
Date: Thu, 17 Aug 2017 11:48:29 +0200
Subject: [PATCH 1/7] tests: split out storage-related helper functions

Signed-off-by: Alberto Donato <alberto.donato at canonical.com>
---
 test/includes/storage.sh | 99 ++++++++++++++++++++++++++++++++++++++++++++++++
 test/main.sh             | 92 +++-----------------------------------------
 2 files changed, 104 insertions(+), 87 deletions(-)
 create mode 100644 test/includes/storage.sh

diff --git a/test/includes/storage.sh b/test/includes/storage.sh
new file mode 100644
index 000000000..ff308c114
--- /dev/null
+++ b/test/includes/storage.sh
@@ -0,0 +1,99 @@
+# Helper functions related to storage backends.
+
+# Whether a storage backend is available
+storage_backend_available() {
+    # shellcheck disable=2039
+    local backends
+    backends="$(available_storage_backends)"
+    [ "${backends#*$1}" != "$backends" ]
+}
+
+# Choose a random available backend, excluding LXD_BACKEND
+random_storage_backend() {
+    # shellcheck disable=2046
+    shuf -e $(available_storage_backends) | head -n 1
+}
+
+# Return the storage backend being used by a LXD instance
+storage_backend() {
+    cat "$1/lxd.backend"
+}
+
+# Return a list of available storage backends
+available_storage_backends() {
+    # shellcheck disable=2039
+    local backend backends storage_backends
+
+    backends="dir" # always available
+
+    storage_backends="btrfs lvm zfs"
+    if [ -n "${LXD_CEPH_CLUSTER:-}" ]; then
+        storage_backends="${storage_backends} ceph"
+    fi
+
+    for backend in $storage_backends; do
+        if which "$backend" >/dev/null 2>&1; then
+            backends="$backends $backend"
+        fi
+    done
+
+    echo "$backends"
+}
+
+configure_loop_device() {
+    # shellcheck disable=SC2039
+    local lv_loop_file pvloopdev
+
+    lv_loop_file=$(mktemp -p "${TEST_DIR}" XXXX.img)
+    truncate -s 10G "${lv_loop_file}"
+    pvloopdev=$(losetup --show -f "${lv_loop_file}")
+    if [ ! -e "${pvloopdev}" ]; then
+        echo "failed to setup loop"
+        false
+    fi
+    echo "${pvloopdev}" >> "${TEST_DIR}/loops"
+
+    # The following code enables to return a value from a shell function by
+    # calling the function as: fun VAR1
+
+    # shellcheck disable=2039
+    local __tmp1="${1}"
+    # shellcheck disable=2039
+    local res1="${lv_loop_file}"
+    if [ "${__tmp1}" ]; then
+        eval "${__tmp1}='${res1}'"
+    fi
+
+    # shellcheck disable=2039
+    local __tmp2="${2}"
+    # shellcheck disable=2039
+    local res2="${pvloopdev}"
+    if [ "${__tmp2}" ]; then
+        eval "${__tmp2}='${res2}'"
+    fi
+}
+
+deconfigure_loop_device() {
+    # shellcheck disable=SC2039
+    local lv_loop_file loopdev success
+    lv_loop_file="${1}"
+    loopdev="${2}"
+    success=0
+    # shellcheck disable=SC2034
+    for i in $(seq 10); do
+        if losetup -d "${loopdev}"; then
+            success=1
+            break
+        fi
+
+        sleep 0.5
+    done
+
+    if [ "${success}" = "0" ]; then
+        echo "Failed to tear down loop device"
+        return 1
+    fi
+
+    rm -f "${lv_loop_file}"
+    sed -i "\|^${loopdev}|d" "${TEST_DIR}/loops"
+}
diff --git a/test/main.sh b/test/main.sh
index 8a7a3222f..974cabfa5 100755
--- a/test/main.sh
+++ b/test/main.sh
@@ -36,6 +36,11 @@ if [ -n "${LXD_LOGS:-}" ] && [ ! -d "${LXD_LOGS}" ]; then
 fi
 
 # Helper functions
+for include in includes/*.sh; do
+    # shellcheck disable=SC1090
+    . "$include"
+done
+
 local_tcp_port() {
   while :; do
     port=$(shuf -i 10000-32768 -n 1)
@@ -48,39 +53,6 @@ local_tcp_port() {
   done
 }
 
-# return a list of available storage backends
-available_storage_backends() {
-  # shellcheck disable=2039
-  local backend backends
-
-  backends="dir"
-  for backend in btrfs lvm zfs; do
-    if which $backend >/dev/null 2>&1; then
-      backends="$backends $backend"
-    fi
-  done
-  echo "$backends"
-}
-
-# whether a storage backend is available
-storage_backend_available() {
-  # shellcheck disable=2039
-  local backends
-  backends="$(available_storage_backends)"
-  [ "${backends#*$1}" != "$backends" ]
-}
-
-# choose a random available backend, excluding LXD_BACKEND
-random_storage_backend() {
-    # shellcheck disable=2046
-    shuf -e $(available_storage_backends) | head -n 1
-}
-
-# return the storage backend being used by a LXD instance
-storage_backend() {
-    cat "$1/lxd.backend"
-}
-
 # Set default backend to dir
 if [ -z "${LXD_BACKEND:-}" ]; then
     LXD_BACKEND=dir
@@ -442,60 +414,6 @@ wipe() {
   rm -Rf "${1}"
 }
 
-configure_loop_device() {
-  lv_loop_file=$(mktemp -p "${TEST_DIR}" XXXX.img)
-  truncate -s 10G "${lv_loop_file}"
-  pvloopdev=$(losetup --show -f "${lv_loop_file}")
-  if [ ! -e "${pvloopdev}" ]; then
-    echo "failed to setup loop"
-    false
-  fi
-  echo "${pvloopdev}" >> "${TEST_DIR}/loops"
-
-  # The following code enables to return a value from a shell function by
-  # calling the function as: fun VAR1
-
-  # shellcheck disable=2039
-  local  __tmp1="${1}"
-  # shellcheck disable=2039
-  local  res1="${lv_loop_file}"
-  if [ "${__tmp1}" ]; then
-      eval "${__tmp1}='${res1}'"
-  fi
-
-  # shellcheck disable=2039
-  local  __tmp2="${2}"
-  # shellcheck disable=2039
-  local  res2="${pvloopdev}"
-  if [ "${__tmp2}" ]; then
-      eval "${__tmp2}='${res2}'"
-  fi
-}
-
-deconfigure_loop_device() {
-  lv_loop_file="${1}"
-  loopdev="${2}"
-
-  SUCCESS=0
-  # shellcheck disable=SC2034
-  for i in $(seq 10); do
-    if losetup -d "${loopdev}"; then
-      SUCCESS=1
-      break
-    fi
-
-    sleep 0.5
-  done
-
-  if [ "${SUCCESS}" = "0" ]; then
-    echo "Failed to tear down loop device"
-    false
-  fi
-
-  rm -f "${lv_loop_file}"
-  sed -i "\|^${loopdev}|d" "${TEST_DIR}/loops"
-}
-
 # Must be set before cleanup()
 TEST_CURRENT=setup
 TEST_RESULT=failure

From 9067127a6ccc7db9bbea60942e381330043abc78 Mon Sep 17 00:00:00 2001
From: Alberto Donato <alberto.donato at canonical.com>
Date: Thu, 17 Aug 2017 11:58:27 +0200
Subject: [PATCH 2/7] tests: split out lxc and lxd related helper functions

Signed-off-by: Alberto Donato <alberto.donato at canonical.com>
---
 test/includes/lxc.sh |  52 +++++++++++
 test/includes/lxd.sh | 258 +++++++++++++++++++++++++++++++++++++++++++++++++++
 test/main.sh         | 258 ---------------------------------------------------
 3 files changed, 310 insertions(+), 258 deletions(-)
 create mode 100644 test/includes/lxc.sh
 create mode 100644 test/includes/lxd.sh

diff --git a/test/includes/lxc.sh b/test/includes/lxc.sh
new file mode 100644
index 000000000..74b45cde6
--- /dev/null
+++ b/test/includes/lxc.sh
@@ -0,0 +1,52 @@
+# lxc CLI related test helpers.
+
+lxc() {
+    LXC_LOCAL=1 lxc_remote "$@"
+}
+
+lxc_remote() {
+    set +x
+    # shellcheck disable=SC2039
+    local injected cmd arg
+
+    injected=0
+    cmd=$(which lxc)
+
+    # shellcheck disable=SC2048,SC2068
+    for arg in $@; do
+        if [ "${arg}" = "--" ]; then
+            injected=1
+            cmd="${cmd} ${DEBUG:-}"
+            [ -n "${LXC_LOCAL}" ] && cmd="${cmd} --force-local"
+            cmd="${cmd} --"
+        elif [ "${arg}" = "--force-local" ]; then
+            continue
+        else
+            cmd="${cmd} \"${arg}\""
+        fi
+    done
+
+    if [ "${injected}" = "0" ]; then
+        cmd="${cmd} ${DEBUG-}"
+    fi
+    if [ -n "${DEBUG:-}" ]; then
+        set -x
+    fi
+    eval "${cmd}"
+}
+
+gen_cert() {
+    # Temporarily move the existing cert to trick LXC into generating a
+    # second cert.  LXC will only generate a cert when adding a remote
+    # server with a HTTPS scheme.  The remote server URL just needs to
+    # be syntactically correct to get past initial checks; in fact, we
+    # don't want it to succeed, that way we don't have to delete it later.
+    [ -f "${LXD_CONF}/${1}.crt" ] && return
+    mv "${LXD_CONF}/client.crt" "${LXD_CONF}/client.crt.bak"
+    mv "${LXD_CONF}/client.key" "${LXD_CONF}/client.key.bak"
+    echo y | lxc_remote remote add "$(uuidgen)" https://0.0.0.0 || true
+    mv "${LXD_CONF}/client.crt" "${LXD_CONF}/${1}.crt"
+    mv "${LXD_CONF}/client.key" "${LXD_CONF}/${1}.key"
+    mv "${LXD_CONF}/client.crt.bak" "${LXD_CONF}/client.crt"
+    mv "${LXD_CONF}/client.key.bak" "${LXD_CONF}/client.key"
+}
diff --git a/test/includes/lxd.sh b/test/includes/lxd.sh
new file mode 100644
index 000000000..b7d627297
--- /dev/null
+++ b/test/includes/lxd.sh
@@ -0,0 +1,258 @@
+# LXD-related test helpers.
+
+spawn_lxd() {
+    set +x
+    # LXD_DIR is local here because since $(lxc) is actually a function, it
+    # overwrites the environment and we would lose LXD_DIR's value otherwise.
+
+    # shellcheck disable=2039
+    local LXD_DIR lxddir lxd_backend
+
+    lxddir=${1}
+    shift
+
+    storage=${1}
+    shift
+
+    if [ "$LXD_BACKEND" = "random" ]; then
+        lxd_backend="$(random_storage_backend)"
+    else
+        lxd_backend="$LXD_BACKEND"
+    fi
+
+    if [ "${LXD_BACKEND}" = "ceph" ] && [ -z "${LXD_CEPH_CLUSTER:-}" ]; then
+        echo "A cluster name must be specified when using the CEPH driver." >&2
+        exit 1
+    fi
+
+    # Copy pre generated Certs
+    cp deps/server.crt "${lxddir}"
+    cp deps/server.key "${lxddir}"
+
+    # setup storage
+    "$lxd_backend"_setup "${lxddir}"
+    echo "$lxd_backend" > "${lxddir}/lxd.backend"
+
+    echo "==> Spawning lxd in ${lxddir}"
+    # shellcheck disable=SC2086
+    LXD_DIR="${lxddir}" lxd --logfile "${lxddir}/lxd.log" ${DEBUG-} "$@" 2>&1 &
+    LXD_PID=$!
+    echo "${LXD_PID}" > "${lxddir}/lxd.pid"
+    echo "${lxddir}" >> "${TEST_DIR}/daemons"
+    echo "==> Spawned LXD (PID is ${LXD_PID})"
+
+    echo "==> Confirming lxd is responsive"
+    LXD_DIR="${lxddir}" lxd waitready --timeout=300
+
+    echo "==> Binding to network"
+    # shellcheck disable=SC2034
+    for i in $(seq 10); do
+        addr="127.0.0.1:$(local_tcp_port)"
+        LXD_DIR="${lxddir}" lxc config set core.https_address "${addr}" || continue
+        echo "${addr}" > "${lxddir}/lxd.addr"
+        echo "==> Bound to ${addr}"
+        break
+    done
+
+    echo "==> Setting trust password"
+    LXD_DIR="${lxddir}" lxc config set core.trust_password foo
+    if [ -n "${DEBUG:-}" ]; then
+        set -x
+    fi
+
+    echo "==> Setting up networking"
+    LXD_DIR="${lxddir}" lxc profile device add default eth0 nic nictype=p2p name=eth0
+
+    if [ "${storage}" = true ]; then
+        echo "==> Configuring storage backend"
+        "$lxd_backend"_configure "${lxddir}"
+    fi
+}
+
+respawn_lxd() {
+    set +x
+    # LXD_DIR is local here because since $(lxc) is actually a function, it
+    # overwrites the environment and we would lose LXD_DIR's value otherwise.
+
+    # shellcheck disable=2039
+    local LXD_DIR
+
+    lxddir=${1}
+    shift
+
+    echo "==> Spawning lxd in ${lxddir}"
+    # shellcheck disable=SC2086
+    LXD_DIR="${lxddir}" lxd --logfile "${lxddir}/lxd.log" ${DEBUG-} "$@" 2>&1 &
+    LXD_PID=$!
+    echo "${LXD_PID}" > "${lxddir}/lxd.pid"
+    echo "==> Spawned LXD (PID is ${LXD_PID})"
+
+    echo "==> Confirming lxd is responsive"
+    LXD_DIR="${lxddir}" lxd waitready --timeout=300
+}
+
+kill_lxd() {
+    # LXD_DIR is local here because since $(lxc) is actually a function, it
+    # overwrites the environment and we would lose LXD_DIR's value otherwise.
+
+    # shellcheck disable=2039
+    local LXD_DIR daemon_dir daemon_pid check_leftovers lxd_backend
+
+    daemon_dir=${1}
+    LXD_DIR=${daemon_dir}
+    daemon_pid=$(cat "${daemon_dir}/lxd.pid")
+    check_leftovers="false"
+    lxd_backend=$(storage_backend "$daemon_dir")
+    echo "==> Killing LXD at ${daemon_dir}"
+
+    if [ -e "${daemon_dir}/unix.socket" ]; then
+        # Delete all containers
+        echo "==> Deleting all containers"
+        for container in $(lxc list --fast --force-local | tail -n+3 | grep "^| " | cut -d' ' -f2); do
+            lxc delete "${container}" --force-local -f || true
+        done
+
+        # Delete all images
+        echo "==> Deleting all images"
+        for image in $(lxc image list --force-local | tail -n+3 | grep "^| " | cut -d'|' -f3 | sed "s/^ //g"); do
+            lxc image delete "${image}" --force-local || true
+        done
+
+        # Delete all networks
+        echo "==> Deleting all networks"
+        for network in $(lxc network list --force-local | grep YES | grep "^| " | cut -d' ' -f2); do
+            lxc network delete "${network}" --force-local || true
+        done
+
+        # Delete all profiles
+        echo "==> Deleting all profiles"
+        for profile in $(lxc profile list --force-local | tail -n+3 | grep "^| " | cut -d' ' -f2); do
+            lxc profile delete "${profile}" --force-local || true
+        done
+
+        echo "==> Deleting all storage pools"
+        for storage in $(lxc storage list --force-local | tail -n+3 | grep "^| " | cut -d' ' -f2); do
+            lxc storage delete "${storage}" --force-local || true
+        done
+
+        echo "==> Checking for locked DB tables"
+        for table in $(echo .tables | sqlite3 "${daemon_dir}/lxd.db"); do
+            echo "SELECT * FROM ${table};" | sqlite3 "${daemon_dir}/lxd.db" >/dev/null
+        done
+
+        # Kill the daemon
+        lxd shutdown || kill -9 "${daemon_pid}" 2>/dev/null || true
+
+        # Cleanup shmounts (needed due to the forceful kill)
+        find "${daemon_dir}" -name shmounts -exec "umount" "-l" "{}" \; >/dev/null 2>&1 || true
+        find "${daemon_dir}" -name devlxd -exec "umount" "-l" "{}" \; >/dev/null 2>&1 || true
+
+        check_leftovers="true"
+    fi
+
+    if [ -n "${LXD_LOGS:-}" ]; then
+        echo "==> Copying the logs"
+        mkdir -p "${LXD_LOGS}/${daemon_pid}"
+        cp -R "${daemon_dir}/logs/" "${LXD_LOGS}/${daemon_pid}/"
+        cp "${daemon_dir}/lxd.log" "${LXD_LOGS}/${daemon_pid}/"
+    fi
+
+    if [ "${check_leftovers}" = "true" ]; then
+        echo "==> Checking for leftover files"
+        rm -f "${daemon_dir}/containers/lxc-monitord.log"
+        rm -f "${daemon_dir}/security/apparmor/cache/.features"
+        check_empty "${daemon_dir}/containers/"
+        check_empty "${daemon_dir}/devices/"
+        check_empty "${daemon_dir}/images/"
+        # FIXME: Once container logging rework is done, uncomment
+        # check_empty "${daemon_dir}/logs/"
+        check_empty "${daemon_dir}/security/apparmor/cache/"
+        check_empty "${daemon_dir}/security/apparmor/profiles/"
+        check_empty "${daemon_dir}/security/seccomp/"
+        check_empty "${daemon_dir}/shmounts/"
+        check_empty "${daemon_dir}/snapshots/"
+
+        echo "==> Checking for leftover DB entries"
+        check_empty_table "${daemon_dir}/lxd.db" "containers"
+        check_empty_table "${daemon_dir}/lxd.db" "containers_config"
+        check_empty_table "${daemon_dir}/lxd.db" "containers_devices"
+        check_empty_table "${daemon_dir}/lxd.db" "containers_devices_config"
+        check_empty_table "${daemon_dir}/lxd.db" "containers_profiles"
+        check_empty_table "${daemon_dir}/lxd.db" "networks"
+        check_empty_table "${daemon_dir}/lxd.db" "networks_config"
+        check_empty_table "${daemon_dir}/lxd.db" "images"
+        check_empty_table "${daemon_dir}/lxd.db" "images_aliases"
+        check_empty_table "${daemon_dir}/lxd.db" "images_properties"
+        check_empty_table "${daemon_dir}/lxd.db" "images_source"
+        check_empty_table "${daemon_dir}/lxd.db" "profiles"
+        check_empty_table "${daemon_dir}/lxd.db" "profiles_config"
+        check_empty_table "${daemon_dir}/lxd.db" "profiles_devices"
+        check_empty_table "${daemon_dir}/lxd.db" "profiles_devices_config"
+        check_empty_table "${daemon_dir}/lxd.db" "storage_pools"
+        check_empty_table "${daemon_dir}/lxd.db" "storage_pools_config"
+        check_empty_table "${daemon_dir}/lxd.db" "storage_volumes"
+        check_empty_table "${daemon_dir}/lxd.db" "storage_volumes_config"
+    fi
+
+    # teardown storage
+    "$lxd_backend"_teardown "${daemon_dir}"
+
+    # Wipe the daemon directory
+    wipe "${daemon_dir}"
+
+    # Remove the daemon from the list
+    sed "\|^${daemon_dir}|d" -i "${TEST_DIR}/daemons"
+}
+
+shutdown_lxd() {
+    # LXD_DIR is local here because since $(lxc) is actually a function, it
+    # overwrites the environment and we would lose LXD_DIR's value otherwise.
+
+    # shellcheck disable=2039
+    local LXD_DIR
+
+    daemon_dir=${1}
+    LXD_DIR=${daemon_dir}
+    daemon_pid=$(cat "${daemon_dir}/lxd.pid")
+    echo "==> Killing LXD at ${daemon_dir}"
+
+    # Kill the daemon
+    lxd shutdown || kill -9 "${daemon_pid}" 2>/dev/null || true
+}
+
+wait_for() {
+    # shellcheck disable=SC2039
+    local addr op
+
+    addr=${1}
+    shift
+    op=$("$@" | jq -r .operation)
+    my_curl "https://${addr}${op}/wait"
+}
+
+wipe() {
+    if which btrfs >/dev/null 2>&1; then
+        rm -Rf "${1}" 2>/dev/null || true
+        if [ -d "${1}" ]; then
+            find "${1}" | tac | xargs btrfs subvolume delete >/dev/null 2>&1 || true
+        fi
+    fi
+
+    # shellcheck disable=SC2039
+    local pid
+    # shellcheck disable=SC2009
+    ps aux | grep lxc-monitord | grep "${1}" | awk '{print $2}' | while read -r pid; do
+        kill -9 "${pid}" || true
+    done
+
+    if [ -f "${TEST_DIR}/loops" ]; then
+        while read -r line; do
+            losetup -d "${line}" || true
+        done < "${TEST_DIR}/loops"
+    fi
+    if mountpoint -q "${1}"; then
+        umount "${1}"
+    fi
+
+    rm -Rf "${1}"
+}
diff --git a/test/main.sh b/test/main.sh
index 974cabfa5..4f8d3e85b 100755
--- a/test/main.sh
+++ b/test/main.sh
@@ -71,131 +71,10 @@ for backend in $(available_storage_backends); do
   . "backends/${backend}.sh"
 done
 
-spawn_lxd() {
-  set +x
-  # LXD_DIR is local here because since $(lxc) is actually a function, it
-  # overwrites the environment and we would lose LXD_DIR's value otherwise.
-
-  # shellcheck disable=2039
-  local LXD_DIR lxddir lxd_backend
-
-  lxddir=${1}
-  shift
-
-  if [ "$LXD_BACKEND" = "random" ]; then
-    lxd_backend="$(random_storage_backend)"
-  else
-    lxd_backend="$LXD_BACKEND"
-  fi
-
-  # Copy pre generated Certs
-  cp deps/server.crt "${lxddir}"
-  cp deps/server.key "${lxddir}"
-
-  # setup storage
-  "$lxd_backend"_setup "${lxddir}"
-  echo "$lxd_backend" > "${lxddir}/lxd.backend"
-
-  echo "==> Spawning lxd in ${lxddir}"
-  # shellcheck disable=SC2086
-  LXD_DIR="${lxddir}" lxd --logfile "${lxddir}/lxd.log" ${DEBUG-} "$@" 2>&1 &
-  LXD_PID=$!
-  echo "${LXD_PID}" > "${lxddir}/lxd.pid"
-  echo "${lxddir}" >> "${TEST_DIR}/daemons"
-  echo "==> Spawned LXD (PID is ${LXD_PID})"
-
-  echo "==> Confirming lxd is responsive"
-  LXD_DIR="${lxddir}" lxd waitready --timeout=300
-
-  echo "==> Binding to network"
-  # shellcheck disable=SC2034
-  for i in $(seq 10); do
-    addr="127.0.0.1:$(local_tcp_port)"
-    LXD_DIR="${lxddir}" lxc config set core.https_address "${addr}" || continue
-    echo "${addr}" > "${lxddir}/lxd.addr"
-    echo "==> Bound to ${addr}"
-    break
-  done
-
-  echo "==> Setting trust password"
-  LXD_DIR="${lxddir}" lxc config set core.trust_password foo
-  if [ -n "${DEBUG:-}" ]; then
-    set -x
-  fi
-
-  echo "==> Configuring storage backend"
-  "$lxd_backend"_configure "${lxddir}"
-}
-
-respawn_lxd() {
-  set +x
-  # LXD_DIR is local here because since $(lxc) is actually a function, it
-  # overwrites the environment and we would lose LXD_DIR's value otherwise.
-
-  # shellcheck disable=2039
-  local LXD_DIR
-
-  lxddir=${1}
-  shift
-
-  echo "==> Spawning lxd in ${lxddir}"
-  # shellcheck disable=SC2086
-  LXD_DIR="${lxddir}" lxd --logfile "${lxddir}/lxd.log" ${DEBUG-} "$@" 2>&1 &
-  LXD_PID=$!
-  echo "${LXD_PID}" > "${lxddir}/lxd.pid"
-  echo "==> Spawned LXD (PID is ${LXD_PID})"
-
-  echo "==> Confirming lxd is responsive"
-  LXD_DIR="${lxddir}" lxd waitready --timeout=300
-}
-
-lxc() {
-  LXC_LOCAL=1
-  lxc_remote "$@"
-  RET=$?
-  unset LXC_LOCAL
-  return ${RET}
-}
-
-lxc_remote() {
-  set +x
-  injected=0
-  cmd=$(which lxc)
-
-  # shellcheck disable=SC2048,SC2068
-  for arg in $@; do
-    if [ "${arg}" = "--" ]; then
-      injected=1
-      cmd="${cmd} ${DEBUG:-}"
-      [ -n "${LXC_LOCAL}" ] && cmd="${cmd} --force-local"
-      cmd="${cmd} --"
-    elif [ "${arg}" = "--force-local" ]; then
-      continue
-    else
-      cmd="${cmd} \"${arg}\""
-    fi
-  done
-
-  if [ "${injected}" = "0" ]; then
-    cmd="${cmd} ${DEBUG-}"
-  fi
-  if [ -n "${DEBUG:-}" ]; then
-    set -x
-  fi
-  eval "${cmd}"
-}
-
 my_curl() {
   curl -k -s --cert "${LXD_CONF}/client.crt" --key "${LXD_CONF}/client.key" "$@"
 }
 
-wait_for() {
-  addr=${1}
-  shift
-  op=$("$@" | jq -r .operation)
-  my_curl "https://${addr}${op}/wait"
-}
-
 ensure_has_localhost_remote() {
   addr=${1}
   if ! lxc remote list | grep -q "localhost"; then
@@ -239,118 +118,6 @@ check_empty_table() {
   fi
 }
 
-kill_lxd() {
-  # LXD_DIR is local here because since $(lxc) is actually a function, it
-  # overwrites the environment and we would lose LXD_DIR's value otherwise.
-
-  # shellcheck disable=2039
-  local LXD_DIR daemon_dir daemon_pid check_leftovers lxd_backend
-
-  daemon_dir=${1}
-  LXD_DIR=${daemon_dir}
-  daemon_pid=$(cat "${daemon_dir}/lxd.pid")
-  check_leftovers="false"
-  lxd_backend=$(storage_backend "$daemon_dir")
-  echo "==> Killing LXD at ${daemon_dir}"
-
-  if [ -e "${daemon_dir}/unix.socket" ]; then
-    # Delete all containers
-    echo "==> Deleting all containers"
-    for container in $(lxc list --force-local | tail -n+3 | grep "^| " | cut -d' ' -f2); do
-      lxc delete "${container}" --force-local -f || true
-    done
-
-    # Delete all images
-    echo "==> Deleting all images"
-    for image in $(lxc image list --force-local | tail -n+3 | grep "^| " | cut -d'|' -f3 | sed "s/^ //g"); do
-      lxc image delete "${image}" --force-local || true
-    done
-
-    # Delete all profiles
-    echo "==> Deleting all profiles"
-    for profile in $(lxc profile list --force-local); do
-      lxc profile delete "${profile}" --force-local || true
-    done
-
-    echo "==> Checking for locked DB tables"
-    for table in $(echo .tables | sqlite3 "${daemon_dir}/lxd.db"); do
-      echo "SELECT * FROM ${table};" | sqlite3 "${daemon_dir}/lxd.db" >/dev/null
-    done
-
-    # Kill the daemon
-    lxd shutdown || kill -9 "${daemon_pid}" 2>/dev/null || true
-
-    # Cleanup shmounts (needed due to the forceful kill)
-    find "${daemon_dir}" -name shmounts -exec "umount" "-l" "{}" \; >/dev/null 2>&1 || true
-    find "${daemon_dir}" -name devlxd -exec "umount" "-l" "{}" \; >/dev/null 2>&1 || true
-
-    check_leftovers="true"
-  fi
-
-  if [ -n "${LXD_LOGS:-}" ]; then
-    echo "==> Copying the logs"
-    mkdir -p "${LXD_LOGS}/${daemon_pid}"
-    cp -R "${daemon_dir}/logs/" "${LXD_LOGS}/${daemon_pid}/"
-    cp "${daemon_dir}/lxd.log" "${LXD_LOGS}/${daemon_pid}/"
-  fi
-
-  if [ "${check_leftovers}" = "true" ]; then
-    echo "==> Checking for leftover files"
-    rm -f "${daemon_dir}/containers/lxc-monitord.log"
-    rm -f "${daemon_dir}/security/apparmor/cache/.features"
-    check_empty "${daemon_dir}/containers/"
-    check_empty "${daemon_dir}/devices/"
-    check_empty "${daemon_dir}/images/"
-    # FIXME: Once container logging rework is done, uncomment
-    # check_empty "${daemon_dir}/logs/"
-    check_empty "${daemon_dir}/security/apparmor/cache/"
-    check_empty "${daemon_dir}/security/apparmor/profiles/"
-    check_empty "${daemon_dir}/security/seccomp/"
-    check_empty "${daemon_dir}/shmounts/"
-    check_empty "${daemon_dir}/snapshots/"
-
-    echo "==> Checking for leftover DB entries"
-    check_empty_table "${daemon_dir}/lxd.db" "containers"
-    check_empty_table "${daemon_dir}/lxd.db" "containers_config"
-    check_empty_table "${daemon_dir}/lxd.db" "containers_devices"
-    check_empty_table "${daemon_dir}/lxd.db" "containers_devices_config"
-    check_empty_table "${daemon_dir}/lxd.db" "containers_profiles"
-    check_empty_table "${daemon_dir}/lxd.db" "images"
-    check_empty_table "${daemon_dir}/lxd.db" "images_aliases"
-    check_empty_table "${daemon_dir}/lxd.db" "images_properties"
-    check_empty_table "${daemon_dir}/lxd.db" "images_source"
-    check_empty_table "${daemon_dir}/lxd.db" "profiles"
-    check_empty_table "${daemon_dir}/lxd.db" "profiles_config"
-    check_empty_table "${daemon_dir}/lxd.db" "profiles_devices"
-    check_empty_table "${daemon_dir}/lxd.db" "profiles_devices_config"
-  fi
-
-  # teardown storage
-  "$lxd_backend"_teardown "${daemon_dir}"
-
-  # Wipe the daemon directory
-  wipe "${daemon_dir}"
-
-  # Remove the daemon from the list
-  sed "\|^${daemon_dir}|d" -i "${TEST_DIR}/daemons"
-}
-
-shutdown_lxd() {
-  # LXD_DIR is local here because since $(lxc) is actually a function, it
-  # overwrites the environment and we would lose LXD_DIR's value otherwise.
-
-  # shellcheck disable=2039
-  local LXD_DIR
-
-  daemon_dir=${1}
-  LXD_DIR=${daemon_dir}
-  daemon_pid=$(cat "${daemon_dir}/lxd.pid")
-  echo "==> Killing LXD at ${daemon_dir}"
-
-  # Kill the daemon
-  lxd shutdown || kill -9 "${daemon_pid}" 2>/dev/null || true
-}
-
 cleanup() {
   # Allow for failures and stop tracing everything
   set +ex
@@ -389,31 +156,6 @@ cleanup() {
   echo "==> Test result: ${TEST_RESULT}"
 }
 
-wipe() {
-  if which btrfs >/dev/null 2>&1; then
-    rm -Rf "${1}" 2>/dev/null || true
-    if [ -d "${1}" ]; then
-      find "${1}" | tac | xargs btrfs subvolume delete >/dev/null 2>&1 || true
-    fi
-  fi
-
-  # shellcheck disable=SC2009
-  ps aux | grep lxc-monitord | grep "${1}" | awk '{print $2}' | while read -r pid; do
-    kill -9 "${pid}" || true
-  done
-
-  if [ -f "${TEST_DIR}/loops" ]; then
-    while read -r line; do
-      losetup -d "${line}" || true
-    done < "${TEST_DIR}/loops"
-  fi
-  if mountpoint -q "${1}"; then
-    umount "${1}"
-  fi
-
-  rm -Rf "${1}"
-}
-
 # Must be set before cleanup()
 TEST_CURRENT=setup
 TEST_RESULT=failure

From e13597d9ad61cb3558cba3beef190ccccd5f1193 Mon Sep 17 00:00:00 2001
From: Alberto Donato <alberto.donato at canonical.com>
Date: Fri, 18 Aug 2017 09:32:08 +0200
Subject: [PATCH 3/7] tests: split out network-related helper functions

Signed-off-by: Alberto Donato <alberto.donato at canonical.com>
---
 test/includes/net.sh | 22 ++++++++++++++++++++++
 test/main.sh         | 17 -----------------
 2 files changed, 22 insertions(+), 17 deletions(-)
 create mode 100644 test/includes/net.sh

diff --git a/test/includes/net.sh b/test/includes/net.sh
new file mode 100644
index 000000000..e6e3c2757
--- /dev/null
+++ b/test/includes/net.sh
@@ -0,0 +1,22 @@
+# Network-related helper functions.
+
+# Return an available random local port
+local_tcp_port() {
+    # shellcheck disable=SC2039
+    local port pid
+
+    while true; do
+        port=$(shuf -i 10000-32768 -n 1)
+        nc -l 127.0.0.1 "${port}" >/dev/null 2>&1 &
+        pid=$!
+        kill "${pid}" >/dev/null 2>&1 || continue
+        wait "${pid}" || true
+        echo "${port}"
+        return
+    done
+}
+
+# Certificate-aware curl wrapper
+my_curl() {
+    curl -k -s --cert "${LXD_CONF}/client.crt" --key "${LXD_CONF}/client.key" "$@"
+}
diff --git a/test/main.sh b/test/main.sh
index 4f8d3e85b..70f16a582 100755
--- a/test/main.sh
+++ b/test/main.sh
@@ -41,19 +41,6 @@ for include in includes/*.sh; do
     . "$include"
 done
 
-local_tcp_port() {
-  while :; do
-    port=$(shuf -i 10000-32768 -n 1)
-    nc -l 127.0.0.1 "${port}" >/dev/null 2>&1 &
-    pid=$!
-    kill "${pid}" >/dev/null 2>&1 || continue
-    wait "${pid}" || true
-    echo "${port}"
-    return
-  done
-}
-
-# Set default backend to dir
 if [ -z "${LXD_BACKEND:-}" ]; then
     LXD_BACKEND=dir
 fi
@@ -71,10 +58,6 @@ for backend in $(available_storage_backends); do
   . "backends/${backend}.sh"
 done
 
-my_curl() {
-  curl -k -s --cert "${LXD_CONF}/client.crt" --key "${LXD_CONF}/client.key" "$@"
-}
-
 ensure_has_localhost_remote() {
   addr=${1}
   if ! lxc remote list | grep -q "localhost"; then

From 95ccfdec3a01ee5870258815432c68199132a6b2 Mon Sep 17 00:00:00 2001
From: Alberto Donato <alberto.donato at canonical.com>
Date: Thu, 17 Aug 2017 13:50:40 +0200
Subject: [PATCH 4/7] tests: split out test setup related helper functions

Signed-off-by: Alberto Donato <alberto.donato at canonical.com>
---
 test/includes/check.sh | 34 ++++++++++++++++++++++
 test/includes/setup.sh | 29 ++++++++++++++++++
 test/main.sh           | 79 ++++++++++++--------------------------------------
 3 files changed, 81 insertions(+), 61 deletions(-)
 create mode 100644 test/includes/check.sh
 create mode 100644 test/includes/setup.sh

diff --git a/test/includes/check.sh b/test/includes/check.sh
new file mode 100644
index 000000000..532cd9306
--- /dev/null
+++ b/test/includes/check.sh
@@ -0,0 +1,34 @@
+# Miscellanous test checks.
+
+check_dependencies() {
+    # shellcheck disable=SC2039
+    local dep missing
+    missing=""
+
+    for dep in "$@"; do
+        if ! which "$dep" >/dev/null 2>&1; then
+            [ "$missing" ] && missing="$missing $dep" || missing="$dep"
+        fi
+    done
+
+    if [ "$missing" ]; then
+       echo "Missing dependencies: $missing" >&2
+       exit 1
+    fi
+}
+
+check_empty() {
+    if [ "$(find "${1}" 2> /dev/null | wc -l)" -gt "1" ]; then
+        echo "${1} is not empty, content:"
+        find "${1}"
+        false
+    fi
+}
+
+check_empty_table() {
+    if [ -n "$(sqlite3 "${1}" "SELECT * FROM ${2};")" ]; then
+        echo "DB table ${2} is not empty, content:"
+        sqlite3 "${1}" "SELECT * FROM ${2};"
+        false
+    fi
+}
diff --git a/test/includes/setup.sh b/test/includes/setup.sh
new file mode 100644
index 000000000..1ecbc5cbe
--- /dev/null
+++ b/test/includes/setup.sh
@@ -0,0 +1,29 @@
+# Test setup helper functions.
+
+ensure_has_localhost_remote() {
+    # shellcheck disable=SC2039
+    local addr=${1}
+    if ! lxc remote list | grep -q "localhost"; then
+        lxc remote add localhost "https://${addr}" --accept-certificate --password foo
+    fi
+}
+
+ensure_import_testimage() {
+    if ! lxc image alias list | grep -q "^| testimage\s*|.*$"; then
+        if [ -e "${LXD_TEST_IMAGE:-}" ]; then
+            lxc image import "${LXD_TEST_IMAGE}" --alias testimage
+        else
+            if [ ! -e "/bin/busybox" ]; then
+                echo "Please install busybox (busybox-static) or set LXD_TEST_IMAGE"
+                exit 1
+            fi
+
+            if ldd /bin/busybox >/dev/null 2>&1; then
+                echo "The testsuite requires /bin/busybox to be a static binary"
+                exit 1
+            fi
+
+            deps/import-busybox --alias testimage
+        fi
+    fi
+}
diff --git a/test/main.sh b/test/main.sh
index 70f16a582..096529ec1 100755
--- a/test/main.sh
+++ b/test/main.sh
@@ -19,11 +19,24 @@ if [ -n "${LXD_DEBUG:-}" ]; then
   DEBUG="--debug"
 fi
 
+if [ -z "${LXD_BACKEND:-}" ]; then
+    LXD_BACKEND="dir"
+fi
+
+import_subdir_files() {
+    test "$1"
+    # shellcheck disable=SC2039
+    local file
+    for file in "$1"/*.sh; do
+        # shellcheck disable=SC1090
+        . "$file"
+    done
+}
+
+import_subdir_files includes
+
 echo "==> Checking for dependencies"
-deps="lxd lxc curl jq git xgettext sqlite3 msgmerge msgfmt shuf setfacl uuidgen"
-for dep in $deps; do
-  which "${dep}" >/dev/null 2>&1 || (echo "Missing dependency: ${dep}" >&2 && exit 1)
-done
+check_dependencies lxd lxc curl jq git xgettext sqlite3 msgmerge msgfmt shuf setfacl uuidgen
 
 if [ "${USER:-'root'}" != "root" ]; then
   echo "The testsuite must be run as root." >&2
@@ -35,16 +48,6 @@ if [ -n "${LXD_LOGS:-}" ] && [ ! -d "${LXD_LOGS}" ]; then
   exit 1
 fi
 
-# Helper functions
-for include in includes/*.sh; do
-    # shellcheck disable=SC1090
-    . "$include"
-done
-
-if [ -z "${LXD_BACKEND:-}" ]; then
-    LXD_BACKEND=dir
-fi
-
 echo "==> Available storage backends: $(available_storage_backends | sort)"
 if [ "$LXD_BACKEND" != "random" ] && ! storage_backend_available "$LXD_BACKEND"; then
   echo "Storage backage \"$LXD_BACKEND\" is not available"
@@ -58,49 +61,6 @@ for backend in $(available_storage_backends); do
   . "backends/${backend}.sh"
 done
 
-ensure_has_localhost_remote() {
-  addr=${1}
-  if ! lxc remote list | grep -q "localhost"; then
-    lxc remote add localhost "https://${addr}" --accept-certificate --password foo
-  fi
-}
-
-ensure_import_testimage() {
-  if ! lxc image alias list | grep -q "^| testimage\s*|.*$"; then
-    if [ -e "${LXD_TEST_IMAGE:-}" ]; then
-      lxc image import "${LXD_TEST_IMAGE}" --alias testimage
-    else
-      if [ ! -e "/bin/busybox" ]; then
-        echo "Please install busybox (busybox-static) or set LXD_TEST_IMAGE"
-        exit 1
-      fi
-
-      if ldd /bin/busybox >/dev/null 2>&1; then
-        echo "The testsuite requires /bin/busybox to be a static binary"
-        exit 1
-      fi
-
-      deps/import-busybox --alias testimage
-    fi
-  fi
-}
-
-check_empty() {
-  if [ "$(find "${1}" 2> /dev/null | wc -l)" -gt "1" ]; then
-    echo "${1} is not empty, content:"
-    find "${1}"
-    false
-  fi
-}
-
-check_empty_table() {
-  if [ -n "$(sqlite3 "${1}" "SELECT * FROM ${2};")" ]; then
-    echo "DB table ${2} is not empty, content:"
-    sqlite3 "${1}" "SELECT * FROM ${2};"
-    false
-  fi
-}
-
 cleanup() {
   # Allow for failures and stop tracing everything
   set +ex
@@ -146,10 +106,7 @@ TEST_RESULT=failure
 trap cleanup EXIT HUP INT TERM
 
 # Import all the testsuites
-for suite in suites/*.sh; do
-  # shellcheck disable=SC1090
- . "${suite}"
-done
+import_subdir_files suites
 
 # Setup test directory
 TEST_DIR=$(mktemp -d -p "$(pwd)" tmp.XXX)

From 5fc92d30dce76a2bfe52bf8c1b910615a42f1603 Mon Sep 17 00:00:00 2001
From: Alberto Donato <alberto.donato at canonical.com>
Date: Thu, 17 Aug 2017 13:28:37 +0200
Subject: [PATCH 5/7] tests: lints

Signed-off-by: Alberto Donato <alberto.donato at canonical.com>
---
 test/includes/lxd.sh           | 2 ++
 test/main.sh                   | 8 ++++++--
 test/suites/static_analysis.sh | 2 +-
 3 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/test/includes/lxd.sh b/test/includes/lxd.sh
index b7d627297..95d4921a5 100644
--- a/test/includes/lxd.sh
+++ b/test/includes/lxd.sh
@@ -14,6 +14,7 @@ spawn_lxd() {
     storage=${1}
     shift
 
+    # shellcheck disable=SC2153
     if [ "$LXD_BACKEND" = "random" ]; then
         lxd_backend="$(random_storage_backend)"
     else
@@ -212,6 +213,7 @@ shutdown_lxd() {
     local LXD_DIR
 
     daemon_dir=${1}
+    # shellcheck disable=2034
     LXD_DIR=${daemon_dir}
     daemon_pid=$(cat "${daemon_dir}/lxd.pid")
     echo "==> Killing LXD at ${daemon_dir}"
diff --git a/test/main.sh b/test/main.sh
index 096529ec1..fb538a377 100755
--- a/test/main.sh
+++ b/test/main.sh
@@ -2,15 +2,16 @@
 [ -n "${GOPATH:-}" ] && export "PATH=${GOPATH}/bin:${PATH}"
 
 # Don't translate lxc output for parsing in it in tests.
-export "LC_ALL=C"
+export LC_ALL="C"
 
 # Force UTC for consistency
-export "TZ=UTC"
+export TZ="UTC"
 
 if [ -n "${LXD_VERBOSE:-}" ] || [ -n "${LXD_DEBUG:-}" ]; then
   set -x
 fi
 
+export DEBUG=""
 if [ -n "${LXD_VERBOSE:-}" ]; then
   DEBUG="--verbose"
 fi
@@ -101,6 +102,7 @@ cleanup() {
 
 # Must be set before cleanup()
 TEST_CURRENT=setup
+# shellcheck disable=SC2034
 TEST_RESULT=failure
 
 trap cleanup EXIT HUP INT TERM
@@ -141,6 +143,7 @@ run_test() {
 # allow for running a specific set of tests
 if [ "$#" -gt 0 ]; then
   run_test "test_${1}"
+  # shellcheck disable=SC2034
   TEST_RESULT=success
   exit
 fi
@@ -173,4 +176,5 @@ run_test test_mem_profiling "memory profiling"
 run_test test_init_auto "lxd init auto"
 run_test test_init_interactive "lxd init interactive"
 
+# shellcheck disable=SC2034
 TEST_RESULT=success
diff --git a/test/suites/static_analysis.sh b/test/suites/static_analysis.sh
index b33fea8fe..0ab48aac7 100644
--- a/test/suites/static_analysis.sh
+++ b/test/suites/static_analysis.sh
@@ -16,7 +16,7 @@ test_static_analysis() {
 
     # Shell static analysis
     if which shellcheck >/dev/null 2>&1; then
-      shellcheck --shell sh test/main.sh test/suites/* test/backends/*
+      shellcheck --shell sh test/*.sh test/includes/*.sh test/suites/*.sh test/backends/*.sh
     else
       echo "shellcheck not found, shell static analysis disabled"
     fi

From 29ad1ca5f1569fc3a396a54ef6ae006c2ed65bc4 Mon Sep 17 00:00:00 2001
From: Alberto Donato <alberto.donato at canonical.com>
Date: Fri, 18 Aug 2017 16:27:12 +0200
Subject: [PATCH 6/7] tests: function to include storage backends helpers

Signed-off-by: Alberto Donato <alberto.donato at canonical.com>
---
 test/includes/storage.sh | 9 +++++++++
 test/main.sh             | 6 +-----
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/test/includes/storage.sh b/test/includes/storage.sh
index ff308c114..b9ee08e92 100644
--- a/test/includes/storage.sh
+++ b/test/includes/storage.sh
@@ -40,6 +40,15 @@ available_storage_backends() {
     echo "$backends"
 }
 
+import_storage_backends() {
+    # shellcheck disable=SC2039
+    local backend
+    for backend in $(available_storage_backends); do
+        # shellcheck disable=SC1090
+        . "backends/${backend}.sh"
+    done
+}
+
 configure_loop_device() {
     # shellcheck disable=SC2039
     local lv_loop_file pvloopdev
diff --git a/test/main.sh b/test/main.sh
index fb538a377..2eb952590 100755
--- a/test/main.sh
+++ b/test/main.sh
@@ -56,11 +56,7 @@ if [ "$LXD_BACKEND" != "random" ] && ! storage_backend_available "$LXD_BACKEND";
 fi
 echo "==> Using storage backend ${LXD_BACKEND}"
 
-# import storage backends
-for backend in $(available_storage_backends); do
-  # shellcheck disable=SC1090
-  . "backends/${backend}.sh"
-done
+import_storage_backends
 
 cleanup() {
   # Allow for failures and stop tracing everything

From b9bb92d4c0e8d7f9b5188fc021c28088f2891fa6 Mon Sep 17 00:00:00 2001
From: Alberto Donato <alberto.donato at canonical.com>
Date: Fri, 18 Aug 2017 17:50:43 +0200
Subject: [PATCH 7/7] tests: refactor cleanup functions

Signed-off-by: Alberto Donato <alberto.donato at canonical.com>
---
 test/includes/lxd.sh     | 32 +++++++++++++++++++++++++++-----
 test/includes/storage.sh | 14 ++++++++++++++
 test/main.sh             |  9 +--------
 3 files changed, 42 insertions(+), 13 deletions(-)

diff --git a/test/includes/lxd.sh b/test/includes/lxd.sh
index 95d4921a5..bc6349a25 100644
--- a/test/includes/lxd.sh
+++ b/test/includes/lxd.sh
@@ -39,6 +39,7 @@ spawn_lxd() {
     LXD_DIR="${lxddir}" lxd --logfile "${lxddir}/lxd.log" ${DEBUG-} "$@" 2>&1 &
     LXD_PID=$!
     echo "${LXD_PID}" > "${lxddir}/lxd.pid"
+    # shellcheck disable=SC2153
     echo "${lxddir}" >> "${TEST_DIR}/daemons"
     echo "==> Spawned LXD (PID is ${LXD_PID})"
 
@@ -247,14 +248,35 @@ wipe() {
         kill -9 "${pid}" || true
     done
 
-    if [ -f "${TEST_DIR}/loops" ]; then
-        while read -r line; do
-            losetup -d "${line}" || true
-        done < "${TEST_DIR}/loops"
-    fi
     if mountpoint -q "${1}"; then
         umount "${1}"
     fi
 
     rm -Rf "${1}"
 }
+
+# Kill and cleanup LXD instances and related resources
+cleanup_lxds() {
+    # shellcheck disable=SC2039
+    local test_dir daemon_dir
+    test_dir="$1"
+
+    # Kill all LXD instances
+    while read -r daemon_dir; do
+        kill_lxd "${daemon_dir}"
+    done < "${test_dir}/daemons"
+
+    # Cleanup leftover networks
+    # shellcheck disable=SC2009
+    ps aux | grep "interface=lxdt$$ " | grep -v grep | awk '{print $2}' | while read -r line; do
+        kill -9 "${line}"
+    done
+    if [ -e "/sys/class/net/lxdt$$" ]; then
+        ip link del lxdt$$
+    fi
+
+    # Wipe the test environment
+    wipe "$test_dir"
+
+    umount_loops "$test_dir"
+}
diff --git a/test/includes/storage.sh b/test/includes/storage.sh
index b9ee08e92..a3f019cac 100644
--- a/test/includes/storage.sh
+++ b/test/includes/storage.sh
@@ -53,6 +53,7 @@ configure_loop_device() {
     # shellcheck disable=SC2039
     local lv_loop_file pvloopdev
 
+    # shellcheck disable=SC2153
     lv_loop_file=$(mktemp -p "${TEST_DIR}" XXXX.img)
     truncate -s 10G "${lv_loop_file}"
     pvloopdev=$(losetup --show -f "${lv_loop_file}")
@@ -60,6 +61,7 @@ configure_loop_device() {
         echo "failed to setup loop"
         false
     fi
+    # shellcheck disable=SC2153
     echo "${pvloopdev}" >> "${TEST_DIR}/loops"
 
     # The following code enables to return a value from a shell function by
@@ -106,3 +108,15 @@ deconfigure_loop_device() {
     rm -f "${lv_loop_file}"
     sed -i "\|^${loopdev}|d" "${TEST_DIR}/loops"
 }
+
+umount_loops() {
+    # shellcheck disable=SC2039
+    local line test_dir
+    test_dir="$1"
+
+    if [ -f "${test_dir}/loops" ]; then
+        while read -r line; do
+            losetup -d "${line}" || true
+        done < "${test_dir}/loops"
+    fi
+}
diff --git a/test/main.sh b/test/main.sh
index 2eb952590..04a931867 100755
--- a/test/main.sh
+++ b/test/main.sh
@@ -79,14 +79,7 @@ cleanup() {
   fi
 
   echo "==> Cleaning up"
-
-  # Kill all the LXD instances
-  while read -r daemon_dir; do
-    kill_lxd "${daemon_dir}"
-  done < "${TEST_DIR}/daemons"
-
-  # Wipe the test environment
-  wipe "${TEST_DIR}"
+  cleanup_lxds "$TEST_DIR"
 
   echo ""
   echo ""


More information about the lxc-devel mailing list