ctl_v3_txn_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2016 The etcd Authors
  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 "testing"
  16. func TestCtlV3TxnInteractiveSuccess(t *testing.T) {
  17. testCtl(t, txnTestSuccess, withInteractive())
  18. }
  19. func TestCtlV3TxnInteractiveSuccessNoTLS(t *testing.T) {
  20. testCtl(t, txnTestSuccess, withInteractive(), withCfg(configNoTLS))
  21. }
  22. func TestCtlV3TxnInteractiveSuccessClientTLS(t *testing.T) {
  23. testCtl(t, txnTestSuccess, withInteractive(), withCfg(configClientTLS))
  24. }
  25. func TestCtlV3TxnInteractiveSuccessPeerTLS(t *testing.T) {
  26. testCtl(t, txnTestSuccess, withInteractive(), withCfg(configPeerTLS))
  27. }
  28. func TestCtlV3TxnInteractiveFail(t *testing.T) {
  29. testCtl(t, txnTestFail, withInteractive())
  30. }
  31. func txnTestSuccess(cx ctlCtx) {
  32. if err := ctlV3Put(cx, "key1", "value1", ""); err != nil {
  33. cx.t.Fatalf("txnTestSuccess ctlV3Put error (%v)", err)
  34. }
  35. if err := ctlV3Put(cx, "key2", "value2", ""); err != nil {
  36. cx.t.Fatalf("txnTestSuccess ctlV3Put error (%v)", err)
  37. }
  38. rqs := txnRequests{
  39. compare: []string{`version("key1") = "1"`, `version("key2") = "1"`},
  40. ifSucess: []string{"get key1", "get key2"},
  41. ifFail: []string{`put key1 "fail"`, `put key2 "fail"`},
  42. results: []string{"SUCCESS", "key1", "value1", "key2", "value2"},
  43. }
  44. if err := ctlV3Txn(cx, rqs); err != nil {
  45. cx.t.Fatal(err)
  46. }
  47. }
  48. func txnTestFail(cx ctlCtx) {
  49. rqs := txnRequests{
  50. compare: []string{`version("key") < "0"`},
  51. ifSucess: []string{`put key "success"`},
  52. ifFail: []string{`put key "fail"`},
  53. results: []string{"FAILURE", "OK"},
  54. }
  55. if err := ctlV3Txn(cx, rqs); err != nil {
  56. cx.t.Fatal(err)
  57. }
  58. }
  59. type txnRequests struct {
  60. compare []string
  61. ifSucess []string
  62. ifFail []string
  63. results []string
  64. }
  65. func ctlV3Txn(cx ctlCtx, rqs txnRequests) error {
  66. // TODO: support non-interactive mode
  67. cmdArgs := append(cx.PrefixArgs(), "txn")
  68. if cx.interactive {
  69. cmdArgs = append(cmdArgs, "--interactive")
  70. }
  71. proc, err := spawnCmd(cmdArgs)
  72. if err != nil {
  73. return err
  74. }
  75. _, err = proc.Expect("compares:")
  76. if err != nil {
  77. return err
  78. }
  79. for _, req := range rqs.compare {
  80. if err = proc.Send(req + "\r"); err != nil {
  81. return err
  82. }
  83. }
  84. if err = proc.Send("\r"); err != nil {
  85. return err
  86. }
  87. _, err = proc.Expect("success requests (get, put, delete):")
  88. if err != nil {
  89. return err
  90. }
  91. for _, req := range rqs.ifSucess {
  92. if err = proc.Send(req + "\r"); err != nil {
  93. return err
  94. }
  95. }
  96. if err = proc.Send("\r"); err != nil {
  97. return err
  98. }
  99. _, err = proc.Expect("failure requests (get, put, delete):")
  100. if err != nil {
  101. return err
  102. }
  103. for _, req := range rqs.ifFail {
  104. if err = proc.Send(req + "\r"); err != nil {
  105. return err
  106. }
  107. }
  108. if err = proc.Send("\r"); err != nil {
  109. return err
  110. }
  111. for _, line := range rqs.results {
  112. _, err = proc.Expect(line)
  113. if err != nil {
  114. return err
  115. }
  116. }
  117. return proc.Close()
  118. }