ctl_v3_kv_test.go 6.1 KB

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