ctl_v3_watch_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2018 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 (
  16. "fmt"
  17. "strings"
  18. )
  19. type kvExec struct {
  20. key, val string
  21. execOutput string
  22. }
  23. func setupWatchArgs(cx ctlCtx, args []string) []string {
  24. cmdArgs := append(cx.PrefixArgs(), "watch")
  25. if cx.interactive {
  26. cmdArgs = append(cmdArgs, "--interactive")
  27. } else {
  28. cmdArgs = append(cmdArgs, args...)
  29. }
  30. return cmdArgs
  31. }
  32. func ctlV3Watch(cx ctlCtx, args []string, kvs ...kvExec) error {
  33. cmdArgs := setupWatchArgs(cx, args)
  34. proc, err := spawnCmd(cmdArgs)
  35. if err != nil {
  36. return err
  37. }
  38. if cx.interactive {
  39. wl := strings.Join(append([]string{"watch"}, args...), " ") + "\r"
  40. if err = proc.Send(wl); err != nil {
  41. return err
  42. }
  43. }
  44. for _, elem := range kvs {
  45. if _, err = proc.Expect(elem.key); err != nil {
  46. return err
  47. }
  48. if _, err = proc.Expect(elem.val); err != nil {
  49. return err
  50. }
  51. if elem.execOutput != "" {
  52. if _, err = proc.Expect(elem.execOutput); err != nil {
  53. return err
  54. }
  55. }
  56. }
  57. return proc.Stop()
  58. }
  59. func ctlV3WatchFailPerm(cx ctlCtx, args []string) error {
  60. cmdArgs := setupWatchArgs(cx, args)
  61. proc, err := spawnCmd(cmdArgs)
  62. if err != nil {
  63. return err
  64. }
  65. if cx.interactive {
  66. wl := strings.Join(append([]string{"watch"}, args...), " ") + "\r"
  67. if err = proc.Send(wl); err != nil {
  68. return err
  69. }
  70. }
  71. // TODO(mitake): after printing accurate error message that includes
  72. // "permission denied", the above string argument of proc.Expect()
  73. // should be updated.
  74. _, err = proc.Expect("watch is canceled by the server")
  75. if err != nil {
  76. return err
  77. }
  78. return proc.Close()
  79. }
  80. func ctlV3Put(cx ctlCtx, key, value, leaseID string, flags ...string) error {
  81. skipValue := false
  82. skipLease := false
  83. for _, f := range flags {
  84. if f == "--ignore-value" {
  85. skipValue = true
  86. }
  87. if f == "--ignore-lease" {
  88. skipLease = true
  89. }
  90. }
  91. cmdArgs := append(cx.PrefixArgs(), "put", key)
  92. if !skipValue {
  93. cmdArgs = append(cmdArgs, value)
  94. }
  95. if leaseID != "" && !skipLease {
  96. cmdArgs = append(cmdArgs, "--lease", leaseID)
  97. }
  98. if len(flags) != 0 {
  99. cmdArgs = append(cmdArgs, flags...)
  100. }
  101. return spawnWithExpect(cmdArgs, "OK")
  102. }
  103. type kv struct {
  104. key, val string
  105. }
  106. func ctlV3Get(cx ctlCtx, args []string, kvs ...kv) error {
  107. cmdArgs := append(cx.PrefixArgs(), "get")
  108. cmdArgs = append(cmdArgs, args...)
  109. if !cx.quorum {
  110. cmdArgs = append(cmdArgs, "--consistency", "s")
  111. }
  112. var lines []string
  113. for _, elem := range kvs {
  114. lines = append(lines, elem.key, elem.val)
  115. }
  116. return spawnWithExpects(cmdArgs, lines...)
  117. }
  118. // ctlV3GetWithErr runs "get" command expecting no output but error
  119. func ctlV3GetWithErr(cx ctlCtx, args []string, errs []string) error {
  120. cmdArgs := append(cx.PrefixArgs(), "get")
  121. cmdArgs = append(cmdArgs, args...)
  122. if !cx.quorum {
  123. cmdArgs = append(cmdArgs, "--consistency", "s")
  124. }
  125. return spawnWithExpects(cmdArgs, errs...)
  126. }
  127. func ctlV3Del(cx ctlCtx, args []string, num int) error {
  128. cmdArgs := append(cx.PrefixArgs(), "del")
  129. cmdArgs = append(cmdArgs, args...)
  130. return spawnWithExpects(cmdArgs, fmt.Sprintf("%d", num))
  131. }