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