~ubuntu-branches/debian/stretch/resource-agents/stretch

« back to all changes in this revision

Viewing changes to tools/ocf-tester.in

  • Committer: Bazaar Package Importer
  • Author(s): Andres Rodriguez
  • Date: 2011-06-10 16:26:35 UTC
  • Revision ID: james.westby@ubuntu.com-20110610162635-yiy0vfopqw4trzgx
Tags: upstream-3.9.0
ImportĀ upstreamĀ versionĀ 3.9.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
#
 
3
#       $Id: ocf-tester,v 1.2 2006/08/14 09:38:20 andrew Exp $
 
4
#
 
5
# Copyright (c) 2006 Novell Inc, Andrew Beekhof
 
6
#                    All Rights Reserved.
 
7
#
 
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.
 
11
#
 
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.
 
15
#
 
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.
 
22
#
 
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.
 
26
#
 
27
 
 
28
LRMD=@libdir@/heartbeat/lrmd
 
29
LRMADMIN=@sbindir@/lrmadmin
 
30
METADATA_LINT="xmllint --noout --valid -"
 
31
 
 
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
 
36
 
 
37
num_errors=0
 
38
 
 
39
usage() {
 
40
    # make sure to output errors on stderr
 
41
    [ "x$1" = "x0" ] || exec >&2
 
42
 
 
43
    echo "Tool for testing if a cluster resource is OCF compliant"
 
44
    echo ""
 
45
    echo "Usage: ocf-tester [-Lh] -n resource_name [-o name=value]* /full/path/to/resource/agent"
 
46
    echo ""
 
47
    echo "Options:"
 
48
    echo "  -h                  This text"
 
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"
 
52
    exit $1
 
53
}
 
54
 
 
55
assert() {
 
56
    rc=$1; shift
 
57
    target=$1; shift
 
58
    msg=$1; shift
 
59
 
 
60
    if [ $# = 0 ]; then
 
61
        exit_code=0
 
62
    else
 
63
        exit_code=$1; shift
 
64
    fi
 
65
 
 
66
    if [ $rc -ne $target ]; then
 
67
        num_errors=`expr $num_errors + 1`
 
68
        echo "* rc=$rc: $msg"
 
69
        if [ $exit_code != 0 ]; then
 
70
            [ -n "$command_output" ] && cat<<EOF
 
71
$command_output
 
72
EOF
 
73
            echo "Aborting tests"
 
74
            exit $exit_code
 
75
        fi
 
76
    fi
 
77
    command_output=""
 
78
}
 
79
 
 
80
done=0
 
81
ra_args=""
 
82
verbose=0
 
83
while test "$done" = "0"; do
 
84
    case "$1" in
 
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;;
 
91
        -?|--help) usage 0;;
 
92
        --version) echo "@PACKAGE_VERSION@"; exit 0;;
 
93
        -*) echo "unknown option: $1" >&2; usage 1;;
 
94
        *) done=1;;
 
95
    esac
 
96
done
 
97
 
 
98
if [ "x" = "x$OCF_ROOT" ]; then
 
99
    if [ -d /usr/lib/ocf ]; then
 
100
        export OCF_ROOT=/usr/lib/ocf
 
101
    else
 
102
        echo "You must supply the location of OCF_ROOT (common location is /usr/lib/ocf)" >&2
 
103
        usage 1
 
104
    fi
 
105
fi
 
106
 
 
107
if [ "x" = "x$OCF_RESOURCE_INSTANCE" ]; then
 
108
    echo "You must give your resource a name, set OCF_RESOURCE_INSTANCE" >&2
 
109
    usage 1
 
110
fi
 
111
 
 
112
agent=$1
 
113
if [ ! -e $agent ]; then
 
114
    echo "You must provide the full path to your resource agent" >&2
 
115
    usage 1
 
116
fi
 
117
stopped_rc=7
 
118
has_demote=1
 
119
has_promote=1
 
120
 
 
121
start_lrmd() {
 
122
        lrmd_timeout=0
 
123
        lrmd_interval=0
 
124
        lrmd_target_rc=EVERYTIME
 
125
        lrmd_started=""
 
126
        $LRMD -s 2>/dev/null
 
127
        rc=$?
 
128
        if [ $rc -eq 3 ]; then
 
129
                lrmd_started=1
 
130
                $LRMD &
 
131
                sleep 1
 
132
                $LRMD -s 2>/dev/null
 
133
        else
 
134
                return $rc
 
135
        fi
 
136
}
 
137
add_resource() {
 
138
        $LRMADMIN -A $OCF_RESOURCE_INSTANCE \
 
139
                ocf \
 
140
                `basename $agent` \
 
141
                $(basename `dirname $agent`) \
 
142
                $lrm_ra_args > /dev/null
 
143
}
 
