-
Notifications
You must be signed in to change notification settings - Fork 744
/
parser_tests.py
398 lines (329 loc) · 17.2 KB
/
parser_tests.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import unittest
import os
import math
from rdbtools import RdbCallback, RdbParser
from rdbtools.compat import range
class RedisParserTestCase(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_empty_rdb(self):
r = load_rdb('empty_database.rdb')
self.assert_('start_rdb' in r.methods_called)
self.assert_('end_rdb' in r.methods_called)
self.assertEquals(len(r.databases), 0, msg = "didn't expect any databases")
def test_multiple_databases(self):
r = load_rdb('multiple_databases.rdb')
self.assert_(len(r.databases), 2)
self.assert_(1 not in r.databases)
self.assertEquals(r.databases[0][b"key_in_zeroth_database"], b"zero")
self.assertEquals(r.databases[2][b"key_in_second_database"], b"second")
def test_keys_with_expiry(self):
r = load_rdb('keys_with_expiry.rdb')
expiry = r.expiry[0][b'expires_ms_precision']
self.assertEquals(expiry.year, 2022)
self.assertEquals(expiry.month, 12)
self.assertEquals(expiry.day, 25)
self.assertEquals(expiry.hour, 10)
self.assertEquals(expiry.minute, 11)
self.assertEquals(expiry.second, 12)
self.assertEquals(expiry.microsecond, 573000)
def test_integer_keys(self):
r = load_rdb('integer_keys.rdb')
self.assertEquals(r.databases[0][125], b"Positive 8 bit integer")
self.assertEquals(r.databases[0][0xABAB], b"Positive 16 bit integer")
self.assertEquals(r.databases[0][0x0AEDD325], b"Positive 32 bit integer")
def test_negative_integer_keys(self):
r = load_rdb('integer_keys.rdb')
self.assertEquals(r.databases[0][-123], b"Negative 8 bit integer")
self.assertEquals(r.databases[0][-0x7325], b"Negative 16 bit integer")
self.assertEquals(r.databases[0][-0x0AEDD325], b"Negative 32 bit integer")
def test_string_key_with_compression(self):
r = load_rdb('easily_compressible_string_key.rdb')
key = b"".join(b'a' for x in range(0, 200))
value = b"Key that redis should compress easily"
self.assertEquals(r.databases[0][key], value)
def test_zipmap_thats_compresses_easily(self):
r = load_rdb('zipmap_that_compresses_easily.rdb')
self.assertEquals(r.databases[0][b"zipmap_compresses_easily"][b"a"], b"aa")
self.assertEquals(r.databases[0][b"zipmap_compresses_easily"][b"aa"], b"aaaa")
self.assertEquals(r.databases[0][b"zipmap_compresses_easily"][b"aaaaa"], b"aaaaaaaaaaaaaa")
def test_zipmap_that_doesnt_compress(self):
r = load_rdb('zipmap_that_doesnt_compress.rdb')
self.assertEquals(r.databases[0][b"zimap_doesnt_compress"][b"MKD1G6"], 2)
self.assertEquals(r.databases[0][b"zimap_doesnt_compress"][b"YNNXK"], b"F7TI")
def test_zipmap_with_big_values(self):
''' See issue https://github.com/sripathikrishnan/redis-rdb-tools/issues/2
Values with length around 253/254/255 bytes are treated specially in the parser
This test exercises those boundary conditions
In order to test a bug with large ziplists, it is necessary to start
Redis with "hash-max-ziplist-value 21000", create this rdb file,
and run the test. That forces the 20kbyte value to be stored as a
ziplist with a length encoding of 5 bytes.
'''
r = load_rdb('zipmap_with_big_values.rdb')
self.assertEquals(len(r.databases[0][b"zipmap_with_big_values"][b"253bytes"]), 253)
self.assertEquals(len(r.databases[0][b"zipmap_with_big_values"][b"254bytes"]), 254)
self.assertEquals(len(r.databases[0][b"zipmap_with_big_values"][b"255bytes"]), 255)
self.assertEquals(len(r.databases[0][b"zipmap_with_big_values"][b"300bytes"]), 300)
self.assertEquals(len(r.databases[0][b"zipmap_with_big_values"][b"20kbytes"]), 20000)
def test_hash_as_ziplist(self):
'''In redis dump version = 4, hashmaps are stored as ziplists'''
r = load_rdb('hash_as_ziplist.rdb')
self.assertEquals(r.databases[0][b"zipmap_compresses_easily"][b"a"], b"aa")
self.assertEquals(r.databases[0][b"zipmap_compresses_easily"][b"aa"], b"aaaa")
self.assertEquals(r.databases[0][b"zipmap_compresses_easily"][b"aaaaa"], b"aaaaaaaaaaaaaa")
def test_dictionary(self):
r = load_rdb('dictionary.rdb')
self.assertEquals(r.lengths[0][b"force_dictionary"], 1000)
self.assertEquals(r.databases[0][b"force_dictionary"][b"ZMU5WEJDG7KU89AOG5LJT6K7HMNB3DEI43M6EYTJ83VRJ6XNXQ"],
b"T63SOS8DQJF0Q0VJEZ0D1IQFCYTIPSBOUIAI9SB0OV57MQR1FI")
self.assertEquals(r.databases[0][b"force_dictionary"][b"UHS5ESW4HLK8XOGTM39IK1SJEUGVV9WOPK6JYA5QBZSJU84491"],
b"6VULTCV52FXJ8MGVSFTZVAGK2JXZMGQ5F8OVJI0X6GEDDR27RZ")
def test_ziplist_that_compresses_easily(self):
r = load_rdb('ziplist_that_compresses_easily.rdb')
self.assertEquals(r.lengths[0][b"ziplist_compresses_easily"], 6)
for idx, length in enumerate([6, 12, 18, 24, 30, 36]) :
self.assertEquals((b"".join(b"a" for x in range(length))), r.databases[0][b"ziplist_compresses_easily"][idx])
def test_ziplist_that_doesnt_compress(self):
r = load_rdb('ziplist_that_doesnt_compress.rdb')
self.assertEquals(r.lengths[0][b"ziplist_doesnt_compress"], 2)
self.assert_(b"aj2410" in r.databases[0][b"ziplist_doesnt_compress"])
self.assert_(b"cc953a17a8e096e76a44169ad3f9ac87c5f8248a403274416179aa9fbd852344"
in r.databases[0][b"ziplist_doesnt_compress"])
def test_ziplist_with_integers(self):
r = load_rdb('ziplist_with_integers.rdb')
expected_numbers = []
for x in range(0,13):
expected_numbers.append(x)
expected_numbers += [-2, 13, 25, -61, 63, 16380, -16000, 65535, -65523, 4194304, 0x7fffffffffffffff]
self.assertEquals(r.lengths[0][b"ziplist_with_integers"], len(expected_numbers))
for num in expected_numbers :
self.assert_(num in r.databases[0][b"ziplist_with_integers"], "Cannot find %d" % num)
def test_linkedlist(self):
r = load_rdb('linkedlist.rdb')
self.assertEquals(r.lengths[0][b"force_linkedlist"], 1000)
self.assert_(b"JYY4GIFI0ETHKP4VAJF5333082J4R1UPNPLE329YT0EYPGHSJQ" in r.databases[0][b"force_linkedlist"])
self.assert_(b"TKBXHJOX9Q99ICF4V78XTCA2Y1UYW6ERL35JCIL1O0KSGXS58S" in r.databases[0][b"force_linkedlist"])
def test_intset_16(self):
r = load_rdb('intset_16.rdb')
self.assertEquals(r.lengths[0][b"intset_16"], 3)
for num in (0x7ffe, 0x7ffd, 0x7ffc) :
self.assert_(num in r.databases[0][b"intset_16"])
def test_intset_32(self):
r = load_rdb('intset_32.rdb')
self.assertEquals(r.lengths[0][b"intset_32"], 3)
for num in (0x7ffefffe, 0x7ffefffd, 0x7ffefffc) :
self.assert_(num in r.databases[0][b"intset_32"])
def test_intset_64(self):
r = load_rdb('intset_64.rdb')
self.assertEquals(r.lengths[0][b"intset_64"], 3)
for num in (0x7ffefffefffefffe, 0x7ffefffefffefffd, 0x7ffefffefffefffc) :
self.assert_(num in r.databases[0][b"intset_64"])
def test_regular_set(self):
r = load_rdb('regular_set.rdb')
self.assertEquals(r.lengths[0][b"regular_set"], 6)
for member in (b"alpha", b"beta", b"gamma", b"delta", b"phi", b"kappa") :
self.assert_(member in r.databases[0][b"regular_set"], msg=('%s missing' % member))
def test_sorted_set_as_ziplist(self):
r = load_rdb('sorted_set_as_ziplist.rdb')
self.assertEquals(r.lengths[0][b"sorted_set_as_ziplist"], 3)
zset = r.databases[0][b"sorted_set_as_ziplist"]
self.assert_(floateq(zset[b'8b6ba6718a786daefa69438148361901'], 1))
self.assert_(floateq(zset[b'cb7a24bb7528f934b841b34c3a73e0c7'], 2.37))
self.assert_(floateq(zset[b'523af537946b79c4f8369ed39ba78605'], 3.423))
def test_filtering_by_keys(self):
r = load_rdb('parser_filters.rdb', filters={"keys":"k[0-9]"})
self.assertEquals(r.databases[0][b'k1'], b"ssssssss")
self.assertEquals(r.databases[0][b'k3'], b"wwwwwwww")
self.assertEquals(len(r.databases[0]), 2)
def test_filtering_by_type(self):
r = load_rdb('parser_filters.rdb', filters={"types":["sortedset"]})
self.assert_(b'z1' in r.databases[0])
self.assert_(b'z2' in r.databases[0])
self.assert_(b'z3' in r.databases[0])
self.assert_(b'z4' in r.databases[0])
self.assertEquals(len(r.databases[0]), 4)
def test_filtering_by_database(self):
r = load_rdb('multiple_databases.rdb', filters={"dbs":[2]})
self.assert_(b'key_in_zeroth_database' not in r.databases[0])
self.assert_(b'key_in_second_database' in r.databases[2])
self.assertEquals(len(r.databases[0]), 0)
self.assertEquals(len(r.databases[2]), 1)
def test_rdb_version_5_with_checksum(self):
r = load_rdb('rdb_version_5_with_checksum.rdb')
self.assertEquals(r.databases[0][b'abcd'], b'efgh')
self.assertEquals(r.databases[0][b'foo'], b'bar')
self.assertEquals(r.databases[0][b'bar'], b'baz')
self.assertEquals(r.databases[0][b'abcdef'], b'abcdef')
self.assertEquals(r.databases[0][b'longerstring'], b'thisisalongerstring.idontknowwhatitmeans')
def test_rdb_version_8_with_64b_length_and_scores(self):
r = load_rdb('rdb_version_8_with_64b_length_and_scores.rdb')
self.assertEquals(r.databases[0][b'foo'], b'bar')
zset = r.databases[0][b"bigset"]
self.assertEquals(len(zset), 1000)
self.assert_(floateq(zset[b'finalfield'], 2.718))
def test_multiple_databases_stream(self):
r = load_rdb_stream('multiple_databases.rdb')
self.assert_(len(r.databases), 2)
self.assert_(1 not in r.databases)
self.assertEquals(r.databases[0][b"key_in_zeroth_database"], b"zero")
self.assertEquals(r.databases[2][b"key_in_second_database"], b"second")
def test_rdb_version_8_with_module(self):
r = load_rdb('redis_40_with_module.rdb')
self.assertEquals(r.databases[0][b'foo']['module_name'], 'ReJSON-RL')
def test_rdb_version_8_with_module_and_skip(self):
r = load_rdb('redis_40_with_module.rdb', {"keys": "bar"}) # skip foo module
self.assert_(b'foo' not in r.databases[0])
def test_rdb_version_9_with_stream(self):
r = load_rdb('redis_50_with_streams.rdb')
self.assertEquals(r.lengths[0][b"mystream"], 4)
self.assertEquals(len(r.databases[0][b'mystream']), 1)
def floateq(f1, f2) :
return math.fabs(f1 - f2) < 0.00001
def load_rdb(file_name, filters=None) :
r = MockRedis()
parser = RdbParser(r, filters)
parser.parse(os.path.join(os.path.dirname(__file__), 'dumps', file_name))
return r
def load_rdb_stream(file_name, filters=None) :
r = MockRedis()
parser = RdbParser(r, filters)
parser.parse_fd(open(os.path.join(os.path.dirname(__file__), 'dumps', file_name), 'rb'))
return r
class MockRedis(RdbCallback):
def __init__(self):
super(MockRedis, self).__init__(string_escape=None)
self.databases = {}
self.lengths = {}
self.expiry = {}
self.methods_called = []
self.dbnum = 0
def currentdb(self) :
return self.databases[self.dbnum]
def store_expiry(self, key, expiry) :
self.expiry[self.dbnum][key] = expiry
def store_length(self, key, length) :
if not self.dbnum in self.lengths :
self.lengths[self.dbnum] = {}
self.lengths[self.dbnum][key] = length
def get_length(self, key) :
if not key in self.lengths[self.dbnum] :
raise Exception('Key %s does not have a length' % key)
return self.lengths[self.dbnum][key]
def start_rdb(self):
self.methods_called.append('start_rdb')
def start_database(self, dbnum):
self.dbnum = dbnum
self.databases[dbnum] = {}
self.expiry[dbnum] = {}
self.lengths[dbnum] = {}
def set(self, key, value, expiry, info):
self.currentdb()[key] = value
if expiry :
self.store_expiry(key, expiry)
def start_hash(self, key, length, expiry, info):
if key in self.currentdb() :
raise Exception('start_hash called with key %s that already exists' % key)
else :
self.currentdb()[key] = {}
if expiry :
self.store_expiry(key, expiry)
self.store_length(key, length)
def hset(self, key, field, value):
if not key in self.currentdb() :
raise Exception('start_hash not called for key = %s', key)
self.currentdb()[key][field] = value
def end_hash(self, key):
if not key in self.currentdb() :
raise Exception('start_hash not called for key = %s', key)
if len(self.currentdb()[key]) != self.lengths[self.dbnum][key] :
raise Exception('Lengths mismatch on hash %s, expected length = %d, actual = %d'
% (key, self.lengths[self.dbnum][key], len(self.currentdb()[key])))
def start_set(self, key, cardinality, expiry, info):
if key in self.currentdb() :
raise Exception('start_set called with key %s that already exists' % key)
else :
self.currentdb()[key] = []
if expiry :
self.store_expiry(key, expiry)
self.store_length(key, cardinality)
def sadd(self, key, member):
if not key in self.currentdb() :
raise Exception('start_set not called for key = %s', key)
self.currentdb()[key].append(member)
def end_set(self, key):
if not key in self.currentdb() :
raise Exception('start_set not called for key = %s', key)
if len(self.currentdb()[key]) != self.lengths[self.dbnum][key] :
raise Exception('Lengths mismatch on set %s, expected length = %d, actual = %d'
% (key, self.lengths[self.dbnum][key], len(self.currentdb()[key])))
def start_list(self, key, expiry, info):
if key in self.currentdb() :
raise Exception('start_list called with key %s that already exists' % key)
else :
self.currentdb()[key] = []
if expiry :
self.store_expiry(key, expiry)
def rpush(self, key, value) :
if not key in self.currentdb() :
raise Exception('start_list not called for key = %s', key)
self.currentdb()[key].append(value)
def end_list(self, key, info):
if not key in self.currentdb() :
raise Exception('start_set not called for key = %s', key)
self.store_length(key, len(self.currentdb()[key]))
def start_sorted_set(self, key, length, expiry, info):
if key in self.currentdb() :
raise Exception('start_sorted_set called with key %s that already exists' % key)
else :
self.currentdb()[key] = {}
if expiry :
self.store_expiry(key, expiry)
self.store_length(key, length)
def zadd(self, key, score, member):
if not key in self.currentdb() :
raise Exception('start_sorted_set not called for key = %s', key)
self.currentdb()[key][member] = score
def end_sorted_set(self, key):
if not key in self.currentdb() :
raise Exception('start_set not called for key = %s', key)
if len(self.currentdb()[key]) != self.lengths[self.dbnum][key] :
raise Exception('Lengths mismatch on sortedset %s, expected length = %d, actual = %d'
% (key, self.lengths[self.dbnum][key], len(self.currentdb()[key])))
def start_module(self, key, module_name, expiry, info):
if key in self.currentdb() :
raise Exception('start_module called with key %s that already exists' % key)
else :
self.currentdb()[key] = {'module_name': module_name}
if expiry :
self.store_expiry(key, expiry)
return False
def end_module(self, key, buffer_size, buffer=None):
if not key in self.currentdb() :
raise Exception('start_module not called for key = %s', key)
self.store_length(key, buffer_size)
pass
def start_stream(self, key, listpacks_count, expiry, info):
if key in self.currentdb() :
raise Exception('start_stream called with key %s that already exists' % key)
else :
self.currentdb()[key] = {}
if expiry :
self.store_expiry(key, expiry)
pass
def stream_listpack(self, key, entry_id, data):
if not key in self.currentdb() :
raise Exception('start_hash not called for key = %s', key)
self.currentdb()[key][entry_id] = data
pass
def end_stream(self, key, items, last_entry_id, cgroups):
if not key in self.currentdb() :
raise Exception('start_stream not called for key = %s', key)
self.store_length(key, items)
def end_database(self, dbnum):
if self.dbnum != dbnum :
raise Exception('start_database called with %d, but end_database called %d instead' % (self.dbnum, dbnum))
def end_rdb(self):
self.methods_called.append('end_rdb')