ctdb-tests: Avoid bulk output in $out, prefer $outfile
[gd/samba-autobuild/.git] / ctdb / tests / simple / 75_readonly_records_basic.sh
1 #!/bin/bash
2
3 test_info()
4 {
5     cat <<EOF
6 Read-only records can be activated at runtime using a ctdb command.
7 If read-only records are not activated, then any attempt to fetch a read-only
8 copy should be automatically upgraded to a read-write fetch_lock().
9
10 If read-only delegations are present, then any attempt to aquire a read-write
11 fetch_lock will trigger all delegations to be revoked before the fetch lock
12 completes.
13
14
15 Prerequisites:
16
17 * An active CTDB cluster with at least 2 active nodes.
18
19 Steps:
20
21 1. Verify that the status on all of the ctdb nodes is 'OK'.
22 2. create a test database and some records
23 3. try to fetch read-only records, this should not result in any delegations
24 4. activate read-only support
25 5. try to fetch read-only records, this should result in delegations
26 6. do a fetchlock  and the delegations should be revoked
27 7. try to fetch read-only records, this should result in delegations
28 8. do a recovery  and the delegations should be revoked
29
30 Expected results:
31
32 Delegations should be created and revoked as above
33
34 EOF
35 }
36
37 . "${TEST_SCRIPTS_DIR}/integration.bash"
38
39 ctdb_test_init
40
41 set -e
42
43 cluster_is_healthy
44
45 ######################################################################
46
47 # Confirm that no nodes have databases with read-only delegations
48 check_no_readonly ()
49 {
50     try_command_on_node all $CTDB cattdb $testdb
51     local ro_flags="RO_HAVE_READONLY|RO_HAVE_DELEGATIONS"
52     local numreadonly=$(grep -c -E "$ro_flags" "$outfile") || true
53     if [ $numreadonly -eq 0 ] ; then
54         echo "GOOD: no read-only delegations"
55     else
56         echo "BAD: there are read-only delegations"
57         cat "$outfile"
58         exit 1
59     fi
60 }
61
62 # Check that the test record has the correct read-only flags on the
63 # given nodes.  The first node is the dmaster, which should know there
64 # are delegations but should not be flagged as having a read-only
65 # copy.  Subsequent nodes should have a read-only copy but not know
66 # about any (other) delegations.
67 check_readonly ()
68 {
69     local dmaster="$1" ; shift
70     local others="$*"
71
72     local count
73
74     try_command_on_node $dmaster $CTDB cattdb $testdb
75     count=$(grep -c -E "RO_HAVE_DELEGATIONS" "$outfile") || true
76     if [ $count -eq 1 ] ; then
77         echo "GOOD: dmaster ${dmaster} has read-only delegations"
78     else
79         echo "BAD: dmaster ${dmaster} has no read-only delegations"
80         cat "$outfile"
81         exit 1
82     fi
83     count=$(grep -c -E "RO_HAVE_READONLY" "$outfile") || true
84     if [ $count -ne 0 ] ; then
85         echo "BAD: dmaster ${dmaster} has a read-only copy"
86         cat "$outfile"
87         exit 1
88     fi
89
90     local o
91     for o in $others ; do
92         try_command_on_node $o $CTDB cattdb $testdb
93         count=$(grep -c -E "RO_HAVE_READONLY" "$outfile") || true
94         if [ $count -eq 1 ] ; then
95             echo "GOOD: node ${o} has a read-only copy"
96         else
97             echo "BAD: node ${o} has no read-only copy"
98             cat "$outfile"
99             exit 1
100         fi
101         count=$(grep -c -E "RO_HAVE_DELEGATIONS" "$outfile") || true
102         if [ $count -ne 0 ] ; then
103             echo "BAD: other node ${o} has read-only delegations"
104             cat "$outfile"
105             exit 1
106         fi
107     done
108 }
109
110 ######################################################################
111
112 echo "Get list of nodes..."
113 try_command_on_node any $CTDB -X listnodes
114 all_nodes=$(awk -F'|' '{print $2}' "$outfile")
115
116 ######################################################################
117
118 testdb="test.tdb"
119 echo "Create test database \"${testdb}\""
120 try_command_on_node 0 $CTDB attach $testdb
121
122 echo "Create some records..."
123 try_command_on_node all $CTDB_TEST_WRAPPER $VALGRIND update_record \
124         -D ${testdb} -k testkey
125
126 ######################################################################
127
128 echo "Try some readonly fetches, these should all be upgraded to full fetchlocks..."
129 try_command_on_node all $CTDB_TEST_WRAPPER $VALGRIND fetch_readonly \
130         -D ${testdb} -k testkey
131
132 check_no_readonly
133
134 ######################################################################
135
136 echo "Activate read-only record support for \"$testdb\"..."
137 try_command_on_node all $CTDB setdbreadonly $testdb
138
139 # Database should be tagged as READONLY
140 try_command_on_node 0 $CTDB getdbmap
141 db_details=$(awk -v db="$testdb" '$2 == foo="name:" db { print }' "$outfile")
142 if grep -q "READONLY" <<<"$db_details" ; then
143     echo "GOOD: read-only record support is enabled"
144 else
145     echo "BAD: could not activate read-only support"
146     echo "$db_details"
147     exit 1
148 fi
149
150 ######################################################################
151
152 echo "Create 1 read-only delegation ..."
153 # dmaster=1
154 try_command_on_node 1 $CTDB_TEST_WRAPPER $VALGRIND update_record \
155         -D ${testdb} -k testkey
156
157 # Fetch read-only to node 0
158 try_command_on_node 0 $CTDB_TEST_WRAPPER $VALGRIND fetch_readonly \
159         -D ${testdb} -k testkey
160
161 check_readonly 1 0
162
163 ######################################################################
164
165 echo "Verify that a fetchlock revokes read-only delegations..."
166 # Node 1 becomes dmaster
167 try_command_on_node 1 $CTDB_TEST_WRAPPER $VALGRIND update_record \
168         -D ${testdb} -k testkey
169
170 check_no_readonly
171
172 ######################################################################
173
174 echo "Create more read-only delegations..."
175 dmaster=1
176 try_command_on_node $dmaster $CTDB_TEST_WRAPPER $VALGRIND update_record \
177         -D ${testdb} -k testkey
178
179 others=""
180 for n in $all_nodes ; do
181     if [ "$n" != "$dmaster" ] ; then
182         # Fetch read-only copy to this node
183         try_command_on_node $n $CTDB_TEST_WRAPPER $VALGRIND fetch_readonly \
184                 -D ${testdb} -k testkey
185         others="${others} ${n}"
186     fi
187 done
188
189 check_readonly $dmaster $others
190
191 ######################################################################
192
193 echo "Verify that a recovery will revoke the delegations..."
194 try_command_on_node 0 $CTDB recover
195
196 check_no_readonly