p2v: Add proper test for command line parsing.

This also adds a couple of deliberately undocumented (and unsupported)
command line parameters to make testing simpler:

p2v.skip_test_connection  - don't try to test the connection
p2v.dump_config_and_exit  - after parsing command line, print it and exit

This updates commit 716244c337.
This commit is contained in:
Richard W.M. Jones
2015-06-10 14:06:03 +01:00
parent a716adac35
commit 54fe6d369d
3 changed files with 78 additions and 6 deletions

View File

@@ -155,8 +155,11 @@ stamp-virt-p2v-make-kickstart.pod: virt-p2v-make-kickstart.pod
TESTS_ENVIRONMENT = $(top_builddir)/run --test
if ENABLE_APPLIANCE
TESTS = \
test-virt-p2v-cmdline.sh
if ENABLE_APPLIANCE
TESTS += \
test-virt-p2v.sh
endif ENABLE_APPLIANCE

View File

@@ -79,12 +79,15 @@ kernel_configuration (struct config *config, char **cmdline, int cmdline_source)
/* We should now be able to connect and interrogate virt-v2v
* on the conversion server.
*/
if (test_connection (config) == -1) {
const char *err = get_ssh_error ();
p = get_cmdline_key (cmdline, "p2v.skip_test_connection");
if (!p) {
if (test_connection (config) == -1) {
const char *err = get_ssh_error ();
fprintf (stderr, "%s: error opening control connection to %s:%d: %s\n",
guestfs_int_program_name, config->server, config->port, err);
exit (EXIT_FAILURE);
fprintf (stderr, "%s: error opening control connection to %s:%d: %s\n",
guestfs_int_program_name, config->server, config->port, err);
exit (EXIT_FAILURE);
}
}
p = get_cmdline_key (cmdline, "p2v.name");
@@ -196,6 +199,13 @@ kernel_configuration (struct config *config, char **cmdline, int cmdline_source)
config->output_storage = strdup (p);
}
/* Undocumented command line tool used for testing command line parsing. */
p = get_cmdline_key (cmdline, "p2v.dump_config_and_exit");
if (p) {
print_config (config, stdout);
exit (EXIT_SUCCESS);
}
/* Perform the conversion in text mode. */
if (start_conversion (config, notify_ui_callback) == -1) {
const char *err = get_conversion_error ();

59
p2v/test-virt-p2v-cmdline.sh Executable file
View File

@@ -0,0 +1,59 @@
#!/bin/bash -
# libguestfs virt-p2v test script
# Copyright (C) 2015 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.
# Test virt-p2v command line parsing in non-GUI mode.
unset CDPATH
export LANG=C
set -e
if [ -n "$SKIP_TEST_VIRT_P2V_CMDLINE_SH" ]; then
echo "$0: test skipped because environment variable is set"
exit 77
fi
out=test-virt-p2v-cmdline.out
rm -f $out
# The Linux kernel command line.
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
# For debugging purposes.
cat $out
# Check the output contains what we expect.
grep "^echo 1 2 3" $out
grep "^1 2 3" $out
grep "^conversion server.*localhost" $out
grep "^port.*123" $out
grep "^username.*user" $out
grep "^sudo.*false" $out
grep "^guest name.*test" $out
grep "^vcpus.*4" $out
grep "^memory.*"$((1024*1024*1024)) $out
grep "^disks.*sda sdb sdc" $out
grep "^removable.*sdd" $out
grep "^interfaces.*eth0 eth1" $out
grep "^network map.*em1:wired other" $out
grep "^output.*local" $out
grep "^output alloc.*sparse" $out
grep "^output conn.*qemu:///session" $out
grep "^output format.*raw" $out
grep "^output storage.*/var/tmp" $out
rm $out