ctdb-tests: Update preamble for INTEGRATION tests
[vlendec/samba-autobuild/.git] / ctdb / tests / INTEGRATION / database / basics.003.detach.sh
1 #!/usr/bin/env bash
2
3 # Verify that 'ctdb detach' works as expected:
4 # 1. Attach test databases
5 # 2. Detach test databases
6 # 3. Confirm test databases are not attached
7
8 . "${TEST_SCRIPTS_DIR}/integration.bash"
9
10 set -e
11
12 ctdb_test_init
13
14 ######################################################################
15
16 try_command_on_node 0 "$CTDB listnodes -X | wc -l"
17 numnodes="$out"
18
19 ######################################################################
20
21 # Confirm that the database is attached
22 check_db_once ()
23 {
24         local db="$1"
25
26         local num_db
27
28         try_command_on_node all "$CTDB getdbmap"
29         num_db=$(grep -cF "name:${db}" "$outfile") || true
30         if [ "$num_db" -eq "$numnodes" ]; then
31                 return 0
32         else
33                 return 1
34         fi
35 }
36
37 check_db ()
38 {
39         local db="$1"
40
41         echo "Waiting until database ${db} is attached on all nodes"
42         wait_until 10 check_db_once "$db"
43 }
44
45 # Confirm that no nodes have databases attached
46 check_no_db_once ()
47 {
48         local db="$1"
49
50         local num_db
51
52         try_command_on_node all "$CTDB getdbmap"
53         num_db=$(grep -cF "name:${db}" "$outfile") || true
54         if [ "$num_db" -eq 0 ]; then
55                 return 0
56         else
57                 return 1
58         fi
59 }
60
61 check_no_db ()
62 {
63         local db="$1"
64
65         echo "Waiting until database ${db} is detached on all nodes"
66         wait_until 10 check_no_db_once "$db"
67 }
68
69 ######################################################################
70
71 testdb1="detach_test1.tdb"
72 testdb2="detach_test2.tdb"
73 testdb3="detach_test3.tdb"
74 testdb4="detach_test4.tdb"
75
76 echo "Create test databases"
77 for db in "$testdb1" "$testdb2" "$testdb3" "$testdb4" ; do
78     echo "  $db"
79     try_command_on_node 0 $CTDB attach "$db"
80 done
81
82 for db in "$testdb1" "$testdb2" "$testdb3" "$testdb4" ; do
83     check_db "$db"
84 done
85
86 ######################################################################
87
88 echo
89 echo "Ensuring AllowClientDBAttach=1 on all nodes"
90 try_command_on_node all $CTDB setvar AllowClientDBAttach 1
91
92 echo "Check failure detaching single test database $testdb1"
93 try_command_on_node 1 "! $CTDB detach $testdb1"
94 check_db "$testdb1"
95
96 echo
97 echo "Setting AllowClientDBAttach=0 on node 0"
98 try_command_on_node 0 $CTDB setvar AllowClientDBAttach 0
99
100 echo "Check failure detaching single test database $testdb1"
101 try_command_on_node 1 "! $CTDB detach $testdb1"
102 check_db "$testdb1"
103
104 echo
105 echo "Setting AllowClientDBAttach=0 on all nodes"
106 try_command_on_node all $CTDB setvar AllowClientDBAttach 0
107
108 echo "Check detaching single test database $testdb1"
109 try_command_on_node 1 "$CTDB detach $testdb1"
110 check_no_db "$testdb1"
111
112 ######################################################################
113
114 echo
115 echo "Detach multiple test databases"
116 echo "    $testdb2, $testdb3, $testdb4"
117 try_command_on_node 0 $CTDB detach $testdb2 $testdb3 $testdb4
118
119 for db in "$testdb2" "$testdb3" "$testdb4" ; do
120     check_no_db "$db"
121 done
122
123 ######################################################################
124
125 echo
126 echo "Attach a single test database"
127 try_command_on_node all $CTDB setvar AllowClientDBAttach 1
128 try_command_on_node 0 $CTDB attach $testdb1
129 check_db "$testdb1"
130
131 echo
132 echo "Write a key to database"
133 try_command_on_node 0 $CTDB writekey $testdb1 foo bar
134 try_command_on_node 0 $CTDB catdb $testdb1
135 num_keys=$(sed -n -e 's/Dumped \([0-9]*\) records/\1/p' "$outfile") || true
136 if [ -n "$num_keys" -a $num_keys -eq 1 ]; then
137     echo "GOOD: Key added to database"
138 else
139     echo "BAD: Key did not get added to database"
140     cat "$outfile"
141     exit 1
142 fi
143
144 echo
145 echo "Detach test database"
146 try_command_on_node all $CTDB setvar AllowClientDBAttach 0
147 try_command_on_node 0 $CTDB detach $testdb1
148 check_no_db "$testdb1"
149
150 echo
151 echo "Re-attach test database"
152 try_command_on_node all $CTDB setvar AllowClientDBAttach 1
153 try_command_on_node 0 $CTDB attach $testdb1
154 check_db "$testdb1"
155
156 echo
157 echo "Check if the database is empty"
158 try_command_on_node 0 $CTDB catdb $testdb1
159 num_keys=$(sed -n -e 's/Dumped \([0-9]*\) records/\1/p' "$outfile") || true
160 if [ -n "$num_keys" -a $num_keys -eq 0 ]; then
161     echo "GOOD: Database $testdb1 is empty"
162 else
163     echo "BAD: Database $testdb1 is not empty"
164     cat "$outfile"
165     exit 1
166 fi