etcdctlv3_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. "os"
  18. "strings"
  19. "testing"
  20. "time"
  21. "github.com/coreos/etcd/pkg/testutil"
  22. "github.com/coreos/etcd/version"
  23. )
  24. func TestCtlV3Put(t *testing.T) { testCtl(t, putTest) }
  25. func TestCtlV3PutNoTLS(t *testing.T) { testCtl(t, putTest, withCfg(configNoTLS)) }
  26. func TestCtlV3PutClientTLS(t *testing.T) { testCtl(t, putTest, withCfg(configClientTLS)) }
  27. func TestCtlV3PutPeerTLS(t *testing.T) { testCtl(t, putTest, withCfg(configPeerTLS)) }
  28. func TestCtlV3PutTimeout(t *testing.T) { testCtl(t, putTest, withDialTimeout(0)) }
  29. func TestCtlV3Get(t *testing.T) { testCtl(t, getTest) }
  30. func TestCtlV3GetNoTLS(t *testing.T) { testCtl(t, getTest, withCfg(configNoTLS)) }
  31. func TestCtlV3GetClientTLS(t *testing.T) { testCtl(t, getTest, withCfg(configClientTLS)) }
  32. func TestCtlV3GetPeerTLS(t *testing.T) { testCtl(t, getTest, withCfg(configPeerTLS)) }
  33. func TestCtlV3GetTimeout(t *testing.T) { testCtl(t, getTest, withDialTimeout(0)) }
  34. func TestCtlV3GetQuorum(t *testing.T) { testCtl(t, getTest, withQuorum()) }
  35. func TestCtlV3Del(t *testing.T) { testCtl(t, delTest) }
  36. func TestCtlV3DelNoTLS(t *testing.T) { testCtl(t, delTest, withCfg(configNoTLS)) }
  37. func TestCtlV3DelClientTLS(t *testing.T) { testCtl(t, delTest, withCfg(configClientTLS)) }
  38. func TestCtlV3DelPeerTLS(t *testing.T) { testCtl(t, delTest, withCfg(configPeerTLS)) }
  39. func TestCtlV3DelTimeout(t *testing.T) { testCtl(t, delTest, withDialTimeout(0)) }
  40. func TestCtlV3Watch(t *testing.T) { testCtl(t, watchTest) }
  41. func TestCtlV3WatchNoTLS(t *testing.T) { testCtl(t, watchTest, withCfg(configNoTLS)) }
  42. func TestCtlV3WatchClientTLS(t *testing.T) { testCtl(t, watchTest, withCfg(configClientTLS)) }
  43. func TestCtlV3WatchPeerTLS(t *testing.T) { testCtl(t, watchTest, withCfg(configPeerTLS)) }
  44. func TestCtlV3WatchTimeout(t *testing.T) { testCtl(t, watchTest, withDialTimeout(0)) }
  45. func TestCtlV3WatchInteractive(t *testing.T) {
  46. testCtl(t, watchTest, withInteractive())
  47. }
  48. func TestCtlV3WatchInteractiveNoTLS(t *testing.T) {
  49. testCtl(t, watchTest, withInteractive(), withCfg(configNoTLS))
  50. }
  51. func TestCtlV3WatchInteractiveClientTLS(t *testing.T) {
  52. testCtl(t, watchTest, withInteractive(), withCfg(configClientTLS))
  53. }
  54. func TestCtlV3WatchInteractivePeerTLS(t *testing.T) {
  55. testCtl(t, watchTest, withInteractive(), withCfg(configPeerTLS))
  56. }
  57. func TestCtlV3TxnInteractiveSuccess(t *testing.T) {
  58. testCtl(t, txnTestSuccess, withInteractive())
  59. }
  60. func TestCtlV3TxnInteractiveSuccessNoTLS(t *testing.T) {
  61. testCtl(t, txnTestSuccess, withInteractive(), withCfg(configNoTLS))
  62. }
  63. func TestCtlV3TxnInteractiveSuccessClientTLS(t *testing.T) {
  64. testCtl(t, txnTestSuccess, withInteractive(), withCfg(configClientTLS))
  65. }
  66. func TestCtlV3TxnInteractiveSuccessPeerTLS(t *testing.T) {
  67. testCtl(t, txnTestSuccess, withInteractive(), withCfg(configPeerTLS))
  68. }
  69. func TestCtlV3TxnInteractiveFail(t *testing.T) {
  70. testCtl(t, txnTestFail, withInteractive())
  71. }
  72. func TestCtlV3Version(t *testing.T) { testCtl(t, versionTest) }
  73. func TestCtlV3EpHealthQuorum(t *testing.T) { testCtl(t, epHealthTest, withQuorum()) }
  74. type ctlCtx struct {
  75. t *testing.T
  76. cfg etcdProcessClusterConfig
  77. epc *etcdProcessCluster
  78. errc chan error
  79. dialTimeout time.Duration
  80. quorum bool // if true, set up 3-node cluster and linearizable read
  81. interactive bool
  82. }
  83. type ctlOption func(*ctlCtx)
  84. func (cx *ctlCtx) applyOpts(opts []ctlOption) {
  85. for _, opt := range opts {
  86. opt(cx)
  87. }
  88. }
  89. func withCfg(cfg etcdProcessClusterConfig) ctlOption {
  90. return func(cx *ctlCtx) { cx.cfg = cfg }
  91. }
  92. func withDialTimeout(timeout time.Duration) ctlOption {
  93. return func(cx *ctlCtx) { cx.dialTimeout = timeout }
  94. }
  95. func withQuorum() ctlOption {
  96. return func(cx *ctlCtx) { cx.quorum = true }
  97. }
  98. func withInteractive() ctlOption {
  99. return func(cx *ctlCtx) { cx.interactive = true }
  100. }
  101. func setupCtlV3Test(t *testing.T, cfg etcdProcessClusterConfig, quorum bool) *etcdProcessCluster {
  102. mustEtcdctl(t)
  103. if !quorum {
  104. cfg = *configStandalone(cfg)
  105. }
  106. epc, err := newEtcdProcessCluster(&cfg)
  107. if err != nil {
  108. t.Fatalf("could not start etcd process cluster (%v)", err)
  109. }
  110. return epc
  111. }
  112. func testCtl(t *testing.T, testFunc func(ctlCtx), opts ...ctlOption) {
  113. defer testutil.AfterTest(t)
  114. ret := ctlCtx{
  115. t: t,
  116. cfg: configAutoTLS,
  117. errc: make(chan error, 1),
  118. dialTimeout: 7 * time.Second,
  119. }
  120. ret.applyOpts(opts)
  121. os.Setenv("ETCDCTL_API", "3")
  122. ret.epc = setupCtlV3Test(ret.t, ret.cfg, ret.quorum)
  123. defer func() {
  124. os.Unsetenv("ETCDCTL_API")
  125. if errC := ret.epc.Close(); errC != nil {
  126. t.Fatalf("error closing etcd processes (%v)", errC)
  127. }
  128. }()
  129. go testFunc(ret)
  130. select {
  131. case <-time.After(2*ret.dialTimeout + time.Second):
  132. if ret.dialTimeout > 0 {
  133. t.Fatalf("test timed out for %v", ret.dialTimeout)
  134. }
  135. case err := <-ret.errc:
  136. if err != nil {
  137. t.Fatal(err)
  138. }
  139. }
  140. return
  141. }
  142. func putTest(cx ctlCtx) {
  143. defer close(cx.errc)
  144. key, value := "foo", "bar"
  145. if err := ctlV3Put(cx, key, value); err != nil {
  146. if cx.dialTimeout > 0 && isGRPCTimedout(err) {
  147. cx.t.Fatalf("putTest ctlV3Put error (%v)", err)
  148. }
  149. }
  150. if err := ctlV3Get(cx, []string{key}, kv{key, value}); err != nil {
  151. if cx.dialTimeout > 0 && isGRPCTimedout(err) {
  152. cx.t.Fatalf("putTest ctlV3Get error (%v)", err)
  153. }
  154. }
  155. }
  156. func getTest(cx ctlCtx) {
  157. defer close(cx.errc)
  158. var (
  159. kvs = []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}}
  160. revkvs = []kv{{"key3", "val3"}, {"key2", "val2"}, {"key1", "val1"}}
  161. )
  162. tests := []struct {
  163. args []string
  164. wkv []kv
  165. }{
  166. {[]string{"key1"}, []kv{{"key1", "val1"}}},
  167. {[]string{"key", "--prefix"}, kvs},
  168. {[]string{"key", "--prefix", "--limit=2"}, kvs[:2]},
  169. {[]string{"key", "--prefix", "--order=ASCEND", "--sort-by=MODIFY"}, kvs},
  170. {[]string{"key", "--prefix", "--order=ASCEND", "--sort-by=VERSION"}, kvs},
  171. {[]string{"key", "--prefix", "--order=DESCEND", "--sort-by=CREATE"}, revkvs},
  172. {[]string{"key", "--prefix", "--order=DESCEND", "--sort-by=KEY"}, revkvs},
  173. }
  174. for i := range kvs {
  175. if err := ctlV3Put(cx, kvs[i].key, kvs[i].val); err != nil {
  176. cx.t.Fatalf("getTest #%d: ctlV3Put error (%v)", i, err)
  177. }
  178. }
  179. for i, tt := range tests {
  180. if err := ctlV3Get(cx, tt.args, tt.wkv...); err != nil {
  181. if cx.dialTimeout > 0 && isGRPCTimedout(err) {
  182. cx.t.Errorf("getTest #%d: ctlV3Get error (%v)", i, err)
  183. }
  184. }
  185. }
  186. }
  187. func delTest(cx ctlCtx) {
  188. defer close(cx.errc)
  189. tests := []struct {
  190. puts []kv
  191. args []string
  192. deletedNum int
  193. }{
  194. {
  195. []kv{{"this", "value"}},
  196. []string{"that"},
  197. 0,
  198. },
  199. {
  200. []kv{{"sample", "value"}},
  201. []string{"sample"},
  202. 1,
  203. },
  204. {
  205. []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}},
  206. []string{"key", "--prefix"},
  207. 3,
  208. },
  209. }
  210. for i, tt := range tests {
  211. for j := range tt.puts {
  212. if err := ctlV3Put(cx, tt.puts[j].key, tt.puts[j].val); err != nil {
  213. cx.t.Fatalf("delTest #%d-%d: ctlV3Put error (%v)", i, j, err)
  214. }
  215. }
  216. if err := ctlV3Del(cx, tt.args, tt.deletedNum); err != nil {
  217. if cx.dialTimeout > 0 && isGRPCTimedout(err) {
  218. cx.t.Fatalf("delTest #%d: ctlV3Del error (%v)", i, err)
  219. }
  220. }
  221. }
  222. }
  223. func watchTest(cx ctlCtx) {
  224. defer close(cx.errc)
  225. tests := []struct {
  226. puts []kv
  227. args []string
  228. wkv []kv
  229. }{
  230. {
  231. []kv{{"sample", "value"}},
  232. []string{"sample", "--rev", "1"},
  233. []kv{{"sample", "value"}},
  234. },
  235. {
  236. []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}},
  237. []string{"key", "--rev", "1", "--prefix"},
  238. []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}},
  239. },
  240. {
  241. []kv{{"etcd", "revision_1"}, {"etcd", "revision_2"}, {"etcd", "revision_3"}},
  242. []string{"etcd", "--rev", "2"},
  243. []kv{{"etcd", "revision_2"}, {"etcd", "revision_3"}},
  244. },
  245. }
  246. for i, tt := range tests {
  247. go func() {
  248. for j := range tt.puts {
  249. if err := ctlV3Put(cx, tt.puts[j].key, tt.puts[j].val); err != nil {
  250. cx.t.Fatalf("watchTest #%d-%d: ctlV3Put error (%v)", i, j, err)
  251. }
  252. }
  253. }()
  254. if err := ctlV3Watch(cx, tt.args, tt.wkv...); err != nil {
  255. if cx.dialTimeout > 0 && isGRPCTimedout(err) {
  256. cx.t.Errorf("watchTest #%d: ctlV3Watch error (%v)", i, err)
  257. }
  258. }
  259. }
  260. }
  261. func versionTest(cx ctlCtx) {
  262. defer close(cx.errc)
  263. if err := ctlV3Version(cx); err != nil {
  264. cx.t.Fatalf("versionTest ctlV3Version error (%v)", err)
  265. }
  266. }
  267. func epHealthTest(cx ctlCtx) {
  268. defer close(cx.errc)
  269. if err := ctlV3EpHealth(cx); err != nil {
  270. cx.t.Fatalf("epHealthTest ctlV3EpHealth error (%v)", err)
  271. }
  272. }
  273. func txnTestSuccess(cx ctlCtx) {
  274. defer close(cx.errc)
  275. if err := ctlV3Put(cx, "key1", "value1"); err != nil {
  276. cx.t.Fatalf("txnTestSuccess ctlV3Put error (%v)", err)
  277. }
  278. if err := ctlV3Put(cx, "key2", "value2"); err != nil {
  279. cx.t.Fatalf("txnTestSuccess ctlV3Put error (%v)", err)
  280. }
  281. rqs := txnRequests{
  282. compare: []string{`version("key1") = "1"`, `version("key2") = "1"`},
  283. ifSucess: []string{"get key1", "get key2"},
  284. ifFail: []string{`put key1 "fail"`, `put key2 "fail"`},
  285. results: []string{"SUCCESS", "key1", "value1", "key2", "value2"},
  286. }
  287. if err := ctlV3Txn(cx, rqs); err != nil {
  288. cx.t.Fatal(err)
  289. }
  290. }
  291. func txnTestFail(cx ctlCtx) {
  292. defer close(cx.errc)
  293. rqs := txnRequests{
  294. compare: []string{`version("key") < "0"`},
  295. ifSucess: []string{`put key "success"`},
  296. ifFail: []string{`put key "fail"`},
  297. results: []string{"FAILURE", "OK"},
  298. }
  299. if err := ctlV3Txn(cx, rqs); err != nil {
  300. cx.t.Fatal(err)
  301. }
  302. }
  303. func ctlV3PrefixArgs(clus *etcdProcessCluster, dialTimeout time.Duration) []string {
  304. if len(clus.proxies()) > 0 { // TODO: add proxy check as in v2
  305. panic("v3 proxy not implemented")
  306. }
  307. endpoints := ""
  308. if backends := clus.backends(); len(backends) != 0 {
  309. es := []string{}
  310. for _, b := range backends {
  311. es = append(es, stripSchema(b.cfg.acurl))
  312. }
  313. endpoints = strings.Join(es, ",")
  314. }
  315. cmdArgs := []string{"../bin/etcdctl", "--endpoints", endpoints, "--dial-timeout", dialTimeout.String()}
  316. if clus.cfg.clientTLS == clientTLS {
  317. cmdArgs = append(cmdArgs, "--cacert", caPath, "--cert", certPath, "--key", privateKeyPath)
  318. }
  319. return cmdArgs
  320. }
  321. func ctlV3Put(cx ctlCtx, key, value string) error {
  322. cmdArgs := append(ctlV3PrefixArgs(cx.epc, cx.dialTimeout), "put", key, value)
  323. return spawnWithExpect(cmdArgs, "OK")
  324. }
  325. type kv struct {
  326. key, val string
  327. }
  328. func ctlV3Get(cx ctlCtx, args []string, kvs ...kv) error {
  329. cmdArgs := append(ctlV3PrefixArgs(cx.epc, cx.dialTimeout), "get")
  330. cmdArgs = append(cmdArgs, args...)
  331. if !cx.quorum {
  332. cmdArgs = append(cmdArgs, "--consistency", "s")
  333. }
  334. var lines []string
  335. for _, elem := range kvs {
  336. lines = append(lines, elem.key, elem.val)
  337. }
  338. return spawnWithExpects(cmdArgs, lines...)
  339. }
  340. func ctlV3Del(cx ctlCtx, args []string, num int) error {
  341. cmdArgs := append(ctlV3PrefixArgs(cx.epc, cx.dialTimeout), "del")
  342. cmdArgs = append(cmdArgs, args...)
  343. return spawnWithExpects(cmdArgs, fmt.Sprintf("%d", num))
  344. }
  345. func ctlV3Watch(cx ctlCtx, args []string, kvs ...kv) error {
  346. cmdArgs := append(ctlV3PrefixArgs(cx.epc, cx.dialTimeout), "watch")
  347. if cx.interactive {
  348. cmdArgs = append(cmdArgs, "--interactive")
  349. } else {
  350. cmdArgs = append(cmdArgs, args...)
  351. }
  352. proc, err := spawnCmd(cmdArgs)
  353. if err != nil {
  354. return err
  355. }
  356. if cx.interactive {
  357. wl := strings.Join(append([]string{"watch"}, args...), " ") + "\r"
  358. if err = proc.Send(wl); err != nil {
  359. return err
  360. }
  361. }
  362. for _, elem := range kvs {
  363. if _, err = proc.Expect(elem.key); err != nil {
  364. return err
  365. }
  366. if _, err = proc.Expect(elem.val); err != nil {
  367. return err
  368. }
  369. }
  370. return proc.Stop()
  371. }
  372. type txnRequests struct {
  373. compare []string
  374. ifSucess []string
  375. ifFail []string
  376. results []string
  377. }
  378. func ctlV3Txn(cx ctlCtx, rqs txnRequests) error {
  379. // TODO: support non-interactive mode
  380. cmdArgs := append(ctlV3PrefixArgs(cx.epc, cx.dialTimeout), "txn")
  381. if cx.interactive {
  382. cmdArgs = append(cmdArgs, "--interactive")
  383. }
  384. proc, err := spawnCmd(cmdArgs)
  385. if err != nil {
  386. return err
  387. }
  388. _, err = proc.Expect("compares:")
  389. if err != nil {
  390. return err
  391. }
  392. for _, req := range rqs.compare {
  393. if err = proc.Send(req + "\r"); err != nil {
  394. return err
  395. }
  396. }
  397. if err = proc.Send("\r"); err != nil {
  398. return err
  399. }
  400. _, err = proc.Expect("success requests (get, put, delete):")
  401. if err != nil {
  402. return err
  403. }
  404. for _, req := range rqs.ifSucess {
  405. if err = proc.Send(req + "\r"); err != nil {
  406. return err
  407. }
  408. }
  409. if err = proc.Send("\r"); err != nil {
  410. return err
  411. }
  412. _, err = proc.Expect("failure requests (get, put, delete):")
  413. if err != nil {
  414. return err
  415. }
  416. for _, req := range rqs.ifFail {
  417. if err = proc.Send(req + "\r"); err != nil {
  418. return err
  419. }
  420. }
  421. if err = proc.Send("\r"); err != nil {
  422. return err
  423. }
  424. for _, line := range rqs.results {
  425. _, err = proc.Expect(line)
  426. if err != nil {
  427. return err
  428. }
  429. }
  430. return proc.Close()
  431. }
  432. func ctlV3Version(cx ctlCtx) error {
  433. cmdArgs := append(ctlV3PrefixArgs(cx.epc, cx.dialTimeout), "version")
  434. return spawnWithExpect(cmdArgs, version.Version)
  435. }
  436. func ctlV3EpHealth(cx ctlCtx) error {
  437. cmdArgs := append(ctlV3PrefixArgs(cx.epc, cx.dialTimeout), "endpoint-health")
  438. lines := make([]string, cx.epc.cfg.clusterSize)
  439. for i := range lines {
  440. lines[i] = "is healthy"
  441. }
  442. return spawnWithExpects(cmdArgs, lines...)
  443. }
  444. func isGRPCTimedout(err error) bool {
  445. return strings.Contains(err.Error(), "grpc: timed out trying to connect")
  446. }
  447. func stripSchema(s string) string {
  448. if strings.HasPrefix(s, "http://") {
  449. s = strings.Replace(s, "http://", "", -1)
  450. }
  451. if strings.HasPrefix(s, "https://") {
  452. s = strings.Replace(s, "https://", "", -1)
  453. }
  454. return s
  455. }