]>
Commit | Line | Data |
---|---|---|
f2dd4769 PN |
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 | ||
12 | proc close_cli {fd} { | |
13 | close $fd | |
14 | } | |
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 | ||
26 | proc write_cli {fd buf} { | |
27 | puts $fd $buf | |
28 | flush $fd | |
29 | } | |
30 | ||
31 | proc run_command {fd cmd} { | |
32 | write_cli $fd $cmd | |
33 | set lines [split [read_cli $fd] "\n"] | |
34 | assert_equal "redis> " [lindex $lines end] | |
35 | join [lrange $lines 0 end-1] "\n" | |
36 | } | |
37 | ||
38 | proc test_interactive_cli {name code} { | |
39 | set fd [open_cli] | |
40 | test "Interactive CLI: $name" $code | |
41 | close_cli $fd | |
42 | } | |
43 | ||
07242c0c PN |
44 | proc run_cli {args} { |
45 | set fd [open [format "|src/redis-cli -p %d -n 9 $args" [srv port]] "r"] | |
46 | fconfigure $fd -buffering none | |
47 | fconfigure $fd -translation binary | |
48 | set resp [read $fd 1048576] | |
49 | close $fd | |
50 | set _ $resp | |
51 | } | |
52 | ||
53 | proc test_noninteractive_cli {name code} { | |
54 | test "Non-interactive CLI: $name" $code | |
55 | } | |
56 | ||
f2dd4769 PN |
57 | test_interactive_cli "INFO response should be printed raw" { |
58 | set lines [split [run_command $fd info] "\n"] | |
59 | foreach line $lines { | |
60 | assert [regexp {^[a-z0-9_]+:[a-z0-9_]+} $line] | |
61 | } | |
62 | } | |
63 | ||
64 | test_interactive_cli "Status reply" { | |
65 | assert_equal "OK" [run_command $fd "set key foo"] | |
66 | } | |
67 | ||
68 | test_interactive_cli "Integer reply" { | |
69 | assert_equal "(integer) 1" [run_command $fd "incr counter"] | |
70 | } | |
71 | ||
72 | test_interactive_cli "Bulk reply" { | |
73 | r set key foo | |
74 | assert_equal "\"foo\"" [run_command $fd "get key"] | |
75 | } | |
76 | ||
77 | test_interactive_cli "Multi-bulk reply" { | |
78 | r rpush list foo | |
79 | r rpush list bar | |
80 | assert_equal "1. \"foo\"\n2. \"bar\"" [run_command $fd "lrange list 0 -1"] | |
81 | } | |
0439d792 PN |
82 | |
83 | test_interactive_cli "Parsing quotes" { | |
84 | assert_equal "OK" [run_command $fd "set key \"bar\""] | |
85 | assert_equal "bar" [r get key] | |
86 | assert_equal "OK" [run_command $fd "set key \" bar \""] | |
87 | assert_equal " bar " [r get key] | |
88 | assert_equal "OK" [run_command $fd "set key \"\\\"bar\\\"\""] | |
89 | assert_equal "\"bar\"" [r get key] | |
90 | assert_equal "OK" [run_command $fd "set key \"\tbar\t\""] | |
91 | assert_equal "\tbar\t" [r get key] | |
92 | ||
93 | # invalid quotation | |
94 | assert_equal "Invalid argument(s)" [run_command $fd "get \"\"key"] | |
95 | assert_equal "Invalid argument(s)" [run_command $fd "get \"key\"x"] | |
96 | ||
97 | # quotes after the argument are weird, but should be allowed | |
98 | assert_equal "OK" [run_command $fd "set key\"\" bar"] | |
99 | assert_equal "bar" [r get key] | |
100 | } | |
07242c0c PN |
101 | |
102 | test_noninteractive_cli "Status reply" { | |
103 | assert_equal "OK\n" [run_cli set key bar] | |
104 | assert_equal "bar" [r get key] | |
105 | } | |
106 | ||
107 | test_noninteractive_cli "Integer reply" { | |
108 | r del counter | |
109 | assert_equal "(integer) 1\n" [run_cli incr counter] | |
110 | } | |
111 | ||
112 | test_noninteractive_cli "Bulk reply" { | |
113 | r set key "tab\tnewline\n" | |
114 | assert_equal "\"tab\\tnewline\\n\"\n" [run_cli get key] | |
115 | } | |
116 | ||
117 | test_noninteractive_cli "Multi-bulk reply" { | |
118 | r del list | |
119 | r rpush list foo | |
120 | r rpush list bar | |
121 | assert_equal "1. \"foo\"\n2. \"bar\"\n" [run_cli lrange list 0 -1] | |
122 | } | |
f2dd4769 | 123 | } |