144
del_resource() {
 
145
        $LRMADMIN -D $OCF_RESOURCE_INSTANCE
 
146
}
 
147
parse_lrmadmin_output() {
 
148
        awk '
 
149
BEGIN{ rc=1; }
 
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 && /---------------------------------------/ {
 
154
        n++;
 
155
        next;
 
156
}
 
157
END{
 
158
        if( n!=3 ) exit 1;
 
159
        else exit rc;
 
160
}
 
161
'
 
162
}
 
163
exec_resource() {
 
164
        op="$1"
 
165
        args="$2"
 
166
        $LRMADMIN -E $OCF_RESOURCE_INSTANCE \
 
167
                $op $lrmd_timeout $lrmd_interval \
 
168
                $lrmd_target_rc \
 
169
                $args | parse_lrmadmin_output
 
170
}
 
171
 
 
172
if [ "$use_lrmd" = 1 ]; then
 
173
        echo "Using lrmd/lrmadmin for all tests"
 
174
        start_lrmd || {
 
175
                echo "could not start lrmd" >&2
 
176
                exit 1
 
177
        }
 
178
        trap '
 
179
                [ "$lrmd_started" = 1 ] && $LRMD -k
 
180
        ' EXIT
 
181
        add_resource || {
 
182
                echo "failed to add resource to lrmd" >&2
 
183
                exit 1
 
184
        }
 
185
fi
 
186
 
 
187
lrm_test_command() {
 
188
        action="$1"
 
189
        msg="$2"
 
190
        [ "$verbose" -eq 0 ] || echo "$msg"
 
191
        exec_resource $action "$lrm_ra_args"
 
192
}
 
193
 
 
194
test_permissions() {
 
195
    action=meta-data
 
196
    msg=${1:-"Testing permissions with uid nobody"}
 
197
    if [ $verbose -ne 0 ]; then
 
198
        echo $msg
 
199
    fi
 
200
    su nobody -s /bin/sh $agent $action > /dev/null
 
201
}
 
202
 
 
203
test_metadata() {
 
204
    action=meta-data
 
205
    msg=${1:-"Testing: $action"}
 
206
    if [ $verbose -ne 0 ]; then
 
207
        echo $msg
 
208
    fi
 
209
    bash $agent $action | (cd /usr/share/resource-agents && $METADATA_LINT)
 
210
    rc=$?
 
211
    #echo rc: $rc
 
212
    return $rc
 
213
}
 
