[Libguestfs] [PATCH 9/9] tests/qemu: isolate MD5 calculation in an own shared function

Pino Toscano ptoscano at redhat.com
Tue Nov 4 15:35:32 UTC 2014


md5sum(1) does not exist everywhere, so wrap it in an own function so
the right implementation can be chosen on each OS.  Also, wrapping it
avoid using awk everytime.
---
 tests/qemu/Makefile.am                |  1 +
 tests/qemu/guestfs-md5.sh             | 30 ++++++++++++++++++++++++++++++
 tests/qemu/qemu-liveness.sh           |  6 ++++--
 tests/qemu/qemu-snapshot-isolation.sh | 14 ++++++++------
 4 files changed, 43 insertions(+), 8 deletions(-)
 create mode 100755 tests/qemu/guestfs-md5.sh

diff --git a/tests/qemu/Makefile.am b/tests/qemu/Makefile.am
index e234063..478b3f1 100644
--- a/tests/qemu/Makefile.am
+++ b/tests/qemu/Makefile.am
@@ -30,6 +30,7 @@ TESTS_ENVIRONMENT = $(top_builddir)/run --test
 
 EXTRA_DIST = \
 	$(TESTS) \
+	guestfs-md5.sh \
 	qemu-boot.c \
 	qemu-speed-test.c
 
diff --git a/tests/qemu/guestfs-md5.sh b/tests/qemu/guestfs-md5.sh
new file mode 100755
index 0000000..79dbd67
--- /dev/null
+++ b/tests/qemu/guestfs-md5.sh
@@ -0,0 +1,30 @@
+#!/bin/bash -
+# libguestfs
+# Copyright (C) 2014 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+do_md5 ()
+{
+  case "$(uname)" in
+    Linux)
+      md5sum "$1" | awk '{print $1}'
+      ;;
+    *)
+      echo "$0: unknown method to calculate MD5 of file on $(uname)"
+      exit 1
+      ;;
+  esac
+}
diff --git a/tests/qemu/qemu-liveness.sh b/tests/qemu/qemu-liveness.sh
index 4db71bd..7129bb2 100755
--- a/tests/qemu/qemu-liveness.sh
+++ b/tests/qemu/qemu-liveness.sh
@@ -22,10 +22,12 @@
 
 set -e
 
+. $srcdir/guestfs-md5.sh
+
 rm -f liveness1.img
 
 guestfish sparse liveness1.img 100M
-liveness1_md5sum="$(md5sum liveness1.img | awk '{print $1}')"
+liveness1_md5sum="$(do_md5 liveness1.img)"
 
 guestfish <<'EOF'
 add liveness1.img format:raw
@@ -41,7 +43,7 @@ write /test "This is a test"
 EOF
 
 # Verify that the disk has changed.
-if [ "$(md5sum liveness1.img | awk '{print $1}')" = "$liveness1_md5sum" ]; then
+if [ "$(do_md5 liveness1.img)" = "$liveness1_md5sum" ]; then
     echo "***** ERROR *****"
     echo "Write operations are not modifying an attached disk."
     echo
diff --git a/tests/qemu/qemu-snapshot-isolation.sh b/tests/qemu/qemu-snapshot-isolation.sh
index daa210f..c217dc4 100755
--- a/tests/qemu/qemu-snapshot-isolation.sh
+++ b/tests/qemu/qemu-snapshot-isolation.sh
@@ -22,6 +22,8 @@
 
 set -e
 
+. $srcdir/guestfs-md5.sh
+
 # UML backend doesn't support qcow2 format.
 supports_qcow2=yes
 if [ "$(guestfish get-backend)" = "uml" ]; then
@@ -31,14 +33,14 @@ fi
 rm -f isolation1.img isolation2.img isolation3.img
 
 guestfish sparse isolation1.img 100M
-isolation1_md5sum="$(md5sum isolation1.img | awk '{print $1}')"
+isolation1_md5sum="$(do_md5 isolation1.img)"
 guestfish sparse isolation2.img 100M
-isolation2_md5sum="$(md5sum isolation2.img | awk '{print $1}')"
+isolation2_md5sum="$(do_md5 isolation2.img)"
 
 if [ "$supports_qcow2" = "yes" ]; then
     guestfish \
         disk-create isolation3.img qcow2 100M preallocation:metadata
-    isolation3_md5sum="$(md5sum isolation3.img | awk '{print $1}')"
+    isolation3_md5sum="$(do_md5 isolation3.img)"
     add3="add-drive-opts isolation3.img format:qcow2 readonly:true"
     cmds3="
       part-disk /dev/sdc mbr
@@ -92,14 +94,14 @@ function serious_error
     exit 1
 }
 
-if [ "$(md5sum isolation1.img | awk '{print $1}')" != "$isolation1_md5sum" ]; then
+if [ "$(do_md5 isolation1.img)" != "$isolation1_md5sum" ]; then
     serious_error
 fi
-if [ "$(md5sum isolation2.img | awk '{print $1}')" != "$isolation2_md5sum" ]; then
+if [ "$(do_md5 isolation2.img)" != "$isolation2_md5sum" ]; then
     serious_error
 fi
 if [ "$supports_qcow2" = "yes" -a \
-     "$(md5sum isolation3.img | awk '{print $1}')" != "$isolation3_md5sum" ]; then
+     "$(do_md5 isolation3.img)" != "$isolation3_md5sum" ]; then
     serious_error
 fi
 
-- 
1.9.3




More information about the Libguestfs mailing list