etcdctlv3_test.go 13 KB

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