ctdb-tests: Update integration tests to use ctdb -X
[vlendec/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 # Reset configuration
46 ctdb_restart_when_done
47
48 ######################################################################
49
50 # Confirm that no nodes have databases with read-only delegations
51 check_no_readonly ()
52 {
53     try_command_on_node all $CTDB cattdb $testdb
54     local ro_flags="RO_HAVE_READONLY|RO_HAVE_DELEGATIONS"
55     local numreadonly=$(grep -c -E "$ro_flags" <<<"$out") || true
56     if [ $numreadonly -eq 0 ] ; then
57         echo "GOOD: no read-only delegations"
58     else
59         echo "BAD: there are read-only delegations"
60         echo "$out"
61         exit 1
62     fi
63 }
64
65 # Check that the test record has the correct read-only flags on the
66 # given nodes.  The first node is the dmaster, which should know there
67 # are delegations but should not be flagged as having a read-only
68 # copy.  Subsequent nodes should have a read-only copy but not know
69 # about any (other) delegations.
70 check_readonly ()
71 {
72     local dmaster="$1" ; shift
73     local others="$*"
74
75     local count
76
77     try_command_on_node $dmaster $CTDB cattdb $testdb
78     count=$(grep -c -E "RO_HAVE_DELEGATIONS" <<<"$out") || true
79     if [ $count -eq 1 ] ; then
80         echo "GOOD: dmaster ${dmaster} has read-only delegations"
81     else
82         echo "BAD: dmaster ${dmaster} has no read-only delegations"
83         echo "$out"
84         exit 1
85     fi
86     count=$(grep -c -E "RO_HAVE_READONLY" <<<"$out") || true
87     if [ $count -ne 0 ] ; then
88         echo "BAD: dmaster ${dmaster} has a read-only copy"
89         echo "$out"
90         exit 1
91     fi
92
93     local o
94     for o in $others ; do
95         try_command_on_node $o $CTDB cattdb $testdb
96         count=$(grep -c -E "RO_HAVE_READONLY" <<<"$out") || true
97         if [ $count -eq 1 ] ; then
98             echo "GOOD: node ${o} has a read-only copy"
99         else
100             echo "BAD: node ${o} has no read-only copy"
101             echo "$out"
102             exit 1
103         fi
104         count=$(grep -c -E "RO_HAVE_DELEGATIONS" <<<"$out") || true
105         if [ $count -ne 0 ] ; then
106             echo "BAD: other node ${o} has read-only delegations"
107             echo "$out"
108             exit 1
109         fi
110     done
111 }
112
113 ######################################################################
114
115 echo "Get list of nodes..."
116 try_command_on_node any $CTDB -X listnodes
117 all_nodes=$(awk -F'|' '{print $2}' <<<"$out")
118
119 ######################################################################
120
121 testdb="test.tdb"
122 echo "Create test database \"${testdb}\""
123 try_command_on_node 0 $CTDB attach $testdb
124
125 echo "Create some records..."
126 try_command_on_node all $CTDB_TEST_WRAPPER ctdb_update_record
127
128 ######################################################################
129
130 echo "Try some readonly fetches, these should all be upgraded to full fetchlocks..."
131 try_command_on_node all $CTDB_TEST_WRAPPER "ctdb_fetch_readonly_once </dev/null"
132
133 check_no_readonly
134
135 ######################################################################
136
137 echo "Activate read-only record support for \"$testdb\"..."
138 try_command_on_node all $CTDB setdbreadonly $testdb
139
140 # Database should be tagged as READONLY
141 try_command_on_node 0 $CTDB getdbmap
142 db_details=$(awk -v db="$testdb" '$2 == foo="name:" db { print }' <<<"$out")
143 if grep -q "READONLY" <<<"$db_details" ; then
144     echo "GOOD: read-only record support is enabled"
145 else
146     echo "BAD: could not activate read-only support"
147     echo "$db_details"
148     exit 1
149 fi
150
151 ######################################################################
152
153 echo "Create 1 read-only delegation ..."
154 # dmaster=1
155 try_command_on_node 1 $CTDB_TEST_WRAPPER ctdb_update_record
156
157 # Fetch read-only to node 0
158 try_command_on_node 0 $CTDB_TEST_WRAPPER "ctdb_fetch_readonly_once </dev/null"
159
160 check_readonly 1 0
161
162 ######################################################################
163
164 echo "Verify that a fetchlock revokes read-only delegations..."
165 # Node 1 becomes dmaster
166 try_command_on_node 1 $CTDB_TEST_WRAPPER ctdb_update_record
167
168 check_no_readonly
169
170 ######################################################################
171
172 echo "Create more read-only delegations..."
173 dmaster=1
174 try_command_on_node $dmaster $CTDB_TEST_WRAPPER ctdb_update_record
175
176 others=""
177 for n in $all_nodes ; do
178     if [ "$n" != "$dmaster" ] ; then
179         # Fetch read-only copy to this node
180         try_command_on_node $n \
181             $CTDB_TEST_WRAPPER "ctdb_fetch_readonly_once </dev/null"
182         others="${others} ${n}"
183     fi
184 done
185
186 check_readonly $dmaster $others
187
188 ######################################################################
189
190 echo "Verify that a recovery will revoke the delegations..."
191 try_command_on_node 0 $CTDB recover
192
193 check_no_readonly