ctl_v3_watch_no_cov_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. // +build !cov
  15. package e2e
  16. import (
  17. "os"
  18. "testing"
  19. )
  20. func TestCtlV3Watch(t *testing.T) { testCtl(t, watchTest) }
  21. func TestCtlV3WatchNoTLS(t *testing.T) { testCtl(t, watchTest, withCfg(configNoTLS)) }
  22. func TestCtlV3WatchClientTLS(t *testing.T) { testCtl(t, watchTest, withCfg(configClientTLS)) }
  23. func TestCtlV3WatchPeerTLS(t *testing.T) { testCtl(t, watchTest, withCfg(configPeerTLS)) }
  24. func TestCtlV3WatchTimeout(t *testing.T) { testCtl(t, watchTest, withDialTimeout(0)) }
  25. func TestCtlV3WatchInteractive(t *testing.T) {
  26. testCtl(t, watchTest, withInteractive())
  27. }
  28. func TestCtlV3WatchInteractiveNoTLS(t *testing.T) {
  29. testCtl(t, watchTest, withInteractive(), withCfg(configNoTLS))
  30. }
  31. func TestCtlV3WatchInteractiveClientTLS(t *testing.T) {
  32. testCtl(t, watchTest, withInteractive(), withCfg(configClientTLS))
  33. }
  34. func TestCtlV3WatchInteractivePeerTLS(t *testing.T) {
  35. testCtl(t, watchTest, withInteractive(), withCfg(configPeerTLS))
  36. }
  37. func watchTest(cx ctlCtx) {
  38. tests := []struct {
  39. puts []kv
  40. envKey string
  41. envRange string
  42. args []string
  43. wkv []kvExec
  44. }{
  45. { // watch 1 key
  46. puts: []kv{{"sample", "value"}},
  47. args: []string{"sample", "--rev", "1"},
  48. wkv: []kvExec{{key: "sample", val: "value"}},
  49. },
  50. { // watch 1 key with env
  51. puts: []kv{{"sample", "value"}},
  52. envKey: "sample",
  53. args: []string{"--rev", "1"},
  54. wkv: []kvExec{{key: "sample", val: "value"}},
  55. },
  56. { // watch 1 key with ${ETCD_WATCH_VALUE}
  57. puts: []kv{{"sample", "value"}},
  58. args: []string{"sample", "--rev", "1", "--", "env"},
  59. wkv: []kvExec{{key: "sample", val: "value", execOutput: `ETCD_WATCH_VALUE="value"`}},
  60. },
  61. { // watch 1 key with "echo watch event received", with env
  62. puts: []kv{{"sample", "value"}},
  63. envKey: "sample",
  64. args: []string{"--rev", "1", "--", "echo", "watch event received"},
  65. wkv: []kvExec{{key: "sample", val: "value", execOutput: "watch event received"}},
  66. },
  67. { // watch 1 key with "echo watch event received"
  68. puts: []kv{{"sample", "value"}},
  69. args: []string{"--rev", "1", "sample", "--", "echo", "watch event received"},
  70. wkv: []kvExec{{key: "sample", val: "value", execOutput: "watch event received"}},
  71. },
  72. { // watch 1 key with "echo \"Hello World!\""
  73. puts: []kv{{"sample", "value"}},
  74. args: []string{"--rev", "1", "sample", "--", "echo", "\"Hello World!\""},
  75. wkv: []kvExec{{key: "sample", val: "value", execOutput: "Hello World!"}},
  76. },
  77. { // watch 1 key with "echo watch event received"
  78. puts: []kv{{"sample", "value"}},
  79. args: []string{"sample", "samplx", "--rev", "1", "--", "echo", "watch event received"},
  80. wkv: []kvExec{{key: "sample", val: "value", execOutput: "watch event received"}},
  81. },
  82. { // watch 1 key with "echo watch event received"
  83. puts: []kv{{"sample", "value"}},
  84. envKey: "sample",
  85. envRange: "samplx",
  86. args: []string{"--rev", "1", "--", "echo", "watch event received"},
  87. wkv: []kvExec{{key: "sample", val: "value", execOutput: "watch event received"}},
  88. },
  89. { // watch 1 key with "echo watch event received"
  90. puts: []kv{{"sample", "value"}},
  91. args: []string{"sample", "--rev", "1", "samplx", "--", "echo", "watch event received"},
  92. wkv: []kvExec{{key: "sample", val: "value", execOutput: "watch event received"}},
  93. },
  94. { // watch 3 keys by prefix
  95. puts: []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}},
  96. args: []string{"key", "--rev", "1", "--prefix"},
  97. wkv: []kvExec{{key: "key1", val: "val1"}, {key: "key2", val: "val2"}, {key: "key3", val: "val3"}},
  98. },
  99. { // watch 3 keys by prefix, with env
  100. puts: []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}},
  101. envKey: "key",
  102. args: []string{"--rev", "1", "--prefix"},
  103. wkv: []kvExec{{key: "key1", val: "val1"}, {key: "key2", val: "val2"}, {key: "key3", val: "val3"}},
  104. },
  105. { // watch by revision
  106. puts: []kv{{"etcd", "revision_1"}, {"etcd", "revision_2"}, {"etcd", "revision_3"}},
  107. args: []string{"etcd", "--rev", "2"},
  108. wkv: []kvExec{{key: "etcd", val: "revision_2"}, {key: "etcd", val: "revision_3"}},
  109. },
  110. { // watch 3 keys by range
  111. puts: []kv{{"key1", "val1"}, {"key3", "val3"}, {"key2", "val2"}},
  112. args: []string{"key", "key3", "--rev", "1"},
  113. wkv: []kvExec{{key: "key1", val: "val1"}, {key: "key2", val: "val2"}},
  114. },
  115. { // watch 3 keys by range, with env
  116. puts: []kv{{"key1", "val1"}, {"key3", "val3"}, {"key2", "val2"}},
  117. envKey: "key",
  118. envRange: "key3",
  119. args: []string{"--rev", "1"},
  120. wkv: []kvExec{{key: "key1", val: "val1"}, {key: "key2", val: "val2"}},
  121. },
  122. }
  123. for i, tt := range tests {
  124. donec := make(chan struct{})
  125. go func(i int, puts []kv) {
  126. for j := range puts {
  127. if err := ctlV3Put(cx, puts[j].key, puts[j].val, ""); err != nil {
  128. cx.t.Errorf("watchTest #%d-%d: ctlV3Put error (%v)", i, j, err)
  129. }
  130. }
  131. close(donec)
  132. }(i, tt.puts)
  133. unsetEnv := func() {}
  134. if tt.envKey != "" || tt.envRange != "" {
  135. if tt.envKey != "" {
  136. os.Setenv("ETCDCTL_WATCH_KEY", tt.envKey)
  137. unsetEnv = func() { os.Unsetenv("ETCDCTL_WATCH_KEY") }
  138. }
  139. if tt.envRange != "" {
  140. os.Setenv("ETCDCTL_WATCH_RANGE_END", tt.envRange)
  141. unsetEnv = func() { os.Unsetenv("ETCDCTL_WATCH_RANGE_END") }
  142. }
  143. if tt.envKey != "" && tt.envRange != "" {
  144. unsetEnv = func() {
  145. os.Unsetenv("ETCDCTL_WATCH_KEY")
  146. os.Unsetenv("ETCDCTL_WATCH_RANGE_END")
  147. }
  148. }
  149. }
  150. if err := ctlV3Watch(cx, tt.args, tt.wkv...); err != nil {
  151. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  152. cx.t.Errorf("watchTest #%d: ctlV3Watch error (%v)", i, err)
  153. }
  154. }
  155. unsetEnv()
  156. <-donec
  157. }
  158. }