aboutsummaryrefslogtreecommitdiff
path: root/examples/redis-unstable/tests/unit/cluster/scripting.tcl
diff options
context:
space:
mode:
Diffstat (limited to 'examples/redis-unstable/tests/unit/cluster/scripting.tcl')
-rw-r--r--examples/redis-unstable/tests/unit/cluster/scripting.tcl91
1 files changed, 91 insertions, 0 deletions
diff --git a/examples/redis-unstable/tests/unit/cluster/scripting.tcl b/examples/redis-unstable/tests/unit/cluster/scripting.tcl
new file mode 100644
index 0000000..76aa882
--- /dev/null
+++ b/examples/redis-unstable/tests/unit/cluster/scripting.tcl
@@ -0,0 +1,91 @@
1start_cluster 1 0 {tags {external:skip cluster}} {
2
3 test {Eval scripts with shebangs and functions default to no cross slots} {
4 # Test that scripts with shebang block cross slot operations
5 assert_error "ERR Script attempted to access keys that do not hash to the same slot*" {
6 r 0 eval {#!lua
7 redis.call('set', 'foo', 'bar')
8 redis.call('set', 'bar', 'foo')
9 return 'OK'
10 } 0}
11
12 # Test the functions by default block cross slot operations
13 r 0 function load REPLACE {#!lua name=crossslot
14 local function test_cross_slot(keys, args)
15 redis.call('set', 'foo', 'bar')
16 redis.call('set', 'bar', 'foo')
17 return 'OK'
18 end
19
20 redis.register_function('test_cross_slot', test_cross_slot)}
21 assert_error "ERR Script attempted to access keys that do not hash to the same slot*" {r FCALL test_cross_slot 0}
22 }
23
24 test {Cross slot commands are allowed by default for eval scripts and with allow-cross-slot-keys flag} {
25 # Old style lua scripts are allowed to access cross slot operations
26 r 0 eval "redis.call('set', 'foo', 'bar'); redis.call('set', 'bar', 'foo')" 0
27
28 # scripts with allow-cross-slot-keys flag are allowed
29 r 0 eval {#!lua flags=allow-cross-slot-keys
30 redis.call('set', 'foo', 'bar'); redis.call('set', 'bar', 'foo')
31 } 0
32
33 # Retrieve data from different slot to verify data has been stored in the correct dictionary in cluster-enabled setup
34 # during cross-slot operation from the above lua script.
35 assert_equal "bar" [r 0 get foo]
36 assert_equal "foo" [r 0 get bar]
37 r 0 del foo
38 r 0 del bar
39
40 # Functions with allow-cross-slot-keys flag are allowed
41 r 0 function load REPLACE {#!lua name=crossslot
42 local function test_cross_slot(keys, args)
43 redis.call('set', 'foo', 'bar')
44 redis.call('set', 'bar', 'foo')
45 return 'OK'
46 end
47
48 redis.register_function{function_name='test_cross_slot', callback=test_cross_slot, flags={ 'allow-cross-slot-keys' }}}
49 r FCALL test_cross_slot 0
50
51 # Retrieve data from different slot to verify data has been stored in the correct dictionary in cluster-enabled setup
52 # during cross-slot operation from the above lua function.
53 assert_equal "bar" [r 0 get foo]
54 assert_equal "foo" [r 0 get bar]
55 }
56
57 test {Cross slot commands are also blocked if they disagree with pre-declared keys} {
58 assert_error "ERR Script attempted to access keys that do not hash to the same slot*" {
59 r 0 eval {#!lua
60 redis.call('set', 'foo', 'bar')
61 return 'OK'
62 } 1 bar}
63 }
64
65 test {Cross slot commands are allowed by default if they disagree with pre-declared keys} {
66 r 0 flushall
67 r 0 eval "redis.call('set', 'foo', 'bar')" 1 bar
68
69 # Make sure the script writes to the right slot
70 assert_equal 1 [r 0 cluster COUNTKEYSINSLOT 12182] ;# foo slot
71 assert_equal 0 [r 0 cluster COUNTKEYSINSLOT 5061] ;# bar slot
72 }
73
74 test "Function no-cluster flag" {
75 R 0 function load {#!lua name=test
76 redis.register_function{function_name='f1', callback=function() return 'hello' end, flags={'no-cluster'}}
77 }
78 catch {R 0 fcall f1 0} e
79 assert_match {*Can not run script on cluster, 'no-cluster' flag is set*} $e
80 }
81
82 test "Script no-cluster flag" {
83 catch {
84 R 0 eval {#!lua flags=no-cluster
85 return 1
86 } 0
87 } e
88
89 assert_match {*Can not run script on cluster, 'no-cluster' flag is set*} $e
90 }
91}