Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / qa / workunits / rbd / test_admin_socket.sh
1 #!/bin/bash -ex
2
3 TMPDIR=/tmp/rbd_test_admin_socket$$
4 mkdir $TMPDIR
5 trap "rm -fr $TMPDIR" 0
6
7 . $(dirname $0)/../../standalone/ceph-helpers.sh
8
9 function expect_false()
10 {
11     set -x
12     if "$@"; then return 1; else return 0; fi
13 }
14
15 function rbd_watch_out_file()
16 {
17     echo ${TMPDIR}/rbd_watch_$1.out
18 }
19
20 function rbd_watch_pid_file()
21 {
22     echo ${TMPDIR}/rbd_watch_$1.pid
23 }
24
25 function rbd_watch_fifo()
26 {
27     echo ${TMPDIR}/rbd_watch_$1.fifo
28 }
29
30 function rbd_watch_asok()
31 {
32     echo ${TMPDIR}/rbd_watch_$1.asok
33 }
34
35 function rbd_get_perfcounter()
36 {
37     local image=$1
38     local counter=$2
39     local name
40
41     name=$(ceph --format xml --admin-daemon $(rbd_watch_asok ${image}) \
42                 perf schema | $XMLSTARLET el -d3 |
43                   grep "/librbd-.*-${image}/${counter}\$")
44     test -n "${name}" || return 1
45
46     ceph --format xml --admin-daemon $(rbd_watch_asok ${image}) perf dump |
47         $XMLSTARLET sel -t -m "${name}" -v .
48 }
49
50 function rbd_check_perfcounter()
51 {
52     local image=$1
53     local counter=$2
54     local expected_val=$3
55     local val=
56
57     val=$(rbd_get_perfcounter ${image} ${counter})
58
59     test "${val}" -eq "${expected_val}"
60 }
61
62 function rbd_watch_start()
63 {
64     local image=$1
65     local asok=$(rbd_watch_asok ${image})
66
67     mkfifo $(rbd_watch_fifo ${image})
68     (cat $(rbd_watch_fifo ${image}) |
69             rbd --admin-socket ${asok} watch ${image} \
70                 > $(rbd_watch_out_file ${image}) 2>&1)&
71
72     # find pid of the started rbd watch process
73     local pid
74     for i in `seq 10`; do
75         pid=$(ps auxww | awk "/[r]bd --admin.* watch ${image}/ {print \$2}")
76         test -n "${pid}" && break
77         sleep 0.1
78     done
79     test -n "${pid}"
80     echo ${pid} > $(rbd_watch_pid_file ${image})
81
82     # find watcher admin socket
83     test -n "${asok}"
84     for i in `seq 10`; do
85         test -S "${asok}" && break
86         sleep 0.1
87     done
88     test -S "${asok}"
89
90     # configure debug level
91     ceph --admin-daemon "${asok}" config set debug_rbd 20
92
93     # check that watcher is registered
94     rbd status ${image} | expect_false grep "Watchers: none"
95 }
96
97 function rbd_watch_end()
98 {
99     local image=$1
100     local regexp=$2
101
102     # send 'enter' to watch to exit
103     echo > $(rbd_watch_fifo ${image})
104     # just in case it is not terminated
105     kill $(cat $(rbd_watch_pid_file ${image})) || :
106
107     # output rbd watch out file for easier troubleshooting
108     cat $(rbd_watch_out_file ${image})
109
110     # cleanup
111     rm -f $(rbd_watch_fifo ${image}) $(rbd_watch_pid_file ${image}) \
112        $(rbd_watch_out_file ${image}) $(rbd_watch_asok ${image})
113 }
114
115 wait_for_clean
116
117 pool="rbd"
118 image=testimg$$
119 ceph_admin="ceph --admin-daemon $(rbd_watch_asok ${image})"
120
121 rbd create --size 128 ${pool}/${image}
122
123 # check rbd cache commands are present in help output
124 rbd_cache_flush="rbd cache flush ${pool}/${image}"
125 rbd_cache_invalidate="rbd cache invalidate ${pool}/${image}"
126
127 rbd_watch_start ${image}
128 ${ceph_admin} help | fgrep "${rbd_cache_flush}"
129 ${ceph_admin} help | fgrep "${rbd_cache_invalidate}"
130 rbd_watch_end ${image}
131
132 # test rbd cache commands with disabled and enabled cache
133 for conf_rbd_cache in false true; do
134
135     rbd image-meta set ${image} conf_rbd_cache ${conf_rbd_cache}
136
137     rbd_watch_start ${image}
138
139     rbd_check_perfcounter ${image} flush 0
140     ${ceph_admin} ${rbd_cache_flush}
141     # 'flush' counter should increase regardless if cache is enabled
142     rbd_check_perfcounter ${image} flush 1
143
144     rbd_check_perfcounter ${image} invalidate_cache 0
145     ${ceph_admin} ${rbd_cache_invalidate}
146     # 'invalidate_cache' counter should increase regardless if cache is enabled
147     rbd_check_perfcounter ${image} invalidate_cache 1
148
149     rbd_watch_end ${image}
150 done
151
152 rbd rm ${image}