Skip to content

Commit 1516db4

Browse files
committed
handle custommode options
1 parent 8afcb25 commit 1516db4

2 files changed

Lines changed: 111 additions & 5 deletions

File tree

resources/tools/generate_robot_files.sh

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#!/bin/bash
2+
23
if [[ -z "$1" || "$1" =~ ^--help|-h$ ]] || [[ ! "$1" =~ ^[a-z0-9:]+$ ]]; then
34
echo "Usage: ${0##*/} path::to::plugin [mode]"
4-
5+
echo
6+
echo "Prepare a robot test file for the mode passed as second argument (or all available modes if none provided)."
7+
echo "Example: ${0##*/} os::linux::snmp::plugin cpu"
8+
echo
59
exit
610
fi
711

@@ -39,6 +43,36 @@ mkdir -p "$tests_path"
3943

4044
# part that is specific to mockoon-based tests
4145
function print_mockoon_tpl() {
46+
# if a custom mode is provided, use it's options in the main command definition
47+
if [[ "$custommode" != "" ]]; then
48+
cat <<EOF
49+
Suite Setup Start Mockoon \${MOCKOON_JSON}
50+
Suite Teardown Stop Mockoon
51+
Test Timeout 120s
52+
53+
54+
*** Variables ***
55+
\${MOCKOON_JSON} \${CURDIR}\${/}mockoon.json
56+
\${CMD} \${CENTREON_PLUGINS}
57+
... --plugin=$plugin
58+
... --mode=$mode
59+
EOF
60+
declare -A option_value=(
61+
[--hostname]="\${HOSTNAME}"
62+
[--port]="\${APIPORT}"
63+
[--proto]="http"
64+
[--username]="username"
65+
[--password]="password"
66+
[--token]="token"
67+
[--timeout]="10"
68+
)
69+
for option in "${custommode_options[@]}"; do
70+
value="${option_value[$option]}"
71+
[[ "$value" == "" ]] && value="${option#--}"
72+
echo "... ${option}=$value"
73+
done
74+
else
75+
# if no custom mode is provided (it barely happens) put default options as placeholders
4276
cat <<EOF
4377
Suite Setup Start Mockoon \${MOCKOON_JSON}
4478
Suite Teardown Stop Mockoon
@@ -57,6 +91,7 @@ Test Timeout 120s
5791
... --password=1
5892
5993
EOF
94+
fi
6095
}
6196

6297
# part that is specific to snmp-based tests
@@ -75,7 +110,8 @@ Test Timeout 120s
75110
... --mode=$mode
76111
... --hostname=\${HOSTNAME}
77112
... --snmp-port=\${SNMPPORT}
78-
... --snmp-community=$community/TO_BE_COMPLETED
113+
... --snmp-version=\${SNMPVERSION}
114+
... --snmp-community=$community/snmpwalk_file_base_name
79115
EOF
80116
}
81117

@@ -158,14 +194,24 @@ fi
158194
for mode in "${modes[@]}"; do
159195
info "Generating tests for mode $mode"
160196
robot_file="${tests_path}/${mode}.robot"
161-
# get the list of options in variable threshold_options
162-
eval $(parse_threshold_options_from_help $base_cmd --mode=$mode --help)
197+
# get the first custom mode
198+
199+
custommode=$(get_first_custommode $base_cmd --mode=$mode --list-custommode)
200+
201+
# get the list of options brought by the custommode if any
202+
declare -a custommode_options
203+
if [[ "$custommode" != "" ]] ; then
204+
eval $(parse_custommode_options_from_help $custommode $base_cmd --custommode=$custommode --mode=$mode --help)
205+
[[ $DEBUG ]] && declare -p custommode_options
206+
fi
207+
208+
eval $(parse_threshold_options_from_help $base_cmd --custommode=$custommode --mode=$mode --help)
163209
[[ $DEBUG ]] && declare -p threshold_options
164210

165211
# Backup the file if it already exists
166212
if [[ -f "$robot_file" ]] ; then
167213
robot_backup="${robot_file}_$(date +%F_%H-%M-%S).${RANDOM}"
168-
warning "Backing up $robot_file to $robot_backup"
214+
warning "$robot_file already exists! Backing it up to $robot_backup"
169215
cp "$robot_file" "$robot_backup"
170216
fi
171217
info "Writing test file ${robot_file}"

resources/tools/lib/common_functions.sh

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,63 @@ function parse_threshold_options_from_help() {
9191
[[ $DEBUG ]] && declare -p threshold_options >&2
9292
declare -p threshold_options
9393
}
94+
95+
# get_first_custommode(command_line arg1 arg2 --help):
96+
# Arguments: command and arguments of a help command
97+
# Output: string containing the first available custom-mode
98+
function get_first_custommode() {
99+
local IFS=$'\n'
100+
[[ $DEBUG ]] && set -x
101+
local HELP_OUTPUT=( $(perl $* 2>/dev/null ) )
102+
[[ $DEBUG ]] && set +x
103+
104+
declare -a threshold_options
105+
# Remove all lines above "Mode:"
106+
107+
local in_custom_modes=
108+
local line
109+
for line in "${HELP_OUTPUT[@]}" ; do
110+
if [[ "$line" == 'Custom Modes Available:' ]] ; then
111+
in_custom_modes=1
112+
continue
113+
fi
114+
[[ -z "$in_custom_modes" ]] && continue
115+
trim $line
116+
break
117+
done
118+
}
119+
120+
121+
# parse_custommode_options_from_help(custommode command_line arg1 arg2 --help):
122+
# Arguments: command and arguments of a help command
123+
# Output: declaration of an array variable named custommode_options with the list of options that are specific to the mode. Result has to be loaded using eval.
124+
function parse_custommode_options_from_help() {
125+
local IFS=$'\n'
126+
local custommode="$1"
127+
shift
128+
[[ $DEBUG ]] && set -x
129+
local HELP_OUTPUT=( $(perl $* 2>/dev/null ) )
130+
[[ $DEBUG ]] && set +x
131+
132+
declare -a custommode_options
133+
134+
# Remove all lines above ".* $custommode Options:"
135+
local in_custommode_help=
136+
local line
137+
regex="$custommode Options:"
138+
for line in "${HELP_OUTPUT[@]}" ; do
139+
if [[ "$line" =~ $regex ]] ; then
140+
in_custommode_help=1
141+
continue
142+
fi
143+
[[ -z "$in_custommode_help" ]] && continue
144+
[[ "$line" =~ ^[[:space:]] ]] || break
145+
[[ "$line" =~ --(warning|critical)-\* ]] && fatal "Not parsing --warning-*/--critical-* thresholds"
146+
[[ "$line" =~ ^[[:space:]]*(--[a-z0-9-]+) ]] || continue
147+
148+
custommode_options+=( ${BASH_REMATCH[1]} )
149+
done
150+
151+
[[ $DEBUG ]] && declare -p custommode_options >&2
152+
declare -p custommode_options
153+
}

0 commit comments

Comments
 (0)