1
0

redis-cli.tcl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. source tests/support/cli.tcl
  2. if {$::singledb} {
  3. set ::dbnum 0
  4. } else {
  5. set ::dbnum 9
  6. }
  7. start_server {tags {"cli"}} {
  8. proc open_cli {{opts ""} {infile ""}} {
  9. if { $opts == "" } {
  10. set opts "-n $::dbnum"
  11. }
  12. set ::env(TERM) dumb
  13. set cmdline [rediscli [srv host] [srv port] $opts]
  14. if {$infile ne ""} {
  15. set cmdline "$cmdline < $infile"
  16. set mode "r"
  17. } else {
  18. set mode "r+"
  19. }
  20. set fd [open "|$cmdline" $mode]
  21. fconfigure $fd -buffering none
  22. fconfigure $fd -blocking false
  23. fconfigure $fd -translation binary
  24. set _ $fd
  25. }
  26. proc close_cli {fd} {
  27. close $fd
  28. }
  29. proc read_cli {fd} {
  30. set ret [read $fd]
  31. while {[string length $ret] == 0} {
  32. after 10
  33. set ret [read $fd]
  34. }
  35. # We may have a short read, try to read some more.
  36. set empty_reads 0
  37. while {$empty_reads < 5} {
  38. set buf [read $fd]
  39. if {[string length $buf] == 0} {
  40. after 10
  41. incr empty_reads
  42. } else {
  43. append ret $buf
  44. set empty_reads 0
  45. }
  46. }
  47. return $ret
  48. }
  49. proc write_cli {fd buf} {
  50. puts $fd $buf
  51. flush $fd
  52. }
  53. # Helpers to run tests in interactive mode
  54. proc format_output {output} {
  55. set _ [string trimright [regsub -all "\r" $output ""] "\n"]
  56. }
  57. proc run_command {fd cmd} {
  58. write_cli $fd $cmd
  59. set _ [format_output [read_cli $fd]]
  60. }
  61. proc test_interactive_cli {name code} {
  62. set ::env(FAKETTY) 1
  63. set fd [open_cli]
  64. test "Interactive CLI: $name" $code
  65. close_cli $fd
  66. unset ::env(FAKETTY)
  67. }
  68. # Helpers to run tests where stdout is not a tty
  69. proc write_tmpfile {contents} {
  70. set tmp [tmpfile "cli"]
  71. set tmpfd [open $tmp "w"]
  72. puts -nonewline $tmpfd $contents
  73. close $tmpfd
  74. set _ $tmp
  75. }
  76. proc _run_cli {host port db opts args} {
  77. set cmd [rediscli $host $port [list -n $db {*}$args]]
  78. foreach {key value} $opts {
  79. if {$key eq "pipe"} {
  80. set cmd "sh -c \"$value | $cmd\""
  81. }
  82. if {$key eq "path"} {
  83. set cmd "$cmd < $value"
  84. }
  85. }
  86. set fd [open "|$cmd" "r"]
  87. fconfigure $fd -buffering none
  88. fconfigure $fd -translation binary
  89. set resp [read $fd 1048576]
  90. close $fd
  91. set _ [format_output $resp]
  92. }
  93. proc run_cli {args} {
  94. _run_cli [srv host] [srv port] $::dbnum {} {*}$args
  95. }
  96. proc run_cli_with_input_pipe {cmd args} {
  97. _run_cli [srv host] [srv port] $::dbnum [list pipe $cmd] -x {*}$args
  98. }
  99. proc run_cli_with_input_file {path args} {
  100. _run_cli [srv host] [srv port] $::dbnum [list path $path] -x {*}$args
  101. }
  102. proc run_cli_host_port_db {host port db args} {
  103. _run_cli $host $port $db {} {*}$args
  104. }
  105. proc test_nontty_cli {name code} {
  106. test "Non-interactive non-TTY CLI: $name" $code
  107. }
  108. # Helpers to run tests where stdout is a tty (fake it)
  109. proc test_tty_cli {name code} {
  110. set ::env(FAKETTY) 1
  111. test "Non-interactive TTY CLI: $name" $code
  112. unset ::env(FAKETTY)
  113. }
  114. test_interactive_cli "INFO response should be printed raw" {
  115. set lines [split [run_command $fd info] "\n"]
  116. foreach line $lines {
  117. if {![regexp {^$|^#|^[^#:]+:} $line]} {
  118. fail "Malformed info line: $line"
  119. }
  120. }
  121. }
  122. test_interactive_cli "Status reply" {
  123. assert_equal "OK" [run_command $fd "set key foo"]
  124. }
  125. test_interactive_cli "Integer reply" {
  126. assert_equal "(integer) 1" [run_command $fd "incr counter"]
  127. }
  128. test_interactive_cli "Bulk reply" {
  129. r set key foo
  130. assert_equal "\"foo\"" [run_command $fd "get key"]
  131. }
  132. test_interactive_cli "Multi-bulk reply" {
  133. r rpush list foo
  134. r rpush list bar
  135. assert_equal "1) \"foo\"\n2) \"bar\"" [run_command $fd "lrange list 0 -1"]
  136. }
  137. test_interactive_cli "Parsing quotes" {
  138. assert_equal "OK" [run_command $fd "set key \"bar\""]
  139. assert_equal "bar" [r get key]
  140. assert_equal "OK" [run_command $fd "set key \" bar \""]
  141. assert_equal " bar " [r get key]
  142. assert_equal "OK" [run_command $fd "set key \"\\\"bar\\\"\""]
  143. assert_equal "\"bar\"" [r get key]
  144. assert_equal "OK" [run_command $fd "set key \"\tbar\t\""]
  145. assert_equal "\tbar\t" [r get key]
  146. # invalid quotation
  147. assert_equal "Invalid argument(s)" [run_command $fd "get \"\"key"]
  148. assert_equal "Invalid argument(s)" [run_command $fd "get \"key\"x"]
  149. # quotes after the argument are weird, but should be allowed
  150. assert_equal "OK" [run_command $fd "set key\"\" bar"]
  151. assert_equal "bar" [r get key]
  152. }
  153. test_tty_cli "Status reply" {
  154. assert_equal "OK" [run_cli set key bar]
  155. assert_equal "bar" [r get key]
  156. }
  157. test_tty_cli "Integer reply" {
  158. r del counter
  159. assert_equal "(integer) 1" [run_cli incr counter]
  160. }
  161. test_tty_cli "Bulk reply" {
  162. r set key "tab\tnewline\n"
  163. assert_equal "\"tab\\tnewline\\n\"" [run_cli get key]
  164. }
  165. test_tty_cli "Multi-bulk reply" {
  166. r del list
  167. r rpush list foo
  168. r rpush list bar
  169. assert_equal "1) \"foo\"\n2) \"bar\"" [run_cli lrange list 0 -1]
  170. }
  171. test_tty_cli "Read last argument from pipe" {
  172. assert_equal "OK" [run_cli_with_input_pipe "echo foo" set key]
  173. assert_equal "foo\n" [r get key]
  174. }
  175. test_tty_cli "Read last argument from file" {
  176. set tmpfile [write_tmpfile "from file"]
  177. assert_equal "OK" [run_cli_with_input_file $tmpfile set key]
  178. assert_equal "from file" [r get key]
  179. file delete $tmpfile
  180. }
  181. test_nontty_cli "Status reply" {
  182. assert_equal "OK" [run_cli set key bar]
  183. assert_equal "bar" [r get key]
  184. }
  185. test_nontty_cli "Integer reply" {
  186. r del counter
  187. assert_equal "1" [run_cli incr counter]
  188. }
  189. test_nontty_cli "Bulk reply" {
  190. r set key "tab\tnewline\n"
  191. assert_equal "tab\tnewline" [run_cli get key]
  192. }
  193. test_nontty_cli "Multi-bulk reply" {
  194. r del list
  195. r rpush list foo
  196. r rpush list bar
  197. assert_equal "foo\nbar" [run_cli lrange list 0 -1]
  198. }
  199. if {!$::tls} { ;# fake_redis_node doesn't support TLS
  200. test_nontty_cli "ASK redirect test" {
  201. # Set up two fake Redis nodes.
  202. set tclsh [info nameofexecutable]
  203. set script "tests/helpers/fake_redis_node.tcl"
  204. set port1 [find_available_port $::baseport $::portcount]
  205. set port2 [find_available_port $::baseport $::portcount]
  206. set p1 [exec $tclsh $script $port1 \
  207. "SET foo bar" "-ASK 12182 127.0.0.1:$port2" &]
  208. set p2 [exec $tclsh $script $port2 \
  209. "ASKING" "+OK" \
  210. "SET foo bar" "+OK" &]
  211. # Make sure both fake nodes have started listening
  212. wait_for_condition 50 50 {
  213. [catch {close [socket "127.0.0.1" $port1]}] == 0 && \
  214. [catch {close [socket "127.0.0.1" $port2]}] == 0
  215. } else {
  216. fail "Failed to start fake Redis nodes"
  217. }
  218. # Run the cli
  219. assert_equal "OK" [run_cli_host_port_db "127.0.0.1" $port1 0 -c SET foo bar]
  220. }
  221. }
  222. test_nontty_cli "Quoted input arguments" {
  223. r set "\x00\x00" "value"
  224. assert_equal "value" [run_cli --quoted-input get {"\x00\x00"}]
  225. }
  226. test_nontty_cli "No accidental unquoting of input arguments" {
  227. run_cli --quoted-input set {"\x41\x41"} quoted-val
  228. run_cli set {"\x41\x41"} unquoted-val
  229. assert_equal "quoted-val" [r get AA]
  230. assert_equal "unquoted-val" [r get {"\x41\x41"}]
  231. }
  232. test_nontty_cli "Invalid quoted input arguments" {
  233. catch {run_cli --quoted-input set {"Unterminated}} err
  234. assert_match {*exited abnormally*} $err
  235. # A single arg that unquotes to two arguments is also not expected
  236. catch {run_cli --quoted-input set {"arg1" "arg2"}} err
  237. assert_match {*exited abnormally*} $err
  238. }
  239. test_nontty_cli "Read last argument from pipe" {
  240. assert_equal "OK" [run_cli_with_input_pipe "echo foo" set key]
  241. assert_equal "foo\n" [r get key]
  242. }
  243. test_nontty_cli "Read last argument from file" {
  244. set tmpfile [write_tmpfile "from file"]
  245. assert_equal "OK" [run_cli_with_input_file $tmpfile set key]
  246. assert_equal "from file" [r get key]
  247. file delete $tmpfile
  248. }
  249. proc test_redis_cli_rdb_dump {} {
  250. r flushdb
  251. set dir [lindex [r config get dir] 1]
  252. assert_equal "OK" [r debug populate 100000 key 1000]
  253. catch {run_cli --rdb "$dir/cli.rdb"} output
  254. assert_match {*Transfer finished with success*} $output
  255. file delete "$dir/dump.rdb"
  256. file rename "$dir/cli.rdb" "$dir/dump.rdb"
  257. assert_equal "OK" [r set should-not-exist 1]
  258. assert_equal "OK" [r debug reload nosave]
  259. assert_equal {} [r get should-not-exist]
  260. }
  261. test "Dumping an RDB" {
  262. # Disk-based master
  263. assert_match "OK" [r config set repl-diskless-sync no]
  264. test_redis_cli_rdb_dump
  265. # Disk-less master
  266. assert_match "OK" [r config set repl-diskless-sync yes]
  267. assert_match "OK" [r config set repl-diskless-sync-delay 0]
  268. test_redis_cli_rdb_dump
  269. } {} {needs:repl}
  270. test "Scan mode" {
  271. r flushdb
  272. populate 1000 key: 1
  273. # basic use
  274. assert_equal 1000 [llength [split [run_cli --scan]]]
  275. # pattern
  276. assert_equal {key:2} [run_cli --scan --pattern "*:2"]
  277. # pattern matching with a quoted string
  278. assert_equal {key:2} [run_cli --scan --quoted-pattern {"*:\x32"}]
  279. }
  280. proc test_redis_cli_repl {} {
  281. set fd [open_cli "--replica"]
  282. wait_for_condition 500 100 {
  283. [string match {*slave0:*state=online*} [r info]]
  284. } else {
  285. fail "redis-cli --replica did not connect"
  286. }
  287. for {set i 0} {$i < 100} {incr i} {
  288. r set test-key test-value-$i
  289. }
  290. wait_for_condition 500 100 {
  291. [string match {*test-value-99*} [read_cli $fd]]
  292. } else {
  293. fail "redis-cli --replica didn't read commands"
  294. }
  295. fconfigure $fd -blocking true
  296. r client kill type slave
  297. catch { close_cli $fd } err
  298. assert_match {*Server closed the connection*} $err
  299. }
  300. test "Connecting as a replica" {
  301. # Disk-based master
  302. assert_match "OK" [r config set repl-diskless-sync no]
  303. test_redis_cli_repl
  304. # Disk-less master
  305. assert_match "OK" [r config set repl-diskless-sync yes]
  306. assert_match "OK" [r config set repl-diskless-sync-delay 0]
  307. test_redis_cli_repl
  308. } {} {needs:repl}
  309. test "Piping raw protocol" {
  310. set cmds [tmpfile "cli_cmds"]
  311. set cmds_fd [open $cmds "w"]
  312. set cmds_count 2101
  313. if {!$::singledb} {
  314. puts $cmds_fd [formatCommand select 9]
  315. incr cmds_count
  316. }
  317. puts $cmds_fd [formatCommand del test-counter]
  318. for {set i 0} {$i < 1000} {incr i} {
  319. puts $cmds_fd [formatCommand incr test-counter]
  320. puts $cmds_fd [formatCommand set large-key [string repeat "x" 20000]]
  321. }
  322. for {set i 0} {$i < 100} {incr i} {
  323. puts $cmds_fd [formatCommand set very-large-key [string repeat "x" 512000]]
  324. }
  325. close $cmds_fd
  326. set cli_fd [open_cli "--pipe" $cmds]
  327. fconfigure $cli_fd -blocking true
  328. set output [read_cli $cli_fd]
  329. assert_equal {1000} [r get test-counter]
  330. assert_match "*All data transferred*errors: 0*replies: ${cmds_count}*" $output
  331. file delete $cmds
  332. }
  333. }