File: ocf-tester.in

package info (click to toggle)
resource-agents 1%3A4.0.0~rc1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,644 kB
  • ctags: 2,191
  • sloc: sh: 47,713; ansic: 4,074; perl: 3,457; makefile: 663; xml: 89
file content (432 lines) | stat: -rwxr-xr-x 11,393 bytes parent folder | download | duplicates (5)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#!/bin/sh
#
#	$Id: ocf-tester,v 1.2 2006/08/14 09:38:20 andrew Exp $
#
# Copyright (c) 2006 Novell Inc, Andrew Beekhof
#                    All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.  Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
#

LRMD=@libdir@/heartbeat/lrmd
LRMADMIN=@sbindir@/lrmadmin
DATADIR=@datadir@
METADATA_LINT="xmllint --noout --valid -"

# set some common meta attributes, which are expected to be
# present by resource agents
export OCF_RESKEY_CRM_meta_timeout=20000  # 20 seconds timeout
export OCF_RESKEY_CRM_meta_interval=10000  # reset this for probes

num_errors=0

info() {
    [ "$quiet" -eq 1 ] && return
    echo "$*"
}
debug() {
    [ "$verbose" -eq 0 ] && return
    echo "$*"
}
usage() {
    # make sure to output errors on stderr
    [ "x$1" = "x0" ] || exec >&2

    echo "Tool for testing if a cluster resource is OCF compliant"
    echo ""
    echo "Usage: ocf-tester [-LhvqdX] -n resource_name [-o name=value]* /full/path/to/resource/agent"
    echo ""
    echo "Options:"
    echo "  -h       		This text"
    echo "  -v       		Be verbose while testing"
    echo "  -q       		Be quiet while testing"
    echo "  -d       		Turn on RA debugging"
    echo "  -X       		Turn on RA tracing (expect large output)"
    echo "  -n name		Name of the resource"	
    echo "  -o name=value		Name and value of any parameters required by the agent"
    echo "  -L			Use lrmadmin/lrmd for tests"
    exit $1
}

assert() {
    rc=$1; shift
    target=$1; shift
    msg=$1; shift
    local targetrc matched

    if [ $# = 0 ]; then
	exit_code=0
    else
	exit_code=$1; shift
    fi

    for targetrc in `echo $target | tr ':' ' '`; do
        [ $rc -eq $targetrc ] && matched=1
    done
    if [ "$matched" != 1 ]; then
	num_errors=`expr $num_errors + 1`
	echo "* rc=$rc: $msg"
	if [ $exit_code != 0 ]; then
	    [ -n "$command_output" ] && cat<<EOF
$command_output
EOF
	    echo "Aborting tests"
	    exit $exit_code
	fi
    fi
    command_output=""
}

done=0
ra_args=""
verbose=0
quiet=0
while test "$done" = "0"; do
    case "$1" in
	-n) OCF_RESOURCE_INSTANCE=$2; ra_args="$ra_args OCF_RESOURCE_INSTANCE=$2"; shift; shift;;
	-o) name=${2%%=*}; value=${2#*=}; 
		lrm_ra_args="$lrm_ra_args $2";
		ra_args="$ra_args OCF_RESKEY_$name='$value'"; shift; shift;;
	-L) use_lrmd=1; shift;;
	-v) verbose=1; shift;;
	-d) export HA_debug=1; shift;;
	-X) export OCF_TRACE_RA=1; verbose=1; shift;;
	-q) quiet=1; shift;;
	-?|--help) usage 0;;
	--version) echo "@PACKAGE_VERSION@"; exit 0;;
	-*) echo "unknown option: $1" >&2; usage 1;;
	*) done=1;;
    esac
done

if [ "x" = "x$OCF_ROOT" ]; then
    if [ -d /usr/lib/ocf ]; then
	export OCF_ROOT=/usr/lib/ocf
    else
	echo "You must supply the location of OCF_ROOT (common location is /usr/lib/ocf)" >&2
	usage 1
    fi
fi

if [ "x" = "x$OCF_RESOURCE_INSTANCE" ]; then
    echo "You must give your resource a name, set OCF_RESOURCE_INSTANCE" >&2
    usage 1
fi

agent=$1
if [ ! -e $agent ]; then
    echo "You must provide the full path to your resource agent" >&2
    usage 1
fi
installed_rc=5
stopped_rc=7
has_demote=1
has_promote=1

