ctl_v3_watch_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 "strings"
  16. type kvExec struct {
  17. key, val string
  18. execOutput string
  19. }
  20. func setupWatchArgs(cx ctlCtx, args []string) []string {
  21. cmdArgs := append(cx.PrefixArgs(), "watch")
  22. if cx.interactive {
  23. cmdArgs = append(cmdArgs, "--interactive")
  24. } else {
  25. cmdArgs = append(cmdArgs, args...)
  26. }
  27. return cmdArgs
  28. }
  29. func ctlV3Watch(cx ctlCtx, args []string, kvs ...kvExec) error {
  30. cmdArgs := setupWatchArgs(cx, args)
  31. proc, err := spawnCmd(cmdArgs)
  32. if err != nil {
  33. return err
  34. }
  35. if cx.interactive {
  36. wl := strings.Join(append([]string{"watch"}, args...), " ") + "\r"
  37. if err = proc.Send(wl); err != nil {
  38. return err
  39. }
  40. }
  41. for _, elem := range kvs {
  42. if _, err = proc.Expect(elem.key); err != nil {
  43. return err
  44. }
  45. if _, err = proc.Expect(elem.val); err != nil {
  46. return err
  47. }
  48. if elem.execOutput != "" {
  49. if _, err = proc.Expect(elem.execOutput); err != nil {
  50. return err
  51. }
  52. }
  53. }
  54. return proc.Stop()
  55. }
  56. func ctlV3WatchFailPerm(cx ctlCtx, args []string) error {
  57. cmdArgs := setupWatchArgs(cx, args)
  58. proc, err := spawnCmd(cmdArgs)
  59. if err != nil {
  60. return err
  61. }
  62. if cx.interactive {
  63. wl := strings.Join(append([]string{"watch"}, args...), " ") + "\r"
  64. if err = proc.Send(wl); err != nil {
  65. return err
  66. }
  67. }
  68. // TODO(mitake): after printing accurate error message that includes
  69. // "permission denied", the above string argument of proc.Expect()
  70. // should be updated.
  71. _, err = proc.Expect("watch is canceled by the server")
  72. if err != nil {
  73. return err
  74. }
  75. return proc.Close()
  76. }