etcdctl_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2016 CoreOS, Inc.
  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. "testing"
  17. "time"
  18. "github.com/coreos/etcd/pkg/fileutil"
  19. "github.com/coreos/etcd/pkg/testutil"
  20. )
  21. func TestBasicOpsV2CtlWatchWithProxy(t *testing.T) {
  22. defer testutil.AfterTest(t)
  23. testProcessClusterV2CtlWatch(
  24. t,
  25. &etcdProcessClusterConfig{
  26. clusterSize: 3,
  27. proxySize: 1,
  28. isClientTLS: false,
  29. isPeerTLS: false,
  30. initialToken: "new",
  31. },
  32. false,
  33. )
  34. }
  35. func TestBasicOpsV2CtlWatchWithProxyNoSync(t *testing.T) {
  36. defer testutil.AfterTest(t)
  37. testProcessClusterV2CtlWatch(
  38. t,
  39. &etcdProcessClusterConfig{
  40. clusterSize: 3,
  41. proxySize: 1,
  42. isClientTLS: false,
  43. isPeerTLS: false,
  44. initialToken: "new",
  45. },
  46. true,
  47. )
  48. }
  49. func etcdctlSet(epc *etcdProcessCluster, key, value string, noSync bool) error {
  50. endpoint := ""
  51. if proxies := epc.proxies(); len(proxies) != 0 {
  52. endpoint = proxies[0].cfg.acurl.String()
  53. } else if backends := epc.backends(); len(backends) != 0 {
  54. endpoint = backends[0].cfg.acurl.String()
  55. }
  56. putArgs := []string{"../bin/etcdctl", "--endpoint", endpoint}
  57. if noSync {
  58. putArgs = append(putArgs, "--no-sync")
  59. }
  60. putArgs = append(putArgs, "set", key, value)
  61. return spawnWithExpect(putArgs, value)
  62. }
  63. func etcdctlWatch(epc *etcdProcessCluster, key, value string, noSync bool, done chan struct{}, errChan chan error) {
  64. endpoint := ""
  65. if proxies := epc.proxies(); len(proxies) != 0 {
  66. endpoint = proxies[0].cfg.acurl.String()
  67. } else if backends := epc.backends(); len(backends) != 0 {
  68. endpoint = backends[0].cfg.acurl.String()
  69. }
  70. watchArgs := []string{"../bin/etcdctl", "--endpoint", endpoint}
  71. if noSync {
  72. watchArgs = append(watchArgs, "--no-sync")
  73. }
  74. watchArgs = append(watchArgs, "watch", key)
  75. if err := spawnWithExpect(watchArgs, value); err != nil {
  76. errChan <- err
  77. return
  78. }
  79. done <- struct{}{}
  80. }
  81. func testProcessClusterV2CtlWatch(t *testing.T, cfg *etcdProcessClusterConfig, noSync bool) {
  82. if fileutil.Exist("../bin/etcdctl") == false {
  83. t.Fatalf("could not find etcdctl binary")
  84. }
  85. epc, errC := newEtcdProcessCluster(cfg)
  86. if errC != nil {
  87. t.Fatalf("could not start etcd process cluster (%v)", errC)
  88. }
  89. defer func() {
  90. if errC := epc.Close(); errC != nil {
  91. t.Fatalf("error closing etcd processes (%v)", errC)
  92. }
  93. }()
  94. key, value := "foo", "bar"
  95. done, errChan := make(chan struct{}), make(chan error)
  96. go etcdctlWatch(epc, key, value, noSync, done, errChan)
  97. if err := etcdctlSet(epc, key, value, noSync); err != nil {
  98. t.Fatalf("failed set (%v)", err)
  99. }
  100. select {
  101. case <-done:
  102. return
  103. case err := <-errChan:
  104. t.Fatalf("failed watch (%v)", err)
  105. case <-time.After(2 * time.Second):
  106. t.Fatalf("watch timed out!")
  107. }
  108. }