3 # Copyright (C) 2015 Ubuntu Kylin
5 # Author: Min Chen <minchen@ubuntukylin.com>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU Library Public License as published by
9 # the Free Software Foundation; either version 2, or (at your option)
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Library Public License for more details.
18 # unit test case for rbd-recover-tool
21 # - write config files: config/osd_host, config/mon_host, config/storage_path, config/mds_host if exist mds
22 #step 1. rbd export all images as you need
23 #step 2. stop all ceph services
24 #step 3. use ceph_rbd_recover_tool to recover all images
25 #step 4. compare md5sum of recover image with that of export image who has the same image name
27 ssh_opt="-o ConnectTimeout=1"
28 my_dir=$(dirname "$0")
31 #storage_path=$my_dir/config/storage_path
32 mon_host=$my_dir/config/mon_host
33 osd_host=$my_dir/config/osd_host
34 mds_host=$my_dir/config/mds_host
36 test_dir= # `cat $storage_path`
37 export_dir= #$test_dir/export
38 recover_dir= #$test_dir/recover
39 image_names= #$test_dir/image_names
40 online_images= #$test_dir/online_images, all images on ceph rbd pool
41 gen_db= #$test_dir/gen_db, label database if exist
45 function get_pool_id()
47 local pool_id_file=/tmp/pool_id_file.$$$$
48 ceph osd pool stats $pool|head -n 1|awk '{print $4}' >$pool_id_file
50 echo "$func: get pool id failed: pool = $pool"
54 pool_id=`cat $pool_id_file`
55 echo "$func: pool_id = $pool_id"
63 echo "$func: must input <path> to storage images, enough disk space is good"
66 if [ ! -s $osd_host ];then
67 echo "$func: config/osd_host not exists or empty"
70 if [ ! -s $mon_host ];then
71 echo "$func: config/mon_host not exists or empty"
74 if [ ! -e $mds_host ];then
75 echo "$func: config/mds_host not exists"
79 export_dir=$test_dir/export
80 recover_dir=$test_dir/recover
81 image_names=$test_dir/image_names
82 online_images=$test_dir/online_images
83 gen_db=$test_dir/gen_db
85 trap 'echo "ceph cluster is stopped ..."; exit;' INT
96 function do_gen_database()
98 local func="do_gen_database"
99 if [ -s $gen_db ] && [ `cat $gen_db` = 1 ];then
100 echo "$func: database already existed"
103 bash $tool_dir/rbd-recover-tool database
107 #check if all ceph processes are stopped
108 function check_ceph_service()
110 local func="check_ceph_service"
111 local res=`cat $osd_host $mon_host $mds_host|sort -u|tr -d [:blank:]|xargs -n 1 -I @ ssh $ssh_opt @ "ps aux|grep -E \"(ceph-osd|ceph-mon|ceph-mds)\"|grep -v grep"`
112 if [ "$res"x != ""x ];then
113 echo "$func: NOT all ceph services are stopped"
117 echo "$func: all ceph services are stopped"
123 local func="stop_ceph"
124 #cat osd_host|xargs -n 1 -I @ ssh $ssh_opt @ "killall ceph-osd"
128 osd=`echo $osd|tr -d [:blank:]`
129 if [ "$osd"x = ""x ];then
132 #ssh $ssh_opt $osd "killall ceph-osd ceph-mon ceph-mds" </dev/null
133 ssh $ssh_opt $osd "killall ceph-osd" </dev/null
137 echo "waiting kill all osd ..."
139 #cat $mon_host|xargs -n 1 -I @ ssh $ssh_opt @ "killall ceph-mon ceph-osd ceph-mds"
140 cat $mon_host|xargs -n 1 -I @ ssh $ssh_opt @ "killall ceph-mon"
141 #cat $mds_host|xargs -n 1 -I @ ssh $ssh_opt @ "killall ceph-mds ceph-mon ceph-osd"
142 cat $mds_host|xargs -n 1 -I @ ssh $ssh_opt @ "killall ceph-mds"
145 function create_image()
147 local func="create_image"
148 if [ ${#} -lt 3 ];then
149 echo "create_image: parameters: <image_name> <size> <image_format>"
154 local image_format=$3
155 if [ $image_format -lt 1 ] || [ $image_format -gt 2 ];then
156 echo "$func: image_format must be 1 or 2"
159 local res=`rbd list|grep -E "^$1$"`
160 echo "$func $image_name ..."
161 if [ "$res"x = ""x ];then
162 rbd -p $pool create $image_name --size $size --image_format $image_format
164 if [ $image_format -eq 2 ];then
165 rbd snap ls $image_name|tail -n +2|awk '{print $2}'|xargs -n 1 -I % rbd snap unprotect $image_name@%
167 rbd snap purge $image_name
169 rbd -p $pool resize --allow-shrink --size $size $image_name
173 function export_image()
175 local func="export_image"
178 echo "$func: parameters: <image_name> <image_format> [<image_size>]"
184 local size=$(($3)) #MB
186 if [ $format -ne 1 ] && [ $format -ne 2 ];then
187 echo "$func: image format must be 1 or 2"
191 if [ $size -eq 0 ];then
193 echo "$func: size = $size"
197 mount |grep "rbd-fuse on /rbdfuse" &>/dev/null
202 create_image $image_name $size $format
204 dd conv=notrunc if=/dev/urandom of=$mnt/$image_name bs=4M count=$(($size/4))
206 local export_image_dir=$export_dir/pool_$pool_id/$image_name
207 mkdir -p $export_image_dir
208 local export_md5_nosnap=$export_image_dir/@md5_nosnap
211 local export_image_path=$export_image_dir/$image_name
212 rm -f $export_image_path
214 rbd export $pool/$image_name $export_image_path
215 md5sum $export_image_path |awk '{print $1}' >$export_md5_nosnap
218 function recover_image()
220 local func="recover_snapshots"
222 echo "$func: parameters: <image_name>"
229 local recover_image_dir=$recover_dir/pool_$pool_id/$image_name
230 mkdir -p $recover_image_dir
231 local recover_md5_nosnap=$recover_image_dir/@md5_nosnap
235 bash $tool_dir/rbd-recover-tool recover $pool_id/$image_name $recover_dir
236 md5sum $recover_image_dir/$image_name|awk '{print $1}' >$recover_md5_nosnap
239 function make_snapshot()
241 local func="make_snapshot"
243 echo "$func: parameters: <ofile> <seek> <count> <snap> <export_image_dir>"
250 local export_image_dir=$5
252 if [ $seek -lt 0 ];then
253 echo "$func: seek can not be minus"
257 if [ $count -lt 1 ];then
258 echo "$func: count must great than zero"
262 echo "[$snap] $func ..."
264 rbd snap ls $image_name|grep $snap;
267 if [ $res -eq 0 ];then
271 dd conv=notrunc if=/dev/urandom of=$ofile bs=1M count=$count seek=$seek 2>/dev/null
272 snapshot=$image_name@$snap
273 rbd snap create $snapshot
274 rm -f $export_image_dir/$snapshot
275 rbd export $pool/$image_name $export_image_dir/$snapshot
276 pushd $export_image_dir >/dev/null
277 md5sum $snapshot >> @md5
281 function recover_snapshots()
283 local func="recover_snapshots"
285 echo "$func: parameters: <image_name>"
292 local recover_image_dir=$recover_dir/pool_$pool_id/$image_name
293 mkdir -p $recover_image_dir
294 local recover_md5=$recover_image_dir/@md5
300 bash $tool_dir/rbd-recover-tool recover $pool_id/$image_name $recover_dir
303 for((i=1; i<10; i++))
306 bash $tool_dir/rbd-recover-tool recover $pool_id/$image_name@$snapshot $recover_dir
307 pushd $recover_image_dir >/dev/null
308 local chksum=`md5sum $image_name|awk '{print $1}'`
309 echo "$chksum $image_name@$snapshot" >>@md5
314 function export_snapshots()
316 local func="export_snapshots"
319 echo "$func: parameters: <image_name> <image_format> [<image_size>]"
325 local size=$(($3)) #MB
327 if [ $format -ne 1 ] && [ $format -ne 2 ];then
328 echo "$func: image format must be 1 or 2"
332 if [ $size -eq 0 ];then
334 echo "$func: size = $size"
338 mount |grep "rbd-fuse on /rbdfuse" &>/dev/null
343 create_image $image_name $size $format
345 local export_image_dir=$export_dir/pool_$pool_id/$image_name
346 mkdir -p $export_image_dir
347 local export_md5=$export_image_dir/@md5
351 # image = {object0, object1, object2, object3, object4, object5, ...}
353 # snap1 : init/write all objects
354 # snap2 : write object0
355 # snap3 : write object1
356 # snap4 : write object2
357 # snap5 : write object3
358 # snap6 : write object4
359 # snap7 : write object5
360 # snap8 : write object0
361 # snap9 : write object3
363 make_snapshot $mnt/$image_name 0 $size snap1 $export_image_dir
364 make_snapshot $mnt/$image_name 0 1 snap2 $export_image_dir
365 make_snapshot $mnt/$image_name 4 1 snap3 $export_image_dir
366 make_snapshot $mnt/$image_name 8 1 snap4 $export_image_dir
367 make_snapshot $mnt/$image_name 12 1 snap5 $export_image_dir
368 make_snapshot $mnt/$image_name 16 1 snap6 $export_image_dir
369 make_snapshot $mnt/$image_name 20 1 snap7 $export_image_dir
370 make_snapshot $mnt/$image_name 1 1 snap8 $export_image_dir
371 make_snapshot $mnt/$image_name 13 1 snap9 $export_image_dir
374 function check_recover_nosnap()
376 local func="check_recover_nosnap"
378 echo "$func: parameters: <export_md5_file> <recover_md5_file> <image_name>"
384 local ifpassed="FAILED"
386 echo "================ < $image_name nosnap > ================"
388 local export_md5sum=`cat $export_md5`
389 local recover_md5sum=`cat $recover_md5`
391 if [ "$export_md5sum"x != ""x ] && [ "$export_md5sum"x = "$recover_md5sum"x ];then
394 echo "export: $export_md5sum"
395 echo "recover: $recover_md5sum $ifpassed"
398 function check_recover_snapshots()
400 local func="check_recover_snapshots"
402 echo "$func: parameters: <export_md5_file> <recover_md5_file> <image_name>"
408 local ifpassed="FAILED"
410 echo "================ < $image_name snapshots > ================"
414 local export_md5s=(`cat $export_md5`)
415 local recover_md5s=(`cat $recover_md5`)
423 local export_arr=(`echo ${export_md5s[$i]}`)
424 local recover_arr=(`echo ${recover_md5s[$i]}`)
425 echo "export: ${export_md5s[$i]}"
426 if [ "${export_arr[1]}"x != ""x ] && [ "${export_arr[1]}"x = "${recover_arr[1]}"x ];then
429 echo "recover: ${recover_md5s[$i]} $ifpassed"
435 # step 1: export image, snapshot
436 function do_export_nosnap()
438 export_image image_v1_nosnap 1
439 export_image image_v2_nosnap 2
442 function do_export_snap()
444 export_snapshots image_v1_snap 1
445 export_snapshots image_v2_snap 2
448 # step 2: stop ceph cluster and gen database
449 function stop_cluster_gen_database()
451 trap 'echo stop ceph cluster failed; exit;' INT HUP
468 # step 3: recover image,snapshot
469 function do_recover_nosnap()
471 recover_image image_v1_nosnap
472 recover_image image_v2_nosnap
475 function do_recover_snap()
477 recover_snapshots image_v1_snap
478 recover_snapshots image_v2_snap
481 # step 4: check md5sum pair<export_md5sum, recover_md5sum>
482 function do_check_recover_nosnap()
484 local image1=image_v1_nosnap
485 local image2=image_v2_nosnap
487 local export_md5_1=$export_dir/pool_$pool_id/$image1/@md5_nosnap
488 local export_md5_2=$export_dir/pool_$pool_id/$image2/@md5_nosnap
489 local recover_md5_1=$recover_dir/pool_$pool_id/$image1/@md5_nosnap
490 local recover_md5_2=$recover_dir/pool_$pool_id/$image2/@md5_nosnap
492 check_recover_nosnap $export_md5_1 $recover_md5_1 $image1
493 check_recover_nosnap $export_md5_2 $recover_md5_2 $image2
496 function do_check_recover_snap()
498 local image1=image_v1_snap
499 local image2=image_v2_snap
501 local export_md5_1=$export_dir/pool_$pool_id/$image1/@md5
502 local export_md5_2=$export_dir/pool_$pool_id/$image2/@md5
503 local recover_md5_1=$recover_dir/pool_$pool_id/$image1/@md5
504 local recover_md5_2=$recover_dir/pool_$pool_id/$image2/@md5
506 check_recover_snapshots $export_md5_1 $recover_md5_1 $image1
507 check_recover_snapshots $export_md5_2 $recover_md5_2 $image2
510 function test_case_1()
513 stop_cluster_gen_database
515 do_check_recover_nosnap
518 function test_case_2()
521 stop_cluster_gen_database
523 do_check_recover_snap
526 function test_case_3()
531 stop_cluster_gen_database
536 do_check_recover_nosnap
537 do_check_recover_snap