ctl_v3_watch_cov_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // coverage tests get extra arguments:
  57. // ./bin/etcdctl_test -test.coverprofile=e2e.1525392462795198897.coverprofile -test.outputdir=../..
  58. // do not test watch exec commands
  59. { // watch 3 keys by prefix
  60. puts: []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}},
  61. args: []string{"key", "--rev", "1", "--prefix"},
  62. wkv: []kvExec{{key: "key1", val: "val1"}, {key: "key2", val: "val2"}, {key: "key3", val: "val3"}},
  63. },
  64. { // watch 3 keys by prefix, with env
  65. puts: []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}},
  66. envKey: "key",
  67. args: []string{"--rev", "1", "--prefix"},
  68. wkv: []kvExec{{key: "key1", val: "val1"}, {key: "key2", val: "val2"}, {key: "key3", val: "val3"}},
  69. },
  70. { // watch by revision
  71. puts: []kv{{"etcd", "revision_1"}, {"etcd", "revision_2"}, {"etcd", "revision_3"}},
  72. args: []string{"etcd", "--rev", "2"},
  73. wkv: []kvExec{{key: "etcd", val: "revision_2"}, {key: "etcd", val: "revision_3"}},
  74. },
  75. { // watch 3 keys by range
  76. puts: []kv{{"key1", "val1"}, {"key3", "val3"}, {"key2", "val2"}},
  77. args: []string{"key", "key3", "--rev", "1"},
  78. wkv: []kvExec{{key: "key1", val: "val1"}, {key: "key2", val: "val2"}},
  79. },
  80. { // watch 3 keys by range, with env
  81. puts: []kv{{"key1", "val1"}, {"key3", "val3"}, {"key2", "val2"}},
  82. envKey: "key",
  83. envRange: "key3",
  84. args: []string{"--rev", "1"},
  85. wkv: []kvExec{{key: "key1", val: "val1"}, {key: "key2", val: "val2"}},
  86. },
  87. }
  88. for i, tt := range tests {
  89. donec := make(chan struct{})
  90. go func(i int, puts []kv) {
  91. for j := range puts {
  92. if err := ctlV3Put(cx, puts[j].key, puts[j].val, ""); err != nil {
  93. cx.t.Fatalf("watchTest #%d-%d: ctlV3Put error (%v)", i, j, err)
  94. }
  95. }
  96. close(donec)
  97. }(i, tt.puts)
  98. unsetEnv := func() {}
  99. if tt.envKey != "" || tt.envRange != "" {
  100. if tt.envKey != "" {
  101. os.Setenv("ETCDCTL_WATCH_KEY", tt.envKey)
  102. unsetEnv = func() { os.Unsetenv("ETCDCTL_WATCH_KEY") }
  103. }
  104. if tt.envRange != "" {
  105. os.Setenv("ETCDCTL_WATCH_RANGE_END", tt.envRange)
  106. unsetEnv = func() { os.Unsetenv("ETCDCTL_WATCH_RANGE_END") }
  107. }
  108. if tt.envKey != "" && tt.envRange != "" {
  109. unsetEnv = func() {
  110. os.Unsetenv("ETCDCTL_WATCH_KEY")
  111. os.Unsetenv("ETCDCTL_WATCH_RANGE_END")
  112. }
  113. }
  114. }
  115. if err := ctlV3Watch(cx, tt.args, tt.wkv...); err != nil {
  116. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  117. cx.t.Errorf("watchTest #%d: ctlV3Watch error (%v)", i, err)
  118. }
  119. }
  120. unsetEnv()
  121. <-donec
  122. }
  123. }