ctl_v3_test.go 14 KB

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