etcdctlv3_test.go 13 KB

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