ctl_v3_txn_test.go 3.5 KB

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