ctl_v3_watch_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. {
  43. []kv{{"sample", "value"}},
  44. []string{"sample", "--rev", "1"},
  45. []kv{{"sample", "value"}},
  46. },
  47. {
  48. []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}},
  49. []string{"key", "--rev", "1", "--prefix"},
  50. []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}},
  51. },
  52. {
  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. }
  58. for i, tt := range tests {
  59. go func() {
  60. for j := range tt.puts {
  61. if err := ctlV3Put(cx, tt.puts[j].key, tt.puts[j].val, ""); err != nil {
  62. cx.t.Fatalf("watchTest #%d-%d: ctlV3Put error (%v)", i, j, err)
  63. }
  64. }
  65. }()
  66. if err := ctlV3Watch(cx, tt.args, tt.wkv...); err != nil {
  67. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  68. cx.t.Errorf("watchTest #%d: ctlV3Watch error (%v)", i, err)
  69. }
  70. }
  71. }
  72. }
  73. func ctlV3Watch(cx ctlCtx, args []string, kvs ...kv) error {
  74. cmdArgs := append(cx.PrefixArgs(), "watch")
  75. if cx.interactive {
  76. cmdArgs = append(cmdArgs, "--interactive")
  77. } else {
  78. cmdArgs = append(cmdArgs, args...)
  79. }
  80. proc, err := spawnCmd(cmdArgs)
  81. if err != nil {
  82. return err
  83. }
  84. if cx.interactive {
  85. wl := strings.Join(append([]string{"watch"}, args...), " ") + "\r"
  86. if err = proc.Send(wl); err != nil {
  87. return err
  88. }
  89. }
  90. for _, elem := range kvs {
  91. if _, err = proc.Expect(elem.key); err != nil {
  92. return err
  93. }
  94. if _, err = proc.Expect(elem.val); err != nil {
  95. return err
  96. }
  97. }
  98. return proc.Stop()
  99. }