etcdctl_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 TestCtlV2Set(t *testing.T) {
  22. defer testutil.AfterTest(t)
  23. testProcessClusterV2CtlSetGet(
  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 TestCtlV2SetNoSync(t *testing.T) {
  36. defer testutil.AfterTest(t)
  37. testProcessClusterV2CtlSetGet(
  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 etcdctlPrefixArgs(epc *etcdProcessCluster, noSync bool) []string {
  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. args := []string{"../bin/etcdctl", "--endpoint", endpoint}
  57. if noSync {
  58. args = append(args, "--no-sync")
  59. }
  60. return args
  61. }
  62. func etcdctlSet(epc *etcdProcessCluster, key, value string, noSync bool) error {
  63. args := append(etcdctlPrefixArgs(epc, noSync), "set", key, value)
  64. return spawnWithExpect(args, value)
  65. }
  66. func etcdctlGet(epc *etcdProcessCluster, key, value string, noSync bool) error {
  67. args := append(etcdctlPrefixArgs(epc, noSync), "get", key)
  68. return spawnWithExpect(args, value)
  69. }
  70. func testProcessClusterV2CtlSetGet(t *testing.T, cfg *etcdProcessClusterConfig, noSync bool) {
  71. if fileutil.Exist("../bin/etcdctl") == false {
  72. t.Fatalf("could not find etcdctl binary")
  73. }
  74. epc, errC := newEtcdProcessCluster(cfg)
  75. if errC != nil {
  76. t.Fatalf("could not start etcd process cluster (%v)", errC)
  77. }
  78. defer func() {
  79. if errC := epc.Close(); errC != nil {
  80. t.Fatalf("error closing etcd processes (%v)", errC)
  81. }
  82. }()
  83. key, value := "foo", "bar"
  84. if err := etcdctlSet(epc, key, value, noSync); err != nil {
  85. t.Fatalf("failed set (%v)", err)
  86. }
  87. if err := etcdctlGet(epc, key, value, noSync); err != nil {
  88. t.Fatalf("failed set (%v)", err)
  89. }
  90. }
  91. func TestCtlV2Ls(t *testing.T) {
  92. defer testutil.AfterTest(t)
  93. testProcessClusterV2CtlLs(
  94. t,
  95. &etcdProcessClusterConfig{
  96. clusterSize: 3,
  97. proxySize: 1,
  98. isClientTLS: false,
  99. isPeerTLS: false,
  100. initialToken: "new",
  101. },
  102. false,
  103. )
  104. }
  105. func TestCtlV2LsNoSync(t *testing.T) {
  106. defer testutil.AfterTest(t)
  107. testProcessClusterV2CtlLs(
  108. t,
  109. &etcdProcessClusterConfig{
  110. clusterSize: 3,
  111. proxySize: 1,
  112. isClientTLS: false,
  113. isPeerTLS: false,
  114. initialToken: "new",
  115. },
  116. true,
  117. )
  118. }
  119. func etcdctlLs(epc *etcdProcessCluster, key string, noSync bool) error {
  120. args := append(etcdctlPrefixArgs(epc, noSync), "ls")
  121. return spawnWithExpect(args, key)
  122. }
  123. func testProcessClusterV2CtlLs(t *testing.T, cfg *etcdProcessClusterConfig, noSync bool) {
  124. if fileutil.Exist("../bin/etcdctl") == false {
  125. t.Fatalf("could not find etcdctl binary")
  126. }
  127. epc, errC := newEtcdProcessCluster(cfg)
  128. if errC != nil {
  129. t.Fatalf("could not start etcd process cluster (%v)", errC)
  130. }
  131. defer func() {
  132. if errC := epc.Close(); errC != nil {
  133. t.Fatalf("error closing etcd processes (%v)", errC)
  134. }
  135. }()
  136. key, value := "foo", "bar"
  137. if err := etcdctlSet(epc, key, value, noSync); err != nil {
  138. t.Fatalf("failed set (%v)", err)
  139. }
  140. if err := etcdctlLs(epc, key, noSync); err != nil {
  141. t.Fatalf("failed set (%v)", err)
  142. }
  143. }
  144. func TestCtlV2WatchWithProxy(t *testing.T) {
  145. defer testutil.AfterTest(t)
  146. testProcessClusterV2CtlWatch(
  147. t,
  148. &etcdProcessClusterConfig{
  149. clusterSize: 3,
  150. proxySize: 1,
  151. isClientTLS: false,
  152. isPeerTLS: false,
  153. initialToken: "new",
  154. },
  155. false,
  156. )
  157. }
  158. func TestCtlV2WatchWithProxyNoSync(t *testing.T) {
  159. defer testutil.AfterTest(t)
  160. testProcessClusterV2CtlWatch(
  161. t,
  162. &etcdProcessClusterConfig{
  163. clusterSize: 3,
  164. proxySize: 1,
  165. isClientTLS: false,
  166. isPeerTLS: false,
  167. initialToken: "new",
  168. },
  169. true,
  170. )
  171. }
  172. func etcdctlWatch(epc *etcdProcessCluster, key, value string, noSync bool, done chan struct{}, errChan chan error) {
  173. args := append(etcdctlPrefixArgs(epc, noSync), "watch", key)
  174. if err := spawnWithExpect(args, value); err != nil {
  175. errChan <- err
  176. return
  177. }
  178. done <- struct{}{}
  179. }
  180. func testProcessClusterV2CtlWatch(t *testing.T, cfg *etcdProcessClusterConfig, noSync bool) {
  181. if fileutil.Exist("../bin/etcdctl") == false {
  182. t.Fatalf("could not find etcdctl binary")
  183. }
  184. epc, errC := newEtcdProcessCluster(cfg)
  185. if errC != nil {
  186. t.Fatalf("could not start etcd process cluster (%v)", errC)
  187. }
  188. defer func() {
  189. if errC := epc.Close(); errC != nil {
  190. t.Fatalf("error closing etcd processes (%v)", errC)
  191. }
  192. }()
  193. key, value := "foo", "bar"
  194. done, errChan := make(chan struct{}), make(chan error)
  195. go etcdctlWatch(epc, key, value, noSync, done, errChan)
  196. if err := etcdctlSet(epc, key, value, noSync); err != nil {
  197. t.Fatalf("failed set (%v)", err)
  198. }
  199. select {
  200. case <-done:
  201. return
  202. case err := <-errChan:
  203. t.Fatalf("failed watch (%v)", err)
  204. case <-time.After(5 * time.Second):
  205. t.Fatalf("watch timed out!")
  206. }
  207. }