start_lrmd() {
	lrmd_timeout=0
	lrmd_interval=0
	lrmd_target_rc=EVERYTIME
	lrmd_started=""
	$LRMD -s 2>/dev/null
	rc=$?
	if [ $rc -eq 3 ]; then
		lrmd_started=1
		$LRMD &
		sleep 1
		$LRMD -s 2>/dev/null
	else
		return $rc
	fi
}
add_resource() {
	$LRMADMIN -A $OCF_RESOURCE_INSTANCE \
		ocf \
		`basename $agent` \
		$(basename `dirname $agent`) \
		$lrm_ra_args > /dev/null
}
del_resource() {
	$LRMADMIN -D $OCF_RESOURCE_INSTANCE
}
parse_lrmadmin_output() {
	awk '
BEGIN{ rc=1; }
/Waiting for lrmd to callback.../ { n=1; next; }
n==1 && /----------------operation--------------/ { n++; next; }
n==2 && /return code:/ { rc=$0; sub("return code: *","",rc); next }
n==2 && /---------------------------------------/ {
        n++;
        next;
}
END{
	if( n!=3 ) exit 1;
	else exit rc;
}
'
}
exec_resource() {
	op="$1"
	args="$2"
	$LRMADMIN -E $OCF_RESOURCE_INSTANCE \
		$op $lrmd_timeout $lrmd_interval \
		$lrmd_target_rc \
		$args | parse_lrmadmin_output
}

if [ "$use_lrmd" = 1 ]; then
	echo "Using lrmd/lrmadmin for all tests"
	start_lrmd || {
		echo "could not start lrmd" >&2
		exit 1
	}
	trap '
		[ "$lrmd_started" = 1 ] && $LRMD -k
	' EXIT
	add_resource || {
		echo "failed to add resource to lrmd" >&2
		exit 1
	}
fi

lrm_test_command() {
	action="$1"
	msg="$2"
	debug "$msg"
	exec_resource $action "$lrm_ra_args"
}

test_permissions() {
    action=meta-data
    debug ${1:-"Testing permissions with uid nobody"}
    su nobody -s /bin/sh -c "$agent $action" > /dev/null
}

test_metadata() {
    action=meta-data
    msg=${1:-"Testing: $action"}
    debug $msg
    $agent $action | (cd $DATADIR/resource-agents && $METADATA_LINT)
    rc=$?
    #echo rc: $rc
    return $rc
}

test_command() {
    action=$1; shift
    export __OCF_ACTION=$action
    msg=${1:-"Testing: $action"}
    if [ "$use_lrmd" = 1 ]; then
    	lrm_test_command $action "$msg"
    	return $?
    fi
    #echo Running: "export $ra_args; $agent $action 2>&1 > /dev/null"
    if [ $verbose -eq 0 ]; then
	command_output=`$agent $action 2>&1`
    else
    	debug $msg
	$agent $action
    fi
    rc=$?
    #echo rc: $rc
    return $rc
}

# Begin tests
info "Beginning tests for $agent..."

if [ ! -f $agent ]; then
    assert 7 0 "Could not find file: $agent"
fi

if [ `id -u` = 0 ]; then
	test_permissions
	assert $? 0 "Your agent has too restrictive permissions: should be 755"
else
	echo "WARN: Can't check agent's permissions because we're not root; they should be 755"
fi

test_metadata
assert $? 0 "Your agent produces meta-data which does not conform to ra-api-1.dtd"

OCF_TESTER_FAIL_HAVE_BINARY=1
export OCF_TESTER_FAIL_HAVE_BINARY
test_command meta-data
rc=$?
if [ $rc -eq 3 ]; then
    assert $rc 0 "Your agent does not support the meta-data action"
else
    assert $rc 0 "The meta-data action cannot fail and must return 0"
fi
unset OCF_TESTER_FAIL_HAVE_BINARY

ra_args="export $ra_args"
eval $ra_args
test_command validate-all
rc=$?
if [ $rc -eq 3 ]; then
    assert $rc 0 "Your agent does not support the validate-all action"
elif [ $rc -ne 0 ]; then
    assert $rc 0 "Validation failed.  Did you supply enough options with -o ?" 1
    usage $rc
fi

test_command monitor "Checking current state"
rc=$?
if [ $rc -eq 3 ]; then
    assert $rc 7 "Your agent does not support the monitor action" 1

