ctl_v3_txn_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. {
  40. compare: []string{`value("key1") != "value2"`, `value("key2") != "value1"`},
  41. ifSucess: []string{"get key1", "get key2"},
  42. results: []string{"SUCCESS", "key1", "value1", "key2", "value2"},
  43. },
  44. {
  45. compare: []string{`version("key1") = "1"`, `version("key2") = "1"`},
  46. ifSucess: []string{"get key1", "get key2", `put "key \"with\" space" "value \x23"`},
  47. ifFail: []string{`put key1 "fail"`, `put key2 "fail"`},
  48. results: []string{"SUCCESS", "key1", "value1", "key2", "value2"},
  49. },
  50. {
  51. compare: []string{`version("key \"with\" space") = "1"`},
  52. ifSucess: []string{`get "key \"with\" space"`},
  53. results: []string{"SUCCESS", `key "with" space`, "value \x23"},
  54. },
  55. }
  56. for _, rq := range rqs {
  57. if err := ctlV3Txn(cx, rq); err != nil {
  58. cx.t.Fatal(err)
  59. }
  60. }
  61. }
  62. func txnTestFail(cx ctlCtx) {
  63. if err := ctlV3Put(cx, "key1", "value1", ""); err != nil {
  64. cx.t.Fatalf("txnTestSuccess ctlV3Put error (%v)", err)
  65. }
  66. rqs := []txnRequests{
  67. {
  68. compare: []string{`version("key") < "0"`},
  69. ifSucess: []string{`put key "success"`},
  70. ifFail: []string{`put key "fail"`},
  71. results: []string{"FAILURE", "OK"},
  72. },
  73. {
  74. compare: []string{`value("key1") != "value1"`},
  75. ifSucess: []string{`put key1 "success"`},
  76. ifFail: []string{`put key1 "fail"`},
  77. results: []string{"FAILURE", "OK"},
  78. },
  79. }
  80. for _, rq := range rqs {
  81. if err := ctlV3Txn(cx, rq); err != nil {
  82. cx.t.Fatal(err)
  83. }
  84. }
  85. }
  86. type txnRequests struct {
  87. compare []string
  88. ifSucess []string
  89. ifFail []string
  90. results []string
  91. }
  92. func ctlV3Txn(cx ctlCtx, rqs txnRequests) error {
  93. // TODO: support non-interactive mode
  94. cmdArgs := append(cx.PrefixArgs(), "txn")
  95. if cx.interactive {
  96. cmdArgs = append(cmdArgs, "--interactive")
  97. }
  98. proc, err := spawnCmd(cmdArgs)
  99. if err != nil {
  100. return err
  101. }
  102. _, err = proc.Expect("compares:")
  103. if err != nil {
  104. return err
  105. }
  106. for _, req := range rqs.compare {
  107. if err = proc.Send(req + "\r"); err != nil {
  108. return err
  109. }
  110. }
  111. if err = proc.Send("\r"); err != nil {
  112. return err
  113. }
  114. _, err = proc.Expect("success requests (get, put, del):")
  115. if err != nil {
  116. return err
  117. }
  118. for _, req := range rqs.ifSucess {
  119. if err = proc.Send(req + "\r"); err != nil {
  120. return err
  121. }
  122. }
  123. if err = proc.Send("\r"); err != nil {
  124. return err
  125. }
  126. _, err = proc.Expect("failure requests (get, put, del):")
  127. if err != nil {
  128. return err
  129. }
  130. for _, req := range rqs.ifFail {
  131. if err = proc.Send(req + "\r"); err != nil {
  132. return err
  133. }
  134. }
  135. if err = proc.Send("\r"); err != nil {
  136. return err
  137. }
  138. for _, line := range rqs.results {
  139. _, err = proc.Expect(line)
  140. if err != nil {
  141. return err
  142. }
  143. }
  144. return proc.Close()
  145. }