ctl_v3_watch_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. func watchTest(cx ctlCtx) {
  37. tests := []struct {
  38. puts []kv
  39. args []string
  40. wkv []kv
  41. }{
  42. { // watch 1 key
  43. []kv{{"sample", "value"}},
  44. []string{"sample", "--rev", "1"},
  45. []kv{{"sample", "value"}},
  46. },
  47. { // watch 3 keys by prefix
  48. []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}},
  49. []string{"key", "--rev", "1", "--prefix"},
  50. []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}},
  51. },
  52. { // watch by revision
  53. []kv{{"etcd", "revision_1"}, {"etcd", "revision_2"}, {"etcd", "revision_3"}},
  54. []string{"etcd", "--rev", "2"},
  55. []kv{{"etcd", "revision_2"}, {"etcd", "revision_3"}},
  56. },
  57. { // watch 3 keys by range
  58. []kv{{"key1", "val1"}, {"key3", "val3"}, {"key2", "val2"}},
  59. []string{"key", "key3", "--rev", "1"},
  60. []kv{{"key1", "val1"}, {"key2", "val2"}},
  61. },
  62. }
  63. for i, tt := range tests {
  64. donec := make(chan struct{})
  65. go func(i int, puts []kv) {
  66. for j := range puts {
  67. if err := ctlV3Put(cx, puts[j].key, puts[j].val, ""); err != nil {
  68. cx.t.Fatalf("watchTest #%d-%d: ctlV3Put error (%v)", i, j, err)
  69. }
  70. }
  71. close(donec)
  72. }(i, tt.puts)
  73. if err := ctlV3Watch(cx, tt.args, tt.wkv...); err != nil {
  74. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  75. cx.t.Errorf("watchTest #%d: ctlV3Watch error (%v)", i, err)
  76. }
  77. }
  78. <-donec
  79. }
  80. }
  81. func setupWatchArgs(cx ctlCtx, args []string) []string {
  82. cmdArgs := append(cx.PrefixArgs(), "watch")
  83. if cx.interactive {
  84. cmdArgs = append(cmdArgs, "--interactive")
  85. } else {
  86. cmdArgs = append(cmdArgs, args...)
  87. }
  88. return cmdArgs
  89. }
  90. func ctlV3Watch(cx ctlCtx, args []string, kvs ...kv) error {
  91. cmdArgs := setupWatchArgs(cx, args)
  92. proc, err := spawnCmd(cmdArgs)
  93. if err != nil {
  94. return err
  95. }
  96. if cx.interactive {
  97. wl := strings.Join(append([]string{"watch"}, args...), " ") + "\r"
  98. if err = proc.Send(wl); err != nil {
  99. return err
  100. }
  101. }
  102. for _, elem := range kvs {
  103. if _, err = proc.Expect(elem.key); err != nil {
  104. return err
  105. }
  106. if _, err = proc.Expect(elem.val); err != nil {
  107. return err
  108. }
  109. }
  110. return proc.Stop()
  111. }
  112. func ctlV3WatchFailPerm(cx ctlCtx, args []string) error {
  113. cmdArgs := setupWatchArgs(cx, args)
  114. proc, err := spawnCmd(cmdArgs)
  115. if err != nil {
  116. return err
  117. }
  118. if cx.interactive {
  119. wl := strings.Join(append([]string{"watch"}, args...), " ") + "\r"
  120. if err = proc.Send(wl); err != nil {
  121. return err
  122. }
  123. }
  124. // TODO(mitake): after printing accurate error message that includes
  125. // "permission denied", the above string argument of proc.Expect()
  126. // should be updated.
  127. _, err = proc.Expect("watch is canceled by the server")
  128. if err != nil {
  129. return err
  130. }
  131. return proc.Close()
  132. }