redis-cli.tcl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. source tests/support/cli.tcl
  2. start_server {tags {"cli"}} {
  3. proc open_cli {} {
  4. set ::env(TERM) dumb
  5. set cmdline [rediscli [srv port] "-n 9"]
  6. set fd [open "|$cmdline" "r+"]
  7. fconfigure $fd -buffering none
  8. fconfigure $fd -blocking false
  9. fconfigure $fd -translation binary
  10. assert_equal "redis> " [read_cli $fd]
  11. set _ $fd
  12. }
  13. proc close_cli {fd} {
  14. close $fd
  15. }
  16. proc read_cli {fd} {
  17. set buf [read $fd]
  18. while {[string length $buf] == 0} {
  19. # wait some time and try again
  20. after 10
  21. set buf [read $fd]
  22. }
  23. set _ $buf
  24. }
  25. proc write_cli {fd buf} {
  26. puts $fd $buf
  27. flush $fd
  28. }
  29. # Helpers to run tests in interactive mode
  30. proc run_command {fd cmd} {
  31. write_cli $fd $cmd
  32. set lines [split [read_cli $fd] "\n"]
  33. assert_equal "redis> " [lindex $lines end]
  34. join [lrange $lines 0 end-1] "\n"
  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} $args {
  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 _ $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] {*}$args
  73. }
  74. proc run_cli_with_input_file {path args} {
  75. _run_cli [list path $path] {*}$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_]+:[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\n" [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\n" [run_cli incr counter]
  130. }
  131. test_tty_cli "Bulk reply" {
  132. r set key "tab\tnewline\n"
  133. assert_equal "\"tab\\tnewline\\n\"\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\"\n" [run_cli lrange list 0 -1]
  140. }
  141. test_tty_cli "Read last argument from pipe" {
  142. assert_equal "OK\n" [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\n" [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\n" [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. }