ctl_v3_kv_test.go 10 KB

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