ctl_v3_test.go 15 KB

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