Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / qa / standalone / mon / osd-crush.sh
1 #!/bin/bash
2 #
3 # Copyright (C) 2014 Cloudwatt <libre.licensing@cloudwatt.com>
4 # Copyright (C) 2014, 2015 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 source $CEPH_ROOT/qa/standalone/ceph-helpers.sh
19
20 function run() {
21     local dir=$1
22     shift
23
24     export CEPH_MON="127.0.0.1:7104" # git grep '\<7104\>' : there must be only one
25     export CEPH_ARGS
26     CEPH_ARGS+="--fsid=$(uuidgen) --auth-supported=none "
27     CEPH_ARGS+="--mon-host=$CEPH_MON "
28
29     local funcs=${@:-$(set | ${SED} -n -e 's/^\(TEST_[0-9a-z_]*\) .*/\1/p')}
30     for func in $funcs ; do
31         setup $dir || return 1
32         $func $dir || return 1
33         teardown $dir || return 1
34     done
35 }
36
37 function TEST_crush_rule_create_simple() {
38     local dir=$1
39
40     run_mon $dir a || return 1
41
42     ceph --format xml osd crush rule dump replicated_rule | \
43         egrep '<op>take</op><item>[^<]+</item><item_name>default</item_name>' | \
44         grep '<op>choose_firstn</op><num>0</num><type>osd</type>' || return 1
45     local ruleset=ruleset0
46     local root=host1
47     ceph osd crush add-bucket $root host
48     local failure_domain=osd
49     ceph osd crush rule create-simple $ruleset $root $failure_domain || return 1
50     ceph osd crush rule create-simple $ruleset $root $failure_domain 2>&1 | \
51         grep "$ruleset already exists" || return 1
52     ceph --format xml osd crush rule dump $ruleset | \
53         egrep '<op>take</op><item>[^<]+</item><item_name>'$root'</item_name>' | \
54         grep '<op>choose_firstn</op><num>0</num><type>'$failure_domain'</type>' || return 1
55     ceph osd crush rule rm $ruleset || return 1
56 }
57
58 function TEST_crush_rule_dump() {
59     local dir=$1
60
61     run_mon $dir a || return 1
62
63     local ruleset=ruleset1
64     ceph osd crush rule create-erasure $ruleset || return 1
65     test $(ceph --format json osd crush rule dump $ruleset | \
66            jq ".rule_name == \"$ruleset\"") == true || return 1
67     test $(ceph --format json osd crush rule dump | \
68            jq "map(select(.rule_name == \"$ruleset\")) | length == 1") == true || return 1
69     ! ceph osd crush rule dump non_existent_ruleset || return 1
70     ceph osd crush rule rm $ruleset || return 1
71 }
72
73 function TEST_crush_rule_rm() {
74     local ruleset=erasure2
75
76     run_mon $dir a || return 1
77
78     ceph osd crush rule create-erasure $ruleset default || return 1
79     ceph osd crush rule ls | grep $ruleset || return 1
80     ceph osd crush rule rm $ruleset || return 1
81     ! ceph osd crush rule ls | grep $ruleset || return 1
82 }
83
84 function TEST_crush_rule_create_erasure() {
85     local dir=$1
86
87     run_mon $dir a || return 1
88     # should have at least one OSD
89     run_osd $dir 0 || return 1
90
91     local ruleset=ruleset3
92     #
93     # create a new ruleset with the default profile, implicitly
94     #
95     ceph osd crush rule create-erasure $ruleset || return 1
96     ceph osd crush rule create-erasure $ruleset 2>&1 | \
97         grep "$ruleset already exists" || return 1
98     ceph --format xml osd crush rule dump $ruleset | \
99         egrep '<op>take</op><item>[^<]+</item><item_name>default</item_name>' | \
100         grep '<op>chooseleaf_indep</op><num>0</num><type>host</type>' || return 1
101     ceph osd crush rule rm $ruleset || return 1
102     ! ceph osd crush rule ls | grep $ruleset || return 1
103     #
104     # create a new ruleset with the default profile, explicitly
105     #
106     ceph osd crush rule create-erasure $ruleset default || return 1
107     ceph osd crush rule ls | grep $ruleset || return 1
108     ceph osd crush rule rm $ruleset || return 1
109     ! ceph osd crush rule ls | grep $ruleset || return 1
110     #
111     # create a new ruleset and the default profile, implicitly
112     #
113     ceph osd erasure-code-profile rm default || return 1
114     ! ceph osd erasure-code-profile ls | grep default || return 1
115     ceph osd crush rule create-erasure $ruleset || return 1
116     CEPH_ARGS='' ceph --admin-daemon $(get_asok_path mon.a) log flush || return 1
117     grep 'profile set default' $dir/mon.a.log || return 1
118     ceph osd erasure-code-profile ls | grep default || return 1
119     ceph osd crush rule rm $ruleset || return 1
120     ! ceph osd crush rule ls | grep $ruleset || return 1
121 }
122
123 function check_ruleset_id_match_rule_id() {
124     local rule_name=$1
125     rule_id=`ceph osd crush rule dump $rule_name | grep "\"rule_id\":" | awk -F ":|," '{print int($2)}'`
126     ruleset_id=`ceph osd crush rule dump $rule_name | grep "\"ruleset\":"| awk -F ":|," '{print int($2)}'`
127     test $ruleset_id = $rule_id || return 1
128 }
129
130 function generate_manipulated_rules() {
131     local dir=$1
132     ceph osd crush add-bucket $root host
133     ceph osd crush rule create-simple test_rule1 $root osd firstn || return 1
134     ceph osd crush rule create-simple test_rule2 $root osd firstn || return 1
135     ceph osd getcrushmap -o $dir/original_map
136     crushtool -d $dir/original_map -o $dir/decoded_original_map
137     #manipulate the rulesets , to make the rule_id != ruleset_id
138     ${SED} -i 's/ruleset 0/ruleset 3/' $dir/decoded_original_map
139     ${SED} -i 's/ruleset 2/ruleset 0/' $dir/decoded_original_map
140     ${SED} -i 's/ruleset 1/ruleset 2/' $dir/decoded_original_map
141
142     crushtool -c $dir/decoded_original_map -o $dir/new_map
143     ceph osd setcrushmap -i $dir/new_map
144
145     ceph osd crush rule dump
146 }
147
148 function TEST_crush_ruleset_match_rule_when_creating() {
149     local dir=$1
150
151     run_mon $dir a || return 1
152
153     local root=host1
154
155     generate_manipulated_rules $dir
156
157     ceph osd crush rule create-simple special_rule_simple $root osd firstn || return 1
158
159     ceph osd crush rule dump
160     #show special_rule_simple has same rule_id and ruleset_id
161     check_ruleset_id_match_rule_id special_rule_simple || return 1
162 }
163
164 function TEST_add_ruleset_failed() {
165     local dir=$1
166
167     run_mon $dir a || return 1
168
169     local root=host1
170
171     ceph osd crush add-bucket $root host
172     ceph osd crush rule create-simple test_rule1 $root osd firstn || return 1
173     ceph osd crush rule create-simple test_rule2 $root osd firstn || return 1
174     ceph osd getcrushmap > $dir/crushmap || return 1
175     crushtool --decompile $dir/crushmap > $dir/crushmap.txt || return 1
176     for i in $(seq 3 255)
177         do
178             cat <<EOF
179 rule test_rule$i {
180         ruleset $i
181         type replicated
182         min_size 1
183         max_size 10
184         step take $root
185         step choose firstn 0 type osd
186         step emit
187 }
188 EOF
189     done >> $dir/crushmap.txt
190     crushtool --compile $dir/crushmap.txt -o $dir/crushmap || return 1
191     ceph osd setcrushmap -i $dir/crushmap  || return 1
192     ceph osd crush rule create-simple test_rule_nospace $root osd firstn 2>&1 | grep "Error ENOSPC" || return 1
193
194 }
195
196 function TEST_crush_rename_bucket() {
197     local dir=$1
198
199     run_mon $dir a || return 1
200
201     ceph osd crush add-bucket host1 host
202     ceph osd tree
203     ! ceph osd tree | grep host2 || return 1
204     ceph osd crush rename-bucket host1 host2 || return 1
205     ceph osd tree
206     ceph osd tree | grep host2 || return 1
207     ceph osd crush rename-bucket host1 host2 || return 1 # idempotency
208     ceph osd crush rename-bucket nonexistent something 2>&1 | grep "Error ENOENT" || return 1
209 }
210
211 function TEST_crush_reject_empty() {
212     local dir=$1
213     run_mon $dir a || return 1
214     # should have at least one OSD
215     run_osd $dir 0 || return 1
216     create_rbd_pool || return 1
217
218     local empty_map=$dir/empty_map
219     :> $empty_map.txt
220     crushtool -c $empty_map.txt -o $empty_map.map || return 1
221     expect_failure $dir "Error EINVAL" \
222         ceph osd setcrushmap -i $empty_map.map || return 1
223 }
224
225 main osd-crush "$@"
226
227 # Local Variables:
228 # compile-command: "cd ../.. ; make -j4 && test/mon/osd-crush.sh"
229 # End: