ctl_v3_kv_test.go 6.4 KB

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