ctl_v3_kv_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 TestCtlV3PutIgnoreValue(t *testing.T) { testCtl(t, putTestIgnoreValue) }
  29. func TestCtlV3Get(t *testing.T) { testCtl(t, getTest) }
  30. func TestCtlV3GetNoTLS(t *testing.T) { testCtl(t, getTest, withCfg(configNoTLS)) }
  31. func TestCtlV3GetClientTLS(t *testing.T) { testCtl(t, getTest, withCfg(configClientTLS)) }
  32. func TestCtlV3GetClientAutoTLS(t *testing.T) { testCtl(t, getTest, withCfg(configClientAutoTLS)) }
  33. func TestCtlV3GetPeerTLS(t *testing.T) { testCtl(t, getTest, withCfg(configPeerTLS)) }
  34. func TestCtlV3GetTimeout(t *testing.T) { testCtl(t, getTest, withDialTimeout(0)) }
  35. func TestCtlV3GetQuorum(t *testing.T) { testCtl(t, getTest, withQuorum()) }
  36. func TestCtlV3GetFormat(t *testing.T) { testCtl(t, getFormatTest) }
  37. func TestCtlV3GetRev(t *testing.T) { testCtl(t, getRevTest) }
  38. func TestCtlV3GetKeysOnly(t *testing.T) { testCtl(t, getKeysOnlyTest) }
  39. func TestCtlV3Del(t *testing.T) { testCtl(t, delTest) }
  40. func TestCtlV3DelNoTLS(t *testing.T) { testCtl(t, delTest, withCfg(configNoTLS)) }
  41. func TestCtlV3DelClientTLS(t *testing.T) { testCtl(t, delTest, withCfg(configClientTLS)) }
  42. func TestCtlV3DelPeerTLS(t *testing.T) { testCtl(t, delTest, withCfg(configPeerTLS)) }
  43. func TestCtlV3DelTimeout(t *testing.T) { testCtl(t, delTest, withDialTimeout(0)) }
  44. func putTest(cx ctlCtx) {
  45. key, value := "foo", "bar"
  46. if err := ctlV3Put(cx, key, value, ""); err != nil {
  47. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  48. cx.t.Fatalf("putTest ctlV3Put error (%v)", err)
  49. }
  50. }
  51. if err := ctlV3Get(cx, []string{key}, kv{key, value}); err != nil {
  52. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  53. cx.t.Fatalf("putTest ctlV3Get error (%v)", err)
  54. }
  55. }
  56. }
  57. func putTestIgnoreValue(cx ctlCtx) {
  58. if err := ctlV3Put(cx, "foo", "bar", ""); err != nil {
  59. cx.t.Fatal(err)
  60. }
  61. if err := ctlV3Get(cx, []string{"foo"}, kv{"foo", "bar"}); err != nil {
  62. cx.t.Fatal(err)
  63. }
  64. if err := ctlV3Put(cx, "foo", "", "", "--ignore-value"); err != nil {
  65. cx.t.Fatal(err)
  66. }
  67. if err := ctlV3Get(cx, []string{"foo"}, kv{"foo", "bar"}); err != nil {
  68. cx.t.Fatal(err)
  69. }
  70. }
  71. func getTest(cx ctlCtx) {
  72. var (
  73. kvs = []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}}
  74. revkvs = []kv{{"key3", "val3"}, {"key2", "val2"}, {"key1", "val1"}}
  75. )
  76. for i := range kvs {
  77. if err := ctlV3Put(cx, kvs[i].key, kvs[i].val, ""); err != nil {
  78. cx.t.Fatalf("getTest #%d: ctlV3Put error (%v)", i, err)
  79. }
  80. }
  81. tests := []struct {
  82. args []string
  83. wkv []kv
  84. }{
  85. {[]string{"key1"}, []kv{{"key1", "val1"}}},
  86. {[]string{"", "--prefix"}, kvs},
  87. {[]string{"", "--from-key"}, kvs},
  88. {[]string{"key", "--prefix"}, kvs},
  89. {[]string{"key", "--prefix", "--limit=2"}, kvs[:2]},
  90. {[]string{"key", "--prefix", "--order=ASCEND", "--sort-by=MODIFY"}, kvs},
  91. {[]string{"key", "--prefix", "--order=ASCEND", "--sort-by=VERSION"}, kvs},
  92. {[]string{"key", "--prefix", "--sort-by=CREATE"}, kvs}, // ASCEND by default
  93. {[]string{"key", "--prefix", "--order=DESCEND", "--sort-by=CREATE"}, revkvs},
  94. {[]string{"key", "--prefix", "--order=DESCEND", "--sort-by=KEY"}, revkvs},
  95. }
  96. for i, tt := range tests {
  97. if err := ctlV3Get(cx, tt.args, tt.wkv...); err != nil {
  98. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  99. cx.t.Errorf("getTest #%d: ctlV3Get error (%v)", i, err)
  100. }
  101. }
  102. }
  103. }
  104. func getFormatTest(cx ctlCtx) {
  105. if err := ctlV3Put(cx, "abc", "123", ""); err != nil {
  106. cx.t.Fatal(err)
  107. }
  108. tests := []struct {
  109. format string
  110. valueOnly bool
  111. wstr string
  112. }{
  113. {"simple", false, "abc"},
  114. {"simple", true, "123"},
  115. {"json", false, `"kvs":[{"key":"YWJj"`},
  116. {"protobuf", false, "\x17\b\x93\xe7\xf6\x93\xd4ņ\xe14\x10\xed"},
  117. }
  118. for i, tt := range tests {
  119. cmdArgs := append(cx.PrefixArgs(), "get")
  120. cmdArgs = append(cmdArgs, "--write-out="+tt.format)
  121. if tt.valueOnly {
  122. cmdArgs = append(cmdArgs, "--print-value-only")
  123. }
  124. cmdArgs = append(cmdArgs, "abc")
  125. if err := spawnWithExpect(cmdArgs, tt.wstr); err != nil {
  126. cx.t.Errorf("#%d: error (%v), wanted %v", i, err, tt.wstr)
  127. }
  128. }
  129. }
  130. func getRevTest(cx ctlCtx) {
  131. var (
  132. kvs = []kv{{"key", "val1"}, {"key", "val2"}, {"key", "val3"}}
  133. )
  134. for i := range kvs {
  135. if err := ctlV3Put(cx, kvs[i].key, kvs[i].val, ""); err != nil {
  136. cx.t.Fatalf("getRevTest #%d: ctlV3Put error (%v)", i, err)
  137. }
  138. }
  139. tests := []struct {
  140. args []string
  141. wkv []kv
  142. }{
  143. {[]string{"key", "--rev", "2"}, kvs[:1]},
  144. {[]string{"key", "--rev", "3"}, kvs[1:2]},
  145. {[]string{"key", "--rev", "4"}, kvs[2:]},
  146. }
  147. for i, tt := range tests {
  148. if err := ctlV3Get(cx, tt.args, tt.wkv...); err != nil {
  149. cx.t.Errorf("getTest #%d: ctlV3Get error (%v)", i, err)
  150. }
  151. }
  152. }
  153. func getKeysOnlyTest(cx ctlCtx) {
  154. var (
  155. kvs = []kv{{"key1", "val1"}}
  156. )
  157. for i := range kvs {
  158. if err := ctlV3Put(cx, kvs[i].key, kvs[i].val, ""); err != nil {
  159. cx.t.Fatalf("getKeysOnlyTest #%d: ctlV3Put error (%v)", i, err)
  160. }
  161. }
  162. cmdArgs := append(cx.PrefixArgs(), "get")
  163. cmdArgs = append(cmdArgs, []string{"--prefix", "--keys-only", "key"}...)
  164. err := spawnWithExpects(cmdArgs, []string{"key1", ""}...)
  165. if err != nil {
  166. cx.t.Fatalf("getKeysOnlyTest : error (%v)", err)
  167. }
  168. }
  169. func delTest(cx ctlCtx) {
  170. tests := []struct {
  171. puts []kv
  172. args []string
  173. deletedNum int
  174. }{
  175. { // delete all keys
  176. []kv{{"foo1", "bar"}, {"foo2", "bar"}, {"foo3", "bar"}},
  177. []string{"", "--prefix"},
  178. 3,
  179. },
  180. { // delete all keys
  181. []kv{{"foo1", "bar"}, {"foo2", "bar"}, {"foo3", "bar"}},
  182. []string{"", "--from-key"},
  183. 3,
  184. },
  185. {
  186. []kv{{"this", "value"}},
  187. []string{"that"},
  188. 0,
  189. },
  190. {
  191. []kv{{"sample", "value"}},
  192. []string{"sample"},
  193. 1,
  194. },
  195. {
  196. []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}},
  197. []string{"key", "--prefix"},
  198. 3,
  199. },
  200. {
  201. []kv{{"zoo1", "bar"}, {"zoo2", "bar2"}, {"zoo3", "bar3"}},
  202. []string{"zoo1", "--from-key"},
  203. 3,
  204. },
  205. }
  206. for i, tt := range tests {
  207. for j := range tt.puts {
  208. if err := ctlV3Put(cx, tt.puts[j].key, tt.puts[j].val, ""); err != nil {
  209. cx.t.Fatalf("delTest #%d-%d: ctlV3Put error (%v)", i, j, err)
  210. }
  211. }
  212. if err := ctlV3Del(cx, tt.args, tt.deletedNum); err != nil {
  213. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  214. cx.t.Fatalf("delTest #%d: ctlV3Del error (%v)", i, err)
  215. }
  216. }
  217. }
  218. }
  219. func ctlV3Put(cx ctlCtx, key, value, leaseID string, flags ...string) error {
  220. skipValue := false
  221. for _, f := range flags {
  222. if f == "--ignore-value" {
  223. skipValue = true
  224. break
  225. }
  226. }
  227. cmdArgs := append(cx.PrefixArgs(), "put", key)
  228. if !skipValue {
  229. cmdArgs = append(cmdArgs, value)
  230. }
  231. if leaseID != "" {
  232. cmdArgs = append(cmdArgs, "--lease", leaseID)
  233. }
  234. if len(flags) != 0 {
  235. cmdArgs = append(cmdArgs, flags...)
  236. }
  237. return spawnWithExpect(cmdArgs, "OK")
  238. }
  239. type kv struct {
  240. key, val string
  241. }
  242. func ctlV3Get(cx ctlCtx, args []string, kvs ...kv) error {
  243. cmdArgs := append(cx.PrefixArgs(), "get")
  244. cmdArgs = append(cmdArgs, args...)
  245. if !cx.quorum {
  246. cmdArgs = append(cmdArgs, "--consistency", "s")
  247. }
  248. var lines []string
  249. for _, elem := range kvs {
  250. lines = append(lines, elem.key, elem.val)
  251. }
  252. return spawnWithExpects(cmdArgs, lines...)
  253. }
  254. func ctlV3Del(cx ctlCtx, args []string, num int) error {
  255. cmdArgs := append(cx.PrefixArgs(), "del")
  256. cmdArgs = append(cmdArgs, args...)
  257. return spawnWithExpects(cmdArgs, fmt.Sprintf("%d", num))
  258. }