]> git.proxmox.com Git - ceph.git/blame - ceph/qa/standalone/ceph-helpers.sh
Add patch for failing prerm scripts
[ceph.git] / ceph / qa / standalone / ceph-helpers.sh
CommitLineData
11fdf7f2 1#!/usr/bin/env bash
7c673cae
FG
2#
3# Copyright (C) 2013,2014 Cloudwatt <libre.licensing@cloudwatt.com>
4# Copyright (C) 2014,2015 Red Hat <contact@redhat.com>
5# Copyright (C) 2014 Federico Gimenez <fgimenez@coit.es>
6#
7# Author: Loic Dachary <loic@dachary.org>
8# Author: Federico Gimenez <fgimenez@coit.es>
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU Library Public License as published by
12# the Free Software Foundation; either version 2, or (at your option)
13# any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU Library Public License for more details.
19#
20TIMEOUT=300
11fdf7f2
TL
21WAIT_FOR_CLEAN_TIMEOUT=90
22MAX_TIMEOUT=15
7c673cae 23PG_NUM=4
11fdf7f2 24TMPDIR=${TMPDIR:-/tmp}
f64942e4
AA
25CEPH_BUILD_VIRTUALENV=${TMPDIR}
26TESTDIR=${TESTDIR:-${TMPDIR}}
7c673cae
FG
27
28if type xmlstarlet > /dev/null 2>&1; then
29 XMLSTARLET=xmlstarlet
30elif type xml > /dev/null 2>&1; then
31 XMLSTARLET=xml
32else
33 echo "Missing xmlstarlet binary!"
34 exit 1
35fi
31f18b77 36
7c673cae
FG
37if [ `uname` = FreeBSD ]; then
38 SED=gsed
f64942e4 39 AWK=gawk
31f18b77 40 DIFFCOLOPTS=""
b5b8bbf5 41 KERNCORE="kern.corefile"
7c673cae
FG
42else
43 SED=sed
f64942e4 44 AWK=awk
31f18b77 45 termwidth=$(stty -a | head -1 | sed -e 's/.*columns \([0-9]*\).*/\1/')
c07f9fc5
FG
46 if [ -n "$termwidth" -a "$termwidth" != "0" ]; then
47 termwidth="-W ${termwidth}"
31f18b77
FG
48 fi
49 DIFFCOLOPTS="-y $termwidth"
b5b8bbf5 50 KERNCORE="kernel.core_pattern"
31f18b77 51fi
7c673cae 52
c07f9fc5 53EXTRA_OPTS=""
c07f9fc5 54
7c673cae
FG
55#! @file ceph-helpers.sh
56# @brief Toolbox to manage Ceph cluster dedicated to testing
57#
58# Example use case:
59#
60# ~~~~~~~~~~~~~~~~{.sh}
61# source ceph-helpers.sh
62#
63# function mytest() {
64# # cleanup leftovers and reset mydir
65# setup mydir
66# # create a cluster with one monitor and three osds
67# run_mon mydir a
68# run_osd mydir 0
69# run_osd mydir 2
70# run_osd mydir 3
71# # put and get an object
72# rados --pool rbd put GROUP /etc/group
73# rados --pool rbd get GROUP /tmp/GROUP
74# # stop the cluster and cleanup the directory
75# teardown mydir
76# }
77# ~~~~~~~~~~~~~~~~
78#
79# The focus is on simplicity and efficiency, in the context of
80# functional tests. The output is intentionally very verbose
81# and functions return as soon as an error is found. The caller
82# is also expected to abort on the first error so that debugging
83# can be done by looking at the end of the output.
84#
85# Each function is documented, implemented and tested independently.
86# When modifying a helper, the test and the documentation are
87# expected to be updated and it is easier of they are collocated. A
88# test for a given function can be run with
89#
90# ~~~~~~~~~~~~~~~~{.sh}
91# ceph-helpers.sh TESTS test_get_osds
92# ~~~~~~~~~~~~~~~~
93#
94# and all the tests (i.e. all functions matching test_*) are run
95# with:
96#
97# ~~~~~~~~~~~~~~~~{.sh}
98# ceph-helpers.sh TESTS
99# ~~~~~~~~~~~~~~~~
100#
101# A test function takes a single argument : the directory dedicated
102# to the tests. It is expected to not create any file outside of this
103# directory and remove it entirely when it completes successfully.
104#
105
106
c07f9fc5
FG
107function get_asok_dir() {
108 if [ -n "$CEPH_ASOK_DIR" ]; then
109 echo "$CEPH_ASOK_DIR"
110 else
111 echo ${TMPDIR:-/tmp}/ceph-asok.$$
112 fi
113}
114
115function get_asok_path() {
116 local name=$1
117 if [ -n "$name" ]; then
118 echo $(get_asok_dir)/ceph-$name.asok
119 else
120 echo $(get_asok_dir)/\$cluster-\$name.asok
121 fi
122}
7c673cae
FG
123##
124# Cleanup any leftovers found in **dir** via **teardown**
125# and reset **dir** as an empty environment.
126#
127# @param dir path name of the environment
128# @return 0 on success, 1 on error
129#
130function setup() {
131 local dir=$1
132 teardown $dir || return 1
133 mkdir -p $dir
c07f9fc5 134 mkdir -p $(get_asok_dir)
11fdf7f2
TL
135 if [ $(ulimit -n) -le 1024 ]; then
136 ulimit -n 4096 || return 1
137 fi
138 if [ -z "$LOCALRUN" ]; then
139 trap "teardown $dir 1" TERM HUP INT
140 fi
7c673cae
FG
141}
142
143function test_setup() {
144 local dir=$dir
145 setup $dir || return 1
146 test -d $dir || return 1
147 setup $dir || return 1
148 test -d $dir || return 1
149 teardown $dir
150}
151
152#######################################################################
153
154##
155# Kill all daemons for which a .pid file exists in **dir** and remove
156# **dir**. If the file system in which **dir** is btrfs, delete all
157# subvolumes that relate to it.
158#
159# @param dir path name of the environment
1adf2230 160# @param dumplogs pass "1" to dump logs otherwise it will only if cores found
7c673cae
FG
161# @return 0 on success, 1 on error
162#
163function teardown() {
164 local dir=$1
b5b8bbf5 165 local dumplogs=$2
7c673cae
FG
166 kill_daemons $dir KILL
167 if [ `uname` != FreeBSD ] \
168 && [ $(stat -f -c '%T' .) == "btrfs" ]; then
169 __teardown_btrfs $dir
170 fi
b5b8bbf5
FG
171 local cores="no"
172 local pattern="$(sysctl -n $KERNCORE)"
173 # See if we have apport core handling
174 if [ "${pattern:0:1}" = "|" ]; then
175 # TODO: Where can we get the dumps?
176 # Not sure where the dumps really are so this will look in the CWD
177 pattern=""
178 fi
179 # Local we start with core and teuthology ends with core
1adf2230 180 if ls $(dirname "$pattern") | grep -q '^core\|core$' ; then
b5b8bbf5
FG
181 cores="yes"
182 if [ -n "$LOCALRUN" ]; then
183 mkdir /tmp/cores.$$ 2> /dev/null || true
184 for i in $(ls $(dirname $(sysctl -n $KERNCORE)) | grep '^core\|core$'); do
185 mv $i /tmp/cores.$$
186 done
187 fi
188 fi
189 if [ "$cores" = "yes" -o "$dumplogs" = "1" ]; then
1adf2230
AA
190 if [ -n "$LOCALRUN" ]; then
191 display_logs $dir
192 else
193 # Move logs to where Teuthology will archive it
194 mkdir -p $TESTDIR/archive/log
195 mv $dir/*.log $TESTDIR/archive/log
196 fi
b5b8bbf5 197 fi
7c673cae 198 rm -fr $dir
c07f9fc5 199 rm -rf $(get_asok_dir)
b5b8bbf5
FG
200 if [ "$cores" = "yes" ]; then
201 echo "ERROR: Failure due to cores found"
202 if [ -n "$LOCALRUN" ]; then
203 echo "Find saved core files in /tmp/cores.$$"
204 fi
205 return 1
206 fi
207 return 0
7c673cae
FG
208}
209
210function __teardown_btrfs() {
211 local btrfs_base_dir=$1
f64942e4
AA
212 local btrfs_root=$(df -P . | tail -1 | $AWK '{print $NF}')
213 local btrfs_dirs=$(cd $btrfs_base_dir; sudo btrfs subvolume list -t . | $AWK '/^[0-9]/ {print $4}' | grep "$btrfs_base_dir/$btrfs_dir")
7c673cae
FG
214 for subvolume in $btrfs_dirs; do
215 sudo btrfs subvolume delete $btrfs_root/$subvolume
216 done
217}
218
219function test_teardown() {
220 local dir=$dir
221 setup $dir || return 1
222 teardown $dir || return 1
223 ! test -d $dir || return 1
224}
225
226#######################################################################
227
228##
229# Sends a signal to a single daemon.
230# This is a helper function for kill_daemons
231#
232# After the daemon is sent **signal**, its actual termination
233# will be verified by sending it signal 0. If the daemon is
234# still alive, kill_daemon will pause for a few seconds and
235# try again. This will repeat for a fixed number of times
236# before kill_daemon returns on failure. The list of
237# sleep intervals can be specified as **delays** and defaults
238# to:
239#
240# 0.1 0.2 1 1 1 2 3 5 5 5 10 10 20 60 60 60 120
241#
242# This sequence is designed to run first a very short sleep time (0.1)
243# if the machine is fast enough and the daemon terminates in a fraction of a
244# second. The increasing sleep numbers should give plenty of time for
245# the daemon to die even on the slowest running machine. If a daemon
246# takes more than a few minutes to stop (the sum of all sleep times),
247# there probably is no point in waiting more and a number of things
248# are likely to go wrong anyway: better give up and return on error.
249#
250# @param pid the process id to send a signal
251# @param send_signal the signal to send
252# @param delays sequence of sleep times before failure
253#
254function kill_daemon() {
7c673cae
FG
255 local pid=$(cat $1)
256 local send_signal=$2
257 local delays=${3:-0.1 0.2 1 1 1 2 3 5 5 5 10 10 20 60 60 60 120}
258 local exit_code=1
11fdf7f2
TL
259 # In order to try after the last large sleep add 0 at the end so we check
260 # one last time before dropping out of the loop
261 for try in $delays 0 ; do
7c673cae
FG
262 if kill -$send_signal $pid 2> /dev/null ; then
263 exit_code=1
264 else
265 exit_code=0
266 break
267 fi
268 send_signal=0
269 sleep $try
270 done;
271 return $exit_code
272}
273
274function test_kill_daemon() {
275 local dir=$1
276 setup $dir || return 1
277 run_mon $dir a --osd_pool_default_size=1 || return 1
278 run_mgr $dir x || return 1
279 run_osd $dir 0 || return 1
280
281 name_prefix=osd
282 for pidfile in $(find $dir 2>/dev/null | grep $name_prefix'[^/]*\.pid') ; do
283 #
284 # sending signal 0 won't kill the daemon
285 # waiting just for one second instead of the default schedule
286 # allows us to quickly verify what happens when kill fails
287 # to stop the daemon (i.e. it must return false)
288 #
289 ! kill_daemon $pidfile 0 1 || return 1
290 #
291 # killing just the osd and verify the mon still is responsive
292 #
293 kill_daemon $pidfile TERM || return 1
294 done
295
296 ceph osd dump | grep "osd.0 down" || return 1
297
298 name_prefix=mgr
299 for pidfile in $(find $dir 2>/dev/null | grep $name_prefix'[^/]*\.pid') ; do
300 #
301 # kill the mgr
302 #
303 kill_daemon $pidfile TERM || return 1
304 done
305
306 name_prefix=mon
307 for pidfile in $(find $dir 2>/dev/null | grep $name_prefix'[^/]*\.pid') ; do
308 #
309 # kill the mon and verify it cannot be reached
310 #
311 kill_daemon $pidfile TERM || return 1
224ce89b 312 ! timeout 5 ceph status || return 1
7c673cae
FG
313 done
314
315 teardown $dir || return 1
316}
317
318##
319# Kill all daemons for which a .pid file exists in **dir**. Each
320# daemon is sent a **signal** and kill_daemons waits for it to exit
321# during a few minutes. By default all daemons are killed. If a
322# **name_prefix** is provided, only the daemons for which a pid
323# file is found matching the prefix are killed. See run_osd and
324# run_mon for more information about the name conventions for
325# the pid files.
326#
327# Send TERM to all daemons : kill_daemons $dir
328# Send KILL to all daemons : kill_daemons $dir KILL
329# Send KILL to all osds : kill_daemons $dir KILL osd
330# Send KILL to osd 1 : kill_daemons $dir KILL osd.1
331#
332# If a daemon is sent the TERM signal and does not terminate
333# within a few minutes, it will still be running even after
c07f9fc5 334# kill_daemons returns.
7c673cae
FG
335#
336# If all daemons are kill successfully the function returns 0
c07f9fc5 337# if at least one daemon remains, this is treated as an
7c673cae
FG
338# error and the function return 1.
339#
340# @param dir path name of the environment
341# @param signal name of the first signal (defaults to TERM)
342# @param name_prefix only kill match daemons (defaults to all)
343# @param delays sequence of sleep times before failure
344# @return 0 on success, 1 on error
345#
346function kill_daemons() {
347 local trace=$(shopt -q -o xtrace && echo true || echo false)
348 $trace && shopt -u -o xtrace
349 local dir=$1
350 local signal=${2:-TERM}
351 local name_prefix=$3 # optional, osd, mon, osd.1
352 local delays=$4 #optional timing
353 local status=0
354 local pids=""
355
356 for pidfile in $(find $dir 2>/dev/null | grep $name_prefix'[^/]*\.pid') ; do
357 run_in_background pids kill_daemon $pidfile $signal $delays
358 done
359
360 wait_background pids
361 status=$?
362
363 $trace && shopt -s -o xtrace
364 return $status
365}
366
367function test_kill_daemons() {
368 local dir=$1
369 setup $dir || return 1
370 run_mon $dir a --osd_pool_default_size=1 || return 1
371 run_mgr $dir x || return 1
372 run_osd $dir 0 || return 1
373 #
374 # sending signal 0 won't kill the daemon
375 # waiting just for one second instead of the default schedule
c07f9fc5 376 # allows us to quickly verify what happens when kill fails
7c673cae
FG
377 # to stop the daemon (i.e. it must return false)
378 #
379 ! kill_daemons $dir 0 osd 1 || return 1
380 #
381 # killing just the osd and verify the mon still is responsive
382 #
383 kill_daemons $dir TERM osd || return 1
384 ceph osd dump | grep "osd.0 down" || return 1
385 #
386 # kill the mgr
387 #
388 kill_daemons $dir TERM mgr || return 1
389 #
390 # kill the mon and verify it cannot be reached
391 #
392 kill_daemons $dir TERM || return 1
224ce89b 393 ! timeout 5 ceph status || return 1
7c673cae
FG
394 teardown $dir || return 1
395}
396
a8e16298
TL
397#
398# return a random TCP port which is not used yet
399#
400# please note, there could be racing if we use this function for
401# a free port, and then try to bind on this port.
402#
403function get_unused_port() {
404 local ip=127.0.0.1
405 python3 -c "import socket; s=socket.socket(); s.bind(('$ip', 0)); print(s.getsockname()[1]); s.close()"
406}
407
7c673cae
FG
408#######################################################################
409
410##
411# Run a monitor by the name mon.**id** with data in **dir**/**id**.
412# The logs can be found in **dir**/mon.**id**.log and the pid file
413# is **dir**/mon.**id**.pid and the admin socket is
414# **dir**/**id**/ceph-mon.**id**.asok.
415#
416# The remaining arguments are passed verbatim to ceph-mon --mkfs
417# and the ceph-mon daemon.
418#
419# Two mandatory arguments must be provided: --fsid and --mon-host
420# Instead of adding them to every call to run_mon, they can be
421# set in the CEPH_ARGS environment variable to be read implicitly
422# by every ceph command.
423#
424# The CEPH_CONF variable is expected to be set to /dev/null to
425# only rely on arguments for configuration.
426#
427# Examples:
428#
429# CEPH_ARGS="--fsid=$(uuidgen) "
430# CEPH_ARGS+="--mon-host=127.0.0.1:7018 "
431# run_mon $dir a # spawn a mon and bind port 7018
432# run_mon $dir a --debug-filestore=20 # spawn with filestore debugging
433#
434# If mon_initial_members is not set, the default rbd pool is deleted
435# and replaced with a replicated pool with less placement groups to
436# speed up initialization. If mon_initial_members is set, no attempt
437# is made to recreate the rbd pool because it would hang forever,
438# waiting for other mons to join.
439#
440# A **dir**/ceph.conf file is created but not meant to be used by any
441# function. It is convenient for debugging a failure with:
442#
443# ceph --conf **dir**/ceph.conf -s
444#
445# @param dir path name of the environment
446# @param id mon identifier
447# @param ... can be any option valid for ceph-mon
448# @return 0 on success, 1 on error
449#
c07f9fc5 450function run_mon() {
7c673cae
FG
451 local dir=$1
452 shift
453 local id=$1
454 shift
455 local data=$dir/$id
456
457 ceph-mon \
458 --id $id \
459 --mkfs \
460 --mon-data=$data \
461 --run-dir=$dir \
462 "$@" || return 1
463
464 ceph-mon \
465 --id $id \
11fdf7f2 466 --osd-failsafe-full-ratio=.99 \
7c673cae
FG
467 --mon-osd-full-ratio=.99 \
468 --mon-data-avail-crit=1 \
b5b8bbf5 469 --mon-data-avail-warn=5 \
7c673cae
FG
470 --paxos-propose-interval=0.1 \
471 --osd-crush-chooseleaf-type=0 \
c07f9fc5 472 $EXTRA_OPTS \
7c673cae
FG
473 --debug-mon 20 \
474 --debug-ms 20 \
475 --debug-paxos 20 \
476 --chdir= \
477 --mon-data=$data \
478 --log-file=$dir/\$name.log \
c07f9fc5 479 --admin-socket=$(get_asok_path) \
7c673cae
FG
480 --mon-cluster-log-file=$dir/log \
481 --run-dir=$dir \
482 --pid-file=$dir/\$name.pid \
483 --mon-allow-pool-delete \
c07f9fc5 484 --mon-osd-backfillfull-ratio .99 \
7c673cae
FG
485 "$@" || return 1
486
487 cat > $dir/ceph.conf <<EOF
488[global]
489fsid = $(get_config mon $id fsid)
490mon host = $(get_config mon $id mon_host)
491EOF
224ce89b
WB
492}
493
7c673cae
FG
494function test_run_mon() {
495 local dir=$1
496
497 setup $dir || return 1
498
499 run_mon $dir a --mon-initial-members=a || return 1
11fdf7f2 500 ceph mon dump | grep "mon.a" || return 1
7c673cae
FG
501 kill_daemons $dir || return 1
502
11fdf7f2
TL
503 run_mon $dir a --osd_pool_default_size=3 || return 1
504 run_osd $dir 0 || return 1
505 run_osd $dir 1 || return 1
506 run_osd $dir 2 || return 1
c07f9fc5 507 create_rbd_pool || return 1
11fdf7f2 508 ceph osd dump | grep "pool 1 'rbd'" || return 1
c07f9fc5 509 local size=$(CEPH_ARGS='' ceph --format=json daemon $(get_asok_path mon.a) \
7c673cae
FG
510 config get osd_pool_default_size)
511 test "$size" = '{"osd_pool_default_size":"3"}' || return 1
512
513 ! CEPH_ARGS='' ceph status || return 1
514 CEPH_ARGS='' ceph --conf $dir/ceph.conf status || return 1
515
516 kill_daemons $dir || return 1
517
518 run_mon $dir a --osd_pool_default_size=1 || return 1
c07f9fc5 519 local size=$(CEPH_ARGS='' ceph --format=json daemon $(get_asok_path mon.a) \
7c673cae
FG
520 config get osd_pool_default_size)
521 test "$size" = '{"osd_pool_default_size":"1"}' || return 1
522 kill_daemons $dir || return 1
523
524 CEPH_ARGS="$CEPH_ARGS --osd_pool_default_size=2" \
525 run_mon $dir a || return 1
c07f9fc5 526 local size=$(CEPH_ARGS='' ceph --format=json daemon $(get_asok_path mon.a) \
7c673cae
FG
527 config get osd_pool_default_size)
528 test "$size" = '{"osd_pool_default_size":"2"}' || return 1
529 kill_daemons $dir || return 1
530
531 teardown $dir || return 1
532}
533
c07f9fc5
FG
534function create_rbd_pool() {
535 ceph osd pool delete rbd rbd --yes-i-really-really-mean-it || return 1
b5b8bbf5 536 create_pool rbd $PG_NUM || return 1
c07f9fc5
FG
537 rbd pool init rbd
538}
539
b5b8bbf5
FG
540function create_pool() {
541 ceph osd pool create "$@"
542 sleep 1
543}
544
28e407b8
AA
545function delete_pool() {
546 local poolname=$1
547 ceph osd pool delete $poolname $poolname --yes-i-really-really-mean-it
548}
549
7c673cae
FG
550#######################################################################
551
552function run_mgr() {
553 local dir=$1
554 shift
555 local id=$1
556 shift
557 local data=$dir/$id
558
559 ceph-mgr \
560 --id $id \
c07f9fc5 561 $EXTRA_OPTS \
11fdf7f2 562 --osd-failsafe-full-ratio=.99 \
7c673cae
FG
563 --debug-mgr 20 \
564 --debug-objecter 20 \
565 --debug-ms 20 \
566 --debug-paxos 20 \
567 --chdir= \
568 --mgr-data=$data \
569 --log-file=$dir/\$name.log \
c07f9fc5 570 --admin-socket=$(get_asok_path) \
7c673cae
FG
571 --run-dir=$dir \
572 --pid-file=$dir/\$name.pid \
11fdf7f2
TL
573 --mgr-module-path=$(realpath ${CEPH_ROOT}/src/pybind/mgr) \
574 "$@" || return 1
575}
576
577function run_mds() {
578 local dir=$1
579 shift
580 local id=$1
581 shift
582 local data=$dir/$id
583
584 ceph-mds \
585 --id $id \
586 $EXTRA_OPTS \
587 --debug-mds 20 \
588 --debug-objecter 20 \
589 --debug-ms 20 \
590 --chdir= \
591 --mds-data=$data \
592 --log-file=$dir/\$name.log \
593 --admin-socket=$(get_asok_path) \
594 --run-dir=$dir \
595 --pid-file=$dir/\$name.pid \
7c673cae
FG
596 "$@" || return 1
597}
598
599#######################################################################
600
601##
602# Create (prepare) and run (activate) an osd by the name osd.**id**
603# with data in **dir**/**id**. The logs can be found in
604# **dir**/osd.**id**.log, the pid file is **dir**/osd.**id**.pid and
605# the admin socket is **dir**/**id**/ceph-osd.**id**.asok.
606#
607# The remaining arguments are passed verbatim to ceph-osd.
608#
609# Two mandatory arguments must be provided: --fsid and --mon-host
610# Instead of adding them to every call to run_osd, they can be
611# set in the CEPH_ARGS environment variable to be read implicitly
612# by every ceph command.
613#
614# The CEPH_CONF variable is expected to be set to /dev/null to
615# only rely on arguments for configuration.
616#
11fdf7f2
TL
617# The run_osd function creates the OSD data directory on the **dir**/**id**
618# directory and relies on the activate_osd function to run the daemon.
7c673cae
FG
619#
620# Examples:
621#
622# CEPH_ARGS="--fsid=$(uuidgen) "
623# CEPH_ARGS+="--mon-host=127.0.0.1:7018 "
624# run_osd $dir 0 # prepare and activate an osd using the monitor listening on 7018
625#
626# @param dir path name of the environment
627# @param id osd identifier
628# @param ... can be any option valid for ceph-osd
629# @return 0 on success, 1 on error
630#
631function run_osd() {
632 local dir=$1
633 shift
634 local id=$1
635 shift
636 local osd_data=$dir/$id
637
11fdf7f2
TL
638 local ceph_args="$CEPH_ARGS"
639 ceph_args+=" --osd-failsafe-full-ratio=.99"
640 ceph_args+=" --osd-journal-size=100"
641 ceph_args+=" --osd-scrub-load-threshold=2000"
642 ceph_args+=" --osd-data=$osd_data"
643 ceph_args+=" --osd-journal=${osd_data}/journal"
644 ceph_args+=" --chdir="
645 ceph_args+=$EXTRA_OPTS
646 ceph_args+=" --run-dir=$dir"
647 ceph_args+=" --admin-socket=$(get_asok_path)"
648 ceph_args+=" --debug-osd=20"
649 ceph_args+=" --log-file=$dir/\$name.log"
650 ceph_args+=" --pid-file=$dir/\$name.pid"
651 ceph_args+=" --osd-max-object-name-len=460"
652 ceph_args+=" --osd-max-object-namespace-len=64"
653 ceph_args+=" --enable-experimental-unrecoverable-data-corrupting-features=*"
654 ceph_args+=" "
655 ceph_args+="$@"
7c673cae 656 mkdir -p $osd_data
7c673cae 657
11fdf7f2
TL
658 local uuid=`uuidgen`
659 echo "add osd$id $uuid"
660 OSD_SECRET=$(ceph-authtool --gen-print-key)
661 echo "{\"cephx_secret\": \"$OSD_SECRET\"}" > $osd_data/new.json
662 ceph osd new $uuid -i $osd_data/new.json
663 rm $osd_data/new.json
664 ceph-osd -i $id $ceph_args --mkfs --key $OSD_SECRET --osd-uuid $uuid
665
666 local key_fn=$osd_data/keyring
667 cat > $key_fn<<EOF
668[osd.$id]
669key = $OSD_SECRET
670EOF
671 echo adding osd$id key to auth repository
672 ceph -i "$key_fn" auth add osd.$id osd "allow *" mon "allow profile osd" mgr "allow profile osd"
673 echo start osd.$id
674 ceph-osd -i $id $ceph_args &
675
eafe8130
TL
676 # If noup is set, then can't wait for this osd
677 if ceph osd dump --format=json | jq '.flags_set[]' | grep -q '"noup"' ; then
678 return 0
679 fi
11fdf7f2
TL
680 wait_for_osd up $id || return 1
681
7c673cae
FG
682}
683
eafe8130 684function run_osd_filestore() {
7c673cae
FG
685 local dir=$1
686 shift
687 local id=$1
688 shift
689 local osd_data=$dir/$id
690
11fdf7f2
TL
691 local ceph_args="$CEPH_ARGS"
692 ceph_args+=" --osd-failsafe-full-ratio=.99"
693 ceph_args+=" --osd-journal-size=100"
694 ceph_args+=" --osd-scrub-load-threshold=2000"
695 ceph_args+=" --osd-data=$osd_data"
696 ceph_args+=" --osd-journal=${osd_data}/journal"
697 ceph_args+=" --chdir="
698 ceph_args+=$EXTRA_OPTS
699 ceph_args+=" --run-dir=$dir"
700 ceph_args+=" --admin-socket=$(get_asok_path)"
701 ceph_args+=" --debug-osd=20"
702 ceph_args+=" --log-file=$dir/\$name.log"
703 ceph_args+=" --pid-file=$dir/\$name.pid"
704 ceph_args+=" --osd-max-object-name-len=460"
705 ceph_args+=" --osd-max-object-namespace-len=64"
706 ceph_args+=" --enable-experimental-unrecoverable-data-corrupting-features=*"
707 ceph_args+=" "
708 ceph_args+="$@"
7c673cae 709 mkdir -p $osd_data
7c673cae 710
11fdf7f2
TL
711 local uuid=`uuidgen`
712 echo "add osd$osd $uuid"
713 OSD_SECRET=$(ceph-authtool --gen-print-key)
714 echo "{\"cephx_secret\": \"$OSD_SECRET\"}" > $osd_data/new.json
715 ceph osd new $uuid -i $osd_data/new.json
716 rm $osd_data/new.json
eafe8130 717 ceph-osd -i $id $ceph_args --mkfs --key $OSD_SECRET --osd-uuid $uuid --osd-objectstore=filestore
11fdf7f2
TL
718
719 local key_fn=$osd_data/keyring
720 cat > $key_fn<<EOF
721[osd.$osd]
722key = $OSD_SECRET
723EOF
724 echo adding osd$id key to auth repository
725 ceph -i "$key_fn" auth add osd.$id osd "allow *" mon "allow profile osd" mgr "allow profile osd"
726 echo start osd.$id
727 ceph-osd -i $id $ceph_args &
728
eafe8130
TL
729 # If noup is set, then can't wait for this osd
730 if ceph osd dump --format=json | jq '.flags_set[]' | grep -q '"noup"' ; then
731 return 0
732 fi
11fdf7f2
TL
733 wait_for_osd up $id || return 1
734
735
7c673cae
FG
736}
737
738function test_run_osd() {
739 local dir=$1
740
741 setup $dir || return 1
742
743 run_mon $dir a || return 1
744 run_mgr $dir x || return 1
745
746 run_osd $dir 0 || return 1
c07f9fc5 747 local backfills=$(CEPH_ARGS='' ceph --format=json daemon $(get_asok_path osd.0) \
7c673cae
FG
748 config get osd_max_backfills)
749 echo "$backfills" | grep --quiet 'osd_max_backfills' || return 1
750
751 run_osd $dir 1 --osd-max-backfills 20 || return 1
c07f9fc5 752 local backfills=$(CEPH_ARGS='' ceph --format=json daemon $(get_asok_path osd.1) \
7c673cae
FG
753 config get osd_max_backfills)
754 test "$backfills" = '{"osd_max_backfills":"20"}' || return 1
755
756 CEPH_ARGS="$CEPH_ARGS --osd-max-backfills 30" run_osd $dir 2 || return 1
c07f9fc5 757 local backfills=$(CEPH_ARGS='' ceph --format=json daemon $(get_asok_path osd.2) \
7c673cae
FG
758 config get osd_max_backfills)
759 test "$backfills" = '{"osd_max_backfills":"30"}' || return 1
760
761 teardown $dir || return 1
762}
763
764#######################################################################
765
766##
767# Shutdown and remove all traces of the osd by the name osd.**id**.
768#
769# The OSD is shutdown with the TERM signal. It is then removed from
770# the auth list, crush map, osd map etc and the files associated with
771# it are also removed.
772#
773# @param dir path name of the environment
774# @param id osd identifier
775# @return 0 on success, 1 on error
776#
777function destroy_osd() {
778 local dir=$1
779 local id=$2
780
7c673cae 781 ceph osd out osd.$id || return 1
c07f9fc5
FG
782 kill_daemons $dir TERM osd.$id || return 1
783 ceph osd purge osd.$id --yes-i-really-mean-it || return 1
7c673cae
FG
784 teardown $dir/$id || return 1
785 rm -fr $dir/$id
786}
787
788function test_destroy_osd() {
789 local dir=$1
790
791 setup $dir || return 1
792 run_mon $dir a || return 1
793 run_mgr $dir x || return 1
794 run_osd $dir 0 || return 1
795 destroy_osd $dir 0 || return 1
796 ! ceph osd dump | grep "osd.$id " || return 1
797 teardown $dir || return 1
798}
799
800#######################################################################
801
802##
803# Run (activate) an osd by the name osd.**id** with data in
804# **dir**/**id**. The logs can be found in **dir**/osd.**id**.log,
805# the pid file is **dir**/osd.**id**.pid and the admin socket is
806# **dir**/**id**/ceph-osd.**id**.asok.
807#
808# The remaining arguments are passed verbatim to ceph-osd.
809#
810# Two mandatory arguments must be provided: --fsid and --mon-host
811# Instead of adding them to every call to activate_osd, they can be
812# set in the CEPH_ARGS environment variable to be read implicitly
813# by every ceph command.
814#
815# The CEPH_CONF variable is expected to be set to /dev/null to
816# only rely on arguments for configuration.
817#
818# The activate_osd function expects a valid OSD data directory
819# in **dir**/**id**, either just created via run_osd or re-using
820# one left by a previous run of ceph-osd. The ceph-osd daemon is
11fdf7f2 821# run directly on the foreground
7c673cae
FG
822#
823# The activate_osd function blocks until the monitor reports the osd
824# up. If it fails to do so within $TIMEOUT seconds, activate_osd
825# fails.
826#
827# Examples:
828#
829# CEPH_ARGS="--fsid=$(uuidgen) "
830# CEPH_ARGS+="--mon-host=127.0.0.1:7018 "
831# activate_osd $dir 0 # activate an osd using the monitor listening on 7018
832#
833# @param dir path name of the environment
834# @param id osd identifier
835# @param ... can be any option valid for ceph-osd
836# @return 0 on success, 1 on error
837#
838function activate_osd() {
839 local dir=$1
840 shift
841 local id=$1
842 shift
843 local osd_data=$dir/$id
844
7c673cae 845 local ceph_args="$CEPH_ARGS"
7c673cae
FG
846 ceph_args+=" --osd-failsafe-full-ratio=.99"
847 ceph_args+=" --osd-journal-size=100"
848 ceph_args+=" --osd-scrub-load-threshold=2000"
849 ceph_args+=" --osd-data=$osd_data"
11fdf7f2 850 ceph_args+=" --osd-journal=${osd_data}/journal"
7c673cae 851 ceph_args+=" --chdir="
c07f9fc5 852 ceph_args+=$EXTRA_OPTS
7c673cae 853 ceph_args+=" --run-dir=$dir"
c07f9fc5 854 ceph_args+=" --admin-socket=$(get_asok_path)"
7c673cae
FG
855 ceph_args+=" --debug-osd=20"
856 ceph_args+=" --log-file=$dir/\$name.log"
857 ceph_args+=" --pid-file=$dir/\$name.pid"
11fdf7f2
TL
858 ceph_args+=" --osd-max-object-name-len=460"
859 ceph_args+=" --osd-max-object-namespace-len=64"
860 ceph_args+=" --enable-experimental-unrecoverable-data-corrupting-features=*"
7c673cae
FG
861 ceph_args+=" "
862 ceph_args+="$@"
863 mkdir -p $osd_data
11fdf7f2
TL
864
865 echo start osd.$id
866 ceph-osd -i $id $ceph_args &
7c673cae
FG
867
868 [ "$id" = "$(cat $osd_data/whoami)" ] || return 1
869
eafe8130
TL
870 # If noup is set, then can't wait for this osd
871 if ceph osd dump --format=json | jq '.flags_set[]' | grep -q '"noup"' ; then
872 return 0
873 fi
7c673cae
FG
874 wait_for_osd up $id || return 1
875}
876
877function test_activate_osd() {
878 local dir=$1
879
880 setup $dir || return 1
881
882 run_mon $dir a || return 1
883 run_mgr $dir x || return 1
884
885 run_osd $dir 0 || return 1
c07f9fc5 886 local backfills=$(CEPH_ARGS='' ceph --format=json daemon $(get_asok_path osd.0) \
7c673cae
FG
887 config get osd_max_backfills)
888 echo "$backfills" | grep --quiet 'osd_max_backfills' || return 1
889
890 kill_daemons $dir TERM osd || return 1
891
892 activate_osd $dir 0 --osd-max-backfills 20 || return 1
c07f9fc5 893 local backfills=$(CEPH_ARGS='' ceph --format=json daemon $(get_asok_path osd.0) \
7c673cae
FG
894 config get osd_max_backfills)
895 test "$backfills" = '{"osd_max_backfills":"20"}' || return 1
896
897 teardown $dir || return 1
898}
899
900#######################################################################
901
902##
903# Wait until the OSD **id** is either up or down, as specified by
904# **state**. It fails after $TIMEOUT seconds.
905#
906# @param state either up or down
907# @param id osd identifier
908# @return 0 on success, 1 on error
909#
910function wait_for_osd() {
911 local state=$1
912 local id=$2
913
914 status=1
915 for ((i=0; i < $TIMEOUT; i++)); do
916 echo $i
917 if ! ceph osd dump | grep "osd.$id $state"; then
918 sleep 1
919 else
920 status=0
921 break
922 fi
923 done
924 return $status
925}
926
927function test_wait_for_osd() {
928 local dir=$1
929 setup $dir || return 1
930 run_mon $dir a --osd_pool_default_size=1 || return 1
931 run_mgr $dir x || return 1
932 run_osd $dir 0 || return 1
933 wait_for_osd up 0 || return 1
934 kill_daemons $dir TERM osd || return 1
935 wait_for_osd down 0 || return 1
936 ( TIMEOUT=1 ; ! wait_for_osd up 0 ) || return 1
937 teardown $dir || return 1
938}
939
940#######################################################################
941
942##
943# Display the list of OSD ids supporting the **objectname** stored in
944# **poolname**, as reported by ceph osd map.
945#
946# @param poolname an existing pool
947# @param objectname an objectname (may or may not exist)
948# @param STDOUT white space separated list of OSD ids
949# @return 0 on success, 1 on error
950#
951function get_osds() {
952 local poolname=$1
953 local objectname=$2
954
31f18b77
FG
955 local osds=$(ceph --format json osd map $poolname $objectname 2>/dev/null | \
956 jq '.acting | .[]')
7c673cae
FG
957 # get rid of the trailing space
958 echo $osds
959}
960
961function test_get_osds() {
962 local dir=$1
963
964 setup $dir || return 1
965 run_mon $dir a --osd_pool_default_size=2 || return 1
966 run_mgr $dir x || return 1
967 run_osd $dir 0 || return 1
968 run_osd $dir 1 || return 1
c07f9fc5 969 create_rbd_pool || return 1
7c673cae 970 wait_for_clean || return 1
c07f9fc5 971 create_rbd_pool || return 1
7c673cae
FG
972 get_osds rbd GROUP | grep --quiet '^[0-1] [0-1]$' || return 1
973 teardown $dir || return 1
974}
975
976#######################################################################
977
978##
979# Wait for the monitor to form quorum (optionally, of size N)
980#
981# @param timeout duration (lower-bound) to wait for quorum to be formed
982# @param quorumsize size of quorum to wait for
983# @return 0 on success, 1 on error
984#
985function wait_for_quorum() {
986 local timeout=$1
987 local quorumsize=$2
988
989 if [[ -z "$timeout" ]]; then
990 timeout=300
991 fi
992
993 if [[ -z "$quorumsize" ]]; then
994 timeout $timeout ceph mon_status --format=json >&/dev/null || return 1
995 return 0
996 fi
997
998 no_quorum=1
c07f9fc5 999 wait_until=$((`date +%s` + $timeout))
7c673cae
FG
1000 while [[ $(date +%s) -lt $wait_until ]]; do
1001 jqfilter='.quorum | length == '$quorumsize
1002 jqinput="$(timeout $timeout ceph mon_status --format=json 2>/dev/null)"
1003 res=$(echo $jqinput | jq "$jqfilter")
1004 if [[ "$res" == "true" ]]; then
1005 no_quorum=0
1006 break
1007 fi
1008 done
1009 return $no_quorum
1010}
1011
1012#######################################################################
1013
1014##
1015# Return the PG of supporting the **objectname** stored in
1016# **poolname**, as reported by ceph osd map.
1017#
1018# @param poolname an existing pool
1019# @param objectname an objectname (may or may not exist)
1020# @param STDOUT a PG
1021# @return 0 on success, 1 on error
1022#
1023function get_pg() {
1024 local poolname=$1
1025 local objectname=$2
1026
31f18b77 1027 ceph --format json osd map $poolname $objectname 2>/dev/null | jq -r '.pgid'
7c673cae
FG
1028}
1029
1030function test_get_pg() {
1031 local dir=$1
1032
1033 setup $dir || return 1
1034 run_mon $dir a --osd_pool_default_size=1 || return 1
1035 run_mgr $dir x || return 1
1036 run_osd $dir 0 || return 1
c07f9fc5 1037 create_rbd_pool || return 1
7c673cae
FG
1038 wait_for_clean || return 1
1039 get_pg rbd GROUP | grep --quiet '^[0-9]\.[0-9a-f][0-9a-f]*$' || return 1
1040 teardown $dir || return 1
1041}
1042
1043#######################################################################
1044
1045##
1046# Return the value of the **config**, obtained via the config get command
1047# of the admin socket of **daemon**.**id**.
1048#
1049# @param daemon mon or osd
1050# @param id mon or osd ID
1051# @param config the configuration variable name as found in config_opts.h
1052# @param STDOUT the config value
1053# @return 0 on success, 1 on error
1054#
1055function get_config() {
1056 local daemon=$1
1057 local id=$2
1058 local config=$3
1059
1060 CEPH_ARGS='' \
c07f9fc5 1061 ceph --format json daemon $(get_asok_path $daemon.$id) \
7c673cae 1062 config get $config 2> /dev/null | \
31f18b77 1063 jq -r ".$config"
7c673cae
FG
1064}
1065
1066function test_get_config() {
1067 local dir=$1
1068
1069 # override the default config using command line arg and check it
1070 setup $dir || return 1
1071 run_mon $dir a --osd_pool_default_size=1 || return 1
1072 test $(get_config mon a osd_pool_default_size) = 1 || return 1
1073 run_mgr $dir x || return 1
1074 run_osd $dir 0 --osd_max_scrubs=3 || return 1
1075 test $(get_config osd 0 osd_max_scrubs) = 3 || return 1
1076 teardown $dir || return 1
1077}
1078
1079#######################################################################
1080
1081##
1082# Set the **config** to specified **value**, via the config set command
1083# of the admin socket of **daemon**.**id**
1084#
1085# @param daemon mon or osd
1086# @param id mon or osd ID
1087# @param config the configuration variable name as found in config_opts.h
1088# @param value the config value
1089# @return 0 on success, 1 on error
1090#
1091function set_config() {
1092 local daemon=$1
1093 local id=$2
1094 local config=$3
1095 local value=$4
1096
c07f9fc5 1097 test $(env CEPH_ARGS='' ceph --format json daemon $(get_asok_path $daemon.$id) \
31f18b77
FG
1098 config set $config $value 2> /dev/null | \
1099 jq 'has("success")') == true
7c673cae
FG
1100}
1101
1102function test_set_config() {
1103 local dir=$1
1104
1105 setup $dir || return 1
1106 run_mon $dir a --osd_pool_default_size=1 || return 1
1107 test $(get_config mon a ms_crc_header) = true || return 1
1108 set_config mon a ms_crc_header false || return 1
1109 test $(get_config mon a ms_crc_header) = false || return 1
1110 set_config mon a ms_crc_header true || return 1
1111 test $(get_config mon a ms_crc_header) = true || return 1
1112 teardown $dir || return 1
1113}
1114
1115#######################################################################
1116
1117##
1118# Return the OSD id of the primary OSD supporting the **objectname**
1119# stored in **poolname**, as reported by ceph osd map.
1120#
1121# @param poolname an existing pool
1122# @param objectname an objectname (may or may not exist)
1123# @param STDOUT the primary OSD id
1124# @return 0 on success, 1 on error
1125#
1126function get_primary() {
1127 local poolname=$1
1128 local objectname=$2
1129
31f18b77
FG
1130 ceph --format json osd map $poolname $objectname 2>/dev/null | \
1131 jq '.acting_primary'
7c673cae
FG
1132}
1133
1134function test_get_primary() {
1135 local dir=$1
1136
1137 setup $dir || return 1
1138 run_mon $dir a --osd_pool_default_size=1 || return 1
1139 local osd=0
1140 run_mgr $dir x || return 1
1141 run_osd $dir $osd || return 1
c07f9fc5 1142 create_rbd_pool || return 1
7c673cae
FG
1143 wait_for_clean || return 1
1144 test $(get_primary rbd GROUP) = $osd || return 1
1145 teardown $dir || return 1
1146}
1147
1148#######################################################################
1149
1150##
1151# Return the id of any OSD supporting the **objectname** stored in
1152# **poolname**, as reported by ceph osd map, except the primary.
1153#
1154# @param poolname an existing pool
1155# @param objectname an objectname (may or may not exist)
1156# @param STDOUT the OSD id
1157# @return 0 on success, 1 on error
1158#
1159function get_not_primary() {
1160 local poolname=$1
1161 local objectname=$2
1162
1163 local primary=$(get_primary $poolname $objectname)
31f18b77
FG
1164 ceph --format json osd map $poolname $objectname 2>/dev/null | \
1165 jq ".acting | map(select (. != $primary)) | .[0]"
7c673cae
FG
1166}
1167
1168function test_get_not_primary() {
1169 local dir=$1
1170
1171 setup $dir || return 1
1172 run_mon $dir a --osd_pool_default_size=2 || return 1
1173 run_mgr $dir x || return 1
1174 run_osd $dir 0 || return 1
1175 run_osd $dir 1 || return 1
c07f9fc5 1176 create_rbd_pool || return 1
7c673cae
FG
1177 wait_for_clean || return 1
1178 local primary=$(get_primary rbd GROUP)
1179 local not_primary=$(get_not_primary rbd GROUP)
1180 test $not_primary != $primary || return 1
1181 test $not_primary = 0 -o $not_primary = 1 || return 1
1182 teardown $dir || return 1
1183}
1184
1185#######################################################################
1186
11fdf7f2
TL
1187function _objectstore_tool_nodown() {
1188 local dir=$1
1189 shift
1190 local id=$1
1191 shift
1192 local osd_data=$dir/$id
1193
11fdf7f2
TL
1194 ceph-objectstore-tool \
1195 --data-path $osd_data \
11fdf7f2
TL
1196 "$@" || return 1
1197}
1198
1199function _objectstore_tool_nowait() {
1200 local dir=$1
1201 shift
1202 local id=$1
1203 shift
1204
1205 kill_daemons $dir TERM osd.$id >&2 < /dev/null || return 1
1206
1207 _objectstore_tool_nodown $dir $id "$@" || return 1
1208 activate_osd $dir $id $ceph_osd_args >&2 || return 1
1209}
1210
7c673cae
FG
1211##
1212# Run ceph-objectstore-tool against the OSD **id** using the data path
1213# **dir**. The OSD is killed with TERM prior to running
1214# ceph-objectstore-tool because access to the data path is
1215# exclusive. The OSD is restarted after the command completes. The
1216# objectstore_tool returns after all PG are active+clean again.
1217#
1218# @param dir the data path of the OSD
1219# @param id the OSD id
1220# @param ... arguments to ceph-objectstore-tool
1221# @param STDIN the input of ceph-objectstore-tool
1222# @param STDOUT the output of ceph-objectstore-tool
1223# @return 0 on success, 1 on error
1224#
1225# The value of $ceph_osd_args will be passed to restarted osds
1226#
1227function objectstore_tool() {
1228 local dir=$1
1229 shift
1230 local id=$1
1231 shift
7c673cae 1232
11fdf7f2 1233 _objectstore_tool_nowait $dir $id "$@" || return 1
7c673cae
FG
1234 wait_for_clean >&2
1235}
1236
1237function test_objectstore_tool() {
1238 local dir=$1
1239
1240 setup $dir || return 1
1241 run_mon $dir a --osd_pool_default_size=1 || return 1
1242 local osd=0
1243 run_mgr $dir x || return 1
1244 run_osd $dir $osd || return 1
c07f9fc5 1245 create_rbd_pool || return 1
7c673cae
FG
1246 wait_for_clean || return 1
1247 rados --pool rbd put GROUP /etc/group || return 1
1248 objectstore_tool $dir $osd GROUP get-bytes | \
1249 diff - /etc/group
1250 ! objectstore_tool $dir $osd NOTEXISTS get-bytes || return 1
1251 teardown $dir || return 1
1252}
1253
1254#######################################################################
1255
1256##
1257# Predicate checking if there is an ongoing recovery in the
1258# cluster. If any of the recovering_{keys,bytes,objects}_per_sec
1259# counters are reported by ceph status, it means recovery is in
1260# progress.
1261#
1262# @return 0 if recovery in progress, 1 otherwise
1263#
1264function get_is_making_recovery_progress() {
31f18b77
FG
1265 local recovery_progress
1266 recovery_progress+=".recovering_keys_per_sec + "
1267 recovery_progress+=".recovering_bytes_per_sec + "
1268 recovery_progress+=".recovering_objects_per_sec"
1269 local progress=$(ceph --format json status 2>/dev/null | \
1270 jq -r ".pgmap | $recovery_progress")
1271 test "$progress" != null
7c673cae
FG
1272}
1273
1274function test_get_is_making_recovery_progress() {
1275 local dir=$1
1276
1277 setup $dir || return 1
1278 run_mon $dir a || return 1
1279 run_mgr $dir x || return 1
1280 ! get_is_making_recovery_progress || return 1
1281 teardown $dir || return 1
1282}
1283
1284#######################################################################
1285
1286##
1287# Return the number of active PGs in the cluster. A PG is active if
1288# ceph pg dump pgs reports it both **active** and **clean** and that
1289# not **stale**.
1290#
1291# @param STDOUT the number of active PGs
1292# @return 0 on success, 1 on error
1293#
1294function get_num_active_clean() {
31f18b77
FG
1295 local expression
1296 expression+="select(contains(\"active\") and contains(\"clean\")) | "
1297 expression+="select(contains(\"stale\") | not)"
1298 ceph --format json pg dump pgs 2>/dev/null | \
11fdf7f2 1299 jq ".pg_stats | [.[] | .state | $expression] | length"
7c673cae
FG
1300}
1301
1302function test_get_num_active_clean() {
1303 local dir=$1
1304
1305 setup $dir || return 1
1306 run_mon $dir a --osd_pool_default_size=1 || return 1
1307 run_mgr $dir x || return 1
1308 run_osd $dir 0 || return 1
c07f9fc5 1309 create_rbd_pool || return 1
7c673cae
FG
1310 wait_for_clean || return 1
1311 local num_active_clean=$(get_num_active_clean)
1312 test "$num_active_clean" = $PG_NUM || return 1
1313 teardown $dir || return 1
1314}
1315
1316#######################################################################
1317
1318##
1319# Return the number of PGs in the cluster, according to
1320# ceph pg dump pgs.
1321#
1322# @param STDOUT the number of PGs
1323# @return 0 on success, 1 on error
1324#
1325function get_num_pgs() {
31f18b77 1326 ceph --format json status 2>/dev/null | jq '.pgmap.num_pgs'
7c673cae
FG
1327}
1328
1329function test_get_num_pgs() {
1330 local dir=$1
1331
1332 setup $dir || return 1
1333 run_mon $dir a --osd_pool_default_size=1 || return 1
1334 run_mgr $dir x || return 1
1335 run_osd $dir 0 || return 1
c07f9fc5 1336 create_rbd_pool || return 1
7c673cae
FG
1337 wait_for_clean || return 1
1338 local num_pgs=$(get_num_pgs)
1339 test "$num_pgs" -gt 0 || return 1
1340 teardown $dir || return 1
1341}
1342
1343#######################################################################
1344
c07f9fc5
FG
1345##
1346# Return the OSD ids in use by at least one PG in the cluster (either
1347# in the up or the acting set), according to ceph pg dump pgs. Every
1348# OSD id shows as many times as they are used in up and acting sets.
1349# If an OSD id is in both the up and acting set of a given PG, it will
1350# show twice.
1351#
1352# @param STDOUT a sorted list of OSD ids
1353# @return 0 on success, 1 on error
1354#
1355function get_osd_id_used_by_pgs() {
11fdf7f2 1356 ceph --format json pg dump pgs 2>/dev/null | jq '.pg_stats | .[] | .up[], .acting[]' | sort
c07f9fc5
FG
1357}
1358
1359function test_get_osd_id_used_by_pgs() {
1360 local dir=$1
1361
1362 setup $dir || return 1
1363 run_mon $dir a --osd_pool_default_size=1 || return 1
1364 run_mgr $dir x || return 1
1365 run_osd $dir 0 || return 1
1366 create_rbd_pool || return 1
1367 wait_for_clean || return 1
1368 local osd_ids=$(get_osd_id_used_by_pgs | uniq)
1369 test "$osd_ids" = "0" || return 1
1370 teardown $dir || return 1
1371}
1372
1373#######################################################################
1374
1375##
1376# Wait until the OSD **id** shows **count** times in the
1377# PGs (see get_osd_id_used_by_pgs for more information about
1378# how OSD ids are counted).
1379#
1380# @param id the OSD id
1381# @param count the number of time it must show in the PGs
1382# @return 0 on success, 1 on error
1383#
1384function wait_osd_id_used_by_pgs() {
1385 local id=$1
1386 local count=$2
1387
1388 status=1
1389 for ((i=0; i < $TIMEOUT / 5; i++)); do
1390 echo $i
1391 if ! test $(get_osd_id_used_by_pgs | grep -c $id) = $count ; then
1392 sleep 5
1393 else
1394 status=0
1395 break
1396 fi
1397 done
1398 return $status
1399}
1400
1401function test_wait_osd_id_used_by_pgs() {
1402 local dir=$1
1403
1404 setup $dir || return 1
1405 run_mon $dir a --osd_pool_default_size=1 || return 1
1406 run_mgr $dir x || return 1
1407 run_osd $dir 0 || return 1
1408 create_rbd_pool || return 1
1409 wait_for_clean || return 1
1410 wait_osd_id_used_by_pgs 0 8 || return 1
1411 ! TIMEOUT=1 wait_osd_id_used_by_pgs 123 5 || return 1
1412 teardown $dir || return 1
1413}
1414
1415#######################################################################
1416
7c673cae
FG
1417##
1418# Return the date and time of the last completed scrub for **pgid**,
1419# as reported by ceph pg dump pgs. Note that a repair also sets this
1420# date.
1421#
1422# @param pgid the id of the PG
1423# @param STDOUT the date and time of the last scrub
1424# @return 0 on success, 1 on error
1425#
1426function get_last_scrub_stamp() {
1427 local pgid=$1
1428 local sname=${2:-last_scrub_stamp}
31f18b77 1429 ceph --format json pg dump pgs 2>/dev/null | \
11fdf7f2 1430 jq -r ".pg_stats | .[] | select(.pgid==\"$pgid\") | .$sname"
7c673cae
FG
1431}
1432
1433function test_get_last_scrub_stamp() {
1434 local dir=$1
1435
1436 setup $dir || return 1
1437 run_mon $dir a --osd_pool_default_size=1 || return 1
1438 run_mgr $dir x || return 1
1439 run_osd $dir 0 || return 1
c07f9fc5 1440 create_rbd_pool || return 1
7c673cae 1441 wait_for_clean || return 1
b5b8bbf5 1442 stamp=$(get_last_scrub_stamp 1.0)
7c673cae
FG
1443 test -n "$stamp" || return 1
1444 teardown $dir || return 1
1445}
1446
1447#######################################################################
1448
1449##
1450# Predicate checking if the cluster is clean, i.e. all of its PGs are
1451# in a clean state (see get_num_active_clean for a definition).
1452#
1453# @return 0 if the cluster is clean, 1 otherwise
1454#
1455function is_clean() {
1456 num_pgs=$(get_num_pgs)
1457 test $num_pgs != 0 || return 1
1458 test $(get_num_active_clean) = $num_pgs || return 1
1459}
1460
1461function test_is_clean() {
1462 local dir=$1
1463
1464 setup $dir || return 1
1465 run_mon $dir a --osd_pool_default_size=1 || return 1
1466 run_mgr $dir x || return 1
1467 run_osd $dir 0 || return 1
c07f9fc5 1468 create_rbd_pool || return 1
7c673cae
FG
1469 wait_for_clean || return 1
1470 is_clean || return 1
1471 teardown $dir || return 1
1472}
1473
1474#######################################################################
1475
f64942e4 1476calc() { $AWK "BEGIN{print $*}"; }
94b18763 1477
7c673cae
FG
1478##
1479# Return a list of numbers that are increasingly larger and whose
1480# total is **timeout** seconds. It can be used to have short sleep
1481# delay while waiting for an event on a fast machine. But if running
1482# very slowly the larger delays avoid stressing the machine even
1483# further or spamming the logs.
1484#
1485# @param timeout sum of all delays, in seconds
1486# @return a list of sleep delays
1487#
1488function get_timeout_delays() {
1489 local trace=$(shopt -q -o xtrace && echo true || echo false)
1490 $trace && shopt -u -o xtrace
1491 local timeout=$1
1492 local first_step=${2:-1}
11fdf7f2 1493 local max_timeout=${3:-$MAX_TIMEOUT}
7c673cae
FG
1494
1495 local i
1496 local total="0"
1497 i=$first_step
94b18763
FG
1498 while test "$(calc $total + $i \<= $timeout)" = "1"; do
1499 echo -n "$(calc $i) "
1500 total=$(calc $total + $i)
1501 i=$(calc $i \* 2)
11fdf7f2
TL
1502 if [ $max_timeout -gt 0 ]; then
1503 # Did we reach max timeout ?
1504 if [ ${i%.*} -eq ${max_timeout%.*} ] && [ ${i#*.} \> ${max_timeout#*.} ] || [ ${i%.*} -gt ${max_timeout%.*} ]; then
1505 # Yes, so let's cap the max wait time to max
1506 i=$max_timeout
1507 fi
1508 fi
7c673cae 1509 done
94b18763
FG
1510 if test "$(calc $total \< $timeout)" = "1"; then
1511 echo -n "$(calc $timeout - $total) "
7c673cae
FG
1512 fi
1513 $trace && shopt -s -o xtrace
1514}
1515
1516function test_get_timeout_delays() {
1517 test "$(get_timeout_delays 1)" = "1 " || return 1
94b18763
FG
1518 test "$(get_timeout_delays 5)" = "1 2 2 " || return 1
1519 test "$(get_timeout_delays 6)" = "1 2 3 " || return 1
7c673cae 1520 test "$(get_timeout_delays 7)" = "1 2 4 " || return 1
94b18763
FG
1521 test "$(get_timeout_delays 8)" = "1 2 4 1 " || return 1
1522 test "$(get_timeout_delays 1 .1)" = "0.1 0.2 0.4 0.3 " || return 1
1523 test "$(get_timeout_delays 1.5 .1)" = "0.1 0.2 0.4 0.8 " || return 1
1524 test "$(get_timeout_delays 5 .1)" = "0.1 0.2 0.4 0.8 1.6 1.9 " || return 1
1525 test "$(get_timeout_delays 6 .1)" = "0.1 0.2 0.4 0.8 1.6 2.9 " || return 1
1526 test "$(get_timeout_delays 6.3 .1)" = "0.1 0.2 0.4 0.8 1.6 3.2 " || return 1
1527 test "$(get_timeout_delays 20 .1)" = "0.1 0.2 0.4 0.8 1.6 3.2 6.4 7.3 " || return 1
11fdf7f2
TL
1528 test "$(get_timeout_delays 300 .1 0)" = "0.1 0.2 0.4 0.8 1.6 3.2 6.4 12.8 25.6 51.2 102.4 95.3 " || return 1
1529 test "$(get_timeout_delays 300 .1 10)" = "0.1 0.2 0.4 0.8 1.6 3.2 6.4 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 7.3 " || return 1
7c673cae
FG
1530}
1531
1532#######################################################################
1533
1534##
1535# Wait until the cluster becomes clean or if it does not make progress
11fdf7f2 1536# for $WAIT_FOR_CLEAN_TIMEOUT seconds.
7c673cae
FG
1537# Progress is measured either via the **get_is_making_recovery_progress**
1538# predicate or if the number of clean PGs changes (as returned by get_num_active_clean)
1539#
1540# @return 0 if the cluster is clean, 1 otherwise
1541#
1542function wait_for_clean() {
a8e16298 1543 local cmd=$1
7c673cae
FG
1544 local num_active_clean=-1
1545 local cur_active_clean
11fdf7f2 1546 local -a delays=($(get_timeout_delays $WAIT_FOR_CLEAN_TIMEOUT .1))
7c673cae 1547 local -i loop=0
31f18b77 1548
3a9019d9 1549 flush_pg_stats || return 1
31f18b77
FG
1550 while test $(get_num_pgs) == 0 ; do
1551 sleep 1
1552 done
7c673cae
FG
1553
1554 while true ; do
1555 # Comparing get_num_active_clean & get_num_pgs is used to determine
1556 # if the cluster is clean. That's almost an inline of is_clean() to
1557 # get more performance by avoiding multiple calls of get_num_active_clean.
1558 cur_active_clean=$(get_num_active_clean)
1559 test $cur_active_clean = $(get_num_pgs) && break
1560 if test $cur_active_clean != $num_active_clean ; then
1561 loop=0
1562 num_active_clean=$cur_active_clean
1563 elif get_is_making_recovery_progress ; then
1564 loop=0
1565 elif (( $loop >= ${#delays[*]} )) ; then
1566 ceph report
1567 return 1
1568 fi
a8e16298
TL
1569 # eval is a no-op if cmd is empty
1570 eval $cmd
7c673cae
FG
1571 sleep ${delays[$loop]}
1572 loop+=1
1573 done
1574 return 0
1575}
1576
1577function test_wait_for_clean() {
1578 local dir=$1
1579
1580 setup $dir || return 1
11fdf7f2
TL
1581 run_mon $dir a --osd_pool_default_size=2 || return 1
1582 run_osd $dir 0 || return 1
7c673cae 1583 run_mgr $dir x || return 1
c07f9fc5 1584 create_rbd_pool || return 1
11fdf7f2
TL
1585 ! WAIT_FOR_CLEAN_TIMEOUT=1 wait_for_clean || return 1
1586 run_osd $dir 1 || return 1
7c673cae
FG
1587 wait_for_clean || return 1
1588 teardown $dir || return 1
1589}
1590
1591#######################################################################
1592
1593##
1adf2230
AA
1594# Wait until the cluster has health condition passed as arg
1595# again for $TIMEOUT seconds.
7c673cae 1596#
1adf2230
AA
1597# @param string to grep for in health detail
1598# @return 0 if the cluster health matches request, 1 otherwise
7c673cae
FG
1599#
1600function wait_for_health() {
1601 local grepstr=$1
1602 local -a delays=($(get_timeout_delays $TIMEOUT .1))
1603 local -i loop=0
1604
1605 while ! ceph health detail | grep "$grepstr" ; do
1606 if (( $loop >= ${#delays[*]} )) ; then
1607 ceph health detail
1608 return 1
1609 fi
1610 sleep ${delays[$loop]}
1611 loop+=1
1612 done
1613}
1614
1adf2230
AA
1615##
1616# Wait until the cluster becomes HEALTH_OK again or if it does not make progress
1617# for $TIMEOUT seconds.
1618#
1619# @return 0 if the cluster is HEALTHY, 1 otherwise
1620#
7c673cae
FG
1621function wait_for_health_ok() {
1622 wait_for_health "HEALTH_OK" || return 1
1623}
1624
1625function test_wait_for_health_ok() {
1626 local dir=$1
1627
1628 setup $dir || return 1
eafe8130 1629 run_mon $dir a --osd_failsafe_full_ratio=.99 --mon_pg_warn_min_per_osd=0 || return 1
31f18b77 1630 run_mgr $dir x --mon_pg_warn_min_per_osd=0 || return 1
eafe8130 1631 # start osd_pool_default_size OSDs
7c673cae 1632 run_osd $dir 0 || return 1
eafe8130
TL
1633 run_osd $dir 1 || return 1
1634 run_osd $dir 2 || return 1
224ce89b 1635 kill_daemons $dir TERM osd || return 1
11fdf7f2 1636 ceph osd down 0 || return 1
eafe8130 1637 # expect TOO_FEW_OSDS warning
224ce89b 1638 ! TIMEOUT=1 wait_for_health_ok || return 1
eafe8130 1639 # resurrect all OSDs
224ce89b 1640 activate_osd $dir 0 || return 1
eafe8130
TL
1641 activate_osd $dir 1 || return 1
1642 activate_osd $dir 2 || return 1
7c673cae
FG
1643 wait_for_health_ok || return 1
1644 teardown $dir || return 1
1645}
1646
1647
1648#######################################################################
1649
1650##
1651# Run repair on **pgid** and wait until it completes. The repair
1652# function will fail if repair does not complete within $TIMEOUT
1653# seconds.
1654#
1655# @param pgid the id of the PG
1656# @return 0 on success, 1 on error
1657#
1658function repair() {
1659 local pgid=$1
1660 local last_scrub=$(get_last_scrub_stamp $pgid)
1661 ceph pg repair $pgid
1662 wait_for_scrub $pgid "$last_scrub"
1663}
1664
1665function test_repair() {
1666 local dir=$1
1667
1668 setup $dir || return 1
1669 run_mon $dir a --osd_pool_default_size=1 || return 1
1670 run_mgr $dir x || return 1
1671 run_osd $dir 0 || return 1
c07f9fc5 1672 create_rbd_pool || return 1
7c673cae 1673 wait_for_clean || return 1
b5b8bbf5 1674 repair 1.0 || return 1
7c673cae 1675 kill_daemons $dir KILL osd || return 1
b5b8bbf5 1676 ! TIMEOUT=1 repair 1.0 || return 1
7c673cae
FG
1677 teardown $dir || return 1
1678}
1679#######################################################################
1680
1681##
1682# Run scrub on **pgid** and wait until it completes. The pg_scrub
1683# function will fail if repair does not complete within $TIMEOUT
1684# seconds. The pg_scrub is complete whenever the
1685# **get_last_scrub_stamp** function reports a timestamp different from
1686# the one stored before starting the scrub.
1687#
1688# @param pgid the id of the PG
1689# @return 0 on success, 1 on error
1690#
1691function pg_scrub() {
1692 local pgid=$1
1693 local last_scrub=$(get_last_scrub_stamp $pgid)
1694 ceph pg scrub $pgid
1695 wait_for_scrub $pgid "$last_scrub"
1696}
1697
1698function pg_deep_scrub() {
1699 local pgid=$1
1700 local last_scrub=$(get_last_scrub_stamp $pgid last_deep_scrub_stamp)
1701 ceph pg deep-scrub $pgid
1702 wait_for_scrub $pgid "$last_scrub" last_deep_scrub_stamp
1703}
1704
1705function test_pg_scrub() {
1706 local dir=$1
1707
1708 setup $dir || return 1
1709 run_mon $dir a --osd_pool_default_size=1 || return 1
1710 run_mgr $dir x || return 1
1711 run_osd $dir 0 || return 1
c07f9fc5 1712 create_rbd_pool || return 1
7c673cae 1713 wait_for_clean || return 1
b5b8bbf5 1714 pg_scrub 1.0 || return 1
7c673cae 1715 kill_daemons $dir KILL osd || return 1
b5b8bbf5 1716 ! TIMEOUT=1 pg_scrub 1.0 || return 1
7c673cae
FG
1717 teardown $dir || return 1
1718}
1719
1720#######################################################################
1721
1722##
1723# Run the *command* and expect it to fail (i.e. return a non zero status).
1724# The output (stderr and stdout) is stored in a temporary file in *dir*
1725# and is expected to contain the string *expected*.
1726#
1727# Return 0 if the command failed and the string was found. Otherwise
1728# return 1 and cat the full output of the command on stderr for debug.
1729#
1730# @param dir temporary directory to store the output
1731# @param expected string to look for in the output
1732# @param command ... the command and its arguments
1733# @return 0 on success, 1 on error
1734#
1735
1736function expect_failure() {
1737 local dir=$1
1738 shift
1739 local expected="$1"
1740 shift
1741 local success
1742
1743 if "$@" > $dir/out 2>&1 ; then
1744 success=true
1745 else
1746 success=false
1747 fi
1748
1749 if $success || ! grep --quiet "$expected" $dir/out ; then
1750 cat $dir/out >&2
1751 return 1
1752 else
1753 return 0
1754 fi
1755}
1756
1757function test_expect_failure() {
1758 local dir=$1
1759
1760 setup $dir || return 1
1761 expect_failure $dir FAIL bash -c 'echo FAIL ; exit 1' || return 1
1762 # the command did not fail
1763 ! expect_failure $dir FAIL bash -c 'echo FAIL ; exit 0' > $dir/out || return 1
1764 grep --quiet FAIL $dir/out || return 1
1765 # the command failed but the output does not contain the expected string
1766 ! expect_failure $dir FAIL bash -c 'echo UNEXPECTED ; exit 1' > $dir/out || return 1
1767 ! grep --quiet FAIL $dir/out || return 1
1768 teardown $dir || return 1
1769}
1770
1771#######################################################################
1772
1773##
1774# Given the *last_scrub*, wait for scrub to happen on **pgid**. It
1775# will fail if scrub does not complete within $TIMEOUT seconds. The
1776# repair is complete whenever the **get_last_scrub_stamp** function
1777# reports a timestamp different from the one given in argument.
1778#
1779# @param pgid the id of the PG
1780# @param last_scrub timestamp of the last scrub for *pgid*
1781# @return 0 on success, 1 on error
1782#
1783function wait_for_scrub() {
1784 local pgid=$1
1785 local last_scrub="$2"
1786 local sname=${3:-last_scrub_stamp}
1787
1788 for ((i=0; i < $TIMEOUT; i++)); do
b5b8bbf5 1789 if test "$(get_last_scrub_stamp $pgid $sname)" '>' "$last_scrub" ; then
7c673cae
FG
1790 return 0
1791 fi
1792 sleep 1
1793 done
1794 return 1
1795}
1796
1797function test_wait_for_scrub() {
1798 local dir=$1
1799
1800 setup $dir || return 1
1801 run_mon $dir a --osd_pool_default_size=1 || return 1
1802 run_mgr $dir x || return 1
1803 run_osd $dir 0 || return 1
c07f9fc5 1804 create_rbd_pool || return 1
7c673cae 1805 wait_for_clean || return 1
b5b8bbf5 1806 local pgid=1.0
7c673cae
FG
1807 ceph pg repair $pgid
1808 local last_scrub=$(get_last_scrub_stamp $pgid)
1809 wait_for_scrub $pgid "$last_scrub" || return 1
1810 kill_daemons $dir KILL osd || return 1
1811 last_scrub=$(get_last_scrub_stamp $pgid)
1812 ! TIMEOUT=1 wait_for_scrub $pgid "$last_scrub" || return 1
1813 teardown $dir || return 1
1814}
1815
1816#######################################################################
1817
1818##
1819# Return 0 if the erasure code *plugin* is available, 1 otherwise.
1820#
1821# @param plugin erasure code plugin
1822# @return 0 on success, 1 on error
1823#
1824
1825function erasure_code_plugin_exists() {
1826 local plugin=$1
1827 local status
1828 local grepstr
1829 local s
1830 case `uname` in
1831 FreeBSD) grepstr="Cannot open.*$plugin" ;;
1832 *) grepstr="$plugin.*No such file" ;;
1833 esac
1834
1835 s=$(ceph osd erasure-code-profile set TESTPROFILE plugin=$plugin 2>&1)
1836 local status=$?
1837 if [ $status -eq 0 ]; then
1838 ceph osd erasure-code-profile rm TESTPROFILE
1839 elif ! echo $s | grep --quiet "$grepstr" ; then
1840 status=1
1841 # display why the string was rejected.
1842 echo $s
1843 fi
1844 return $status
1845}
1846
1847function test_erasure_code_plugin_exists() {
1848 local dir=$1
1849
1850 setup $dir || return 1
1851 run_mon $dir a || return 1
1852 run_mgr $dir x || return 1
1853 erasure_code_plugin_exists jerasure || return 1
1854 ! erasure_code_plugin_exists FAKE || return 1
1855 teardown $dir || return 1
1856}
1857
1858#######################################################################
1859
1860##
1861# Display all log files from **dir** on stdout.
1862#
1863# @param dir directory in which all data is stored
1864#
1865
1866function display_logs() {
1867 local dir=$1
1868
1869 find $dir -maxdepth 1 -name '*.log' | \
1870 while read file ; do
1871 echo "======================= $file"
1872 cat $file
1873 done
1874}
1875
1876function test_display_logs() {
1877 local dir=$1
1878
1879 setup $dir || return 1
1880 run_mon $dir a || return 1
1881 kill_daemons $dir || return 1
1882 display_logs $dir > $dir/log.out
1883 grep --quiet mon.a.log $dir/log.out || return 1
1884 teardown $dir || return 1
1885}
1886
1887#######################################################################
1888##
1889# Spawn a command in background and save the pid in the variable name
1890# passed in argument. To make the output reading easier, the output is
1891# prepend with the process id.
1892#
1893# Example:
1894# pids1=""
1895# run_in_background pids1 bash -c 'sleep 1; exit 1'
1896#
1897# @param pid_variable the variable name (not value) where the pids will be stored
1898# @param ... the command to execute
1899# @return only the pid_variable output should be considered and used with **wait_background**
1900#
1901function run_in_background() {
1902 local pid_variable=$1
94b18763 1903 shift
7c673cae 1904 # Execute the command and prepend the output with its pid
f64942e4 1905 # We enforce to return the exit status of the command and not the sed one.
94b18763 1906 ("$@" |& sed 's/^/'$$': /'; return "${PIPESTATUS[0]}") >&2 &
7c673cae
FG
1907 eval "$pid_variable+=\" $!\""
1908}
1909
94b18763
FG
1910function save_stdout {
1911 local out="$1"
1912 shift
1913 "$@" > "$out"
1914}
1915
7c673cae
FG
1916function test_run_in_background() {
1917 local pids
1918 run_in_background pids sleep 1
1919 run_in_background pids sleep 1
1920 test $(echo $pids | wc -w) = 2 || return 1
1921 wait $pids || return 1
1922}
1923
1924#######################################################################
1925##
1926# Wait for pids running in background to complete.
1927# This function is usually used after a **run_in_background** call
1928# Example:
1929# pids1=""
1930# run_in_background pids1 bash -c 'sleep 1; exit 1'
1931# wait_background pids1
1932#
1933# @param pids The variable name that contains the active PIDS. Set as empty at then end of the function.
1934# @return returns 1 if at least one process exits in error unless returns 0
1935#
1936function wait_background() {
1937 # We extract the PIDS from the variable name
1938 pids=${!1}
1939
1940 return_code=0
1941 for pid in $pids; do
1942 if ! wait $pid; then
1943 # If one process failed then return 1
1944 return_code=1
1945 fi
1946 done
1947
1948 # We empty the variable reporting that all process ended
1949 eval "$1=''"
1950
1951 return $return_code
1952}
1953
1954
1955function test_wait_background() {
1956 local pids=""
1957 run_in_background pids bash -c "sleep 1; exit 1"
1958 run_in_background pids bash -c "sleep 2; exit 0"
1959 wait_background pids
1960 if [ $? -ne 1 ]; then return 1; fi
1961
1962 run_in_background pids bash -c "sleep 1; exit 0"
1963 run_in_background pids bash -c "sleep 2; exit 0"
1964 wait_background pids
1965 if [ $? -ne 0 ]; then return 1; fi
1966
1967 if [ ! -z "$pids" ]; then return 1; fi
1968}
1969
31f18b77
FG
1970function flush_pg_stats()
1971{
1972 local timeout=${1:-$TIMEOUT}
1973
1974 ids=`ceph osd ls`
1975 seqs=''
1976 for osd in $ids; do
1977 seq=`ceph tell osd.$osd flush_pg_stats`
1978 seqs="$seqs $osd-$seq"
1979 done
1980
1981 for s in $seqs; do
1982 osd=`echo $s | cut -d - -f 1`
1983 seq=`echo $s | cut -d - -f 2`
1984 echo "waiting osd.$osd seq $seq"
1985 while test $(ceph osd last-stat-seq $osd) -lt $seq; do
1986 sleep 1
1987 if [ $((timeout--)) -eq 0 ]; then
1988 return 1
1989 fi
1990 done
1991 done
1992}
1993
1994function test_flush_pg_stats()
1995{
1996 local dir=$1
1997
1998 setup $dir || return 1
1999 run_mon $dir a --osd_pool_default_size=1 || return 1
2000 run_mgr $dir x || return 1
2001 run_osd $dir 0 || return 1
c07f9fc5 2002 create_rbd_pool || return 1
31f18b77 2003 rados -p rbd put obj /etc/group
3a9019d9 2004 flush_pg_stats || return 1
31f18b77 2005 local jq_filter='.pools | .[] | select(.name == "rbd") | .stats'
11fdf7f2
TL
2006 stored=`ceph df detail --format=json | jq "$jq_filter.stored"`
2007 stored_raw=`ceph df detail --format=json | jq "$jq_filter.stored_raw"`
2008 test $stored -gt 0 || return 1
2009 test $stored == $stored_raw || return 1
b5b8bbf5 2010 teardown $dir
31f18b77
FG
2011}
2012
7c673cae
FG
2013#######################################################################
2014
2015##
2016# Call the **run** function (which must be defined by the caller) with
2017# the **dir** argument followed by the caller argument list.
2018#
2019# If the **run** function returns on error, all logs found in **dir**
2020# are displayed for diagnostic purposes.
2021#
2022# **teardown** function is called when the **run** function returns
2023# (on success or on error), to cleanup leftovers. The CEPH_CONF is set
2024# to /dev/null and CEPH_ARGS is unset so that the tests are protected from
2025# external interferences.
2026#
2027# It is the responsibility of the **run** function to call the
2028# **setup** function to prepare the test environment (create a temporary
2029# directory etc.).
2030#
2031# The shell is required (via PS4) to display the function and line
2032# number whenever a statement is executed to help debugging.
2033#
2034# @param dir directory in which all data is stored
2035# @param ... arguments passed transparently to **run**
2036# @return 0 on success, 1 on error
2037#
2038function main() {
2039 local dir=td/$1
2040 shift
2041
2042 shopt -s -o xtrace
2043 PS4='${BASH_SOURCE[0]}:$LINENO: ${FUNCNAME[0]}: '
2044
11fdf7f2
TL
2045 export PATH=.:$PATH # make sure program from sources are preferred
2046 export PYTHONWARNINGS=ignore
7c673cae
FG
2047 export CEPH_CONF=/dev/null
2048 unset CEPH_ARGS
2049
2050 local code
2051 if run $dir "$@" ; then
2052 code=0
2053 else
7c673cae
FG
2054 code=1
2055 fi
b5b8bbf5 2056 teardown $dir $code || return 1
7c673cae
FG
2057 return $code
2058}
2059
2060#######################################################################
2061
2062function run_tests() {
2063 shopt -s -o xtrace
2064 PS4='${BASH_SOURCE[0]}:$LINENO: ${FUNCNAME[0]}: '
2065
11fdf7f2 2066 export .:$PATH # make sure program from sources are preferred
7c673cae
FG
2067
2068 export CEPH_MON="127.0.0.1:7109" # git grep '\<7109\>' : there must be only one
2069 export CEPH_ARGS
b5b8bbf5 2070 CEPH_ARGS+=" --fsid=$(uuidgen) --auth-supported=none "
7c673cae
FG
2071 CEPH_ARGS+="--mon-host=$CEPH_MON "
2072 export CEPH_CONF=/dev/null
2073
2074 local funcs=${@:-$(set | sed -n -e 's/^\(test_[0-9a-z_]*\) .*/\1/p')}
2075 local dir=td/ceph-helpers
2076
2077 for func in $funcs ; do
b5b8bbf5
FG
2078 if ! $func $dir; then
2079 teardown $dir 1
2080 return 1
2081 fi
7c673cae
FG
2082 done
2083}
2084
2085if test "$1" = TESTS ; then
2086 shift
2087 run_tests "$@"
b5b8bbf5 2088 exit $?
7c673cae
FG
2089fi
2090
224ce89b
WB
2091# NOTE:
2092# jq only support --exit-status|-e from version 1.4 forwards, which makes
2093# returning on error waaaay prettier and straightforward.
2094# However, the current automated upstream build is running with v1.3,
2095# which has no idea what -e is. Hence the convoluted error checking we
2096# need. Sad.
2097# The next time someone changes this code, please check if v1.4 is now
2098# a thing, and, if so, please change these to use -e. Thanks.
2099
2100# jq '.all.supported | select([.[] == "foo"] | any)'
2101function jq_success() {
2102 input="$1"
2103 filter="$2"
2104 expects="\"$3\""
2105
2106 in_escaped=$(printf %s "$input" | sed "s/'/'\\\\''/g")
2107 filter_escaped=$(printf %s "$filter" | sed "s/'/'\\\\''/g")
2108
2109 ret=$(echo "$in_escaped" | jq "$filter_escaped")
2110 if [[ "$ret" == "true" ]]; then
2111 return 0
2112 elif [[ -n "$expects" ]]; then
2113 if [[ "$ret" == "$expects" ]]; then
2114 return 0
2115 fi
2116 fi
2117 return 1
2118 input=$1
2119 filter=$2
2120 expects="$3"
2121
2122 ret="$(echo $input | jq \"$filter\")"
2123 if [[ "$ret" == "true" ]]; then
2124 return 0
2125 elif [[ -n "$expects" && "$ret" == "$expects" ]]; then
2126 return 0
2127 fi
2128 return 1
2129}
2130
b5b8bbf5
FG
2131function inject_eio() {
2132 local pooltype=$1
2133 shift
2134 local which=$1
2135 shift
2136 local poolname=$1
2137 shift
2138 local objname=$1
2139 shift
2140 local dir=$1
2141 shift
2142 local shard_id=$1
2143 shift
2144
2145 local -a initial_osds=($(get_osds $poolname $objname))
2146 local osd_id=${initial_osds[$shard_id]}
2147 if [ "$pooltype" != "ec" ]; then
2148 shard_id=""
2149 fi
eafe8130
TL
2150 type=$(cat $dir/$osd_id/type)
2151 set_config osd $osd_id ${type}_debug_inject_read_err true || return 1
b5b8bbf5
FG
2152 local loop=0
2153 while ( CEPH_ARGS='' ceph --admin-daemon $(get_asok_path osd.$osd_id) \
2154 inject${which}err $poolname $objname $shard_id | grep -q Invalid ); do
2155 loop=$(expr $loop + 1)
2156 if [ $loop = "10" ]; then
2157 return 1
2158 fi
2159 sleep 1
2160 done
2161}
2162
1adf2230
AA
2163function multidiff() {
2164 if ! diff $@ ; then
2165 if [ "$DIFFCOLOPTS" = "" ]; then
2166 return 1
2167 fi
2168 diff $DIFFCOLOPTS $@
2169 fi
2170}
2171
eafe8130
TL
2172function create_ec_pool() {
2173 local pool_name=$1
2174 shift
2175 local allow_overwrites=$1
2176 shift
2177
2178 ceph osd erasure-code-profile set myprofile crush-failure-domain=osd "$@" || return 1
2179
2180 create_pool "$poolname" 1 1 erasure myprofile || return 1
2181
2182 if [ "$allow_overwrites" = "true" ]; then
2183 ceph osd pool set "$poolname" allow_ec_overwrites true || return 1
2184 fi
2185
2186 wait_for_clean || return 1
2187 return 0
2188}
2189
7c673cae 2190# Local Variables:
c07f9fc5 2191# compile-command: "cd ../../src ; make -j4 && ../qa/standalone/ceph-helpers.sh TESTS # test_get_config"
7c673cae 2192# End: