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