Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / qa / standalone / mon / mkfs.sh
1 #!/bin/bash
2 #
3 # Copyright (C) 2013 Cloudwatt <libre.licensing@cloudwatt.com>
4 # Copyright (C) 2014 Red Hat <contact@redhat.com>
5 #
6 # Author: Loic Dachary <loic@dachary.org>
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU Library Public License as published by
10 # the Free Software Foundation; either version 2, or (at your option)
11 # any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU Library Public License for more details.
17 #
18 set -xe
19 PS4='${BASH_SOURCE[0]}:$LINENO: ${FUNCNAME[0]}:  '
20
21
22 DIR=mkfs
23 export CEPH_CONF=/dev/null
24 unset CEPH_ARGS
25 MON_ID=a
26 MON_DIR=$DIR/$MON_ID
27 CEPH_MON=127.0.0.1:7110 # git grep '\<7110\>' : there must be only one
28 TIMEOUT=360
29
30 EXTRAOPTS=""
31 if [ -n "$CEPH_LIB" ]; then
32     EXTRAOPTS+=" --erasure-code-dir $CEPH_LIB"
33     EXTRAOPTS+=" --plugin-dir $CEPH_LIB"
34     EXTRAOPTS+=" --osd-class-dir $CEPH_LIB"
35 fi
36
37 function setup() {
38     teardown
39     mkdir $DIR
40 }
41
42 function teardown() {
43     kill_daemons
44     rm -fr $DIR
45 }
46
47 function mon_mkfs() {
48     local fsid=$(uuidgen)
49
50     ceph-mon \
51         --id $MON_ID \
52         --fsid $fsid \
53         $EXTRAOPTS \
54         --mkfs \
55         --mon-data=$MON_DIR \
56         --mon-initial-members=$MON_ID \
57         --mon-host=$CEPH_MON \
58         "$@"
59 }
60
61 function mon_run() {
62     ceph-mon \
63         --id $MON_ID \
64         --chdir= \
65         --mon-osd-full-ratio=.99 \
66         --mon-data-avail-crit=1 \
67         $EXTRAOPTS \
68         --mon-data=$MON_DIR \
69         --log-file=$MON_DIR/log \
70         --mon-cluster-log-file=$MON_DIR/log \
71         --run-dir=$MON_DIR \
72         --pid-file=$MON_DIR/pidfile \
73         --public-addr $CEPH_MON \
74         "$@"
75 }
76
77 function kill_daemons() {
78     for pidfile in $(find $DIR -name pidfile) ; do
79         pid=$(cat $pidfile)
80         for try in 0 1 1 1 2 3 ; do
81             kill $pid || break
82             sleep $try
83         done
84     done
85 }
86
87 function auth_none() {
88     mon_mkfs --auth-supported=none
89
90     ceph-mon \
91         --id $MON_ID \
92         --mon-osd-full-ratio=.99 \
93         --mon-data-avail-crit=1 \
94         $EXTRAOPTS \
95         --mon-data=$MON_DIR \
96         --extract-monmap $MON_DIR/monmap
97
98     [ -f $MON_DIR/monmap ] || return 1
99
100     [ ! -f $MON_DIR/keyring ] || return 1
101
102     mon_run --auth-supported=none
103
104     timeout $TIMEOUT ceph --mon-host $CEPH_MON mon stat || return 1
105 }
106
107 function auth_cephx_keyring() {
108     cat > $DIR/keyring <<EOF
109 [mon.]
110         key = AQDUS79S0AF9FRAA2cgRLFscVce0gROn/s9WMg==
111         caps mon = "allow *"
112 EOF
113
114     mon_mkfs --keyring=$DIR/keyring
115
116     [ -f $MON_DIR/keyring ] || return 1
117
118     mon_run
119
120     timeout $TIMEOUT ceph \
121         --name mon. \
122         --keyring $MON_DIR/keyring \
123         --mon-host $CEPH_MON mon stat || return 1
124 }
125
126 function auth_cephx_key() {
127     if [ -f /etc/ceph/keyring ] ; then
128         echo "Please move /etc/ceph/keyring away for testing!"
129         return 1
130     fi
131
132     local key=$(ceph-authtool --gen-print-key)
133
134     if mon_mkfs --key='corrupted key' ; then
135         return 1
136     else
137         rm -fr $MON_DIR/store.db
138         rm -fr $MON_DIR/kv_backend
139     fi
140
141     mon_mkfs --key=$key
142
143     [ -f $MON_DIR/keyring ] || return 1
144     grep $key $MON_DIR/keyring
145
146     mon_run
147
148     timeout $TIMEOUT ceph \
149         --name mon. \
150         --keyring $MON_DIR/keyring \
151         --mon-host $CEPH_MON mon stat || return 1
152 }
153
154 function makedir() {
155     local toodeep=$MON_DIR/toodeep
156
157     # fail if recursive directory creation is needed
158     ceph-mon \
159         --id $MON_ID \
160         --mon-osd-full-ratio=.99 \
161         --mon-data-avail-crit=1 \
162         $EXTRAOPTS \
163         --mkfs \
164         --mon-data=$toodeep 2>&1 | tee $DIR/makedir.log
165     grep 'toodeep.*No such file' $DIR/makedir.log > /dev/null
166     rm $DIR/makedir.log
167
168     # an empty directory does not mean the mon exists
169     mkdir $MON_DIR
170     mon_mkfs --auth-supported=none 2>&1 | tee $DIR/makedir.log
171     ! grep "$MON_DIR already exists" $DIR/makedir.log || return 1
172 }
173
174 function idempotent() {
175     mon_mkfs --auth-supported=none
176     mon_mkfs --auth-supported=none 2>&1 | tee $DIR/makedir.log
177     grep "'$MON_DIR' already exists" $DIR/makedir.log > /dev/null || return 1
178 }
179
180 function run() {
181     local actions
182     actions+="makedir "
183     actions+="idempotent "
184     actions+="auth_cephx_key "
185     actions+="auth_cephx_keyring "
186     actions+="auth_none "
187     for action in $actions  ; do
188         setup
189         $action || return 1
190         teardown
191     done
192 }
193
194 run
195
196 # Local Variables:
197 # compile-command: "cd ../.. ; make TESTS=test/mon/mkfs.sh check"
198 # End: