-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathtest_cluster.py
261 lines (197 loc) · 8.33 KB
/
test_cluster.py
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import time
import warnings
import pytest
from packaging import version
from arango.errno import DATABASE_NOT_FOUND, FORBIDDEN
from arango.exceptions import (
ClusterEndpointsError,
ClusterHealthError,
ClusterMaintenanceModeError,
ClusterRebalanceError,
ClusterServerCountError,
ClusterServerEngineError,
ClusterServerIDError,
ClusterServerModeError,
ClusterServerRoleError,
ClusterServerStatisticsError,
ClusterServerVersionError,
ClusterVpackSortMigrationError,
)
from tests.helpers import assert_raises
def test_cluster_server_id(sys_db, bad_db, cluster):
if not cluster:
pytest.skip("Only tested in a cluster setup")
result = sys_db.cluster.server_id()
assert isinstance(result, str)
with assert_raises(ClusterServerIDError) as err:
bad_db.cluster.server_id()
assert err.value.error_code in {FORBIDDEN, DATABASE_NOT_FOUND}
def test_cluster_server_role(sys_db, bad_db, cluster):
if not cluster:
pytest.skip("Only tested in a cluster setup")
result = sys_db.cluster.server_role()
assert isinstance(result, str)
with assert_raises(ClusterServerRoleError) as err:
bad_db.cluster.server_role()
assert err.value.error_code in {FORBIDDEN, DATABASE_NOT_FOUND}
def test_cluster_server_mode(sys_db, bad_db, cluster):
if not cluster:
pytest.skip("Only tested in a cluster setup")
result = sys_db.cluster.server_mode()
assert result == "default"
with assert_raises(ClusterServerModeError) as err:
bad_db.cluster.server_mode()
assert err.value.error_code in {FORBIDDEN, DATABASE_NOT_FOUND}
def test_cluster_health(sys_db, bad_db, cluster):
if not cluster:
pytest.skip("Only tested in a cluster setup")
result = sys_db.cluster.health()
assert "Health" in result
assert "ClusterId" in result
with assert_raises(ClusterHealthError) as err:
bad_db.cluster.health()
assert err.value.error_code in {FORBIDDEN, DATABASE_NOT_FOUND}
def test_cluster_server_version(sys_db, bad_db, cluster):
if not cluster:
pytest.skip("Only tested in a cluster setup")
server_id = sys_db.cluster.server_id()
result = sys_db.cluster.server_version(server_id)
assert "server" in result
assert "version" in result
with assert_raises(ClusterServerVersionError) as err:
bad_db.cluster.server_version(server_id)
assert err.value.error_code in {FORBIDDEN, DATABASE_NOT_FOUND}
def test_cluster_server_engine(sys_db, bad_db, cluster):
if not cluster:
pytest.skip("Only tested in a cluster setup")
server_id = sys_db.cluster.server_id()
result = sys_db.cluster.server_engine(server_id)
assert "name" in result
assert "supports" in result
with assert_raises(ClusterServerEngineError) as err:
bad_db.cluster.server_engine(server_id)
assert err.value.error_code in {FORBIDDEN, DATABASE_NOT_FOUND}
def test_cluster_server_statistics(sys_db, bad_db, cluster):
if not cluster:
pytest.skip("Only tested in a cluster setup")
server_id = sys_db.cluster.server_id()
result = sys_db.cluster.server_statistics(server_id)
assert "time" in result
assert "system" in result
assert "enabled" in result
with assert_raises(ClusterServerStatisticsError) as err:
bad_db.cluster.server_statistics(server_id)
assert err.value.error_code in {FORBIDDEN, DATABASE_NOT_FOUND}
def test_cluster_server_maintenance_mode(sys_db, bad_db, cluster):
if not cluster:
pytest.skip("Only tested in a cluster setup")
# Must be a DBServer
health = sys_db.cluster.health()
server_id = None
for server_id, info in health["Health"].items():
if info["Role"] == "DBServer":
server_id = server_id
break
if server_id is None:
pytest.skip("No DBServer found in cluster")
result = sys_db.cluster.server_maintenance_mode(server_id)
assert result == {}
with assert_raises(ClusterMaintenanceModeError) as err:
bad_db.cluster.server_maintenance_mode(server_id)
assert err.value.error_code in {FORBIDDEN, DATABASE_NOT_FOUND}
sys_db.cluster.toggle_server_maintenance_mode(server_id, "maintenance", timeout=2)
result = sys_db.cluster.server_maintenance_mode(server_id)
assert "Mode" in result
assert "Until" in result
time.sleep(5)
result = sys_db.cluster.server_maintenance_mode(server_id)
assert result == {}
def test_cluster_toggle_maintenance_mode(sys_db, bad_db, cluster):
if not cluster:
pytest.skip("Only tested in a cluster setup")
result = sys_db.cluster.toggle_maintenance_mode("on")
assert "error" in result or "warning" in result
result = sys_db.cluster.toggle_maintenance_mode("off")
assert "error" in result or "warning" in result
with assert_raises(ClusterMaintenanceModeError) as err:
bad_db.cluster.toggle_maintenance_mode("on")
assert err.value.error_code in {FORBIDDEN, DATABASE_NOT_FOUND}
def test_cluster_endpoints(db, bad_db, cluster):
if not cluster:
pytest.skip("Only tested in a cluster setup")
# Test get server endpoints
assert len(db.cluster.endpoints()) > 0
# Test get server endpoints with bad database
with assert_raises(ClusterEndpointsError) as err:
bad_db.cluster.endpoints()
assert err.value.error_code in {FORBIDDEN, DATABASE_NOT_FOUND}
def test_cluster_server_count(db, bad_db, cluster):
if not cluster:
pytest.skip("Only tested in a cluster setup")
# Test get server count
db.cluster.server_count()
# Test get server endpoints with bad database
with assert_raises(ClusterServerCountError) as err:
bad_db.cluster.server_count()
assert err.value.error_code in {FORBIDDEN, DATABASE_NOT_FOUND}
def test_cluster_rebalance(sys_db, bad_db, cluster):
if not cluster:
pytest.skip("Only tested in a cluster setup")
# Test imbalance retrieval
imbalance = sys_db.cluster.calculate_imbalance()
assert "leader" in imbalance
assert "shards" in imbalance
assert imbalance["pendingMoveShards"] == 0
assert imbalance["todoMoveShards"] == 0
with assert_raises(ClusterRebalanceError) as err:
bad_db.cluster.calculate_imbalance()
assert err.value.error_code == FORBIDDEN
# Test rebalance computation
rebalance = sys_db.cluster.calculate_rebalance_plan(
max_moves=3,
leader_changes=True,
move_leaders=True,
move_followers=True,
pi_factor=1234.5,
databases_excluded=["_system"],
)
assert "imbalanceBefore" in rebalance
assert "imbalanceAfter" in rebalance
assert "moves" in rebalance
with assert_raises(ClusterRebalanceError) as err:
bad_db.cluster.calculate_rebalance_plan()
assert err.value.error_code == FORBIDDEN
# Test rebalance execution
if sys_db.cluster.execute_rebalance_plan(rebalance["moves"]) is False:
warnings.warn(
"Rebalance plan was not executed."
"This may happen independent of the driver."
)
with assert_raises(ClusterRebalanceError) as err:
bad_db.cluster.execute_rebalance_plan(rebalance["moves"])
assert err.value.error_code == FORBIDDEN
# Rebalance cluster in one go
rebalance = sys_db.cluster.rebalance()
assert "imbalanceBefore" in rebalance
assert "imbalanceAfter" in rebalance
assert "moves" in rebalance
with assert_raises(ClusterRebalanceError) as err:
bad_db.cluster.rebalance()
assert err.value.error_code == FORBIDDEN
def test_vpack_sort_migration(sys_db, bad_db, db_version, cluster):
if not cluster:
pytest.skip("Only tested in a cluster setup")
if db_version < version.parse("3.12.2"):
pytest.skip("vpackSortMigration is only tested in 3.12.2+")
sys_db.cluster.vpack_sort_migration_status()
with assert_raises(ClusterVpackSortMigrationError) as err:
bad_db.cluster.vpack_sort_migration_status()
assert err.value.error_code == FORBIDDEN
sys_db.cluster.vpack_sort_migration_index_check()
with assert_raises(ClusterVpackSortMigrationError) as err:
bad_db.cluster.vpack_sort_migration_index_check()
assert err.value.error_code == FORBIDDEN
sys_db.cluster.migrate_vpack_sorting()
with assert_raises(ClusterVpackSortMigrationError) as err:
bad_db.cluster.migrate_vpack_sorting()
assert err.value.error_code == FORBIDDEN