redis-cli.tcl 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. source tests/support/cli.tcl
  2. start_server {tags {"cli"}} {
  3. proc open_cli {{opts "-n 9"}} {
  4. set ::env(TERM) dumb
  5. set cmdline [rediscli [srv port] $opts]
  6. set fd [open "|$cmdline" "r+"]
  7. fconfigure $fd -buffering none
  8. fconfigure $fd -blocking false
  9. fconfigure $fd -translation binary
  10. set _ $fd
  11. }
  12. proc close_cli {fd} {
  13. close $fd
  14. }
  15. proc read_cli {fd} {
  16. set buf [read $fd]
  17. while {[string length $buf] == 0} {
  18. # wait some time and try again
  19. after 10
  20. set buf [read $fd]
  21. }
  22. set _ $buf
  23. }
  24. proc write_cli {fd buf} {
  25. puts $fd $buf
  26. flush $fd
  27. }
  28. # Helpers to run tests in interactive mode
  29. proc format_output {output} {
  30. set _ [string trimright [regsub -all "\r" $output ""] "\n"]
  31. }
  32. proc run_command {fd cmd} {
  33. write_cli $fd $cmd
  34. set _ [format_output [read_cli $fd]]
  35. }
  36. proc test_interactive_cli {name code} {
  37. set ::env(FAKETTY) 1
  38. set fd [open_cli]
  39. test "Interactive CLI: $name" $code
  40. close_cli $fd
  41. unset ::env(FAKETTY)
  42. }
  43. # Helpers to run tests where stdout is not a tty
  44. proc write_tmpfile {contents} {
  45. set tmp [tmpfile "cli"]
  46. set tmpfd [open $tmp "w"]
  47. puts -nonewline $tmpfd $contents
  48. close $tmpfd
  49. set _ $tmp
  50. }
  51. proc _run_cli {opts args} {
  52. set cmd [rediscli [srv port] [list -n 9 {*}$args]]
  53. foreach {key value} $opts {
  54. if {$key eq "pipe"} {
  55. set cmd "sh -c \"$value | $cmd\""
  56. }
  57. if {$key eq "path"} {
  58. set cmd "$cmd < $value"
  59. }
  60. }
  61. set fd [open "|$cmd" "r"]
  62. fconfigure $fd -buffering none
  63. fconfigure $fd -translation binary
  64. set resp [read $fd 1048576]
  65. close $fd
  66. set _ [format_output $resp]
  67. }
  68. proc run_cli {args} {
  69. _run_cli {} {*}$args
  70. }
  71. proc run_cli_with_input_pipe {cmd args} {
  72. _run_cli [list pipe $cmd] -x {*}$args
  73. }
  74. proc run_cli_with_input_file {path args} {
  75. _run_cli [list path $path] -x {*}$args
  76. }
  77. proc test_nontty_cli {name code} {
  78. test "Non-interactive non-TTY CLI: $name" $code
  79. }
  80. # Helpers to run tests where stdout is a tty (fake it)
  81. proc test_tty_cli {name code} {
  82. set ::env(FAKETTY) 1
  83. test "Non-interactive TTY CLI: $name" $code
  84. unset ::env(FAKETTY)
  85. }
  86. test_interactive_cli "INFO response should be printed raw" {
  87. set lines [split [run_command $fd info] "\n"]
  88. foreach line $lines {
  89. assert [regexp {^$|^#|^[a-z0-9_]+:.+} $line]
  90. }
  91. }
  92. test_interactive_cli "Status reply" {
  93. assert_equal "OK" [run_command $fd "set key foo"]
  94. }
  95. test_interactive_cli "Integer reply" {
  96. assert_equal "(integer) 1" [run_command $fd "incr counter"]
  97. }
  98. test_interactive_cli "Bulk reply" {
  99. r set key foo
  100. assert_equal "\"foo\"" [run_command $fd "get key"]
  101. }
  102. test_interactive_cli "Multi-bulk reply" {
  103. r rpush list foo
  104. r rpush list bar
  105. assert_equal "1) \"foo\"\n2) \"bar\"" [run_command $fd "lrange list 0 -1"]
  106. }
  107. test_interactive_cli "Parsing quotes" {
  108. assert_equal "OK" [run_command $fd "set key \"bar\""]
  109. assert_equal "bar" [r get key]
  110. assert_equal "OK" [run_command $fd "set key \" bar \""]
  111. assert_equal " bar " [r get key]
  112. assert_equal "OK" [run_command $fd "set key \"\\\"bar\\\"\""]
  113. assert_equal "\"bar\"" [r get key]
  114. assert_equal "OK" [run_command $fd "set key \"\tbar\t\""]
  115. assert_equal "\tbar\t" [r get key]
  116. # invalid quotation
  117. assert_equal "Invalid argument(s)" [run_command $fd "get \"\"key"]
  118. assert_equal "Invalid argument(s)" [run_command $fd "get \"key\"x"]
  119. # quotes after the argument are weird, but should be allowed
  120. assert_equal "OK" [run_command $fd "set key\"\" bar"]
  121. assert_equal "bar" [r get key]
  122. }
  123. test_tty_cli "Status reply" {
  124. assert_equal "OK" [run_cli set key bar]
  125. assert_equal "bar" [r get key]
  126. }
  127. test_tty_cli "Integer reply" {
  128. r del counter
  129. assert_equal "(integer) 1" [run_cli incr counter]
  130. }
  131. test_tty_cli "Bulk reply" {
  132. r set key "tab\tnewline\n"
  133. assert_equal "\"tab\\tnewline\\n\"" [run_cli get key]
  134. }
  135. test_tty_cli "Multi-bulk reply" {
  136. r del list
  137. r rpush list foo
  138. r rpush list bar
  139. assert_equal "1) \"foo\"\n2) \"bar\"" [run_cli lrange list 0 -1]
  140. }
  141. test_tty_cli "Read last argument from pipe" {
  142. assert_equal "OK" [run_cli_with_input_pipe "echo foo" set key]
  143. assert_equal "foo\n" [r get key]
  144. }
  145. test_tty_cli "Read last argument from file" {
  146. set tmpfile [write_tmpfile "from file"]
  147. assert_equal "OK" [run_cli_with_input_file $tmpfile set key]
  148. assert_equal "from file" [r get key]
  149. }
  150. test_nontty_cli "Status reply" {
  151. assert_equal "OK" [run_cli set key bar]
  152. assert_equal "bar" [r get key]
  153. }
  154. test_nontty_cli "Integer reply" {
  155. r del counter
  156. assert_equal "1" [run_cli incr counter]
  157. }
  158. test_nontty_cli "Bulk reply" {
  159. r set key "tab\tnewline\n"
  160. assert_equal "tab\tnewline" [run_cli get key]
  161. }
  162. test_nontty_cli "Multi-bulk reply" {
  163. r del list
  164. r rpush list foo
  165. r rpush list bar
  166. assert_equal "foo\nbar" [run_cli lrange list 0 -1]
  167. }
  168. test_nontty_cli "Read last argument from pipe" {
  169. assert_equal "OK" [run_cli_with_input_pipe "echo foo" set key]
  170. assert_equal "foo\n" [r get key]
  171. }
  172. test_nontty_cli "Read last argument from file" {
  173. set tmpfile [write_tmpfile "from file"]
  174. assert_equal "OK" [run_cli_with_input_file $tmpfile set key]
  175. assert_equal "from file" [r get key]
  176. }
  177. proc test_redis_cli_rdb_dump {} {
  178. r flushdb
  179. set dir [lindex [r config get dir] 1]
  180. assert_equal "OK" [r debug populate 100000 key 1000]
  181. catch {run_cli --rdb "$dir/cli.rdb"} output
  182. assert_match {*Transfer finished with success*} $output
  183. file delete "$dir/dump.rdb"
  184. file rename "$dir/cli.rdb" "$dir/dump.rdb"
  185. assert_equal "OK" [r set should-not-exist 1]
  186. assert_equal "OK" [r debug reload nosave]
  187. assert_equal {} [r get should-not-exist]
  188. }
  189. test_nontty_cli "Dumping an RDB" {
  190. # Disk-based master
  191. assert_match "OK" [r config set repl-diskless-sync no]
  192. test_redis_cli_rdb_dump
  193. # Disk-less master
  194. assert_match "OK" [r config set repl-diskless-sync yes]
  195. assert_match "OK" [r config set repl-diskless-sync-delay 0]
  196. test_redis_cli_rdb_dump
  197. }
  198. test_nontty_cli "Connecting as a replica" {
  199. set fd [open_cli "--replica"]
  200. wait_for_condition 50 500 {
  201. [string match {*slave0:*state=online*} [r info]]
  202. } else {
  203. fail "redis-cli --replica did not connect"
  204. }
  205. for {set i 0} {$i < 100} {incr i} {
  206. r set test-key test-value-$i
  207. }
  208. r client kill type slave
  209. catch {
  210. assert_match {*SET*key-a*} [read_cli $fd]
  211. }
  212. close_cli $fd
  213. }
  214. test_nontty_cli "Piping raw protocol" {
  215. set fd [open_cli "--pipe"]
  216. fconfigure $fd -blocking true
  217. # Create a new deferring client and overwrite its fd
  218. set client [redis [srv 0 "host"] [srv 0 "port"] 1 0]
  219. set ::redis::fd($::redis::id) $fd
  220. $client select 9
  221. r del test-counter
  222. for {set i 0} {$i < 10000} {incr i} {
  223. $client incr test-counter
  224. $client set large-key [string repeat "x" 20000]
  225. }
  226. for {set i 0} {$i < 1000} {incr i} {
  227. $client set very-large-key [string repeat "x" 512000]
  228. }
  229. close $fd write
  230. set output [read_cli $fd]
  231. assert_equal {10000} [r get test-counter]
  232. assert_match {*All data transferred*errors: 0*replies: 21001*} $output
  233. close_cli $fd
  234. }
  235. }