forked from alicebob/miniredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_scripting_test.go
639 lines (543 loc) · 13.1 KB
/
cmd_scripting_test.go
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
package miniredis
import (
"testing"
"github.com/gomodule/redigo/redis"
)
func TestEval(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
defer c.Close()
{
b, err := redis.Int(c.Do("EVAL", "return 42", 0))
ok(t, err)
equals(t, 42, b)
}
{
b, err := redis.Strings(c.Do("EVAL", "return {KEYS[1], ARGV[1]}", 1, "key1", "key2"))
ok(t, err)
equals(t, []string{"key1", "key2"}, b)
}
{
b, err := redis.Strings(c.Do("EVAL", "return {ARGV[1]}", 0, "key1"))
ok(t, err)
equals(t, []string{"key1"}, b)
}
// Invalid args
_, err = c.Do("EVAL", 42, 0)
assert(t, err != nil, "no EVAL error")
_, err = c.Do("EVAL", "return 42")
mustFail(t, err, errWrongNumber("eval"))
_, err = c.Do("EVAL", "return 42", 1)
mustFail(t, err, msgInvalidKeysNumber)
_, err = c.Do("EVAL", "return 42", -1)
mustFail(t, err, msgNegativeKeysNumber)
_, err = c.Do("EVAL", "return 42", "letter")
mustFail(t, err, msgInvalidInt)
_, err = c.Do("EVAL", "[", 0)
assert(t, err != nil, "no EVAL error")
_, err = c.Do("EVAL", "os.exit(42)", 0)
assert(t, err != nil, "no EVAL error")
{
b, err := redis.String(c.Do("EVAL", `return string.gsub("foo", "o", "a")`, 0))
ok(t, err)
equals(t, "faa", b)
}
}
func TestEvalCall(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
defer c.Close()
_, err = c.Do("EVAL", "redis.call()", "0")
assert(t, err != nil, "no EVAL error")
_, err = c.Do("EVAL", "redis.call({})", "0")
assert(t, err != nil, "no EVAL error")
_, err = c.Do("EVAL", "redis.call(1)", "0")
assert(t, err != nil, "no EVAL error")
}
func TestScript(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
defer c.Close()
var (
script1sha = "a42059b356c875f0717db19a51f6aaca9ae659ea"
script2sha = "1fa00e76656cc152ad327c13fe365858fd7be306" // "return 42"
)
{
v, err := redis.String(c.Do("SCRIPT", "LOAD", "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}"))
ok(t, err)
equals(t, script1sha, v)
}
{
v, err := redis.String(c.Do("SCRIPT", "LOAD", "return 42"))
ok(t, err)
equals(t, script2sha, v)
}
{
v, err := redis.Int64s(c.Do("SCRIPT", "EXISTS", script1sha, script2sha, "invalid sha"))
ok(t, err)
equals(t, []int64{1, 1, 0}, v)
}
{
v, err := redis.String(c.Do("SCRIPT", "FLUSH"))
ok(t, err)
equals(t, "OK", v)
}
{
v, err := redis.Int64s(c.Do("SCRIPT", "EXISTS", script1sha))
ok(t, err)
equals(t, []int64{0}, v)
}
{
v, err := redis.Int64s(c.Do("SCRIPT", "EXISTS"))
ok(t, err)
equals(t, []int64{}, v)
}
_, err = c.Do("SCRIPT")
mustFail(t, err, errWrongNumber("script"))
_, err = c.Do("SCRIPT", "LOAD")
mustFail(t, err, "ERR Unknown subcommand or wrong number of arguments for 'LOAD'. Try SCRIPT HELP.")
_, err = c.Do("SCRIPT", "LOAD", "return 42", "FOO")
mustFail(t, err, "ERR Unknown subcommand or wrong number of arguments for 'LOAD'. Try SCRIPT HELP.")
_, err = c.Do("SCRIPT", "LOAD", "[")
assert(t, err != nil, "no SCRIPT lOAD error")
_, err = c.Do("SCRIPT", "FLUSH", "1")
mustFail(t, err, "ERR Unknown subcommand or wrong number of arguments for 'FLUSH'. Try SCRIPT HELP.")
_, err = c.Do("SCRIPT", "FOO")
mustFail(t, err, "ERR Unknown subcommand or wrong number of arguments for 'FOO'. Try SCRIPT HELP.")
}
func TestCJSON(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
defer c.Close()
test := func(expr, want string) {
t.Helper()
str, err := redis.String(c.Do("EVAL", expr, 0))
ok(t, err)
equals(t, str, want)
}
test(
`return cjson.decode('{"id":"foo"}')['id']`,
"foo",
)
test(
`return cjson.encode({foo=42})`,
`{"foo":42}`,
)
_, err = c.Do("EVAL", `redis.encode()`, 0)
assert(t, err != nil, "lua error")
_, err = c.Do("EVAL", `redis.encode("1", "2")`, 0)
assert(t, err != nil, "lua error")
_, err = c.Do("EVAL", `redis.decode()`, 0)
assert(t, err != nil, "lua error")
_, err = c.Do("EVAL", `redis.decode("{")`, 0)
assert(t, err != nil, "lua error")
_, err = c.Do("EVAL", `redis.decode("1", "2")`, 0)
assert(t, err != nil, "lua error")
}
func TestSha1Hex(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
defer c.Close()
test1 := func(val interface{}, want string) {
t.Helper()
str, err := redis.String(c.Do("EVAL", "return redis.sha1hex(ARGV[1])", 0, val))
ok(t, err)
equals(t, str, want)
}
test1("foo", "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
test1("bar", "62cdb7020ff920e5aa642c3d4066950dd1f01f4d")
test1("0", "b6589fc6ab0dc82cf12099d1c2d40ab994e8410c")
test1(0, "b6589fc6ab0dc82cf12099d1c2d40ab994e8410c")
test1(nil, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
test2 := func(eval, want string) {
t.Helper()
have, err := redis.String(c.Do("EVAL", eval, 0))
ok(t, err)
equals(t, have, want)
}
test2("return redis.sha1hex({})", "da39a3ee5e6b4b0d3255bfef95601890afd80709")
test2("return redis.sha1hex(nil)", "da39a3ee5e6b4b0d3255bfef95601890afd80709")
test2("return redis.sha1hex(42)", "92cfceb39d57d914ed8b14d0e37643de0797ae56")
_, err = c.Do("EVAL", "redis.sha1hex()", 0)
assert(t, err != nil, "lua error")
}
func TestEvalsha(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
defer c.Close()
script1sha := "bfbf458525d6a0b19200bfd6db3af481156b367b"
{
v, err := redis.String(c.Do("SCRIPT", "LOAD", "return {KEYS[1],ARGV[1]}"))
ok(t, err)
equals(t, script1sha, v)
}
{
b, err := redis.Strings(c.Do("EVALSHA", script1sha, 1, "key1", "key2"))
ok(t, err)
equals(t, []string{"key1", "key2"}, b)
}
_, err = c.Do("EVALSHA")
mustFail(t, err, errWrongNumber("evalsha"))
_, err = c.Do("EVALSHA", "foo")
mustFail(t, err, errWrongNumber("evalsha"))
_, err = c.Do("EVALSHA", "foo", 0)
mustFail(t, err, msgNoScriptFound)
_, err = c.Do("EVALSHA", script1sha, script1sha)
mustFail(t, err, msgInvalidInt)
_, err = c.Do("EVALSHA", script1sha, -1)
mustFail(t, err, msgNegativeKeysNumber)
_, err = c.Do("EVALSHA", script1sha, 1)
mustFail(t, err, msgInvalidKeysNumber)
_, err = c.Do("EVALSHA", "foo", 1, "bar")
mustFail(t, err, msgNoScriptFound)
}
func TestCmdEvalReply(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
defer c.Close()
test := func(script string, args []interface{}, expected interface{}) {
t.Helper()
reply, err := c.Do("EVAL", append([]interface{}{script}, args...)...)
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
equals(t, expected, reply)
}
// return nil
test(
"",
[]interface{}{
0,
},
nil,
)
// return boolean true
test(
"return true",
[]interface{}{
0,
},
int64(1),
)
// return boolean false
test(
"return false",
[]interface{}{
0,
},
nil,
)
// return single number
test(
"return 10",
[]interface{}{
0,
},
int64(10),
)
// return single float
test(
"return 12.345",
[]interface{}{
0,
},
int64(12),
)
// return multiple numbers
test(
"return 10, 20",
[]interface{}{
0,
},
int64(10),
)
// return single string
test(
"return 'test'",
[]interface{}{
0,
},
[]byte("test"),
)
// return multiple string
test(
"return 'test1', 'test2'",
[]interface{}{
0,
},
[]byte("test1"),
)
// return single table multiple integer
test(
"return {10, 20}",
[]interface{}{
0,
},
[]interface{}{
int64(10),
int64(20),
},
)
// return single table multiple string
test(
"return {'test1', 'test2'}",
[]interface{}{
0,
},
[]interface{}{
[]byte("test1"),
[]byte("test2"),
},
)
// return nested table
test(
"return {10, 20, {30, 40}}",
[]interface{}{
0,
},
[]interface{}{
int64(10),
int64(20),
[]interface{}{
int64(30),
int64(40),
},
},
)
// return combination table
test(
"return {10, 20, {30, 'test', true, 40}, false}",
[]interface{}{
0,
},
[]interface{}{
int64(10),
int64(20),
[]interface{}{
int64(30),
[]byte("test"),
int64(1),
int64(40),
},
nil,
},
)
// KEYS and ARGV
test(
"return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}",
[]interface{}{
2,
"key1",
"key2",
"first",
"second",
},
[]interface{}{
[]byte("key1"),
[]byte("key2"),
[]byte("first"),
[]byte("second"),
},
)
{
_, err := c.Do("EVAL", `return {err="broken"}`, 0)
mustFail(t, err, "broken")
_, err = c.Do("EVAL", `return redis.error_reply("broken")`, 0)
mustFail(t, err, "broken")
}
{
v, err := redis.String(c.Do("EVAL", `return {ok="good"}`, 0))
ok(t, err)
equals(t, "good", v)
v, err = redis.String(c.Do("EVAL", `return redis.status_reply("good")`, 0))
ok(t, err)
equals(t, "good", v)
}
_, err = c.Do("EVAL", `return redis.error_reply()`, 0)
assert(t, err != nil, "no EVAL error")
_, err = c.Do("EVAL", `return redis.error_reply(1)`, 0)
assert(t, err != nil, "no EVAL error")
_, err = c.Do("EVAL", `return redis.status_reply()`, 0)
assert(t, err != nil, "no EVAL error")
_, err = c.Do("EVAL", `return redis.status_reply(1)`, 0)
assert(t, err != nil, "no EVAL error")
}
func TestCmdEvalResponse(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
defer c.Close()
{
v, err := redis.String(c.Do("EVAL", "return redis.call('set','foo','bar')", 0))
ok(t, err)
equals(t, "OK", v)
}
{
v, err := redis.String(c.Do("EVAL", "return redis.call('get','foo')", 0))
ok(t, err)
equals(t, "bar", v)
}
{
v, err := c.Do("EVAL", "return redis.call('get','nosuch')", 0)
ok(t, err)
equals(t, nil, v)
}
{
v, err := redis.String(c.Do("EVAL", "return redis.call('HMSET', 'mkey', 'foo','bar','foo1','bar1')", 0))
ok(t, err)
equals(t, "OK", v)
}
{
v, err := redis.Strings(c.Do("EVAL", "return redis.call('HGETALL','mkey')", 0))
ok(t, err)
equals(t, []string{"foo", "bar", "foo1", "bar1"}, v)
}
{
v, err := redis.Strings(c.Do("EVAL", "return redis.call('HMGET','mkey', 'foo1')", 0))
ok(t, err)
equals(t, []string{"bar1"}, v)
}
{
v, err := redis.Strings(c.Do("EVAL", "return redis.call('HMGET','mkey', 'foo')", 0))
ok(t, err)
equals(t, []string{"bar"}, v)
}
{
v, err := c.Do("EVAL", "return redis.call('HMGET','mkey', 'bad', 'key')", 0)
ok(t, err)
equals(t, []interface{}{nil, nil}, v)
}
}
func TestCmdEvalAuth(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
defer c.Close()
eval := "return redis.call('set','foo','bar')"
s.RequireAuth("123password")
_, err = c.Do("EVAL", eval, 0)
mustFail(t, err, "NOAUTH Authentication required.")
_, err = c.Do("AUTH", "123password")
ok(t, err)
_, err = c.Do("EVAL", eval, 0)
ok(t, err)
}
func TestLuaReplicate(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
defer c.Close()
_, err = c.Do("EVAL", "redis.replicate_commands()", 0)
ok(t, err)
}
func TestLuaTX(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
defer c.Close()
// EVAL
{
b, err := redis.String(c.Do("MULTI"))
ok(t, err)
equals(t, "OK", b)
b, err = redis.String(c.Do("EVAL", "return {ARGV[1]}", 0, "key1"))
ok(t, err)
equals(t, "QUEUED", b)
v, err := redis.Values(c.Do("EXEC"))
ok(t, err)
equals(t, 1, len(redis.Args(v)))
v0, err := redis.Strings(v[0], nil)
ok(t, err)
equals(t, []string{"key1"}, v0)
}
// EVALSHA
{
script1sha := "bfbf458525d6a0b19200bfd6db3af481156b367b"
b, err := redis.String(c.Do("MULTI"))
ok(t, err)
equals(t, "OK", b)
b, err = redis.String(c.Do("SCRIPT", "LOAD", "return {KEYS[1],ARGV[1]}"))
ok(t, err)
equals(t, "QUEUED", b)
b, err = redis.String(c.Do("EVALSHA", script1sha, 1, "key1", "key2"))
ok(t, err)
equals(t, "QUEUED", b)
v, err := redis.Values(c.Do("EXEC"))
ok(t, err)
equals(t, 2, len(redis.Args(v)))
v0, err := redis.String(v[0], nil)
ok(t, err)
v1, err := redis.Strings(v[1], nil)
ok(t, err)
equals(t, script1sha, v0) // SCRIPT
equals(t, []string{"key1", "key2"}, v1) // EVALSHA
}
// compiling is done inside the transaction
{
b, err := redis.String(c.Do("SET", "foo", "12"))
ok(t, err)
equals(t, "OK", b)
b, err = redis.String(c.Do("MULTI"))
ok(t, err)
equals(t, "OK", b)
b, err = redis.String(c.Do("SCRIPT", "LOAD", "foobar"))
ok(t, err)
equals(t, "QUEUED", b)
b, err = redis.String(c.Do("GET", "foo"))
ok(t, err)
equals(t, "QUEUED", b)
v, err := redis.Values(c.Do("EXEC"))
ok(t, err)
equals(t, 2, len(redis.Args(v)))
_, err = redis.String(v[0], nil)
mustFail(t, err, "ERR Error compiling script (new function): user_script at EOF: parse error ")
v1, err := redis.String(v[1], nil)
ok(t, err)
equals(t, "12", v1)
}
// misc SCRIPT subcommands
{
b, err := redis.String(c.Do("MULTI"))
ok(t, err)
equals(t, "OK", b)
b, err = redis.String(c.Do("SCRIPT", "EXISTS", "123"))
ok(t, err)
equals(t, "QUEUED", b)
b, err = redis.String(c.Do("SCRIPT", "FLUSH"))
ok(t, err)
equals(t, "QUEUED", b)
v, err := redis.Values(c.Do("EXEC"))
ok(t, err)
equals(t, 2, len(redis.Args(v)))
}
}