Blame SOURCES/0190-p2v-Add-proper-test-for-command-line-parsing.patch

ffd6ed
From 1dc7e819286898b621dfb47b4ec2b9872b7df0db Mon Sep 17 00:00:00 2001
ffd6ed
From: "Richard W.M. Jones" <rjones@redhat.com>
ffd6ed
Date: Wed, 10 Jun 2015 14:06:03 +0100
ffd6ed
Subject: [PATCH] p2v: Add proper test for command line parsing.
ffd6ed
ffd6ed
This also adds a couple of deliberately undocumented (and unsupported)
ffd6ed
command line parameters to make testing simpler:
ffd6ed
ffd6ed
p2v.skip_test_connection  - don't try to test the connection
ffd6ed
p2v.dump_config_and_exit  - after parsing command line, print it and exit
ffd6ed
ffd6ed
This updates commit 716244c33718c866edce9e7ee8f21ee612f53337.
ffd6ed
ffd6ed
(cherry picked from commit 54fe6d369d18680e53d3f60be3dbdb42356bd94f)
ffd6ed
---
ffd6ed
 p2v/Makefile.am              |  5 +++-
ffd6ed
 p2v/kernel.c                 | 20 +++++++++++----
ffd6ed
 p2v/test-virt-p2v-cmdline.sh | 59 ++++++++++++++++++++++++++++++++++++++++++++
ffd6ed
 3 files changed, 78 insertions(+), 6 deletions(-)
ffd6ed
 create mode 100755 p2v/test-virt-p2v-cmdline.sh
ffd6ed
ffd6ed
diff --git a/p2v/Makefile.am b/p2v/Makefile.am
ffd6ed
index cafe597..1cc32d7 100644
ffd6ed
--- a/p2v/Makefile.am
ffd6ed
+++ b/p2v/Makefile.am
ffd6ed
@@ -140,8 +140,11 @@ stamp-virt-p2v-make-kickstart.pod: virt-p2v-make-kickstart.pod
ffd6ed
 
ffd6ed
 TESTS_ENVIRONMENT = $(top_builddir)/run --test
ffd6ed
 
ffd6ed
-if ENABLE_APPLIANCE
ffd6ed
 TESTS = \
ffd6ed
+	test-virt-p2v-cmdline.sh
ffd6ed
+
ffd6ed
+if ENABLE_APPLIANCE
ffd6ed
+TESTS += \
ffd6ed
 	test-virt-p2v.sh
ffd6ed
 endif ENABLE_APPLIANCE
ffd6ed
 
ffd6ed
diff --git a/p2v/kernel.c b/p2v/kernel.c
ffd6ed
index 397d19d..fd67921 100644
ffd6ed
--- a/p2v/kernel.c
ffd6ed
+++ b/p2v/kernel.c
ffd6ed
@@ -79,12 +79,15 @@ kernel_configuration (struct config *config, char **cmdline, int cmdline_source)
ffd6ed
   /* We should now be able to connect and interrogate virt-v2v
ffd6ed
    * on the conversion server.
ffd6ed
    */
ffd6ed
-  if (test_connection (config) == -1) {
ffd6ed
-    const char *err = get_ssh_error ();
ffd6ed
+  p = get_cmdline_key (cmdline, "p2v.skip_test_connection");
ffd6ed
+  if (!p) {
ffd6ed
+    if (test_connection (config) == -1) {
ffd6ed
+      const char *err = get_ssh_error ();
ffd6ed
 
ffd6ed
-    fprintf (stderr, "%s: error opening control connection to %s:%d: %s\n",
ffd6ed
-             guestfs_int_program_name, config->server, config->port, err);
ffd6ed
-    exit (EXIT_FAILURE);
ffd6ed
+      fprintf (stderr, "%s: error opening control connection to %s:%d: %s\n",
ffd6ed
+               guestfs_int_program_name, config->server, config->port, err);
ffd6ed
+      exit (EXIT_FAILURE);
ffd6ed
+    }
ffd6ed
   }
ffd6ed
 
ffd6ed
   p = get_cmdline_key (cmdline, "p2v.name");
ffd6ed
@@ -196,6 +199,13 @@ kernel_configuration (struct config *config, char **cmdline, int cmdline_source)
ffd6ed
     config->output_storage = strdup (p);
ffd6ed
   }
ffd6ed
 
