Files
snapd/tests/lib/tools/tests.nested
Sergio Cazzolato e8aa3f5757 Fix wording on help
2021-07-13 09:22:07 -03:00

207 lines
4.4 KiB
Bash
Executable File

#!/bin/bash -e
show_help() {
echo "usage: prepare"
echo " restore"
echo " build-image IMAGE-TYPE"
echo " create-vm IMAGE-TYPE [--param-cdrom PARAM] [--param-mem PARAM]"
echo " start-vm"
echo " stop-vm"
echo " remove-vm"
echo " exec <CMD>"
echo " exec <USER> <PWD> <CMD>"
echo " copy <FILEPATH>"
echo " wait-for <EVENT>"
echo ""
echo "Available options:"
echo " -h --help show this help message."
echo ""
echo "COMMANDS:"
echo " prepare: creates all the directories needed to run a nested test"
echo " restore: removes all the directories and data used by nested tests"
echo " build-image: creates an image using ubuntu image tool"
echo " create-vm: creates new virtual machine and leave it running"
echo " start-vm: starts a stopped vm"
echo " stop-vm: shutdowns a running vm"
echo " remove-vm: removes a vm"
echo " exec: executes a command in the vm"
echo " exec-as: executes a command in the vm as the specified user"
echo " copy: copies a file to the vm"
echo " wait-for: waits for the specified event"
echo ""
echo "IMAGE-TYPES:"
echo " core: work with a core image"
echo " classic: work with a classic image"
echo ""
echo "EVENT:"
echo " ssh: it is possible to ssh to the vm"
echo " no-ssh: is not possible to ssh to the vm"
echo " snap-command: snap command is available in the vm"
echo " reboot: reboot is done in the vm"
echo ""
}
prepare() {
nested_prepare_env
}
restore() {
nested_cleanup_env
}
build_image() {
if [ $# -eq 0 ]; then
show_help
exit 1
fi
while [ $# -gt 0 ]; do
case "$1" in
classic)
nested_create_classic_vm
exit
;;
core)
nested_create_core_vm
exit
;;
*)
echo "nested-state: expected either classic or core as argument" >&2
exit 1
;;
esac
done
}
create_vm() {
if [ $# -eq 0 ]; then
show_help
exit 1
fi
local action=
case "$1" in
classic)
shift 1
action=nested_start_classic_vm
;;
core)
shift 1
action=nested_start_core_vm
;;
*)
echo "nested-state: unsupported parameter $1" >&2
exit 1
;;
esac
while [ $# -gt 0 ]; do
case "$1" in
--param-cdrom)
export NESTED_PARAM_CD="$2"
shift 2
;;
--param-mem)
export NESTED_PARAM_MEM="$2"
shift 2
;;
*)
echo "nested-state: unsupported parameter $1" >&2
exit 1
;;
esac
done
"$action"
}
start_vm() {
nested_start
}
stop_vm() {
nested_shutdown
}
remove_vm() {
nested_destroy_vm
}
exec() {
nested_exec "$@"
}
exec_as() {
nested_exec_as "$@"
}
copy() {
nested_copy "$@"
}
wait_for() {
if [ $# -eq 0 ]; then
show_help
exit 1
fi
local action=
case "$1" in
ssh)
action=nested_wait_for_ssh
shift
;;
no-ssh)
action=nested_wait_for_no_ssh
shift
;;
snap-command)
action=nested_wait_for_snap_command
shift
;;
reboot)
action=nested_wait_for_reboot
shift
;;
*)
echo "tests.nested: unsupported parameter $1" >&2
exit 1
;;
esac
"$action" "$@"
}
main() {
if [ $# -eq 0 ]; then
show_help
exit 0
fi
local subcommand="$1"
local action=
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
show_help
exit 0
;;
*)
action=$(echo "$subcommand" | tr '-' '_')
shift
break
;;
esac
done
if [ -z "$(declare -f "$action")" ]; then
echo "tests.nested: no such command: $subcommand"
show_help
exit 1
fi
#shellcheck source=tests/lib/nested.sh
. "$TESTSLIB/nested.sh"
"$action" "$@"
}
main "$@"