214
 
 
215
test_command() {
 
216
    action=$1; shift
 
217
    export __OCF_ACTION=$action
 
218
    msg=${1:-"Testing: $action"}
 
219
    if [ "$use_lrmd" = 1 ]; then
 
220
        lrm_test_command $action "$msg"
 
221
        return $?
 
222
    fi
 
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`
 
226
    else
 
227
        echo $msg
 
228
        bash $agent $action
 
229
    fi
 
230
    rc=$?
 
231
    #echo rc: $rc
 
232
    return $rc
 
233
}
 
234
 
 
235
# Begin tests
 
236
echo Beginning tests for $agent...
 
237
 
 
238
if [ ! -f $agent ]; then
 
239
    assert 7 0 "Could not find file: $agent"
 
240
fi
 
241
 
 
242
if [ `id -u` = 0 ]; then
 
243
        test_permissions
 
244
        assert $? 0 "Your agent has too restrictive permissions: should be 755"
 
245
else
 
246
        echo "WARN: Can't check agent's permissions because we're not root; they should be 755"
 
247
fi
 
248
 
 
249
test_metadata
 
250
assert $? 0 "Your agent produces meta-data which does not conform to ra-api-1.dtd"
 
251
 
 
252
OCF_TESTER_FAIL_HAVE_BINARY=1
 
253
export OCF_TESTER_FAIL_HAVE_BINARY
 
254
test_command meta-data
 
255
rc=$?
 
256
if [ $rc -eq 3 ]; then
 
257
    assert $rc 0 "Your agent does not support the meta-data action"
 
258
else
 
259
    assert $rc 0 "The meta-data action cannot fail and must return 0"
 
260
fi
 
261
unset OCF_TESTER_FAIL_HAVE_BINARY
 
262
 
 
263
export $ra_args;
 
264
test_command validate-all
 
265
rc=$?
 
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
 
270
    usage $rc
 
271
fi
 
272
 
 
273
test_command monitor "Checking current state"
 
274
rc=$?
 
275
if [ $rc -eq 3 ]; then
 
276
    assert $rc 7 "Your agent does not support the monitor action" 1
 
277
 
 
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
 
281
 
 
282
    test_command stop "Cleanup, stop"
 
283
    assert $? 0 "Your agent was a master and could not be stopped" 1
 
284
 
 
285
elif [ $rc -ne 7 ]; then
 
286
    test_command stop
 
287
    assert $? 0 "Your agent was active and could not be stopped" 1
 
288
fi
 
289
 
 
290
test_command monitor
 
291
assert $? $stopped_rc "Monitoring a stopped resource should return $stopped_rc"
 
292
 
 
293
OCF_TESTER_FAIL_HAVE_BINARY=1
 
294
export OCF_TESTER_FAIL_HAVE_BINARY
 
295
OCF_RESKEY_CRM_meta_interval=0
 
296
test_command monitor
 
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
 
300
 
 
301
test_command start
 
302
assert $? 0 "Start failed.  Did you supply enough options with -o ?" 1
 
303
 
 
304
test_command monitor
 
305
assert $? 0 "Monitoring an active resource should return 0"
 
306
 
 
307
test_command notify
 
308
rc=$?
 
309
if [ $rc -eq 3 ]; then
 
310
    echo "* Your agent does not support the notify action (optional)"
 
311
else
 
312
    assert $rc 0 "The notify action cannot fail and must return 0"
 
313
fi
 
314
 
 
315
test_command demote "Checking for demote action"
 
316
if [ $? -eq 3 ]; then
 
317
    has_demote=0
 
318
    echo "* Your agent does not support the demote action (optional)"
 
319
fi
 
320
 
 
321
test_command promote "Checking for promote action"
 
322
if [ $? -eq 3 ]; then
 
323
    has_promote=0
 
324
    echo "* Your agent does not support the promote action (optional)"
 
325
fi
 
326
 
 
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"
 
330
 
 
331
    test_command promote
 
332
    assert $? 0 "Promote failed"
 
333
 
 
334
    test_command demote
 
335
    assert $? 0 "Demote failed" 1
 
336
 
 
337
    test_command demote "Testing: demotion of demoted resource"
 
338
    assert $? 0 "Demoting a demoted resource should not fail"
 
339
 
 
340
    test_command promote "Promoting resource"
 
341
    assert $? 0 "Promote failed" 1
 
342
 
 
343
    test_command promote "Testing: promotion of promoted resource"
 
344
    assert $? 0 "Promoting a promoted resource should not fail"
 
345
 
 
346
    test_command demote "Demoting resource"
 
347
    assert $? 0 "Demote failed" 1
 
348
 
 
349
elif [ $has_promote -eq 0 -a $has_demote -eq 0 ]; then
 
350
    echo "* Your agent does not support master/slave (optional)"
 
351
 
 
352
else
 
353
    echo "* Your agent partially supports master/slave"
 
354
    num_errors=`expr $num_errors + 1`
 
355
fi
 
356
 
 
357
test_command stop
 
358
assert $? 0 "Stop failed" 1
 
359
 
 
360
test_command monitor
 
361
assert $? $stopped_rc "Monitoring a stopped resource should return $stopped_rc"
 
362
 
 
363
test_command start "Restarting resource..."
 
364
assert $? 0 "Start failed" 1
 
365
 
 
366
test_command monitor
 
367
assert $? 0 "Monitoring an active resource should return 0"
 
368
 
 
369
test_command start "Testing: starting a started resource"
 
370
assert $? 0 "Starting a running resource is required to succeed"
 
371
 
 
372
test_command monitor
 
373
assert $? 0 "Monitoring an active resource should return 0"
 
374
 
 
375
test_command stop "Stopping resource"
 
376
assert $? 0 "Stop could not clean up after multiple starts" 1
 
377
 
 
378
test_command monitor
 
379
assert $? $stopped_rc "Monitoring a stopped resource should return $stopped_rc"
 
380
 
 
381
test_command stop "Testing: stopping a stopped resource"
 
382
assert $? 0 "Stopping a stopped resource is required to succeed"
 
383
 
 
384
test_command monitor
 
385
assert $? $stopped_rc "Monitoring a stopped resource should return $stopped_rc"
 
386
 
 
387
test_command migrate_to "Checking for migrate_to action"
 
388
rc=$?
 
389
if [ $rc -ne 3 ]; then
 
390
    test_command migrate_from "Checking for migrate_from action"
 
391
fi
 
392
if [ $? -eq 3 ]; then
 
393
    echo "* Your agent does not support the migrate action (optional)"
 
394
fi
 
395
 
 
396
test_command reload "Checking for reload action"
 
397
if [ $? -eq 3 ]; then
 
398
    echo "* Your agent does not support the reload action (optional)"
 
399
fi
 
400
 
 
401
if [ $num_errors -gt 0 ]; then
 
402
    echo "Tests failed: $agent failed $num_errors tests" >&2
 
403
    exit 1
 
404
else 
 
405
    echo $agent passed all tests
 
406
    exit 0
 
407
fi
 
408
 
 
409
# vim:et:ts=8:sw=4