ctl_v3_test.go 15 KB

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