ctl_v3_kv_test.go 7.1 KB

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