]> git.saurik.com Git - redis.git/blob - tests/test_helper.tcl
a6c3e83355c01fa60c1db0e01910d47ecca0e872
[redis.git] / tests / test_helper.tcl
1 # Redis test suite. Copyright (C) 2009 Salvatore Sanfilippo antirez@gmail.com
2 # This softare is released under the BSD License. See the COPYING file for
3 # more information.
4
5 set tcl_precision 17
6 source tests/support/redis.tcl
7 source tests/support/server.tcl
8 source tests/support/tmpfile.tcl
9 source tests/support/test.tcl
10 source tests/support/util.tcl
11
12 set ::host 127.0.0.1
13 set ::port 16379
14 set ::traceleaks 0
15 set ::valgrind 0
16 set ::denytags {}
17 set ::allowtags {}
18 set ::external 0; # If "1" this means, we are running against external instance
19 set ::file ""; # If set, runs only the tests in this comma separated list
20
21 proc execute_tests name {
22 source "tests/$name.tcl"
23 }
24
25 # Setup a list to hold a stack of server configs. When calls to start_server
26 # are nested, use "srv 0 pid" to get the pid of the inner server. To access
27 # outer servers, use "srv -1 pid" etcetera.
28 set ::servers {}
29 proc srv {args} {
30 set level 0
31 if {[string is integer [lindex $args 0]]} {
32 set level [lindex $args 0]
33 set property [lindex $args 1]
34 } else {
35 set property [lindex $args 0]
36 }
37 set srv [lindex $::servers end+$level]
38 dict get $srv $property
39 }
40
41 # Provide easy access to the client for the inner server. It's possible to
42 # prepend the argument list with a negative level to access clients for
43 # servers running in outer blocks.
44 proc r {args} {
45 set level 0
46 if {[string is integer [lindex $args 0]]} {
47 set level [lindex $args 0]
48 set args [lrange $args 1 end]
49 }
50 [srv $level "client"] {*}$args
51 }
52
53 proc reconnect {args} {
54 set level [lindex $args 0]
55 if {[string length $level] == 0 || ![string is integer $level]} {
56 set level 0
57 }
58
59 set srv [lindex $::servers end+$level]
60 set host [dict get $srv "host"]
61 set port [dict get $srv "port"]
62 set config [dict get $srv "config"]
63 set client [redis $host $port]
64 dict set srv "client" $client
65
66 # select the right db when we don't have to authenticate
67 if {![dict exists $config "requirepass"]} {
68 $client select 9
69 }
70
71 # re-set $srv in the servers list
72 set ::servers [lreplace $::servers end+$level 1 $srv]
73 }
74
75 proc redis_deferring_client {args} {
76 set level 0
77 if {[llength $args] > 0 && [string is integer [lindex $args 0]]} {
78 set level [lindex $args 0]
79 set args [lrange $args 1 end]
80 }
81
82 # create client that defers reading reply
83 set client [redis [srv $level "host"] [srv $level "port"] 1]
84
85 # select the right db and read the response (OK)
86 $client select 9
87 $client read
88 return $client
89 }
90
91 # Provide easy access to INFO properties. Same semantic as "proc r".
92 proc s {args} {
93 set level 0
94 if {[string is integer [lindex $args 0]]} {
95 set level [lindex $args 0]
96 set args [lrange $args 1 end]
97 }
98 status [srv $level "client"] [lindex $args 0]
99 }
100
101 proc cleanup {} {
102 catch {exec rm -rf {*}[glob tests/tmp/redis.conf.*]}
103 catch {exec rm -rf {*}[glob tests/tmp/server.*]}
104 }
105
106 proc execute_everything {} {
107 execute_tests "unit/auth"
108 execute_tests "unit/protocol"
109 execute_tests "unit/basic"
110 execute_tests "unit/type/list"
111 execute_tests "unit/type/set"
112 execute_tests "unit/type/zset"
113 execute_tests "unit/type/hash"
114 execute_tests "unit/sort"
115 execute_tests "unit/expire"
116 execute_tests "unit/other"
117 execute_tests "unit/cas"
118 execute_tests "integration/replication"
119 execute_tests "integration/aof"
120 # execute_tests "integration/redis-cli"
121 execute_tests "unit/pubsub"
122
123 # run tests with VM enabled
124 set ::global_overrides {vm-enabled yes}
125 execute_tests "unit/protocol"
126 execute_tests "unit/basic"
127 execute_tests "unit/type/list"
128 execute_tests "unit/type/set"
129 execute_tests "unit/type/zset"
130 execute_tests "unit/type/hash"
131 execute_tests "unit/sort"
132 execute_tests "unit/expire"
133 execute_tests "unit/other"
134 execute_tests "unit/cas"
135 }
136
137 proc main {} {
138 cleanup
139
140 if {[string length $::file] > 0} {
141 foreach {file} [split $::file ,] {
142 execute_tests $file
143 }
144 } else {
145 execute_everything
146 }
147
148 cleanup
149 puts "\n[expr $::passed+$::failed] tests, $::passed passed, $::failed failed"
150 if {$::failed > 0} {
151 puts "\n*** WARNING!!! $::failed FAILED TESTS ***\n"
152 exit 1
153 }
154 }
155
156 # parse arguments
157 for {set j 0} {$j < [llength $argv]} {incr j} {
158 set opt [lindex $argv $j]
159 set arg [lindex $argv [expr $j+1]]
160 if {$opt eq {--tags}} {
161 foreach tag $arg {
162 if {[string index $tag 0] eq "-"} {
163 lappend ::denytags [string range $tag 1 end]
164 } else {
165 lappend ::allowtags $tag
166 }
167 }
168 incr j
169 } elseif {$opt eq {--file}} {
170 set ::file $arg
171 incr j
172 } elseif {$opt eq {--host}} {
173 set ::external 1
174 set ::host $arg
175 incr j
176 } elseif {$opt eq {--port}} {
177 set ::port $arg
178 incr j
179 } else {
180 puts "Wrong argument: $opt"
181 exit 1
182 }
183 }
184
185 if {[catch { main } err]} {
186 if {[string length $err] > 0} {
187 # only display error when not generated by the test suite
188 if {$err ne "exception"} {
189 puts $err
190 }
191 exit 1
192 }
193 }