summaryrefslogtreecommitdiffstats
path: root/ctdb/tests/INTEGRATION/database/basics.003.detach.sh
blob: cb44955d1e8361c95e6298f87906f6d90b8578f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env bash

# Verify that 'ctdb detach' works as expected:
# 1. Attach test databases
# 2. Detach test databases
# 3. Confirm test databases are not attached

. "${TEST_SCRIPTS_DIR}/integration.bash"

set -e

ctdb_test_init

######################################################################

try_command_on_node 0 "$CTDB listnodes -X | wc -l"
numnodes="$out"

######################################################################

# Confirm that the database is attached
check_db_once ()
{
	local db="$1"

	local num_db

	try_command_on_node all "$CTDB getdbmap"
	num_db=$(grep -cF "name:${db}" "$outfile") || true
	if [ "$num_db" -eq "$numnodes" ]; then
		return 0
	else
		return 1
	fi
}

check_db ()
{
	local db="$1"

	echo "Waiting until database ${db} is attached on all nodes"
	wait_until 10 check_db_once "$db"
}

# Confirm that no nodes have databases attached
check_no_db_once ()
{
	local db="$1"

	local num_db

	try_command_on_node all "$CTDB getdbmap"
	num_db=$(grep -cF "name:${db}" "$outfile") || true
	if [ "$num_db" -eq 0 ]; then
		return 0
	else
		return 1
	fi
}

check_no_db ()
{
	local db="$1"

	echo "Waiting until database ${db} is detached on all nodes"
	wait_until 10 check_no_db_once "$db"
}

######################################################################

testdb1="detach_test1.tdb"
testdb2="detach_test2.tdb"
testdb3="detach_test3.tdb"
testdb4="detach_test4.tdb"

echo "Create test databases"
for db in "$testdb1" "$testdb2" "$testdb3" "$testdb4" ; do
    echo "  $db"
    try_command_on_node 0 $CTDB attach "$db"
done

for db in "$testdb1" "$testdb2" "$testdb3" "$testdb4" ; do
    check_db "$db"
done

######################################################################

echo
echo "Ensuring AllowClientDBAttach=1 on all nodes"
try_command_on_node all $CTDB setvar AllowClientDBAttach 1

echo "Check failure detaching single test database $testdb1"
try_command_on_node 1 "! $CTDB detach $testdb1"
check_db "$testdb1"

echo
echo "Setting AllowClientDBAttach=0 on node 0"
try_command_on_node 0 $CTDB setvar AllowClientDBAttach 0

echo "Check failure detaching single test database $testdb1"
try_command_on_node 1 "! $CTDB detach $testdb1"
check_db "$testdb1"

echo
echo "Setting AllowClientDBAttach=0 on all nodes"
try_command_on_node all $CTDB setvar AllowClientDBAttach 0

echo "Check detaching single test database $testdb1"
try_command_on_node 1 "$CTDB detach $testdb1"
check_no_db "$testdb1"

######################################################################

echo
echo "Detach multiple test databases"
echo "    $testdb2, $testdb3, $testdb4"
try_command_on_node 0 $CTDB detach $testdb2 $testdb3 $testdb4

for db in "$testdb2" "$testdb3" "$testdb4" ; do
    check_no_db "$db"
done

######################################################################

echo
echo "Attach a single test database"
try_command_on_node all $CTDB setvar AllowClientDBAttach 1
try_command_on_node 0 $CTDB attach $testdb1
check_db "$testdb1"

echo
echo "Write a key to database"
try_command_on_node 0 $CTDB writekey $testdb1 foo bar
try_command_on_node 0 $CTDB catdb $testdb1
num_keys=$(sed -n -e 's/Dumped \([0-9]*\) records/\1/p' "$outfile") || true
if [ -n "$num_keys" -a $num_keys -eq 1 ]; then
    echo "GOOD: Key added to database"
else
    echo "BAD: Key did not get added to database"
    cat "$outfile"
    exit 1
fi

echo
echo "Detach test database"
try_command_on_node all $CTDB setvar AllowClientDBAttach 0
try_command_on_node 0 $CTDB detach $testdb1
check_no_db "$testdb1"

echo
echo "Re-attach test database"
try_command_on_node all $CTDB setvar AllowClientDBAttach 1
try_command_on_node 0 $CTDB attach $testdb1
check_db "$testdb1"

echo
echo "Check if the database is empty"
try_command_on_node 0 $CTDB catdb $testdb1
num_keys=$(sed -n -e 's/Dumped \([0-9]*\) records/\1/p' "$outfile") || true
if [ -n "$num_keys" -a $num_keys -eq 0 ]; then
    echo "GOOD: Database $testdb1 is empty"
else
    echo "BAD: Database $testdb1 is not empty"
    cat "$outfile"
    exit 1
fi