diff mbox

[kvm-unit-tests,v4,1/2] Add the possibility to do simple migration tests

Message ID 1490171287-30893-2-git-send-email-thuth@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Thomas Huth March 22, 2017, 8:28 a.m. UTC
To be able to do simple migration tests with kvm-unit-tests, too,
add a helper script that does all the necessary work: Start two
instances of QEMU (source and destination) with QMP sockets for
sending commands to them, then trigger the migration from one
instance to the other and finally signal the end of the migration
to the guest by injecting an NMI.
This helper script is now used automatically for powerpc tests
if the test is put into the "migration" group in the unittests.cfg
file.

Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 powerpc/run           |  2 +-
 scripts/arch-run.bash | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++
 scripts/runtime.bash  |  3 +++
 3 files changed, 67 insertions(+), 1 deletion(-)

Comments

Andrew Jones March 22, 2017, 8:41 a.m. UTC | #1
On Wed, Mar 22, 2017 at 09:28:06AM +0100, Thomas Huth wrote:
> To be able to do simple migration tests with kvm-unit-tests, too,
> add a helper script that does all the necessary work: Start two
> instances of QEMU (source and destination) with QMP sockets for
> sending commands to them, then trigger the migration from one
> instance to the other and finally signal the end of the migration
> to the guest by injecting an NMI.
> This helper script is now used automatically for powerpc tests
> if the test is put into the "migration" group in the unittests.cfg
> file.
> 
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---
>  powerpc/run           |  2 +-
>  scripts/arch-run.bash | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++
>  scripts/runtime.bash  |  3 +++
>  3 files changed, 67 insertions(+), 1 deletion(-)
> 
> diff --git a/powerpc/run b/powerpc/run
> index 6269abb..d92608e 100755
> --- a/powerpc/run
> +++ b/powerpc/run
> @@ -46,7 +46,7 @@ M+=",accel=$ACCEL"
>  command="$qemu -nodefaults $M -bios $FIRMWARE"
>  [ -f "$ENV" ] && command+=" -initrd $ENV"
>  command+=" -display none -serial stdio -kernel"
> -command="$(timeout_cmd) $command"
> +command="$(migration_cmd) $(timeout_cmd) $command"
>  echo $command "$@"
>  
>  # powerpc tests currently exit with rtas-poweroff, which exits with 0.
> diff --git a/scripts/arch-run.bash b/scripts/arch-run.bash
> index 1610f3b..96bd5c4 100644
> --- a/scripts/arch-run.bash
> +++ b/scripts/arch-run.bash
> @@ -70,3 +70,66 @@ timeout_cmd ()
>  		echo "timeout -k 1s --foreground $TIMEOUT"
>  	fi
>  }
> +
> +qmp ()
> +{
> +	echo '{ "execute": "qmp_capabilities" }{ "execute":' "$2" '}' | nc -U $1
> +}
> +
> +run_migration ()
> +{
> +	if ! command -v nc >/dev/null 2>&1; then
> +		echo "$0 needs nc (netcat)" >&2
> +		exit 2

I would sort of still prefer $FUNCNAME over $0 here, but I don't feel
that strongly about it. Nice catch on the 'exit 2'. 2 is indeed our
designated error code for run-script failures.

> +	fi
> +
> +	qemu=$1
> +	shift
> +
> +	migsock=`mktemp -u -t mig-helper-socket.XXXXXXXXXX`
> +	migout1=`mktemp -t mig-helper-stdout1.XXXXXXXXXX`
> +	qmp1=`mktemp -u -t mig-helper-qmp1.XXXXXXXXXX`
> +	qmp2=`mktemp -u -t mig-helper-qmp2.XXXXXXXXXX`
> +	qmpout1=/dev/null
> +	qmpout2=/dev/null
> +
> +	trap 'rm -f ${migout1} ${migsock} ${qmp1} ${qmp2}' EXIT
> +
> +	$qemu "$@" -chardev socket,id=mon1,path=${qmp1},server,nowait \
> +		 -mon chardev=mon1,mode=control | tee ${migout1} &
> +
> +	$qemu "$@" -chardev socket,id=mon2,path=${qmp2},server,nowait \
> +		 -mon chardev=mon2,mode=control -incoming unix:${migsock} &
> +
> +	# The test must prompt the user to migrate, so wait for the "migrate" keyword
> +	while ! grep -q -i "migrate" < ${migout1} ; do
> +		sleep 1
> +	done
> +
> +	qmp ${qmp1} '"migrate", "arguments": { "uri": "unix:'${migsock}'" }' > ${qmpout1}
> +
> +	# Wait for the migration to complete
> +	migstatus=`qmp ${qmp1} '"query-migrate"' | grep return`
> +	while ! grep -q '"completed"' <<<"$migstatus" ; do
> +		sleep 1
> +		migstatus=`qmp ${qmp1} '"query-migrate"' | grep return`
> +		if grep -q '"failed"' <<<"$migstatus" ; then
> +			echo "ERROR: Migration failed." >&2
> +			qmp ${qmp1} '"quit"'> ${qmpout1} 2>/dev/null
> +			qmp ${qmp2} '"quit"'> ${qmpout2} 2>/dev/null
> +			exit 2

Also a nice improvement, ensuring the qemu processes quit.

> +		fi
> +	done
> +	qmp ${qmp1} '"quit"'> ${qmpout1} 2>/dev/null
> +
> +	qmp ${qmp2} '"inject-nmi"'> ${qmpout2}
> +
> +	wait
> +}
> +
> +migration_cmd ()
> +{
> +	if [ "$MIGRATION" = "yes" ]; then
> +		echo "run_migration"
> +	fi
> +}
> diff --git a/scripts/runtime.bash b/scripts/runtime.bash
> index 98f1835..e630279 100644
> --- a/scripts/runtime.bash
> +++ b/scripts/runtime.bash
> @@ -99,6 +99,9 @@ function run()
>      }
>  
>      cmdline=$(get_cmdline $kernel)
> +    if grep -qw "migration" <<<$groups ; then
> +        cmdline="MIGRATION=yes $cmdline"
> +    fi
>      if [ "$verbose" = "yes" ]; then
>          echo $cmdline
>      fi
> -- 
> 1.8.3.1
>

