2
0

redis-cli.tcl 8.2 KB

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