3
# $Id: ocf-tester,v 1.2 2006/08/14 09:38:20 andrew Exp $
5
# Copyright (c) 2006 Novell Inc, Andrew Beekhof
8
# This program is free software; you can redistribute it and/or modify
9
# it under the terms of version 2 of the GNU General Public License as
10
# published by the Free Software Foundation.
12
# This program is distributed in the hope that it would be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
# Further, this software is distributed without any warranty that it is
17
# free of the rightful claim of any third person regarding infringement
18
# or the like. Any license provided herein, whether implied or
19
# otherwise, applies only to this software file. Patent licenses, if
20
# any, provided herein do not apply to combinations of this program with
21
# other software, or any other product whatsoever.
23
# You should have received a copy of the GNU General Public License
24
# along with this program; if not, write the Free Software Foundation,
25
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
28
LRMD=@libdir@/heartbeat/lrmd
29
LRMADMIN=@sbindir@/lrmadmin
30
METADATA_LINT="xmllint --noout --valid -"
32
# set some common meta attributes, which are expected to be
33
# present by resource agents
34
export OCF_RESKEY_CRM_meta_timeout=20000 # 20 seconds timeout
35
export OCF_RESKEY_CRM_meta_interval=10000 # reset this for probes
40
# make sure to output errors on stderr
41
[ "x$1" = "x0" ] || exec >&2
43
echo "Tool for testing if a cluster resource is OCF compliant"
45
echo "Usage: ocf-tester [-Lh] -n resource_name [-o name=value]* /full/path/to/resource/agent"
49
echo " -n name Name of the resource"
50
echo " -o name=value Name and value of any parameters required by the agent"
51
echo " -L Use lrmadmin/lrmd for tests"
66
if [ $rc -ne $target ]; then
67
num_errors=`expr $num_errors + 1`
69
if [ $exit_code != 0 ]; then
70
[ -n "$command_output" ] && cat<<EOF
83
while test "$done" = "0"; do
85
-n) OCF_RESOURCE_INSTANCE=$2; ra_args="$ra_args OCF_RESOURCE_INSTANCE=$2"; shift; shift;;
86
-o) name=${2%%=*}; value=${2#*=};
87
lrm_ra_args="$lrm_ra_args $2";
88
ra_args="$ra_args OCF_RESKEY_$name=$value"; shift; shift;;
89
-L) use_lrmd=1; shift;;
90
-v) verbose=1; shift;;
92
--version) echo "@PACKAGE_VERSION@"; exit 0;;
93
-*) echo "unknown option: $1" >&2; usage 1;;
98
if [ "x" = "x$OCF_ROOT" ]; then
99
if [ -d /usr/lib/ocf ]; then
100
export OCF_ROOT=/usr/lib/ocf
102
echo "You must supply the location of OCF_ROOT (common location is /usr/lib/ocf)" >&2
107
if [ "x" = "x$OCF_RESOURCE_INSTANCE" ]; then
108
echo "You must give your resource a name, set OCF_RESOURCE_INSTANCE" >&2
113
if [ ! -e $agent ]; then
114
echo "You must provide the full path to your resource agent" >&2
124
lrmd_target_rc=EVERYTIME
128
if [ $rc -eq 3 ]; then
138
$LRMADMIN -A $OCF_RESOURCE_INSTANCE \
141
$(basename `dirname $agent`) \
142
$lrm_ra_args > /dev/null
145
$LRMADMIN -D $OCF_RESOURCE_INSTANCE
147
parse_lrmadmin_output() {
150
/Waiting for lrmd to callback.../ { n=1; next; }
151
n==1 && /----------------operation--------------/ { n++; next; }
152
n==2 && /return code:/ { rc=$0; sub("return code: *","",rc); next }
153
n==2 && /---------------------------------------/ {
166
$LRMADMIN -E $OCF_RESOURCE_INSTANCE \
167
$op $lrmd_timeout $lrmd_interval \
169
$args | parse_lrmadmin_output
172
if [ "$use_lrmd" = 1 ]; then
173
echo "Using lrmd/lrmadmin for all tests"
175
echo "could not start lrmd" >&2
179
[ "$lrmd_started" = 1 ] && $LRMD -k
182
echo "failed to add resource to lrmd" >&2
190
[ "$verbose" -eq 0 ] || echo "$msg"
191
exec_resource $action "$lrm_ra_args"
196
msg=${1:-"Testing permissions with uid nobody"}
197
if [ $verbose -ne 0 ]; then
200
su nobody -s /bin/sh $agent $action > /dev/null
205
msg=${1:-"Testing: $action"}
206
if [ $verbose -ne 0 ]; then
209
bash $agent $action | (cd /usr/share/resource-agents && $METADATA_LINT)
217
export __OCF_ACTION=$action
218
msg=${1:-"Testing: $action"}
219
if [ "$use_lrmd" = 1 ]; then
220
lrm_test_command $action "$msg"
223
#echo Running: "export $ra_args; bash $agent $action 2>&1 > /dev/null"
224
if [ $verbose -eq 0 ]; then
225
command_output=`bash $agent $action 2>&1`
236
echo Beginning tests for $agent...
238
if [ ! -f $agent ]; then
239
assert 7 0 "Could not find file: $agent"
242
if [ `id -u` = 0 ]; then
244
assert $? 0 "Your agent has too restrictive permissions: should be 755"
246
echo "WARN: Can't check agent's permissions because we're not root; they should be 755"
250
assert $? 0 "Your agent produces meta-data which does not conform to ra-api-1.dtd"
252
OCF_TESTER_FAIL_HAVE_BINARY=1
253
export OCF_TESTER_FAIL_HAVE_BINARY
254
test_command meta-data
256
if [ $rc -eq 3 ]; then
257
assert $rc 0 "Your agent does not support the meta-data action"
259
assert $rc 0 "The meta-data action cannot fail and must return 0"
261
unset OCF_TESTER_FAIL_HAVE_BINARY
264
test_command validate-all
266
if [ $rc -eq 3 ]; then
267
assert $rc 0 "Your agent does not support the validate-all action"
268
elif [ $rc -ne 0 ]; then
269
assert $rc 0 "Validation failed. Did you supply enough options with -o ?" 1
273
test_command monitor "Checking current state"
275
if [ $rc -eq 3 ]; then
276
assert $rc 7 "Your agent does not support the monitor action" 1
278
elif [ $rc -eq 8 ]; then
279
test_command demote "Cleanup, demote"
280
assert $? 0 "Your agent was a master and could not be demoted" 1
282
test_command stop "Cleanup, stop"
283
assert $? 0 "Your agent was a master and could not be stopped" 1
285
elif [ $rc -ne 7 ]; then
287
assert $? 0 "Your agent was active and could not be stopped" 1
291
assert $? $stopped_rc "Monitoring a stopped resource should return $stopped_rc"
293
OCF_TESTER_FAIL_HAVE_BINARY=1
294
export OCF_TESTER_FAIL_HAVE_BINARY
295
OCF_RESKEY_CRM_meta_interval=0
297
assert $? $stopped_rc "The initial probe for a stopped resource should return $stopped_rc even if all binaries are missing"
298
unset OCF_TESTER_FAIL_HAVE_BINARY
299
OCF_RESKEY_CRM_meta_interval=20000
302
assert $? 0 "Start failed. Did you supply enough options with -o ?" 1
305
assert $? 0 "Monitoring an active resource should return 0"
309
if [ $rc -eq 3 ]; then
310
echo "* Your agent does not support the notify action (optional)"
312
assert $rc 0 "The notify action cannot fail and must return 0"
315
test_command demote "Checking for demote action"
316
if [ $? -eq 3 ]; then
318
echo "* Your agent does not support the demote action (optional)"
321
test_command promote "Checking for promote action"
322
if [ $? -eq 3 ]; then
324
echo "* Your agent does not support the promote action (optional)"
327
if [ $has_promote -eq 1 -a $has_demote -eq 1 ]; then
328
test_command demote "Testing: demotion of started resource"
329
assert $? 0 "Demoting a start resource should not fail"
332
assert $? 0 "Promote failed"
335
assert $? 0 "Demote failed" 1
337
test_command demote "Testing: demotion of demoted resource"
338
assert $? 0 "Demoting a demoted resource should not fail"
340
test_command promote "Promoting resource"
341
assert $? 0 "Promote failed" 1
343
test_command promote "Testing: promotion of promoted resource"
344
assert $? 0 "Promoting a promoted resource should not fail"
346
test_command demote "Demoting resource"
347
assert $? 0 "Demote failed" 1
349
elif [ $has_promote -eq 0 -a $has_demote -eq 0 ]; then
350
echo "* Your agent does not support master/slave (optional)"
353
echo "* Your agent partially supports master/slave"
354
num_errors=`expr $num_errors + 1`
358
assert $? 0 "Stop failed" 1
361
assert $? $stopped_rc "Monitoring a stopped resource should return $stopped_rc"
363
test_command start "Restarting resource..."
364
assert $? 0 "Start failed" 1
367
assert $? 0 "Monitoring an active resource should return 0"
369
test_command start "Testing: starting a started resource"
370
assert $? 0 "Starting a running resource is required to succeed"
373
assert $? 0 "Monitoring an active resource should return 0"
375
test_command stop "Stopping resource"
376
assert $? 0 "Stop could not clean up after multiple starts" 1
379
assert $? $stopped_rc "Monitoring a stopped resource should return $stopped_rc"
381
test_command stop "Testing: stopping a stopped resource"
382
assert $? 0 "Stopping a stopped resource is required to succeed"
385
assert $? $stopped_rc "Monitoring a stopped resource should return $stopped_rc"
387
test_command migrate_to "Checking for migrate_to action"
389
if [ $rc -ne 3 ]; then
390
test_command migrate_from "Checking for migrate_from action"
392
if [ $? -eq 3 ]; then
393
echo "* Your agent does not support the migrate action (optional)"
396
test_command reload "Checking for reload action"
397
if [ $? -eq 3 ]; then
398
echo "* Your agent does not support the reload action (optional)"
401
if [ $num_errors -gt 0 ]; then
402
echo "Tests failed: $agent failed $num_errors tests" >&2
405
echo $agent passed all tests