2
0

redis-cli.tcl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. start_server {tags {"cli"}} {
  2. proc open_cli {} {
  3. set ::env(TERM) dumb
  4. set fd [open [format "|src/redis-cli -p %d -n 9" [srv port]] "r+"]
  5. fconfigure $fd -buffering none
  6. fconfigure $fd -blocking false
  7. fconfigure $fd -translation binary
  8. assert_equal "redis> " [read_cli $fd]
  9. set _ $fd
  10. }
  11. proc close_cli {fd} {
  12. close $fd
  13. }
  14. proc read_cli {fd} {
  15. set buf [read $fd]
  16. while {[string length $buf] == 0} {
  17. # wait some time and try again
  18. after 10
  19. set buf [read $fd]
  20. }
  21. set _ $buf
  22. }
  23. proc write_cli {fd buf} {
  24. puts $fd $buf
  25. flush $fd
  26. }
  27. # Helpers to run tests in interactive mode
  28. proc run_command {fd cmd} {
  29. write_cli $fd $cmd
  30. set lines [split [read_cli $fd] "\n"]
  31. assert_equal "redis> " [lindex $lines end]
  32. join [lrange $lines 0 end-1] "\n"
  33. }
  34. proc test_interactive_cli {name code} {
  35. set ::env(FAKETTY) 1
  36. set fd [open_cli]
  37. test "Interactive CLI: $name" $code
  38. close_cli $fd
  39. unset ::env(FAKETTY)
  40. }
  41. # Helpers to run tests where stdout is not a tty
  42. proc write_tmpfile {contents} {
  43. set tmp [tmpfile "cli"]
  44. set tmpfd [open $tmp "w"]
  45. puts -nonewline $tmpfd $contents
  46. close $tmpfd
  47. set _ $tmp
  48. }
  49. proc _run_cli {opts args} {
  50. set cmd [format "src/redis-cli -p %d -n 9 $args" [srv port]]
  51. foreach {key value} $opts {
  52. if {$key eq "pipe"} {
  53. set cmd "sh -c \"$value | $cmd\""
  54. }
  55. if {$key eq "path"} {
  56. set cmd "$cmd < $value"
  57. }
  58. }
  59. set fd [open "|$cmd" "r"]
  60. fconfigure $fd -buffering none
  61. fconfigure $fd -translation binary
  62. set resp [read $fd 1048576]
  63. close $fd
  64. set _ $resp
  65. }
  66. proc run_cli {args} {
  67. _run_cli {} {*}$args
  68. }
  69. proc run_cli_with_input_pipe {cmd args} {
  70. _run_cli [list pipe $cmd] {*}$args
  71. }
  72. proc run_cli_with_input_file {path args} {
  73. _run_cli [list path $path] {*}$args
  74. }
  75. proc test_nontty_cli {name code} {
  76. test "Non-interactive non-TTY CLI: $name" $code
  77. }
  78. # Helpers to run tests where stdout is a tty (fake it)
  79. proc test_tty_cli {name code} {
  80. set ::env(FAKETTY) 1
  81. test "Non-interactive TTY CLI: $name" $code
  82. unset ::env(FAKETTY)
  83. }
  84. test_interactive_cli "INFO response should be printed raw" {
  85. set lines [split [run_command $fd info] "\n"]
  86. foreach line $lines {
  87. assert [regexp {^[a-z0-9_]+:[a-z0-9_]+} $line]
  88. }
  89. }
  90. test_interactive_cli "Status reply" {
  91. assert_equal "OK" [run_command $fd "set key foo"]
  92. }
  93. test_interactive_cli "Integer reply" {
  94. assert_equal "(integer) 1" [run_command $fd "incr counter"]
  95. }
  96. test_interactive_cli "Bulk reply" {
  97. r set key foo
  98. assert_equal "\"foo\"" [run_command $fd "get key"]
  99. }
  100. test_interactive_cli "Multi-bulk reply" {
  101. r rpush list foo
  102. r rpush list bar
  103. assert_equal "1. \"foo\"\n2. \"bar\"" [run_command $fd "lrange list 0 -1"]
  104. }
  105. test_interactive_cli "Parsing quotes" {
  106. assert_equal "OK" [run_command $fd "set key \"bar\""]
  107. assert_equal "bar" [r get key]
  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 \"\tbar\t\""]
  113. assert_equal "\tbar\t" [r get key]
  114. # invalid quotation
  115. assert_equal "Invalid argument(s)" [run_command $fd "get \"\"key"]
  116. assert_equal "Invalid argument(s)" [run_command $fd "get \"key\"x"]
  117. # quotes after the argument are weird, but should be allowed
  118. assert_equal "OK" [run_command $fd "set key\"\" bar"]
  119. assert_equal "bar" [r get key]
  120. }
  121. test_tty_cli "Status reply" {
  122. assert_equal "OK\n" [run_cli set key bar]
  123. assert_equal "bar" [r get key]
  124. }
  125. test_tty_cli "Integer reply" {
  126. r del counter
  127. assert_equal "(integer) 1\n" [run_cli incr counter]
  128. }
  129. test_tty_cli "Bulk reply" {
  130. r set key "tab\tnewline\n"
  131. assert_equal "\"tab\\tnewline\\n\"\n" [run_cli get key]
  132. }
  133. test_tty_cli "Multi-bulk reply" {
  134. r del list
  135. r rpush list foo
  136. r rpush list bar
  137. assert_equal "1. \"foo\"\n2. \"bar\"\n" [run_cli lrange list 0 -1]
  138. }
  139. test_tty_cli "Read last argument from pipe" {
  140. assert_equal "OK\n" [run_cli_with_input_pipe "echo foo" set key]
  141. assert_equal "foo\n" [r get key]
  142. }
  143. test_tty_cli "Read last argument from file" {
  144. set tmpfile [write_tmpfile "from file"]
  145. assert_equal "OK\n" [run_cli_with_input_file $tmpfile set key]
  146. assert_equal "from file" [r get key]
  147. }
  148. test_nontty_cli "Status reply" {
  149. assert_equal "OK" [run_cli set key bar]
  150. assert_equal "bar" [r get key]
  151. }
  152. test_nontty_cli "Integer reply" {
  153. r del counter
  154. assert_equal "1" [run_cli incr counter]
  155. }
  156. test_nontty_cli "Bulk reply" {
  157. r set key "tab\tnewline\n"
  158. assert_equal "tab\tnewline\n" [run_cli get key]
  159. }
  160. test_nontty_cli "Multi-bulk reply" {
  161. r del list
  162. r rpush list foo
  163. r rpush list bar
  164. assert_equal "foo\nbar" [run_cli lrange list 0 -1]
  165. }
  166. test_nontty_cli "Read last argument from pipe" {
  167. assert_equal "OK" [run_cli_with_input_pipe "echo foo" set key]
  168. assert_equal "foo\n" [r get key]
  169. }
  170. test_nontty_cli "Read last argument from file" {
  171. set tmpfile [write_tmpfile "from file"]
  172. assert_equal "OK" [run_cli_with_input_file $tmpfile set key]
  173. assert_equal "from file" [r get key]
  174. }
  175. }