ctl_v3_kv_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package e2e
  15. import (
  16. "fmt"
  17. "testing"
  18. )
  19. func TestCtlV3Put(t *testing.T) { testCtl(t, putTest) }
  20. func TestCtlV3PutNoTLS(t *testing.T) { testCtl(t, putTest, withCfg(configNoTLS)) }
  21. func TestCtlV3PutClientTLS(t *testing.T) { testCtl(t, putTest, withCfg(configClientTLS)) }
  22. func TestCtlV3PutClientAutoTLS(t *testing.T) { testCtl(t, putTest, withCfg(configClientAutoTLS)) }
  23. func TestCtlV3PutPeerTLS(t *testing.T) { testCtl(t, putTest, withCfg(configPeerTLS)) }
  24. func TestCtlV3PutTimeout(t *testing.T) { testCtl(t, putTest, withDialTimeout(0)) }
  25. func TestCtlV3PutClientTLSFlagByEnv(t *testing.T) {
  26. testCtl(t, putTest, withCfg(configClientTLS), withFlagByEnv())
  27. }
  28. func TestCtlV3Get(t *testing.T) { testCtl(t, getTest) }
  29. func TestCtlV3GetNoTLS(t *testing.T) { testCtl(t, getTest, withCfg(configNoTLS)) }
  30. func TestCtlV3GetClientTLS(t *testing.T) { testCtl(t, getTest, withCfg(configClientTLS)) }
  31. func TestCtlV3GetClientAutoTLS(t *testing.T) { testCtl(t, getTest, withCfg(configClientAutoTLS)) }
  32. func TestCtlV3GetPeerTLS(t *testing.T) { testCtl(t, getTest, withCfg(configPeerTLS)) }
  33. func TestCtlV3GetTimeout(t *testing.T) { testCtl(t, getTest, withDialTimeout(0)) }
  34. func TestCtlV3GetQuorum(t *testing.T) { testCtl(t, getTest, withQuorum()) }
  35. func TestCtlV3GetFormat(t *testing.T) { testCtl(t, getFormatTest) }
  36. func TestCtlV3GetRev(t *testing.T) { testCtl(t, getRevTest) }
  37. func TestCtlV3GetKeysOnly(t *testing.T) { testCtl(t, getKeysOnlyTest) }
  38. func TestCtlV3Del(t *testing.T) { testCtl(t, delTest) }
  39. func TestCtlV3DelNoTLS(t *testing.T) { testCtl(t, delTest, withCfg(configNoTLS)) }
  40. func TestCtlV3DelClientTLS(t *testing.T) { testCtl(t, delTest, withCfg(configClientTLS)) }
  41. func TestCtlV3DelPeerTLS(t *testing.T) { testCtl(t, delTest, withCfg(configPeerTLS)) }
  42. func TestCtlV3DelTimeout(t *testing.T) { testCtl(t, delTest, withDialTimeout(0)) }
  43. func putTest(cx ctlCtx) {
  44. key, value := "foo", "bar"
  45. if err := ctlV3Put(cx, key, value, ""); err != nil {
  46. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  47. cx.t.Fatalf("putTest ctlV3Put error (%v)", err)
  48. }
  49. }
  50. if err := ctlV3Get(cx, []string{key}, kv{key, value}); err != nil {
  51. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  52. cx.t.Fatalf("putTest ctlV3Get error (%v)", err)
  53. }
  54. }
  55. }
  56. func getTest(cx ctlCtx) {
  57. var (
  58. kvs = []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}}
  59. revkvs = []kv{{"key3", "val3"}, {"key2", "val2"}, {"key1", "val1"}}
  60. )
  61. for i := range kvs {
  62. if err := ctlV3Put(cx, kvs[i].key, kvs[i].val, ""); err != nil {
  63. cx.t.Fatalf("getTest #%d: ctlV3Put error (%v)", i, err)
  64. }
  65. }
  66. tests := []struct {
  67. args []string
  68. wkv []kv
  69. }{
  70. {[]string{"key1"}, []kv{{"key1", "val1"}}},
  71. {[]string{"", "--prefix"}, kvs},
  72. {[]string{"", "--from-key"}, kvs},
  73. {[]string{"key", "--prefix"}, kvs},
  74. {[]string{"key", "--prefix", "--limit=2"}, kvs[:2]},
  75. {[]string{"key", "--prefix", "--order=ASCEND", "--sort-by=MODIFY"}, kvs},
  76. {[]string{"key", "--prefix", "--order=ASCEND", "--sort-by=VERSION"}, kvs},
  77. {[]string{"key", "--prefix", "--order=DESCEND", "--sort-by=CREATE"}, revkvs},
  78. {[]string{"key", "--prefix", "--order=DESCEND", "--sort-by=KEY"}, revkvs},
  79. }
  80. for i, tt := range tests {
  81. if err := ctlV3Get(cx, tt.args, tt.wkv...); err != nil {
  82. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  83. cx.t.Errorf("getTest #%d: ctlV3Get error (%v)", i, err)
  84. }
  85. }
  86. }
  87. }
  88. func getFormatTest(cx ctlCtx) {
  89. if err := ctlV3Put(cx, "abc", "123", ""); err != nil {
  90. cx.t.Fatal(err)
  91. }
  92. tests := []struct {
  93. format string
  94. valueOnly bool
  95. wstr string
  96. }{
  97. {"simple", false, "abc"},
  98. {"simple", true, "123"},
  99. {"json", false, `"kvs":[{"key":"YWJj"`},
  100. {"protobuf", false, "\x17\b\x93\xe7\xf6\x93\xd4ņ\xe14\x10\xed"},
  101. }
  102. for i, tt := range tests {
  103. cmdArgs := append(cx.PrefixArgs(), "get")
  104. cmdArgs = append(cmdArgs, "--write-out="+tt.format)
  105. if tt.valueOnly {
  106. cmdArgs = append(cmdArgs, "--print-value-only")
  107. }
  108. cmdArgs = append(cmdArgs, "abc")
  109. if err := spawnWithExpect(cmdArgs, tt.wstr); err != nil {
  110. cx.t.Errorf("#%d: error (%v), wanted %v", i, err, tt.wstr)
  111. }
  112. }
  113. }
  114. func getRevTest(cx ctlCtx) {
  115. var (
  116. kvs = []kv{{"key", "val1"}, {"key", "val2"}, {"key", "val3"}}
  117. )
  118. for i := range kvs {
  119. if err := ctlV3Put(cx, kvs[i].key, kvs[i].val, ""); err != nil {
  120. cx.t.Fatalf("getRevTest #%d: ctlV3Put error (%v)", i, err)
  121. }
  122. }
  123. tests := []struct {
  124. args []string
  125. wkv []kv
  126. }{
  127. {[]string{"key", "--rev", "2"}, kvs[:1]},
  128. {[]string{"key", "--rev", "3"}, kvs[1:2]},
  129. {[]string{"key", "--rev", "4"}, kvs[2:]},
  130. }
  131. for i, tt := range tests {
  132. if err := ctlV3Get(cx, tt.args, tt.wkv...); err != nil {
  133. cx.t.Errorf("getTest #%d: ctlV3Get error (%v)", i, err)
  134. }
  135. }
  136. }
  137. func getKeysOnlyTest(cx ctlCtx) {
  138. var (
  139. kvs = []kv{{"key1", "val1"}}
  140. )
  141. for i := range kvs {
  142. if err := ctlV3Put(cx, kvs[i].key, kvs[i].val, ""); err != nil {
  143. cx.t.Fatalf("getKeysOnlyTest #%d: ctlV3Put error (%v)", i, err)
  144. }
  145. }
  146. cmdArgs := append(cx.PrefixArgs(), "get")
  147. cmdArgs = append(cmdArgs, []string{"--prefix", "--keys-only", "key"}...)
  148. err := spawnWithExpects(cmdArgs, []string{"key1", ""}...)
  149. if err != nil {
  150. cx.t.Fatalf("getKeysOnlyTest : error (%v)", err)
  151. }
  152. }
  153. func delTest(cx ctlCtx) {
  154. tests := []struct {
  155. puts []kv
  156. args []string
  157. deletedNum int
  158. }{
  159. { // delete all keys
  160. []kv{{"foo1", "bar"}, {"foo2", "bar"}, {"foo3", "bar"}},
  161. []string{"", "--prefix"},
  162. 3,
  163. },
  164. { // delete all keys
  165. []kv{{"foo1", "bar"}, {"foo2", "bar"}, {"foo3", "bar"}},
  166. []string{"", "--from-key"},
  167. 3,
  168. },
  169. {
  170. []kv{{"this", "value"}},
  171. []string{"that"},
  172. 0,
  173. },
  174. {
  175. []kv{{"sample", "value"}},
  176. []string{"sample"},
  177. 1,
  178. },
  179. {
  180. []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}},
  181. []string{"key", "--prefix"},
  182. 3,
  183. },
  184. {
  185. []kv{{"zoo1", "bar"}, {"zoo2", "bar2"}, {"zoo3", "bar3"}},
  186. []string{"zoo1", "--from-key"},
  187. 3,
  188. },
  189. }
  190. for i, tt := range tests {
  191. for j := range tt.puts {
  192. if err := ctlV3Put(cx, tt.puts[j].key, tt.puts[j].val, ""); err != nil {
  193. cx.t.Fatalf("delTest #%d-%d: ctlV3Put error (%v)", i, j, err)
  194. }
  195. }
  196. if err := ctlV3Del(cx, tt.args, tt.deletedNum); err != nil {
  197. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  198. cx.t.Fatalf("delTest #%d: ctlV3Del error (%v)", i, err)
  199. }
  200. }
  201. }
  202. }
  203. func ctlV3Put(cx ctlCtx, key, value, leaseID string) error {
  204. cmdArgs := append(cx.PrefixArgs(), "put", key, value)
  205. if leaseID != "" {
  206. cmdArgs = append(cmdArgs, "--lease", leaseID)
  207. }
  208. return spawnWithExpect(cmdArgs, "OK")
  209. }
  210. type kv struct {
  211. key, val string
  212. }
  213. func ctlV3Get(cx ctlCtx, args []string, kvs ...kv) error {
  214. cmdArgs := append(cx.PrefixArgs(), "get")
  215. cmdArgs = append(cmdArgs, args...)
  216. if !cx.quorum {
  217. cmdArgs = append(cmdArgs, "--consistency", "s")
  218. }
  219. var lines []string
  220. for _, elem := range kvs {
  221. lines = append(lines, elem.key, elem.val)
  222. }
  223. return spawnWithExpects(cmdArgs, lines...)
  224. }
  225. func ctlV3Del(cx ctlCtx, args []string, num int) error {
  226. cmdArgs := append(cx.PrefixArgs(), "del")
  227. cmdArgs = append(cmdArgs, args...)
  228. return spawnWithExpects(cmdArgs, fmt.Sprintf("%d", num))
  229. }