Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / qa / workunits / rbd / journal.sh
1 #!/bin/bash -e
2
3 . $(dirname $0)/../../standalone/ceph-helpers.sh
4
5 function list_tests()
6 {
7   echo "AVAILABLE TESTS"
8   for i in $TESTS; do
9     echo "  $i"
10   done
11 }
12
13 function usage()
14 {
15   echo "usage: $0 [-h|-l|-t <testname> [-t <testname>...] [--no-sanity-check] [--no-cleanup]]"
16 }
17
18 function expect_false()
19 {
20     set -x
21     if "$@"; then return 1; else return 0; fi
22 }
23
24 function save_commit_position()
25 {
26     local journal=$1
27
28     rados -p rbd getomapval journal.${journal} client_ \
29           $TMPDIR/${journal}.client_.omap
30 }
31
32 function restore_commit_position()
33 {
34     local journal=$1
35
36     rados -p rbd setomapval journal.${journal} client_ \
37           < $TMPDIR/${journal}.client_.omap
38 }
39
40 test_rbd_journal()
41 {
42     local image=testrbdjournal$$
43
44     rbd create --image-feature exclusive-lock --image-feature journaling \
45         --size 128 ${image}
46     local journal=$(rbd info ${image} --format=xml 2>/dev/null |
47                            $XMLSTARLET sel -t -v "//image/journal")
48     test -n "${journal}"
49     rbd journal info ${journal}
50     rbd journal info --journal ${journal}
51     rbd journal info --image ${image}
52
53     rbd feature disable ${image} journaling
54
55     rbd info ${image} --format=xml 2>/dev/null |
56         expect_false $XMLSTARLET sel -t -v "//image/journal"
57     expect_false rbd journal info ${journal}
58     expect_false rbd journal info --image ${image}
59
60     rbd feature enable ${image} journaling
61
62     local journal1=$(rbd info ${image} --format=xml 2>/dev/null |
63                             $XMLSTARLET sel -t -v "//image/journal")
64     test "${journal}" = "${journal1}"
65
66     rbd journal info ${journal}
67
68     rbd journal status ${journal}
69
70     local count=10
71     save_commit_position ${journal}
72     rbd bench-write ${image} --io-size 4096 --io-threads 1 \
73         --io-total $((4096 * count)) --io-pattern seq
74     rbd journal status --image ${image} | fgrep "tid=$((count - 1))"
75     restore_commit_position ${journal}
76     rbd journal status --image ${image} | fgrep "positions=[]"
77     local count1=$(rbd journal inspect --verbose ${journal} |
78                           grep -c 'event_type.*AioWrite')
79     test "${count}" -eq "${count1}"
80
81     rbd journal export ${journal} $TMPDIR/journal.export
82     local size=$(stat -c "%s" $TMPDIR/journal.export)
83     test "${size}" -gt 0
84
85     rbd export ${image} $TMPDIR/${image}.export
86
87     local image1=${image}1
88     rbd create --image-feature exclusive-lock --image-feature journaling \
89         --size 128 ${image1}
90     journal1=$(rbd info ${image1} --format=xml 2>/dev/null |
91                       $XMLSTARLET sel -t -v "//image/journal")
92
93     save_commit_position ${journal1}
94     rbd journal import --dest ${image1} $TMPDIR/journal.export
95     rbd snap create ${image1}@test
96     restore_commit_position ${journal1}
97     # check that commit position is properly updated: the journal should contain
98     # 12 entries (10 AioWrite + 1 SnapCreate + 1 OpFinish) and commit
99     # position set to tid=11
100     rbd journal inspect --image ${image1} --verbose | awk '
101       /AioWrite/          {w++}         # match: "event_type": "AioWrite",
102       /SnapCreate/        {s++}         # match: "event_type": "SnapCreate",
103       /OpFinish/          {f++}         # match: "event_type": "OpFinish",
104       /entries inspected/ {t=$1; e=$4}  # match: 12 entries inspected, 0 errors
105                           {print}       # for diagnostic
106       END                 {
107         if (w != 10 || s != 1 || f != 1 || t != 12 || e != 0) exit(1)
108       }
109     '
110
111     rbd export ${image1}@test $TMPDIR/${image1}.export
112     cmp $TMPDIR/${image}.export $TMPDIR/${image1}.export
113
114     rbd journal reset ${journal}
115
116     rbd journal inspect --verbose ${journal} | expect_false grep 'event_type'
117
118     rbd snap purge ${image1}
119     rbd remove ${image1}
120     rbd remove ${image}
121 }
122
123
124 rbd_assert_eq() {
125     local image=$1
126     local cmd=$2
127     local param=$3
128     local expected_val=$4
129
130     local val=$(rbd --format xml ${cmd} --image ${image} |
131                        $XMLSTARLET sel -t -v "${param}")
132     test "${val}" = "${expected_val}"
133 }
134
135 test_rbd_create()
136 {
137     local image=testrbdcreate$$
138
139     rbd create --image-feature exclusive-lock --image-feature journaling \
140         --journal-pool rbd \
141         --journal-object-size 20M \
142         --journal-splay-width 6 \
143         --size 256 ${image}
144
145     rbd_assert_eq ${image} 'journal info' '//journal/order' 25
146     rbd_assert_eq ${image} 'journal info' '//journal/splay_width' 6
147     rbd_assert_eq ${image} 'journal info' '//journal/object_pool' rbd
148
149     rbd remove ${image}
150 }
151
152 test_rbd_copy()
153 {
154     local src=testrbdcopys$$
155     rbd create --size 256 ${src}
156
157     local image=testrbdcopy$$
158     rbd copy --image-feature exclusive-lock --image-feature journaling \
159         --journal-pool rbd \
160         --journal-object-size 20M \
161         --journal-splay-width 6 \
162         ${src} ${image}
163
164     rbd remove ${src}
165
166     rbd_assert_eq ${image} 'journal info' '//journal/order' 25
167     rbd_assert_eq ${image} 'journal info' '//journal/splay_width' 6
168     rbd_assert_eq ${image} 'journal info' '//journal/object_pool' rbd
169
170     rbd remove ${image}
171 }
172
173 test_rbd_clone()
174 {
175     local parent=testrbdclonep$$
176     rbd create --image-feature layering --size 256 ${parent}
177     rbd snap create ${parent}@snap
178     rbd snap protect ${parent}@snap
179
180     local image=testrbdclone$$
181     rbd clone --image-feature layering --image-feature exclusive-lock --image-feature journaling \
182         --journal-pool rbd \
183         --journal-object-size 20M \
184         --journal-splay-width 6 \
185         ${parent}@snap ${image}
186
187     rbd_assert_eq ${image} 'journal info' '//journal/order' 25
188     rbd_assert_eq ${image} 'journal info' '//journal/splay_width' 6
189     rbd_assert_eq ${image} 'journal info' '//journal/object_pool' rbd
190
191     rbd remove ${image}
192     rbd snap unprotect ${parent}@snap
193     rbd snap purge ${parent}
194     rbd remove ${parent}
195 }
196
197 test_rbd_import()
198 {
199     local src=testrbdimports$$
200     rbd create --size 256 ${src}
201
202     rbd export ${src} $TMPDIR/${src}.export
203     rbd remove ${src}
204
205     local image=testrbdimport$$
206     rbd import --image-feature exclusive-lock --image-feature journaling \
207         --journal-pool rbd \
208         --journal-object-size 20M \
209         --journal-splay-width 6 \
210         $TMPDIR/${src}.export ${image}
211
212     rbd_assert_eq ${image} 'journal info' '//journal/order' 25
213     rbd_assert_eq ${image} 'journal info' '//journal/splay_width' 6
214     rbd_assert_eq ${image} 'journal info' '//journal/object_pool' rbd
215
216     rbd remove ${image}
217 }
218
219 test_rbd_feature()
220 {
221     local image=testrbdfeature$$
222
223     rbd create --image-feature exclusive-lock --size 256 ${image}
224
225     rbd feature enable ${image} journaling \
226         --journal-pool rbd \
227         --journal-object-size 20M \
228         --journal-splay-width 6
229
230     rbd_assert_eq ${image} 'journal info' '//journal/order' 25
231     rbd_assert_eq ${image} 'journal info' '//journal/splay_width' 6
232     rbd_assert_eq ${image} 'journal info' '//journal/object_pool' rbd
233
234     rbd remove ${image}
235 }
236
237 TESTS+=" rbd_journal"
238 TESTS+=" rbd_create"
239 TESTS+=" rbd_copy"
240 TESTS+=" rbd_clone"
241 TESTS+=" rbd_import"
242 TESTS+=" rbd_feature"
243
244 #
245 # "main" follows
246 #
247
248 tests_to_run=()
249
250 sanity_check=true
251 cleanup=true
252
253 while [[ $# -gt 0 ]]; do
254     opt=$1
255
256     case "$opt" in
257         "-l" )
258             do_list=1
259             ;;
260         "--no-sanity-check" )
261             sanity_check=false
262             ;;
263         "--no-cleanup" )
264             cleanup=false
265             ;;
266         "-t" )
267             shift
268             if [[ -z "$1" ]]; then
269                 echo "missing argument to '-t'"
270                 usage ;
271                 exit 1
272             fi
273             tests_to_run+=" $1"
274             ;;
275         "-h" )
276             usage ;
277             exit 0
278             ;;
279     esac
280     shift
281 done
282
283 if [[ $do_list -eq 1 ]]; then
284     list_tests ;
285     exit 0
286 fi
287
288 TMPDIR=/tmp/rbd_journal$$
289 mkdir $TMPDIR
290 if $cleanup; then
291     trap "rm -fr $TMPDIR" 0
292 fi
293
294 if test -z "$tests_to_run" ; then
295     tests_to_run="$TESTS"
296 fi
297
298 for i in $tests_to_run; do
299     if $sanity_check ; then
300         wait_for_clean
301     fi
302     set -x
303     test_${i}
304     set +x
305 done
306 if $sanity_check ; then
307     wait_for_clean
308 fi
309
310 echo OK