ctl_v3_kv_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. wstr string
  92. }{
  93. {"simple", "abc"},
  94. {"json", `"kvs":[{"key":"YWJj"`},
  95. {"protobuf", "\x17\b\x93\xe7\xf6\x93\xd4ņ\xe14\x10\xed"},
  96. }
  97. for i, tt := range tests {
  98. cmdArgs := append(cx.PrefixArgs(), "get")
  99. cmdArgs = append(cmdArgs, "--write-out="+tt.format)
  100. cmdArgs = append(cmdArgs, "abc")
  101. if err := spawnWithExpect(cmdArgs, tt.wstr); err != nil {
  102. cx.t.Errorf("#%d: error (%v), wanted %v", i, err, tt.wstr)
  103. }
  104. }
  105. }
  106. func getRevTest(cx ctlCtx) {
  107. var (
  108. kvs = []kv{{"key", "val1"}, {"key", "val2"}, {"key", "val3"}}
  109. )
  110. for i := range kvs {
  111. if err := ctlV3Put(cx, kvs[i].key, kvs[i].val, ""); err != nil {
  112. cx.t.Fatalf("getRevTest #%d: ctlV3Put error (%v)", i, err)
  113. }
  114. }
  115. tests := []struct {
  116. args []string
  117. wkv []kv
  118. }{
  119. {[]string{"key", "--rev", "2"}, kvs[:1]},
  120. {[]string{"key", "--rev", "3"}, kvs[1:2]},
  121. {[]string{"key", "--rev", "4"}, kvs[2:]},
  122. }
  123. for i, tt := range tests {
  124. if err := ctlV3Get(cx, tt.args, tt.wkv...); err != nil {
  125. cx.t.Errorf("getTest #%d: ctlV3Get error (%v)", i, err)
  126. }
  127. }
  128. }
  129. func getKeysOnlyTest(cx ctlCtx) {
  130. var (
  131. kvs = []kv{{"key1", "val1"}}
  132. )
  133. for i := range kvs {
  134. if err := ctlV3Put(cx, kvs[i].key, kvs[i].val, ""); err != nil {
  135. cx.t.Fatalf("getKeysOnlyTest #%d: ctlV3Put error (%v)", i, err)
  136. }
  137. }
  138. cmdArgs := append(cx.PrefixArgs(), "get")
  139. cmdArgs = append(cmdArgs, []string{"--prefix", "--keys-only", "key"}...)
  140. err := spawnWithExpects(cmdArgs, []string{"key1", ""}...)
  141. if err != nil {
  142. cx.t.Fatalf("getKeysOnlyTest : error (%v)", err)
  143. }
  144. }
  145. func delTest(cx ctlCtx) {
  146. tests := []struct {
  147. puts []kv
  148. args []string
  149. deletedNum int
  150. }{
  151. {
  152. []kv{{"this", "value"}},
  153. []string{"that"},
  154. 0,
  155. },
  156. {
  157. []kv{{"sample", "value"}},
  158. []string{"sample"},
  159. 1,
  160. },
  161. {
  162. []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}},
  163. []string{"key", "--prefix"},
  164. 3,
  165. },
  166. }
  167. for i, tt := range tests {
  168. for j := range tt.puts {
  169. if err := ctlV3Put(cx, tt.puts[j].key, tt.puts[j].val, ""); err != nil {
  170. cx.t.Fatalf("delTest #%d-%d: ctlV3Put error (%v)", i, j, err)
  171. }
  172. }
  173. if err := ctlV3Del(cx, tt.args, tt.deletedNum); err != nil {
  174. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  175. cx.t.Fatalf("delTest #%d: ctlV3Del error (%v)", i, err)
  176. }
  177. }
  178. }
  179. }
  180. func ctlV3Put(cx ctlCtx, key, value, leaseID string) error {
  181. cmdArgs := append(cx.PrefixArgs(), "put", key, value)
  182. if leaseID != "" {
  183. cmdArgs = append(cmdArgs, "--lease", leaseID)
  184. }
  185. return spawnWithExpect(cmdArgs, "OK")
  186. }
  187. type kv struct {
  188. key, val string
  189. }
  190. func ctlV3Get(cx ctlCtx, args []string, kvs ...kv) error {
  191. cmdArgs := append(cx.PrefixArgs(), "get")
  192. cmdArgs = append(cmdArgs, args...)
  193. if !cx.quorum {
  194. cmdArgs = append(cmdArgs, "--consistency", "s")
  195. }
  196. var lines []string
  197. for _, elem := range kvs {
  198. lines = append(lines, elem.key, elem.val)
  199. }
  200. return spawnWithExpects(cmdArgs, lines...)
  201. }
  202. func ctlV3Del(cx ctlCtx, args []string, num int) error {
  203. cmdArgs := append(cx.PrefixArgs(), "del")
  204. cmdArgs = append(cmdArgs, args...)
  205. return spawnWithExpects(cmdArgs, fmt.Sprintf("%d", num))
  206. }