elif [ $rc -eq 8 ]; then
    test_command demote "Cleanup, demote"
    assert $? 0 "Your agent was a master and could not be demoted" 1

    test_command stop "Cleanup, stop"
    assert $? 0 "Your agent was a master and could not be stopped" 1

elif [ $rc -ne 7 ]; then
    test_command stop
    assert $? 0 "Your agent was active and could not be stopped" 1
fi

test_command monitor
assert $? $stopped_rc "Monitoring a stopped resource should return $stopped_rc"

OCF_TESTER_FAIL_HAVE_BINARY=1
export OCF_TESTER_FAIL_HAVE_BINARY
OCF_RESKEY_CRM_meta_interval=0
test_command monitor
assert $? $stopped_rc:$installed_rc "The initial probe for a stopped resource should return $stopped_rc or $installed_rc even if all binaries are missing"
unset OCF_TESTER_FAIL_HAVE_BINARY
OCF_RESKEY_CRM_meta_interval=20000

test_command start
assert $? 0 "Start failed.  Did you supply enough options with -o ?" 1

test_command monitor
assert $? 0 "Monitoring an active resource should return 0"

OCF_RESKEY_CRM_meta_interval=0
test_command monitor
assert $? 0 "Probing an active resource should return 0"
OCF_RESKEY_CRM_meta_interval=20000

test_command notify
rc=$?
if [ $rc -eq 3 ]; then
    info "* Your agent does not support the notify action (optional)"
else
    assert $rc 0 "The notify action cannot fail and must return 0"
fi

test_command demote "Checking for demote action"
if [ $? -eq 3 ]; then
    has_demote=0
    info "* Your agent does not support the demote action (optional)"
fi

test_command promote "Checking for promote action"
if [ $? -eq 3 ]; then
    has_promote=0
    info "* Your agent does not support the promote action (optional)"
fi

if [ $has_promote -eq 1 -a $has_demote -eq 1 ]; then
    test_command demote "Testing: demotion of started resource"
    assert $? 0 "Demoting a start resource should not fail"

    test_command promote
    assert $? 0 "Promote failed"

    test_command demote
    assert $? 0 "Demote failed" 1

    test_command demote "Testing: demotion of demoted resource"
    assert $? 0 "Demoting a demoted resource should not fail"

    test_command promote "Promoting resource"
    assert $? 0 "Promote failed" 1

    test_command promote "Testing: promotion of promoted resource"
    assert $? 0 "Promoting a promoted resource should not fail"

    test_command demote "Demoting resource"
    assert $? 0 "Demote failed" 1

elif [ $has_promote -eq 0 -a $has_demote -eq 0 ]; then
    info "* Your agent does not support master/slave (optional)"

else
    echo "* Your agent partially supports master/slave"
    num_errors=`expr $num_errors + 1`
fi

test_command stop
assert $? 0 "Stop failed" 1

test_command monitor
assert $? $stopped_rc "Monitoring a stopped resource should return $stopped_rc"

test_command start "Restarting resource..."
assert $? 0 "Start failed" 1

test_command monitor
assert $? 0 "Monitoring an active resource should return 0"

test_command start "Testing: starting a started resource"
assert $? 0 "Starting a running resource is required to succeed"

test_command monitor
assert $? 0 "Monitoring an active resource should return 0"

test_command stop "Stopping resource"
assert $? 0 "Stop could not clean up after multiple starts" 1

test_command monitor
assert $? $stopped_rc "Monitoring a stopped resource should return $stopped_rc"

test_command stop "Testing: stopping a stopped resource"
assert $? 0 "Stopping a stopped resource is required to succeed"

test_command monitor
assert $? $stopped_rc "Monitoring a stopped resource should return $stopped_rc"

test_command migrate_to "Checking for migrate_to action"
rc=$?
if [ $rc -ne 3 ]; then
    test_command migrate_from "Checking for migrate_from action"
fi
if [ $? -eq 3 ]; then
    info "* Your agent does not support the migrate action (optional)"
fi

test_command reload "Checking for reload action"
if [ $? -eq 3 ]; then
    info "* Your agent does not support the reload action (optional)"
fi

if [ $num_errors -gt 0 ]; then
    echo "Tests failed: $agent failed $num_errors tests" >&2
    exit 1
else 
    echo $agent passed all tests
    exit 0
fi

# vim:et:ts=8:sw=4