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