[lxc-devel] [lxd/master] Tests cleanup refactor

albertodonato on Github lxc-bot at linuxcontainers.org
Fri Aug 18 16:06:40 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/20170818/ecda32ee/attachment.bin>
-------------- next part --------------
From 82fcb239d9cf36cbac1431e6208d104db60b6a65 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 1/2] 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 1f38cd70d..c71ab4651 100755
--- a/test/main.sh
+++ b/test/main.sh
@@ -60,11 +60,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 d273b8f60676d9d6dde25f85c1c9b91f65a4a1e2 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 2/2] tests: refactor cleanup functions

Signed-off-by: Alberto Donato <alberto.donato at canonical.com>
---
 test/includes/lxd.sh     | 32 +++++++++++++++++++++++++++-----
 test/includes/storage.sh | 15 +++++++++++++++
 test/main.sh             | 16 +---------------
 3 files changed, 43 insertions(+), 20 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..284d3209d 100644
--- a/test/includes/storage.sh
+++ b/test/includes/storage.sh
@@ -53,13 +53,16 @@ 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}")
     if [ ! -e "${pvloopdev}" ]; then
         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 +109,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 c71ab4651..6c92928f7 100755
--- a/test/main.sh
+++ b/test/main.sh
@@ -84,22 +84,8 @@ cleanup() {
 
   echo "==> Cleaning up"
 
-  # Kill all the 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
+  cleanup_lxds "$TEST_DIR"
 
-  # Wipe the test environment
-  wipe "${TEST_DIR}"
 
   echo ""
   echo ""


More information about the lxc-devel mailing list