Repository navigation
Expand file tree
/
Copy pathreplicator_test.py
More file actions
348 lines (282 loc) · 11.5 KB
/
Copy pathreplicator_test.py
File metadata and controls
348 lines (282 loc) · 11.5 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env python
# Copyright (c) 2015 IBM. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
_replicator_test_
replicator integration tests
"""
import logging
import sys
import time
import uuid
import unittest
from cloudant import cloudant
from cloudant.credentials import read_dot_cloudant
from cloudant.replicator import Replicator
from .. import unicode_
def setup_logging():
log = logging.getLogger()
log.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
log.addHandler(handler)
return log
LOG = setup_logging()
class ReplicatorTest(unittest.TestCase):
"""
Excercise our replicator class to ensure that it does its thing.
"""
def setUp(self):
self.user, self.passwd = read_dot_cloudant(filename="~/.clou")
self.replication_ids = []
self.dbs = []
def tearDown(self):
with cloudant(self.user, self.passwd, account=self.user) as c:
replicator = Replicator(c)
while self.replication_ids:
replicator.stop_replication(self.replication_ids.pop())
while self.dbs:
c.delete_database(self.dbs.pop())
def test_init(self):
"""
_test_init_
Verify that we can init our database object.
"""
with cloudant(self.user, self.passwd, account=self.user) as c:
replicator = Replicator(c)
replicator.all_docs()
def test_create_replication(self):
"""
_test_create_replication_
Make a couple of test databases, and confirm that docs from
one get transferred to t'other.
"""
dbsource = unicode_("test_create_replication_source_{}".format(
unicode_(uuid.uuid4())))
dbtarget = unicode_("test_create_replication_target_{}".format(
unicode_(uuid.uuid4())))
self.dbs = [dbsource, dbtarget]
with cloudant(self.user, self.passwd, account=self.user) as c:
dbs = c.create_database(dbsource)
dbt = c.create_database(dbtarget)
doc1 = dbs.create_document(
{"_id": "doc1", "testing": "document 1"}
)
doc2 = dbs.create_document(
{"_id": "doc2", "testing": "document 1"}
)
doc3 = dbs.create_document(
{"_id": "doc3", "testing": "document 1"}
)
replicator = Replicator(c)
repl_id = unicode_("test_create_replication_{}".format(
unicode_(uuid.uuid4())))
self.replication_ids.append(repl_id)
ret = replicator.create_replication(
source_db=dbs,
target_db=dbt,
repl_id=repl_id,
continuous=False
)
try:
repl_doc = replicator[repl_id]
except KeyError:
repl_doc = None
if not repl_doc or not (repl_doc.get(
'_replication_state', "none") in ('completed', 'error')):
for change in replicator.changes():
if change.get('id') == repl_id:
try:
repl_doc = replicator[repl_id]
repl_doc.fetch()
except KeyError:
pass
if repl_doc and (repl_doc.get(
'_replication_state',
"none") in ('completed', 'error')):
break
else:
LOG.debug(
unicode_("Waiting for replication to complete "
"(repl_doc: {})".format(repl_doc))
)
self.assertTrue(repl_doc)
self.assertEqual(repl_doc.get('_replication_state'), 'completed')
for d in ['doc1', 'doc2', 'doc3']:
self.assertTrue(dbt[d])
self.assertEqual(dbt[d]['testing'], dbs[d]['testing'])
def test_follow_replication(self):
"""
_test_follow_replication_
Test to make sure that we can follow a replication.
"""
dbsource = unicode_("test_follow_replication_source_{}".format(
unicode_(uuid.uuid4())))
dbtarget = unicode_("test_follow_replication_target_{}".format(
unicode_(uuid.uuid4())))
self.dbs = [dbsource, dbtarget]
with cloudant(self.user, self.passwd, account=self.user) as c:
dbs = c.create_database(dbsource)
dbt = c.create_database(dbtarget)
doc1 = dbs.create_document(
{"_id": "doc1", "testing": "document 1"}
)
doc2 = dbs.create_document(
{"_id": "doc2", "testing": "document 1"}
)
doc3 = dbs.create_document(
{"_id": "doc3", "testing": "document 1"}
)
replicator = Replicator(c)
repl_id = unicode_("test_follow_replication_{}".format(
unicode_(uuid.uuid4())))
self.replication_ids.append(repl_id)
ret = replicator.create_replication(
source_db=dbs,
target_db=dbt,
repl_id=repl_id,
continuous=False,
)
updates = [
update for update in replicator.follow_replication(repl_id)
]
self.assertTrue(len(updates) > 0)
self.assertEqual(updates[-1]['_replication_state'], 'completed')
@unittest.skip("Doesn't reliably get into error state on couch side.")
def test_follow_replication_with_errors(self):
"""
_test_follow_replication_with_errors_
Test to make sure that we exit the follow loop when we submit
a bad replication.
"""
dbsource = unicode_("test_follow_replication_source_error_{}".format(
unicode_(uuid.uuid4())))
dbtarget = unicode_("test_follow_replication_target_error_{}".format(
unicode_(uuid.uuid4())))
self.dbs = [dbsource, dbtarget]
with cloudant(self.user, self.passwd, account=self.user) as c:
dbs = c.create_database(dbsource)
dbt = c.create_database(dbtarget)
doc1 = dbs.create_document(
{"_id": "doc1", "testing": "document 1"}
)
doc2 = dbs.create_document(
{"_id": "doc2", "testing": "document 1"}
)
doc3 = dbs.create_document(
{"_id": "doc3", "testing": "document 1"}
)
replicator = Replicator(c)
repl_id = unicode_("test_follow_replication_{}".format(
unicode_(uuid.uuid4())))
self.replication_ids.append(repl_id)
ret = replicator.create_replication(
source_db=dbs,
target_db=dbt,
# Deliberately override these good params with bad params
source=dbsource + "foo",
target=dbtarget + "foo",
repl_id=repl_id,
continuous=False,
)
updates = [
update for update in replicator.follow_replication(repl_id)
]
self.assertTrue(len(updates) > 0)
self.assertEqual(updates[-1]['_replication_state'], 'error')
def test_replication_state(self):
"""
_test_replication_state_
Verify that we can get the replication state.
"""
dbsource = unicode_("test_replication_state_source_{}".format(
unicode_(uuid.uuid4())))
dbtarget = unicode_("test_replication_state_target_{}".format(
unicode_(uuid.uuid4())))
self.dbs = [dbsource, dbtarget]
with cloudant(self.user, self.passwd, account=self.user) as c:
dbs = c.create_database(dbsource)
dbt = c.create_database(dbtarget)
doc1 = dbs.create_document(
{"_id": "doc1", "testing": "document 1"}
)
doc2 = dbs.create_document(
{"_id": "doc2", "testing": "document 1"}
)
doc3 = dbs.create_document(
{"_id": "doc3", "testing": "document 1"}
)
replicator = Replicator(c)
repl_id = unicode_("test_replication_state_{}".format(
unicode_(uuid.uuid4())))
self.replication_ids.append(repl_id)
ret = replicator.create_replication(
source_db=dbs,
target_db=dbt,
repl_id=repl_id,
continuous=False,
)
replication_state = "not_yet_set"
while True:
# Verify that replication_state returns either None
# (if the field doesn't exist yet), or a valid
# replication state.
replication_state = replicator.replication_state(repl_id)
if replication_state is not None:
self.assertTrue(
replication_state in [
'completed',
'error',
'triggered'
]
)
if replication_state in ('error', 'completed'):
break
LOG.debug("got replication state: {}".format(
replication_state))
time.sleep(1)
def test_list_replications(self):
"""
_test_list_replications_
Verify that we get a list of replications documents back when
we got to list replications.
"""
with cloudant(self.user, self.passwd, account=self.user) as c:
replicator = Replicator(c)
repl_ids = []
num_reps = 3
for i in range(0, num_reps):
tag = "{0}_{1}".format(i, unicode_(uuid.uuid4()))
dbsource = unicode_("test_list_repl_src_{}".format(tag))
dbtarget = unicode_("test_list_repl_tgt_{}".format(tag))
self.dbs.append(dbsource)
self.dbs.append(dbtarget)
dbs = c.create_database(dbsource)
dbt = c.create_database(dbtarget)
doc1 = dbs.create_document(
{"_id": "doc1", "testing": "document 1"}
)
repl_id = unicode_("test_create_replication_{}".format(tag))
self.replication_ids.append(repl_id)
repl_ids.append(repl_id)
ret = replicator.create_replication(
source_db=dbs,
target_db=dbt,
repl_id=repl_id,
continuous=False
)
replications = replicator.list_replications()
ids = [doc['_id'] for doc in replications]
found_ids = [i for i in ids if i in repl_ids]
self.assertEqual(num_reps, len(found_ids))