ctl_v3_kv_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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", "--sort-by=CREATE"}, kvs}, // ASCEND by default
  78. {[]string{"key", "--prefix", "--order=DESCEND", "--sort-by=CREATE"}, revkvs},
  79. {[]string{"key", "--prefix", "--order=DESCEND", "--sort-by=KEY"}, revkvs},
  80. }
  81. for i, tt := range tests {
  82. if err := ctlV3Get(cx, tt.args, tt.wkv...); err != nil {
  83. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  84. cx.t.Errorf("getTest #%d: ctlV3Get error (%v)", i, err)
  85. }
  86. }
  87. }
  88. }
  89. func getFormatTest(cx ctlCtx) {
  90. if err := ctlV3Put(cx, "abc", "123", ""); err != nil {
  91. cx.t.Fatal(err)
  92. }
  93. tests := []struct {
  94. format string
  95. valueOnly bool
  96. wstr string
  97. }{
  98. {"simple", false, "abc"},
  99. {"simple", true, "123"},
  100. {"json", false, `"kvs":[{"key":"YWJj"`},
  101. {"protobuf", false, "\x17\b\x93\xe7\xf6\x93\xd4ņ\xe14\x10\xed"},
  102. }
  103. for i, tt := range tests {
  104. cmdArgs := append(cx.PrefixArgs(), "get")
  105. cmdArgs = append(cmdArgs, "--write-out="+tt.format)
  106. if tt.valueOnly {
  107. cmdArgs = append(cmdArgs, "--print-value-only")
  108. }
  109. cmdArgs = append(cmdArgs, "abc")
  110. if err := spawnWithExpect(cmdArgs, tt.wstr); err != nil {
  111. cx.t.Errorf("#%d: error (%v), wanted %v", i, err, tt.wstr)
  112. }
  113. }
  114. }
  115. func getRevTest(cx ctlCtx) {
  116. var (
  117. kvs = []kv{{"key", "val1"}, {"key", "val2"}, {"key", "val3"}}
  118. )
  119. for i := range kvs {
  120. if err := ctlV3Put(cx, kvs[i].key, kvs[i].val, ""); err != nil {
  121. cx.t.Fatalf("getRevTest #%d: ctlV3Put error (%v)", i, err)
  122. }
  123. }
  124. tests := []struct {
  125. args []string
  126. wkv []kv
  127. }{
  128. {[]string{"key", "--rev", "2"}, kvs[:1]},
  129. {[]string{"key", "--rev", "3"}, kvs[1:2]},
  130. {[]string{"key", "--rev", "4"}, kvs[2:]},
  131. }
  132. for i, tt := range tests {
  133. if err := ctlV3Get(cx, tt.args, tt.wkv...); err != nil {
  134. cx.t.Errorf("getTest #%d: ctlV3Get error (%v)", i, err)
  135. }
  136. }
  137. }
  138. func getKeysOnlyTest(cx ctlCtx) {
  139. var (
  140. kvs = []kv{{"key1", "val1"}}
  141. )
  142. for i := range kvs {
  143. if err := ctlV3Put(cx, kvs[i].key, kvs[i].val, ""); err != nil {
  144. cx.t.Fatalf("getKeysOnlyTest #%d: ctlV3Put error (%v)", i, err)
  145. }
  146. }
  147. cmdArgs := append(cx.PrefixArgs(), "get")
  148. cmdArgs = append(cmdArgs, []string{"--prefix", "--keys-only", "key"}...)
  149. err := spawnWithExpects(cmdArgs, []string{"key1", ""}...)
  150. if err != nil {
  151. cx.t.Fatalf("getKeysOnlyTest : error (%v)", err)
  152. }
  153. }
  154. func delTest(cx ctlCtx) {
  155. tests := []struct {
  156. puts []kv
  157. args []string
  158. deletedNum int
  159. }{
  160. { // delete all keys
  161. []kv{{"foo1", "bar"}, {"foo2", "bar"}, {"foo3", "bar"}},
  162. []string{"", "--prefix"},
  163. 3,
  164. },
  165. { // delete all keys
  166. []kv{{"foo1", "bar"}, {"foo2", "bar"}, {"foo3", "bar"}},
  167. []string{"", "--from-key"},
  168. 3,
  169. },
  170. {
  171. []kv{{"this", "value"}},
  172. []string{"that"},
  173. 0,
  174. },
  175. {
  176. []kv{{"sample", "value"}},
  177. []string{"sample"},
  178. 1,
  179. },
  180. {
  181. []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}},
  182. []string{"key", "--prefix"},
  183. 3,
  184. },
  185. {
  186. []kv{{"zoo1", "bar"}, {"zoo2", "bar2"}, {"zoo3", "bar3"}},
  187. []string{"zoo1", "--from-key"},
  188. 3,
  189. },
  190. }
  191. for i, tt := range tests {
  192. for j := range tt.puts {
  193. if err := ctlV3Put(cx, tt.puts[j].key, tt.puts[j].val, ""); err != nil {
  194. cx.t.Fatalf("delTest #%d-%d: ctlV3Put error (%v)", i, j, err)
  195. }
  196. }
  197. if err := ctlV3Del(cx, tt.args, tt.deletedNum); err != nil {
  198. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  199. cx.t.Fatalf("delTest #%d: ctlV3Del error (%v)", i, err)
  200. }
  201. }
  202. }
  203. }
  204. func ctlV3Put(cx ctlCtx, key, value, leaseID string) error {
  205. cmdArgs := append(cx.PrefixArgs(), "put", key, value)
  206. if leaseID != "" {
  207. cmdArgs = append(cmdArgs, "--lease", leaseID)
  208. }
  209. return spawnWithExpect(cmdArgs, "OK")
  210. }
  211. type kv struct {
  212. key, val string
  213. }
  214. func ctlV3Get(cx ctlCtx, args []string, kvs ...kv) error {
  215. cmdArgs := append(cx.PrefixArgs(), "get")
  216. cmdArgs = append(cmdArgs, args...)
  217. if !cx.quorum {
  218. cmdArgs = append(cmdArgs, "--consistency", "s")
  219. }
  220. var lines []string
  221. for _, elem := range kvs {
  222. lines = append(lines, elem.key, elem.val)
  223. }
  224. return spawnWithExpects(cmdArgs, lines...)
  225. }
  226. func ctlV3Del(cx ctlCtx, args []string, num int) error {
  227. cmdArgs := append(cx.PrefixArgs(), "del")
  228. cmdArgs = append(cmdArgs, args...)
  229. return spawnWithExpects(cmdArgs, fmt.Sprintf("%d", num))
  230. }