ffd6ed
+  /* Undocumented command line tool used for testing command line parsing. */
ffd6ed
+  p = get_cmdline_key (cmdline, "p2v.dump_config_and_exit");
ffd6ed
+  if (p) {
ffd6ed
+    print_config (config, stdout);
ffd6ed
+    exit (EXIT_SUCCESS);
ffd6ed
+  }
ffd6ed
+
ffd6ed
   /* Perform the conversion in text mode. */
ffd6ed
   if (start_conversion (config, notify_ui_callback) == -1) {
ffd6ed
     const char *err = get_conversion_error ();
ffd6ed
diff --git a/p2v/test-virt-p2v-cmdline.sh b/p2v/test-virt-p2v-cmdline.sh
ffd6ed
new file mode 100755
ffd6ed
index 0000000..bdd51d3
ffd6ed
--- /dev/null
ffd6ed
+++ b/p2v/test-virt-p2v-cmdline.sh
ffd6ed
@@ -0,0 +1,59 @@
ffd6ed
+#!/bin/bash -
ffd6ed
+# libguestfs virt-p2v test script
ffd6ed
+# Copyright (C) 2015 Red Hat Inc.
ffd6ed
+#
ffd6ed
+# This program is free software; you can redistribute it and/or modify
ffd6ed
+# it under the terms of the GNU General Public License as published by
ffd6ed
+# the Free Software Foundation; either version 2 of the License, or
ffd6ed
+# (at your option) any later version.
ffd6ed
+#
ffd6ed
+# This program is distributed in the hope that it will be useful,
ffd6ed
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
ffd6ed
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ffd6ed
+# GNU General Public License for more details.
ffd6ed
+#
ffd6ed
+# You should have received a copy of the GNU General Public License
ffd6ed
+# along with this program; if not, write to the Free Software
ffd6ed
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ffd6ed
+
ffd6ed
+# Test virt-p2v command line parsing in non-GUI mode.
ffd6ed
+
ffd6ed
+unset CDPATH
ffd6ed
+export LANG=C
ffd6ed
+set -e
ffd6ed
+
ffd6ed
+if [ -n "$SKIP_TEST_VIRT_P2V_CMDLINE_SH" ]; then
ffd6ed
+    echo "$0: test skipped because environment variable is set"
ffd6ed
+    exit 77
ffd6ed
+fi
ffd6ed
+
ffd6ed
+out=test-virt-p2v-cmdline.out
ffd6ed
+rm -f $out
ffd6ed
+
ffd6ed
+# The Linux kernel command line.
ffd6ed
+virt-p2v --cmdline='p2v.pre="echo 1 2 3" p2v.server=localhost p2v.port=123 p2v.username=user p2v.password=secret p2v.skip_test_connection p2v.name=test p2v.vcpus=4 p2v.memory=1G p2v.disks=sda,sdb,sdc p2v.removable=sdd p2v.interfaces=eth0,eth1 p2v.o=local p2v.oa=sparse p2v.oc=qemu:///session p2v.of=raw p2v.os=/var/tmp p2v.network=em1:wired,other p2v.dump_config_and_exit' > $out
ffd6ed
+
ffd6ed
+# For debugging purposes.
ffd6ed
+cat $out
ffd6ed
+
ffd6ed
+# Check the output contains what we expect.
ffd6ed
+grep "^echo 1 2 3" $out
ffd6ed
+grep "^1 2 3" $out
ffd6ed
+grep "^conversion server.*localhost" $out
ffd6ed
+grep "^port.*123" $out
ffd6ed
+grep "^username.*user" $out
ffd6ed
+grep "^sudo.*false" $out
ffd6ed
+grep "^guest name.*test" $out
ffd6ed
+grep "^vcpus.*4" $out
ffd6ed
+grep "^memory.*"$((1024*1024*1024)) $out
ffd6ed
+grep "^disks.*sda sdb sdc" $out
ffd6ed
+grep "^removable.*sdd" $out
ffd6ed
+grep "^interfaces.*eth0 eth1" $out
ffd6ed
+grep "^network map.*em1:wired other" $out
ffd6ed
+grep "^output.*local" $out
ffd6ed
+grep "^output alloc.*sparse" $out
ffd6ed
+grep "^output conn.*qemu:///session" $out
ffd6ed
+grep "^output format.*raw" $out
ffd6ed
+grep "^output storage.*/var/tmp" $out
ffd6ed
+
ffd6ed
+rm $out
ffd6ed
-- 
ffd6ed
1.8.3.1
ffd6ed