test_helper.tcl 6.0 KB

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