ctl_v3_kv_test.go 9.4 KB

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