ctl_v3_kv_test.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. var (
  180. kvs = []kv{{"key1", "val1"}}
  181. )
  182. for i := range kvs {
  183. if err := ctlV3Put(cx, kvs[i].key, kvs[i].val, ""); err != nil {
  184. cx.t.Fatalf("getKeysOnlyTest #%d: ctlV3Put error (%v)", i, err)
  185. }
  186. }
  187. cmdArgs := append(cx.PrefixArgs(), "get")
  188. cmdArgs = append(cmdArgs, []string{"--prefix", "--keys-only", "key"}...)
  189. err := spawnWithExpects(cmdArgs, []string{"key1", ""}...)
  190. if err != nil {
  191. cx.t.Fatalf("getKeysOnlyTest : error (%v)", err)
  192. }
  193. }
  194. func delTest(cx ctlCtx) {
  195. tests := []struct {
  196. puts []kv
  197. args []string
  198. deletedNum int
  199. }{
  200. { // delete all keys
  201. []kv{{"foo1", "bar"}, {"foo2", "bar"}, {"foo3", "bar"}},
  202. []string{"", "--prefix"},
  203. 3,
  204. },
  205. { // delete all keys
  206. []kv{{"foo1", "bar"}, {"foo2", "bar"}, {"foo3", "bar"}},
  207. []string{"", "--from-key"},
  208. 3,
  209. },
  210. {
  211. []kv{{"this", "value"}},
  212. []string{"that"},
  213. 0,
  214. },
  215. {
  216. []kv{{"sample", "value"}},
  217. []string{"sample"},
  218. 1,
  219. },
  220. {
  221. []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}},
  222. []string{"key", "--prefix"},
  223. 3,
  224. },
  225. {
  226. []kv{{"zoo1", "bar"}, {"zoo2", "bar2"}, {"zoo3", "bar3"}},
  227. []string{"zoo1", "--from-key"},
  228. 3,
  229. },
  230. }
  231. for i, tt := range tests {
  232. for j := range tt.puts {
  233. if err := ctlV3Put(cx, tt.puts[j].key, tt.puts[j].val, ""); err != nil {
  234. cx.t.Fatalf("delTest #%d-%d: ctlV3Put error (%v)", i, j, err)
  235. }
  236. }
  237. if err := ctlV3Del(cx, tt.args, tt.deletedNum); err != nil {
  238. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  239. cx.t.Fatalf("delTest #%d: ctlV3Del error (%v)", i, err)
  240. }
  241. }
  242. }
  243. }
  244. func ctlV3Put(cx ctlCtx, key, value, leaseID string, flags ...string) error {
  245. skipValue := false
  246. skipLease := false
  247. for _, f := range flags {
  248. if f == "--ignore-value" {
  249. skipValue = true
  250. }
  251. if f == "--ignore-lease" {
  252. skipLease = true
  253. }
  254. }
  255. cmdArgs := append(cx.PrefixArgs(), "put", key)
  256. if !skipValue {
  257. cmdArgs = append(cmdArgs, value)
  258. }
  259. if leaseID != "" && !skipLease {
  260. cmdArgs = append(cmdArgs, "--lease", leaseID)
  261. }
  262. if len(flags) != 0 {
  263. cmdArgs = append(cmdArgs, flags...)
  264. }
  265. return spawnWithExpect(cmdArgs, "OK")
  266. }
  267. type kv struct {
  268. key, val string
  269. }
  270. func ctlV3Get(cx ctlCtx, args []string, kvs ...kv) error {
  271. cmdArgs := append(cx.PrefixArgs(), "get")
  272. cmdArgs = append(cmdArgs, args...)
  273. if !cx.quorum {
  274. cmdArgs = append(cmdArgs, "--consistency", "s")
  275. }
  276. var lines []string
  277. for _, elem := range kvs {
  278. lines = append(lines, elem.key, elem.val)
  279. }
  280. return spawnWithExpects(cmdArgs, lines...)
  281. }
  282. func ctlV3Del(cx ctlCtx, args []string, num int) error {
  283. cmdArgs := append(cx.PrefixArgs(), "del")
  284. cmdArgs = append(cmdArgs, args...)
  285. return spawnWithExpects(cmdArgs, fmt.Sprintf("%d", num))
  286. }