Reviewed-by: Andrew Jones <drjones@redhat.com>
diff mbox

Patch

diff --git a/powerpc/run b/powerpc/run
index 6269abb..d92608e 100755
--- a/powerpc/run
+++ b/powerpc/run
@@ -46,7 +46,7 @@  M+=",accel=$ACCEL"
 command="$qemu -nodefaults $M -bios $FIRMWARE"
 [ -f "$ENV" ] && command+=" -initrd $ENV"
 command+=" -display none -serial stdio -kernel"
-command="$(timeout_cmd) $command"
+command="$(migration_cmd) $(timeout_cmd) $command"
 echo $command "$@"
 
 # powerpc tests currently exit with rtas-poweroff, which exits with 0.
diff --git a/scripts/arch-run.bash b/scripts/arch-run.bash
index 1610f3b..96bd5c4 100644
--- a/scripts/arch-run.bash
+++ b/scripts/arch-run.bash
@@ -70,3 +70,66 @@  timeout_cmd ()
 		echo "timeout -k 1s --foreground $TIMEOUT"
 	fi
 }
+
+qmp ()
+{
+	echo '{ "execute": "qmp_capabilities" }{ "execute":' "$2" '}' | nc -U $1
+}
+
+run_migration ()
+{
+	if ! command -v nc >/dev/null 2>&1; then
+		echo "$0 needs nc (netcat)" >&2
+		exit 2
+	fi
+
+	qemu=$1
+	shift
+
+	migsock=`mktemp -u -t mig-helper-socket.XXXXXXXXXX`
+	migout1=`mktemp -t mig-helper-stdout1.XXXXXXXXXX`
+	qmp1=`mktemp -u -t mig-helper-qmp1.XXXXXXXXXX`
+	qmp2=`mktemp -u -t mig-helper-qmp2.XXXXXXXXXX`
+	qmpout1=/dev/null
+	qmpout2=/dev/null
+
+	trap 'rm -f ${migout1} ${migsock} ${qmp1} ${qmp2}' EXIT
+
+	$qemu "$@" -chardev socket,id=mon1,path=${qmp1},server,nowait \
+		 -mon chardev=mon1,mode=control | tee ${migout1} &
+
+	$qemu "$@" -chardev socket,id=mon2,path=${qmp2},server,nowait \
+		 -mon chardev=mon2,mode=control -incoming unix:${migsock} &
+
+	# The test must prompt the user to migrate, so wait for the "migrate" keyword
+	while ! grep -q -i "migrate" < ${migout1} ; do
+		sleep 1
+	done
+
+	qmp ${qmp1} '"migrate", "arguments": { "uri": "unix:'${migsock}'" }' > ${qmpout1}
+
+	# Wait for the migration to complete
+	migstatus=`qmp ${qmp1} '"query-migrate"' | grep return`
+	while ! grep -q '"completed"' <<<"$migstatus" ; do
+		sleep 1
+		migstatus=`qmp ${qmp1} '"query-migrate"' | grep return`
+		if grep -q '"failed"' <<<"$migstatus" ; then
+			echo "ERROR: Migration failed." >&2
+			qmp ${qmp1} '"quit"'> ${qmpout1} 2>/dev/null
+			qmp ${qmp2} '"quit"'> ${qmpout2} 2>/dev/null
+			exit 2
+		fi
+	done
+	qmp ${qmp1} '"quit"'> ${qmpout1} 2>/dev/null
+
+	qmp ${qmp2} '"inject-nmi"'> ${qmpout2}
+
+	wait
+}
+
+migration_cmd ()
+{
+	if [ "$MIGRATION" = "yes" ]; then
+		echo "run_migration"
+	fi
+}
diff --git a/scripts/runtime.bash b/scripts/runtime.bash
index 98f1835..e630279 100644
--- a/scripts/runtime.bash
+++ b/scripts/runtime.bash
@@ -99,6 +99,9 @@  function run()
     }
 
     cmdline=$(get_cmdline $kernel)
+    if grep -qw "migration" <<<$groups ; then
+        cmdline="MIGRATION=yes $cmdline"
+    fi
     if [ "$verbose" = "yes" ]; then
         echo $cmdline
     fi