]> git.proxmox.com Git - ceph.git/blame - ceph/src/test/test_common.sh
buildsys: switch source download to quincy
[ceph.git] / ceph / src / test / test_common.sh
CommitLineData
11fdf7f2
TL
1#!/usr/bin/env bash
2set -x
7c673cae
FG
3
4#
5# test_common.sh
6#
7# Common routines for tests
8#
9#
10# Environment variables that affect tests:
11# KEEP_TEMPDIR If set, the tempdir will not be deleted
12# when the test is over.
13#
14
15# Clean up the temporary directory
16cleanup() {
17 if [ -n ${TEMPDIR} ]; then
18 rm -rf "${TEMPDIR}"
19 fi
20}
21
22# Create a temporary directory where test files will be stored.
23setup_tempdir() {
24 TEMPDIR=`mktemp -d`
25 if [ -z $KEEP_TEMPDIR ]; then
26 trap cleanup INT TERM EXIT
27 fi
28}
29
30# Standard initialization function for tests
31init() {
32 setup_tempdir
33 cd `dirname $0`/..
34}
35
36# Exit with an error message.
37die() {
38 echo $@
39 exit 1
40}
41
42# Test that flag is set (the element is found in the list)
43is_set()
44{
45 local flag=$1; shift
46 local flags="$@"
47 local i
48
49 for i in ${flags}; do
50 if [ "${flag}" = "${i}" ]; then
51 return 0
52 fi
53 done
54 return 1
55}
56
57# Stop an OSD started by vstart
58stop_osd() {
59 osd_index=$1
60 pidfile="out/osd.$osd_index.pid"
61 if [ -e $pidfile ]; then
62 if kill `cat $pidfile` ; then
63 poll_cmd "eval test -e $pidfile ; echo \$?" "1" 1 30
64 [ $? -eq 1 ] && return 0
65 echo "ceph-osd process did not terminate correctly"
66 else
67 echo "kill `cat $pidfile` failed"
68 fi
69 else
70 echo "ceph-osd process $osd_index is not running"
71 fi
72 return 1
73}
74
75# Restart an OSD started by vstart
76restart_osd() {
77 osd_index=$1
78 ./ceph-osd -i $osd_index -c ceph.conf &
79}
80
81# Ask the user a yes/no question and get the response
82yes_or_no_choice() {
83 while true; do
84 echo -n "${1} [y/n] "
85 read ans
86 case "${ans}" in
87 y|Y|yes|YES|Yes) return 0 ;;
88 n|N|no|NO|No) return 1 ;;
89 *) echo "Please type yes or no."
90 echo ;;
91 esac
92 done
93}
94
95# Block until the user says "continue" or "c"
96continue_prompt() {
97 prompt=${1:-"to go on"}
98 while true; do
99 echo "Please type 'c' or 'continue' ${prompt}."
100 read ans
101 case "${ans}" in
102 c|continue) return 0 ;;
103 *) echo ;;
104 esac
105 done
106}
107
108# Write a bunch of objects to rados
109write_objects() {
110 start_ver=$1
111 stop_ver=$2
112 num_objs=$3
113 obj_size=$4
114 pool=$5
115 [ -d "${TEMPDIR}" ] || die "must setup_tempdir"
116 for v in `seq $start_ver $stop_ver`; do
117 chr=`perl -e "print chr(48+$v)"`
118 head -c $obj_size /dev/zero | tr '\0' "$chr" > $TEMPDIR/ver$v
119 for i in `seq -w 1 $num_objs`; do
120 ./rados -c ./ceph.conf -p $pool put obj$i $TEMPDIR/ver$v || die "radostool failed"
121 done
122 done
123}
124
125read_objects() {
126 ver=$1
127 num_objs=$2
128 obj_size=$3
129 [ -d "${TEMPDIR}" ] || die "must setup_tempdir"
130 chr=`perl -e "print chr(48+$ver)"`
131 head -c $obj_size /dev/zero | tr '\0' "$chr" > $TEMPDIR/exemplar
132 for i in `seq -w 1 $num_objs`; do
133 ./rados -c ./ceph.conf -p $pool get obj$i $TEMPDIR/out$i || die "radostool failed"
134 cmp $TEMPDIR/out$i $TEMPDIR/exemplar || die "got back incorrect obj$i"
135 done
136}
137
138poll_cmd() {
139 command=$1
140 search_str=$2
141 polling_interval=$3
142 total_time=$4
143
144 t=0
145 while [ $t -lt $total_time ]; do
146 $command | grep "$search_str"
147 [ $? -eq 0 ] && return 1
148 sleep $polling_interval
149 t=$(($t+$polling_interval))
150 done
151
152 return 0
153}
154
155dump_osd_store() {
156 set +x
157 echo "dumping osd store..."
158 find ./dev/osd* -type f | grep obj | grep head$ | sort | while read file; do
159 echo $file
160 head -c 10 $file
161 echo
162 done
163}
164
165start_recovery() {
166 CEPH_NUM_OSD=$1
167 osd=0
168 while [ $osd -lt $CEPH_NUM_OSD ]; do
169 ./ceph -c ./ceph.conf tell osd.$osd debug kick_recovery_wq 0
170 osd=$((osd+1))
171 done
172}
173
174init