forked from rapido-linux/rapido
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrapido
executable file
·167 lines (146 loc) · 3.49 KB
/
rapido
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/bash
#
# Copyright (C) SUSE LINUX GmbH 2018, all rights reserved.
#
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; either version 2.1 of the License, or
# (at your option) version 3.
#
# This library 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 Lesser General Public
# License for more details.
RAPIDO_DIR="$(realpath -e ${0%/*})"
declare -A short_help
short_help["teardown-network"]="Teardown network for VMs"
rapido_teardown_network()
{
${RAPIDO_DIR}/tools/br_tap_teardown.sh "$@"
}
short_help["setup-network"]="Setup network for VMs"
rapido_setup_network()
{
${RAPIDO_DIR}/tools/br_tap_setup.sh "$@"
}
# vm is an alias for boot
rapido_vm()
{
${RAPIDO_DIR}/vm.sh
}
short_help["boot"]="Boot previously prepared test"
rapido_boot()
{
${RAPIDO_DIR}/vm.sh
}
cut_usage()
{
local progname="$(basename $0)"
cat << EOF
Usage: $progname cut [-B] [-f file] [-x cmd] testname
-B: cut testcase image only, don't boot it
-f <file>: run script file within the VM upon boot
-x <cmd>: run command string within the VM upon boot
<testname>: testcase name. See '$progname list' for options
EOF
}
short_help["cut"]="Prepare testcase and boot it"
rapido_cut()
{
local boot_img="yes"
local option=""
local post_autorun_files=()
local cleanup="rm "
local t cut_script
while getopts "Bf:x:" option; do
case $option in
B)
unset boot_img
;;
f)
# existence checked via _rt_require_dracut_args()
post_autorun_files+=($(realpath "$OPTARG"))
;;
x)
t="$(mktemp --tmpdir rapido_post_autorun.XXXXXXXXXX)" \
|| exit 1
printf "%s" "$OPTARG" > "$t" || exit 1
post_autorun_files+=("$t")
cleanup="$cleanup \"$t\""
;;
*)
echo "Invalid cut parameter"
cut_usage
exit 1
;;
esac
done
[ -f "$t" ] && trap "$cleanup" 0 1 2 3 15
# shift away any processed params, so we're left with testname
shift $(($OPTIND - 1))
local testname=$1
if [ $# -ne 1 -o -z "$testname" -o x"$testname" = "xhelp" ]; then
cut_usage
exit 1
fi
pushd "$RAPIDO_DIR" > /dev/null
cut_script="cut/${testname//-/_}.sh"
if [[ ! -x $cut_script ]]; then
[[ -f $cut_script ]] \
&& echo "$cut_script lacks execute permission." \
|| echo "$testname not found. See \"rapido list\"."
popd > /dev/null
exit
fi
./$cut_script "${post_autorun_files[@]}"
local cut_status=$?
popd > /dev/null
[ $cut_status -ne 0 ] && exit $cut_status
[ -n "$boot_img" ] || exit 0
"${RAPIDO_DIR}/vm.sh"
}
short_help["list"]="List testcases"
rapido_list()
{
local t
pushd "$RAPIDO_DIR" > /dev/null
shopt -s nullglob
for t in cut/*.sh; do
[ -x "$t" ] || continue
t="${t%.sh}"
t="${t//_/-}"
echo "${t#cut/}"
done
popd > /dev/null
}
list_commands()
{
local command
for command in $(declare -F); do
[[ $command =~ rapido_([a-zA-Z_]*) ]] || continue
command="${BASH_REMATCH[1]}"
command="${command//_/-}"
if [ -n "${short_help[$command]}" ]; then
echo -e "\t$command - ${short_help[$command]}"
fi
done
}
short_help["help"]="Show help"
rapido_help()
{
echo "Usage: $(basename $0) SUBCOMMAND [ARGUMENTS]"
echo
echo "The available subcommands are:"
list_commands
}
if [ "$#" = "0" ]; then
subcmd_func=rapido_help
else
subcmd_func=rapido_${1//-/_}
shift
fi
if ! declare -f $subcmd_func > /dev/null; then
rapido_help
exit 1
fi
$subcmd_func "$@"