[lxc-devel] [lxd/master] test: improve import tests

brauner on Github lxc-bot at linuxcontainers.org
Wed Mar 22 19:00:14 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 581 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170322/1f05f76e/attachment.bin>
-------------- next part --------------
From 42549e32e836ef274b153fb5207d9fc460b56156 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Wed, 22 Mar 2017 19:58:01 +0100
Subject: [PATCH] test: improve import tests

- Add respawn_lxd() and shutdown_lxd() to allow stopping and restarting an
  already running LXD daemon in the test-suite.
- Stop the daemon before calling sqlite3 to avoid db locked errors in the
  test-suite.

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 test/main.sh          |  38 ++++++++++++++++
 test/suites/backup.sh | 119 ++++++++++++++++++++++++++++----------------------
 2 files changed, 106 insertions(+), 51 deletions(-)

diff --git a/test/main.sh b/test/main.sh
index 17e0f2b..9a705b2 100755
--- a/test/main.sh
+++ b/test/main.sh
@@ -118,6 +118,28 @@ spawn_lxd() {
   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
+}
+
 lxc() {
   LXC_LOCAL=1
   lxc_remote "$@"
@@ -326,6 +348,22 @@ kill_lxd() {
   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
diff --git a/test/suites/backup.sh b/test/suites/backup.sh
index 55c5034..ed1fe13 100644
--- a/test/suites/backup.sh
+++ b/test/suites/backup.sh
@@ -3,65 +3,82 @@
 test_container_import() {
   ensure_import_testimage
 
-  lxc init testimage ctImport
-  lxc snapshot ctImport
-  lxc start ctImport
-  ! lxd import ctImport
-  lxd import ctImport --force
-  lxc info ctImport | grep snap0
-  lxc delete --force ctImport
-
-  lxc init testimage ctImport
-  lxc snapshot ctImport
-  lxc start ctImport
-  sqlite3 "${LXD_DIR}/lxd.db" "DELETE FROM containers WHERE name='ctImport'"
-  ! lxd import ctImport
-  lxd import ctImport --force
-  lxc info ctImport | grep snap0
-  lxc delete --force ctImport
+  LXD_IMPORT_DIR=$(mktemp -d -p "${TEST_DIR}" XXX)
+  chmod +x "${LXD_IMPORT_DIR}"
+  spawn_lxd "${LXD_IMPORT_DIR}" true
+  (
+    lxc init testimage ctImport
+    lxc snapshot ctImport
+    lxc start ctImport
+    ! lxd import ctImport
+    lxd import ctImport --force
+    lxc info ctImport | grep snap0
+    lxc delete --force ctImport
 
-  lxc init testimage ctImport
-  lxc snapshot ctImport
-  lxc start ctImport
-  sqlite3 "${LXD_DIR}/lxd.db" "DELETE FROM containers WHERE name='ctImport/snap0'"
-  ! lxd import ctImport
-  lxd import ctImport --force
-  lxc info ctImport | grep snap0
-  lxc delete --force ctImport
+    lxc init testimage ctImport
+    lxc snapshot ctImport
+    lxc start ctImport
+    shutdown_lxd "${LXD_IMPORT_DIR}"
+    sqlite3 "${LXD_DIR}/lxd.db" "DELETE FROM containers WHERE name='ctImport'"
+    respawn_lxd "${LXD_IMPORT_DIR}"
+    ! lxd import ctImport
+    lxd import ctImport --force
+    lxc info ctImport | grep snap0
+    lxc delete --force ctImport
 
-  lxc init testimage ctImport
-  lxc snapshot ctImport
-  lxc start ctImport
-  sqlite3 "${LXD_DIR}/lxd.db" "DELETE FROM containers WHERE name='ctImport'"
-  sqlite3 "${LXD_DIR}/lxd.db" "DELETE FROM containers WHERE name='ctImport/snap0'"
-  sqlite3 "${LXD_DIR}/lxd.db" "DELETE FROM storage_volumes WHERE name='ctImport'"
-  ! lxd import ctImport
-  lxd import ctImport --force
-  lxc info ctImport | grep snap0
-  lxc delete --force ctImport
+    lxc init testimage ctImport
+    lxc snapshot ctImport
+    lxc start ctImport
+    shutdown_lxd "${LXD_IMPORT_DIR}"
+    sqlite3 "${LXD_DIR}/lxd.db" "DELETE FROM containers WHERE name='ctImport/snap0'"
+    respawn_lxd "${LXD_IMPORT_DIR}"
+    ! lxd import ctImport
+    lxd import ctImport --force
+    lxc info ctImport | grep snap0
+    lxc delete --force ctImport
 
-  lxc init testimage ctImport
-  lxc snapshot ctImport
-  lxc start ctImport
-  sqlite3 "${LXD_DIR}/lxd.db" "DELETE FROM containers WHERE name='ctImport'"
-  sqlite3 "${LXD_DIR}/lxd.db" "DELETE FROM containers WHERE name='ctImport/snap0'"
-  sqlite3 "${LXD_DIR}/lxd.db" "DELETE FROM storage_volumes WHERE name='ctImport'"
-  sqlite3 "${LXD_DIR}/lxd.db" "DELETE FROM storage_volumes WHERE name='ctImport/snap0'"
-  lxd import ctImport
-  lxd import ctImport --force
-  lxc info ctImport | grep snap0
-  lxc delete --force ctImport
+    lxc init testimage ctImport
+    lxc snapshot ctImport
+    lxc start ctImport
+    shutdown_lxd "${LXD_IMPORT_DIR}"
+    sqlite3 "${LXD_DIR}/lxd.db" "DELETE FROM containers WHERE name='ctImport'"
+    sqlite3 "${LXD_DIR}/lxd.db" "DELETE FROM containers WHERE name='ctImport/snap0'"
+    sqlite3 "${LXD_DIR}/lxd.db" "DELETE FROM storage_volumes WHERE name='ctImport'"
+    respawn_lxd "${LXD_IMPORT_DIR}"
+    ! lxd import ctImport
+    lxd import ctImport --force
+    lxc info ctImport | grep snap0
+    lxc delete --force ctImport
 
-  # Test whether a snapshot that exists on disk but not in the "backup.yaml"
-  # file is correctly restored. This can be done by not starting the parent
-  # container which avoids that the backup file is written out.
-  if [ "${LXD_BACKEND}" = "dir" ]; then
     lxc init testimage ctImport
     lxc snapshot ctImport
+    lxc start ctImport
+    shutdown_lxd "${LXD_IMPORT_DIR}"
+    sqlite3 "${LXD_DIR}/lxd.db" "DELETE FROM containers WHERE name='ctImport'"
+    sqlite3 "${LXD_DIR}/lxd.db" "DELETE FROM containers WHERE name='ctImport/snap0'"
+    sqlite3 "${LXD_DIR}/lxd.db" "DELETE FROM storage_volumes WHERE name='ctImport'"
     sqlite3 "${LXD_DIR}/lxd.db" "DELETE FROM storage_volumes WHERE name='ctImport/snap0'"
-    ! lxd import ctImport
+    respawn_lxd "${LXD_IMPORT_DIR}"
+    lxd import ctImport
     lxd import ctImport --force
     lxc info ctImport | grep snap0
     lxc delete --force ctImport
-  fi
+
+    # Test whether a snapshot that exists on disk but not in the "backup.yaml"
+    # file is correctly restored. This can be done by not starting the parent
+    # container which avoids that the backup file is written out.
+    if [ "${LXD_BACKEND}" = "dir" ]; then
+      lxc init testimage ctImport
+      lxc snapshot ctImport
+      shutdown_lxd "${LXD_IMPORT_DIR}"
+      sqlite3 "${LXD_DIR}/lxd.db" "DELETE FROM storage_volumes WHERE name='ctImport/snap0'"
+      respawn_lxd "${LXD_IMPORT_DIR}"
+      ! lxd import ctImport
+      lxd import ctImport --force
+      lxc info ctImport | grep snap0
+      lxc delete --force ctImport
+    fi
+  )
+  # shellcheck disable=SC2031
+  kill_lxd "${LXD_IMPORT_DIR}"
 }


More information about the lxc-devel mailing list