ctl_v3_watch_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 ctlV3Watch(cx ctlCtx, args []string, kvs ...kv) error {
  82. cmdArgs := append(cx.PrefixArgs(), "watch")
  83. if cx.interactive {
  84. cmdArgs = append(cmdArgs, "--interactive")
  85. } else {
  86. cmdArgs = append(cmdArgs, args...)
  87. }
  88. proc, err := spawnCmd(cmdArgs)
  89. if err != nil {
  90. return err
  91. }
  92. if cx.interactive {
  93. wl := strings.Join(append([]string{"watch"}, args...), " ") + "\r"
  94. if err = proc.Send(wl); err != nil {
  95. return err
  96. }
  97. }
  98. for _, elem := range kvs {
  99. if _, err = proc.Expect(elem.key); err != nil {
  100. return err
  101. }
  102. if _, err = proc.Expect(elem.val); err != nil {
  103. return err
  104. }
  105. }
  106. return proc.Stop()
  107. }