ctl_v3_watch_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 (
  16. "strings"
  17. "testing"
  18. )
  19. func TestCtlV3Watch(t *testing.T) { testCtl(t, watchTest) }
  20. func TestCtlV3WatchNoTLS(t *testing.T) { testCtl(t, watchTest, withCfg(configNoTLS)) }
  21. func TestCtlV3WatchClientTLS(t *testing.T) { testCtl(t, watchTest, withCfg(configClientTLS)) }
  22. func TestCtlV3WatchPeerTLS(t *testing.T) { testCtl(t, watchTest, withCfg(configPeerTLS)) }
  23. func TestCtlV3WatchTimeout(t *testing.T) { testCtl(t, watchTest, withDialTimeout(0)) }
  24. func TestCtlV3WatchInteractive(t *testing.T) {
  25. testCtl(t, watchTest, withInteractive())
  26. }
  27. func TestCtlV3WatchInteractiveNoTLS(t *testing.T) {
  28. testCtl(t, watchTest, withInteractive(), withCfg(configNoTLS))
  29. }
  30. func TestCtlV3WatchInteractiveClientTLS(t *testing.T) {
  31. testCtl(t, watchTest, withInteractive(), withCfg(configClientTLS))
  32. }
  33. func TestCtlV3WatchInteractivePeerTLS(t *testing.T) {
  34. testCtl(t, watchTest, withInteractive(), withCfg(configPeerTLS))
  35. }
  36. type kvExec struct {
  37. key, val string
  38. execOutput string
  39. }
  40. func watchTest(cx ctlCtx) {
  41. tests := []struct {
  42. puts []kv
  43. args []string
  44. wkv []kvExec
  45. }{
  46. { // watch 1 key
  47. []kv{{"sample", "value"}},
  48. []string{"sample", "--rev", "1"},
  49. []kvExec{{key: "sample", val: "value"}},
  50. },
  51. { // watch 1 key with "echo watch event received"
  52. []kv{{"sample", "value"}},
  53. []string{"sample", "--rev", "1", "--", "echo", "watch event received"},
  54. []kvExec{{key: "sample", val: "value", execOutput: "watch event received"}},
  55. },
  56. { // watch 1 key with "echo watch event received"
  57. []kv{{"sample", "value"}},
  58. []string{"--rev", "1", "sample", "--", "echo", "watch event received"},
  59. []kvExec{{key: "sample", val: "value", execOutput: "watch event received"}},
  60. },
  61. { // watch 1 key with "echo \"Hello World!\""
  62. []kv{{"sample", "value"}},
  63. []string{"--rev", "1", "sample", "--", "echo", "\"Hello World!\""},
  64. []kvExec{{key: "sample", val: "value", execOutput: "Hello World!"}},
  65. },
  66. { // watch 1 key with "echo watch event received"
  67. []kv{{"sample", "value"}},
  68. []string{"sample", "samplx", "--rev", "1", "--", "echo", "watch event received"},
  69. []kvExec{{key: "sample", val: "value", execOutput: "watch event received"}},
  70. },
  71. { // watch 1 key with "echo watch event received"
  72. []kv{{"sample", "value"}},
  73. []string{"sample", "--rev", "1", "samplx", "--", "echo", "watch event received"},
  74. []kvExec{{key: "sample", val: "value", execOutput: "watch event received"}},
  75. },
  76. { // watch 3 keys by prefix
  77. []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}},
  78. []string{"key", "--rev", "1", "--prefix"},
  79. []kvExec{{key: "key1", val: "val1"}, {key: "key2", val: "val2"}, {key: "key3", val: "val3"}},
  80. },
  81. { // watch by revision
  82. []kv{{"etcd", "revision_1"}, {"etcd", "revision_2"}, {"etcd", "revision_3"}},
  83. []string{"etcd", "--rev", "2"},
  84. []kvExec{{key: "etcd", val: "revision_2"}, {key: "etcd", val: "revision_3"}},
  85. },
  86. { // watch 3 keys by range
  87. []kv{{"key1", "val1"}, {"key3", "val3"}, {"key2", "val2"}},
  88. []string{"key", "key3", "--rev", "1"},
  89. []kvExec{{key: "key1", val: "val1"}, {key: "key2", val: "val2"}},
  90. },
  91. }
  92. for i, tt := range tests {
  93. donec := make(chan struct{})
  94. go func(i int, puts []kv) {
  95. for j := range puts {
  96. if err := ctlV3Put(cx, puts[j].key, puts[j].val, ""); err != nil {
  97. cx.t.Fatalf("watchTest #%d-%d: ctlV3Put error (%v)", i, j, err)
  98. }
  99. }
  100. close(donec)
  101. }(i, tt.puts)
  102. if err := ctlV3Watch(cx, tt.args, tt.wkv...); err != nil {
  103. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  104. cx.t.Errorf("watchTest #%d: ctlV3Watch error (%v)", i, err)
  105. }
  106. }
  107. <-donec
  108. }
  109. }
  110. func setupWatchArgs(cx ctlCtx, args []string) []string {
  111. cmdArgs := append(cx.PrefixArgs(), "watch")
  112. if cx.interactive {
  113. cmdArgs = append(cmdArgs, "--interactive")
  114. } else {
  115. cmdArgs = append(cmdArgs, args...)
  116. }
  117. return cmdArgs
  118. }
  119. func ctlV3Watch(cx ctlCtx, args []string, kvs ...kvExec) error {
  120. cmdArgs := setupWatchArgs(cx, args)
  121. proc, err := spawnCmd(cmdArgs)
  122. if err != nil {
  123. return err
  124. }
  125. if cx.interactive {
  126. wl := strings.Join(append([]string{"watch"}, args...), " ") + "\r"
  127. if err = proc.Send(wl); err != nil {
  128. return err
  129. }
  130. }
  131. for _, elem := range kvs {
  132. if _, err = proc.Expect(elem.key); err != nil {
  133. return err
  134. }
  135. if _, err = proc.Expect(elem.val); err != nil {
  136. return err
  137. }
  138. if elem.execOutput != "" {
  139. if _, err = proc.Expect(elem.execOutput); err != nil {
  140. return err
  141. }
  142. }
  143. }
  144. return proc.Stop()
  145. }
  146. func ctlV3WatchFailPerm(cx ctlCtx, args []string) error {
  147. cmdArgs := setupWatchArgs(cx, args)
  148. proc, err := spawnCmd(cmdArgs)
  149. if err != nil {
  150. return err
  151. }
  152. if cx.interactive {
  153. wl := strings.Join(append([]string{"watch"}, args...), " ") + "\r"
  154. if err = proc.Send(wl); err != nil {
  155. return err
  156. }
  157. }
  158. // TODO(mitake): after printing accurate error message that includes
  159. // "permission denied", the above string argument of proc.Expect()
  160. // should be updated.
  161. _, err = proc.Expect("watch is canceled by the server")
  162. if err != nil {
  163. return err
  164. }
  165. return proc.Close()
  166. }