3 # Simple test wrapper around radclient to allow automated UATs
5 # Author Arran Cudbard-Bell <a.cudbardb@freeradius.org>
6 # Copyright 2014-2015 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
7 # Copyright 2015 The FreeRADIUS Project
10 OPTIND=1 # Reset in case getopts has been used previously in the shell.
12 # Environmental variables
13 : ${TESTDIR=$(dirname $0)"/tests"}
14 : ${RADCLIENT='radclient'}
15 : ${FILTER_SUFFIX='_expected'}
16 : ${DICTPATH=$(dirname $0)/share}
17 PATH="$(dirname $0)/bin:${PATH}"
19 # Initialize our own variables
30 # Some very basic logging functions
43 if [ $verbose -gt 0 ]; then
50 echo $(basename $0)" [options] [-- <test_glob0> <test_glob1> <test_globN>]"
51 echo " -h Display this help message."
52 echo " -H <host>[:port] Send test packets to specified host and port (defaults to 127.0.0.1)"
53 echo " -v Verbose mode."
54 echo " -p <number> Run tests in parallel (defaults to 20)."
55 if [ ! -z "$role_types" ]; then
56 echo " -c <cluster> Specify cluster type one of ($cluster_types)."
57 echo " -r <type> Specify server role one of ($role_types)."
59 echo "Note: Test path globs are relative to ${TESTDIR}/<cluster>/<type>/"
63 echo "For role based test file layout create test files under ${TESTDIR}/<cluster>/<type>"
64 echo "Where <cluster> and <type> are substrings found in the FQDN of <host>."
65 echo "For simplified test layout create test files under ${TESTDIR}"
67 echo "The directory containing the tests should contains pairs of request files and filter files."
68 echo "The request file name must contain 'test<num><num><num>."
69 echo "The filter name must match the test name but with a '${FILTER_SUFFIX}' suffix."
71 echo " ${TESTDIR}/test000_my_first_test"
72 echo " ${TESTDIR}/test000_my_first_test${FILTER_SUFFIX}"
74 echo "The directory containing the tests may have multiple subdirectories to group the tests."
77 RADCLIENT=$(which "$RADCLIENT")
78 if [ ! -x "$RADCLIENT" ]; then
79 ERROR "Can't find radclient binary, modify your \$PATH or set RADCLIENT"
83 if [ ! -d "$TESTDIR" ]; then
84 ERROR "Test dir $TESTDIR does not exist, create it or specify it with TESTDIR=<dir>"
89 # Definitions (build these dynamically by looking at the files under tests)
90 cluster_dirs=$(find "$TESTDIR/" -mindepth 1 -maxdepth 1 -type d)
91 cluster_types=$(echo $cluster_dirs | sed 's/\s/ /g')
94 for i in $cluster_dirs; do
95 for j in $(find "$TESTDIR/$i/" -mindepth 1 -maxdepth 1 -type d); do
97 if [ "$role_types" == '' ]; then
100 role_types+="\n$role"
105 if [ -z "$role_types" ]; then
106 DEBUG "Using simple test file layout"
108 DEBUG "Using role based test file layout"
109 role_types=$(echo -e "$role_types" | sort | uniq) # Remove duplicates
110 role_types=$(echo $role_types | sed 's/\s/ /g') # Change \n back to spaces
113 while getopts "h?H:vc:r:p:" opt; do
126 for i in $cluster_types; do
127 if [ "$i" == "$OPTARG" ]; then
131 if [ $found -ne 1 ]; then
132 ERROR "'$OPTARG' is not a valid cluster type"
141 for i in $role_types; do
142 if [ "$i" == "$OPTARG" ]; then
146 if [ $found -ne 1 ]; then
147 ERROR "'$OPTARG' is not a valid role type"
155 if ! echo "$OPTARG" | grep -E '^[0-9]+$' > /dev/null; then
156 ERROR "Non integer argument '$OPTARG' specified for -p"
172 [ "$1" = "--" ] && shift
176 # Match keywords from the hostname to clusters or roles
178 if [ ! -z "$role_types" ]; then
179 this_host=$(hostname -f)
180 for tok in $(echo "$this_host" | sed 's/\./ /g'); do
181 for key in ${cluster_types}; do
182 if echo "$tok" | grep "$key" > /dev/null && [ "$cluster" = '' ]; then cluster="$key"; fi
184 for key in ${role_types}; do
185 if echo "$tok" | grep "$key" > /dev/null && [ "$role" = '' ]; then role="$key"; fi
189 if [ "$cluster" == '' ]; then
190 ERROR "Couldn't determine the cluster $this_host belongs to";
195 if [ "$role" == '' ]; then
196 ERROR "Couldn't determine the role $this_host performs";
201 test_path="${TESTDIR}/${cluster}/${role}"
203 # Otherwise just use the tests in the test dir
206 test_path="${TESTDIR}"
209 if [ "$test_files" != '' ]; then
211 for glob in $test_files; do
212 # Filter out response files (makes wildcards easier), and expand the globs
213 for file in $(find "${test_path}" -depth -path "*${glob}" -and -not -path "*${FILTER_SUFFIX}" -and '(' -type f -or -type l ')'); do
219 # Lexicographical, depth-first
220 test_files=$(find "$test_path" -depth -path '*test[0-9][0-9][0-9]*' -and -not -path "*${FILTER_SUFFIX}" -and '(' -type f -or -type l ')')
221 if [ "$test_files" == '' ]; then
222 ERROR "No test files found in $test_path"
225 INFO "Executing"$(echo "$test_files" | wc -l)" test(s) from ${test_path}"
229 # Check if we got any test files
231 if [ "$test_files" == '' ]; then
232 ERROR "No test files to process"
237 # Output which files were going to be using for testing
239 if [ $verbose -eq 0 ]; then
240 INFO "Executing specified tests"
241 INFO "Use -v to see full list"
243 INFO "Executing specified tests:"
244 for i in $test_files; do
250 # Figure out which tests we can munge into a single file which we can
251 # use to parallelise testing
254 packets=$(mktemp -t "${base}XXX") || exit 1
255 filters=$(mktemp -t "${base}XXX") || exit 1
260 for i in $test_files; do
261 if [ ! -f "$i" -a ! -L "$i" ]; then
262 INFO "Skipping $i: not file"
266 if [ ! -r "$i" ]; then
267 INFO "Skipping $i: not readable (check permissions)"
271 expected="${i}${FILTER_SUFFIX}"
272 if [ ! -f "$expected" -a ! -L "$expected" ]; then
273 DEBUG "$i cannot be parallelised: Can't find 'expected' file"
274 file_args+=" -f \"$i\""
278 if [ ! -r "$expected" ]; then
279 INFO "$i cannot be parallelised: 'expected' file not readable"
280 file_args+=" -f \"${i}:${expected}\""
284 if head -n 1 "$i" | grep -i -E '^#\s*serial' > /dev/null; then
285 DEBUG "$i marked as serial only"
286 serial_file_args+=" -f \"${i}:${expected}\""
290 # Else add it to the master test file
291 cat "$i" >> "$packets"
293 # Add the name of the file so it appears in radclient debug output
294 # and can later be specified with -v -- <test> to drill down.
295 echo "Radclient-Test-Name := \""$(echo "$i" | sed -e "s@${test_path}/\?@@")"\"" >> "$packets"
297 cat "${i}_expected" >> "$filters"
301 if [ `cat "$packets" | wc -l` -gt 0 ]; then
302 file_args+=" -f \"${packets}:${filters}\""
305 if [ ! -z "$file_args" ]; then
308 if [ $verbose -ne 0 ]; then
313 args+=" -t \"$timeout\""
314 args+=" -r \"$retries\""
315 args+=" -p \"$parallel\""
320 DEBUG "Executing: radclient $args"
321 eval radclient $args; ret=$?
322 INFO "(Parallelised tests)"
325 rm -f "$packets" 2>&1 > /dev/null
326 rm -f "$filters" 2>&1 > /dev/null
328 if [ $ret -ne 0 ]; then
329 ERROR "One or more tests failed (radclient exited with $ret)"
334 if [ ! -z "$serial_file_args" ]; then
335 args="$serial_file_args"
337 if [ $verbose -ne 0 ]; then
342 args+=" -t \"$timeout\""
343 args+=" -r \"$retries\""
349 DEBUG "Executing: radclient $args"
350 eval radclient $args; ret=$?
351 INFO "(Serialised tests)"
353 if [ $ret -ne 0 ]; then
354 ERROR "One or more tests failed (radclient exited with $ret)"