ctl_v3_kv_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. "strings"
  18. "testing"
  19. )
  20. func TestCtlV3Put(t *testing.T) { testCtl(t, putTest) }
  21. func TestCtlV3PutNoTLS(t *testing.T) { testCtl(t, putTest, withCfg(configNoTLS)) }
  22. func TestCtlV3PutClientTLS(t *testing.T) { testCtl(t, putTest, withCfg(configClientTLS)) }
  23. func TestCtlV3PutClientAutoTLS(t *testing.T) { testCtl(t, putTest, withCfg(configClientAutoTLS)) }
  24. func TestCtlV3PutPeerTLS(t *testing.T) { testCtl(t, putTest, withCfg(configPeerTLS)) }
  25. func TestCtlV3PutTimeout(t *testing.T) { testCtl(t, putTest, withDialTimeout(0)) }
  26. func TestCtlV3PutClientTLSFlagByEnv(t *testing.T) {
  27. testCtl(t, putTest, withCfg(configClientTLS), withFlagByEnv())
  28. }
  29. func TestCtlV3PutIgnoreValue(t *testing.T) { testCtl(t, putTestIgnoreValue) }
  30. func TestCtlV3PutIgnoreLease(t *testing.T) { testCtl(t, putTestIgnoreLease) }
  31. func TestCtlV3Get(t *testing.T) { testCtl(t, getTest) }
  32. func TestCtlV3GetNoTLS(t *testing.T) { testCtl(t, getTest, withCfg(configNoTLS)) }
  33. func TestCtlV3GetClientTLS(t *testing.T) { testCtl(t, getTest, withCfg(configClientTLS)) }
  34. func TestCtlV3GetClientAutoTLS(t *testing.T) { testCtl(t, getTest, withCfg(configClientAutoTLS)) }
  35. func TestCtlV3GetPeerTLS(t *testing.T) { testCtl(t, getTest, withCfg(configPeerTLS)) }
  36. func TestCtlV3GetTimeout(t *testing.T) { testCtl(t, getTest, withDialTimeout(0)) }
  37. func TestCtlV3GetQuorum(t *testing.T) { testCtl(t, getTest, withQuorum()) }
  38. func TestCtlV3GetFormat(t *testing.T) { testCtl(t, getFormatTest) }
  39. func TestCtlV3GetRev(t *testing.T) { testCtl(t, getRevTest) }
  40. func TestCtlV3GetKeysOnly(t *testing.T) { testCtl(t, getKeysOnlyTest) }
  41. func TestCtlV3Del(t *testing.T) { testCtl(t, delTest) }
  42. func TestCtlV3DelNoTLS(t *testing.T) { testCtl(t, delTest, withCfg(configNoTLS)) }
  43. func TestCtlV3DelClientTLS(t *testing.T) { testCtl(t, delTest, withCfg(configClientTLS)) }
  44. func TestCtlV3DelPeerTLS(t *testing.T) { testCtl(t, delTest, withCfg(configPeerTLS)) }
  45. func TestCtlV3DelTimeout(t *testing.T) { testCtl(t, delTest, withDialTimeout(0)) }
  46. func TestCtlV3GetRevokedCRL(t *testing.T) {
  47. cfg := etcdProcessClusterConfig{
  48. clusterSize: 1,
  49. initialToken: "new",
  50. clientTLS: clientTLS,
  51. isClientCRL: true,
  52. clientCertAuthEnabled: true,
  53. }
  54. testCtl(t, testGetRevokedCRL, withCfg(cfg))
  55. }
  56. func testGetRevokedCRL(cx ctlCtx) {
  57. // test reject
  58. if err := ctlV3Put(cx, "k", "v", ""); err == nil || !strings.Contains(err.Error(), "Error:") {
  59. cx.t.Fatalf("expected reset connection on put, got %v", err)
  60. }
  61. // test accept
  62. cx.epc.cfg.isClientCRL = false
  63. if err := ctlV3Put(cx, "k", "v", ""); err != nil {
  64. cx.t.Fatal(err)
  65. }
  66. }
  67. func putTest(cx ctlCtx) {
  68. key, value := "foo", "bar"
  69. if err := ctlV3Put(cx, key, value, ""); err != nil {
  70. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  71. cx.t.Fatalf("putTest ctlV3Put error (%v)", err)
  72. }
  73. }
  74. if err := ctlV3Get(cx, []string{key}, kv{key, value}); err != nil {
  75. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  76. cx.t.Fatalf("putTest ctlV3Get error (%v)", err)
  77. }
  78. }
  79. }
  80. func putTestIgnoreValue(cx ctlCtx) {
  81. if err := ctlV3Put(cx, "foo", "bar", ""); err != nil {
  82. cx.t.Fatal(err)
  83. }
  84. if err := ctlV3Get(cx, []string{"foo"}, kv{"foo", "bar"}); err != nil {
  85. cx.t.Fatal(err)
  86. }
  87. if err := ctlV3Put(cx, "foo", "", "", "--ignore-value"); err != nil {
  88. cx.t.Fatal(err)
  89. }
  90. if err := ctlV3Get(cx, []string{"foo"}, kv{"foo", "bar"}); err != nil {
  91. cx.t.Fatal(err)
  92. }
  93. }
  94. func putTestIgnoreLease(cx ctlCtx) {
  95. leaseID, err := ctlV3LeaseGrant(cx, 10)
  96. if err != nil {
  97. cx.t.Fatalf("putTestIgnoreLease: ctlV3LeaseGrant error (%v)", err)
  98. }
  99. if err := ctlV3Put(cx, "foo", "bar", leaseID); err != nil {
  100. cx.t.Fatalf("putTestIgnoreLease: ctlV3Put error (%v)", err)
  101. }
  102. if err := ctlV3Get(cx, []string{"foo"}, kv{"foo", "bar"}); err != nil {
  103. cx.t.Fatalf("putTestIgnoreLease: ctlV3Get error (%v)", err)
  104. }
  105. if err := ctlV3Put(cx, "foo", "bar1", "", "--ignore-lease"); err != nil {
  106. cx.t.Fatalf("putTestIgnoreLease: ctlV3Put error (%v)", err)
  107. }
  108. if err := ctlV3Get(cx, []string{"foo"}, kv{"foo", "bar1"}); err != nil {
  109. cx.t.Fatalf("putTestIgnoreLease: ctlV3Get error (%v)", err)
  110. }
  111. if err := ctlV3LeaseRevoke(cx, leaseID); err != nil {
  112. cx.t.Fatalf("putTestIgnoreLease: ctlV3LeaseRevok error (%v)", err)
  113. }
  114. if err := ctlV3Get(cx, []string{"key"}); err != nil { // expect no output
  115. cx.t.Fatalf("putTestIgnoreLease: ctlV3Get error (%v)", err)
  116. }
  117. }
  118. func getTest(cx ctlCtx) {
  119. var (
  120. kvs = []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}}
  121. revkvs = []kv{{"key3", "val3"}, {"key2", "val2"}, {"key1", "val1"}}
  122. )
  123. for i := range kvs {
  124. if err := ctlV3Put(cx, kvs[i].key, kvs[i].val, ""); err != nil {
  125. cx.t.Fatalf("getTest #%d: ctlV3Put error (%v)", i, err)
  126. }
  127. }
  128. tests := []struct {
  129. args []string
  130. wkv []kv
  131. }{
  132. {[]string{"key1"}, []kv{{"key1", "val1"}}},
  133. {[]string{"", "--prefix"}, kvs},
  134. {[]string{"", "--from-key"}, kvs},
  135. {[]string{"key", "--prefix"}, kvs},
  136. {[]string{"key", "--prefix", "--limit=2"}, kvs[:2]},
  137. {[]string{"key", "--prefix", "--order=ASCEND", "--sort-by=MODIFY"}, kvs},
  138. {[]string{"key", "--prefix", "--order=ASCEND", "--sort-by=VERSION"}, kvs},
  139. {[]string{"key", "--prefix", "--sort-by=CREATE"}, kvs}, // ASCEND by default
  140. {[]string{"key", "--prefix", "--order=DESCEND", "--sort-by=CREATE"}, revkvs},
  141. {[]string{"key", "--prefix", "--order=DESCEND", "--sort-by=KEY"}, revkvs},
  142. }
  143. for i, tt := range tests {
  144. if err := ctlV3Get(cx, tt.args, tt.wkv...); err != nil {
  145. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  146. cx.t.Errorf("getTest #%d: ctlV3Get error (%v)", i, err)
  147. }
  148. }
  149. }
  150. }
  151. func getFormatTest(cx ctlCtx) {
  152. if err := ctlV3Put(cx, "abc", "123", ""); err != nil {
  153. cx.t.Fatal(err)
  154. }
  155. tests := []struct {
  156. format string
  157. valueOnly bool
  158. wstr string
  159. }{
  160. {"simple", false, "abc"},
  161. {"simple", true, "123"},
  162. {"json", false, `"kvs":[{"key":"YWJj"`},
  163. {"protobuf", false, "\x17\b\x93\xe7\xf6\x93\xd4ņ\xe14\x10\xed"},
  164. }
  165. for i, tt := range tests {
  166. cmdArgs := append(cx.PrefixArgs(), "get")
  167. cmdArgs = append(cmdArgs, "--write-out="+tt.format)
  168. if tt.valueOnly {
  169. cmdArgs = append(cmdArgs, "--print-value-only")
  170. }
  171. cmdArgs = append(cmdArgs, "abc")
  172. if err := spawnWithExpect(cmdArgs, tt.wstr); err != nil {
  173. cx.t.Errorf("#%d: error (%v), wanted %v", i, err, tt.wstr)
  174. }
  175. }
  176. }
  177. func getRevTest(cx ctlCtx) {
  178. var (
  179. kvs = []kv{{"key", "val1"}, {"key", "val2"}, {"key", "val3"}}
  180. )
  181. for i := range kvs {
  182. if err := ctlV3Put(cx, kvs[i].key, kvs[i].val, ""); err != nil {
  183. cx.t.Fatalf("getRevTest #%d: ctlV3Put error (%v)", i, err)
  184. }
  185. }
  186. tests := []struct {
  187. args []string
  188. wkv []kv
  189. }{
  190. {[]string{"key", "--rev", "2"}, kvs[:1]},
  191. {[]string{"key", "--rev", "3"}, kvs[1:2]},
  192. {[]string{"key", "--rev", "4"}, kvs[2:]},
  193. }
  194. for i, tt := range tests {
  195. if err := ctlV3Get(cx, tt.args, tt.wkv...); err != nil {
  196. cx.t.Errorf("getTest #%d: ctlV3Get error (%v)", i, err)
  197. }
  198. }
  199. }
  200. func getKeysOnlyTest(cx ctlCtx) {
  201. if err := ctlV3Put(cx, "key", "val", ""); err != nil {
  202. cx.t.Fatal(err)
  203. }
  204. cmdArgs := append(cx.PrefixArgs(), []string{"get", "--keys-only", "key"}...)
  205. if err := spawnWithExpect(cmdArgs, "key"); err != nil {
  206. cx.t.Fatal(err)
  207. }
  208. if err := spawnWithExpects(cmdArgs, "val"); err == nil {
  209. cx.t.Fatalf("got value but passed --keys-only")
  210. }
  211. }
  212. func delTest(cx ctlCtx) {
  213. tests := []struct {
  214. puts []kv
  215. args []string
  216. deletedNum int
  217. }{
  218. { // delete all keys
  219. []kv{{"foo1", "bar"}, {"foo2", "bar"}, {"foo3", "bar"}},
  220. []string{"", "--prefix"},
  221. 3,
  222. },
  223. { // delete all keys
  224. []kv{{"foo1", "bar"}, {"foo2", "bar"}, {"foo3", "bar"}},
  225. []string{"", "--from-key"},
  226. 3,
  227. },
  228. {
  229. []kv{{"this", "value"}},
  230. []string{"that"},
  231. 0,
  232. },
  233. {
  234. []kv{{"sample", "value"}},
  235. []string{"sample"},
  236. 1,
  237. },
  238. {
  239. []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}},
  240. []string{"key", "--prefix"},
  241. 3,
  242. },
  243. {
  244. []kv{{"zoo1", "bar"}, {"zoo2", "bar2"}, {"zoo3", "bar3"}},
  245. []string{"zoo1", "--from-key"},
  246. 3,
  247. },
  248. }
  249. for i, tt := range tests {
  250. for j := range tt.puts {
  251. if err := ctlV3Put(cx, tt.puts[j].key, tt.puts[j].val, ""); err != nil {
  252. cx.t.Fatalf("delTest #%d-%d: ctlV3Put error (%v)", i, j, err)
  253. }
  254. }
  255. if err := ctlV3Del(cx, tt.args, tt.deletedNum); err != nil {
  256. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  257. cx.t.Fatalf("delTest #%d: ctlV3Del error (%v)", i, err)
  258. }
  259. }
  260. }
  261. }
  262. func ctlV3Put(cx ctlCtx, key, value, leaseID string, flags ...string) error {
  263. skipValue := false
  264. skipLease := false
  265. for _, f := range flags {
  266. if f == "--ignore-value" {
  267. skipValue = true
  268. }
  269. if f == "--ignore-lease" {
  270. skipLease = true
  271. }
  272. }
  273. cmdArgs := append(cx.PrefixArgs(), "put", key)
  274. if !skipValue {
  275. cmdArgs = append(cmdArgs, value)
  276. }
  277. if leaseID != "" && !skipLease {
  278. cmdArgs = append(cmdArgs, "--lease", leaseID)
  279. }
  280. if len(flags) != 0 {
  281. cmdArgs = append(cmdArgs, flags...)
  282. }
  283. return spawnWithExpect(cmdArgs, "OK")
  284. }
  285. type kv struct {
  286. key, val string
  287. }
  288. func ctlV3Get(cx ctlCtx, args []string, kvs ...kv) error {
  289. cmdArgs := append(cx.PrefixArgs(), "get")
  290. cmdArgs = append(cmdArgs, args...)
  291. if !cx.quorum {
  292. cmdArgs = append(cmdArgs, "--consistency", "s")
  293. }
  294. var lines []string
  295. for _, elem := range kvs {
  296. lines = append(lines, elem.key, elem.val)
  297. }
  298. return spawnWithExpects(cmdArgs, lines...)
  299. }
  300. func ctlV3Del(cx ctlCtx, args []string, num int) error {
  301. cmdArgs := append(cx.PrefixArgs(), "del")
  302. cmdArgs = append(cmdArgs, args...)
  303. return spawnWithExpects(cmdArgs, fmt.Sprintf("%d", num))
  304. }