etcdctl_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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) { testCtlV2Set(t, &defaultConfig, false) }
  22. func TestCtlV2SetClientTLS(t *testing.T) { testCtlV2Set(t, &defaultConfigClientTLS, false) }
  23. func TestCtlV2SetPeerTLS(t *testing.T) { testCtlV2Set(t, &defaultConfigPeerTLS, false) }
  24. func TestCtlV2SetTLS(t *testing.T) { testCtlV2Set(t, &defaultConfigTLS, false) }
  25. func testCtlV2Set(t *testing.T, cfg *etcdProcessClusterConfig, noSync bool) {
  26. defer testutil.AfterTest(t)
  27. if fileutil.Exist("../bin/etcdctl") == false {
  28. t.Fatalf("could not find etcdctl binary")
  29. }
  30. epc, errC := newEtcdProcessCluster(cfg)
  31. if errC != nil {
  32. t.Fatalf("could not start etcd process cluster (%v)", errC)
  33. }
  34. defer func() {
  35. if errC := epc.Close(); errC != nil {
  36. t.Fatalf("error closing etcd processes (%v)", errC)
  37. }
  38. }()
  39. key, value := "foo", "bar"
  40. if err := etcdctlSet(epc, key, value, noSync); err != nil {
  41. t.Fatalf("failed set (%v)", err)
  42. }
  43. if err := etcdctlGet(epc, key, value, noSync); err != nil {
  44. t.Fatalf("failed get (%v)", err)
  45. }
  46. }
  47. func TestCtlV2Mk(t *testing.T) { testCtlV2Mk(t, &defaultConfig, false) }
  48. func TestCtlV2MkTLS(t *testing.T) { testCtlV2Mk(t, &defaultConfigTLS, false) }
  49. func testCtlV2Mk(t *testing.T, cfg *etcdProcessClusterConfig, noSync bool) {
  50. defer testutil.AfterTest(t)
  51. if fileutil.Exist("../bin/etcdctl") == false {
  52. t.Fatalf("could not find etcdctl binary")
  53. }
  54. epc, errC := newEtcdProcessCluster(cfg)
  55. if errC != nil {
  56. t.Fatalf("could not start etcd process cluster (%v)", errC)
  57. }
  58. defer func() {
  59. if errC := epc.Close(); errC != nil {
  60. t.Fatalf("error closing etcd processes (%v)", errC)
  61. }
  62. }()
  63. key, value := "foo", "bar"
  64. if err := etcdctlMk(epc, key, value, true, noSync); err != nil {
  65. t.Fatalf("failed mk (%v)", err)
  66. }
  67. if err := etcdctlMk(epc, key, value, false, noSync); err != nil {
  68. t.Fatalf("failed mk (%v)", err)
  69. }
  70. if err := etcdctlGet(epc, key, value, noSync); err != nil {
  71. t.Fatalf("failed get (%v)", err)
  72. }
  73. }
  74. func TestCtlV2Rm(t *testing.T) { testCtlV2Rm(t, &defaultConfig, false) }
  75. func TestCtlV2RmTLS(t *testing.T) { testCtlV2Rm(t, &defaultConfigTLS, false) }
  76. func testCtlV2Rm(t *testing.T, cfg *etcdProcessClusterConfig, noSync bool) {
  77. defer testutil.AfterTest(t)
  78. if fileutil.Exist("../bin/etcdctl") == false {
  79. t.Fatalf("could not find etcdctl binary")
  80. }
  81. epc, errC := newEtcdProcessCluster(cfg)
  82. if errC != nil {
  83. t.Fatalf("could not start etcd process cluster (%v)", errC)
  84. }
  85. defer func() {
  86. if errC := epc.Close(); errC != nil {
  87. t.Fatalf("error closing etcd processes (%v)", errC)
  88. }
  89. }()
  90. key, value := "foo", "bar"
  91. if err := etcdctlSet(epc, key, value, noSync); err != nil {
  92. t.Fatalf("failed set (%v)", err)
  93. }
  94. if err := etcdctlRm(epc, key, value, true, noSync); err != nil {
  95. t.Fatalf("failed rm (%v)", err)
  96. }
  97. if err := etcdctlRm(epc, key, value, false, noSync); err != nil {
  98. t.Fatalf("failed rm (%v)", err)
  99. }
  100. }
  101. func TestCtlV2Ls(t *testing.T) { testCtlV2Ls(t, &defaultConfig, false) }
  102. func TestCtlV2LsTLS(t *testing.T) { testCtlV2Ls(t, &defaultConfigTLS, false) }
  103. func testCtlV2Ls(t *testing.T, cfg *etcdProcessClusterConfig, noSync bool) {
  104. defer testutil.AfterTest(t)
  105. if fileutil.Exist("../bin/etcdctl") == false {
  106. t.Fatalf("could not find etcdctl binary")
  107. }
  108. epc, errC := newEtcdProcessCluster(cfg)
  109. if errC != nil {
  110. t.Fatalf("could not start etcd process cluster (%v)", errC)
  111. }
  112. defer func() {
  113. if errC := epc.Close(); errC != nil {
  114. t.Fatalf("error closing etcd processes (%v)", errC)
  115. }
  116. }()
  117. key, value := "foo", "bar"
  118. if err := etcdctlSet(epc, key, value, noSync); err != nil {
  119. t.Fatalf("failed set (%v)", err)
  120. }
  121. if err := etcdctlLs(epc, key, noSync); err != nil {
  122. t.Fatalf("failed ls (%v)", err)
  123. }
  124. }
  125. func TestCtlV2Watch(t *testing.T) { testCtlV2Watch(t, &defaultConfig, false) }
  126. func TestCtlV2WatchTLS(t *testing.T) { testCtlV2Watch(t, &defaultConfigTLS, false) }
  127. func TestCtlV2WatchWithProxy(t *testing.T) { testCtlV2Watch(t, &defaultConfigWithProxy, false) }
  128. func TestCtlV2WatchWithProxyNoSync(t *testing.T) { testCtlV2Watch(t, &defaultConfigWithProxy, true) }
  129. func testCtlV2Watch(t *testing.T, cfg *etcdProcessClusterConfig, noSync bool) {
  130. defer testutil.AfterTest(t)
  131. if fileutil.Exist("../bin/etcdctl") == false {
  132. t.Fatalf("could not find etcdctl binary")
  133. }
  134. epc, errC := newEtcdProcessCluster(cfg)
  135. if errC != nil {
  136. t.Fatalf("could not start etcd process cluster (%v)", errC)
  137. }
  138. defer func() {
  139. if errC := epc.Close(); errC != nil {
  140. t.Fatalf("error closing etcd processes (%v)", errC)
  141. }
  142. }()
  143. key, value := "foo", "bar"
  144. errc := etcdctlWatch(epc, key, value, noSync)
  145. if err := etcdctlSet(epc, key, value, noSync); err != nil {
  146. t.Fatalf("failed set (%v)", err)
  147. }
  148. select {
  149. case err := <-errc:
  150. if err != nil {
  151. t.Fatalf("failed watch (%v)", err)
  152. }
  153. case <-time.After(5 * time.Second):
  154. t.Fatalf("watch timed out")
  155. }
  156. }
  157. func etcdctlPrefixArgs(clus *etcdProcessCluster, noSync bool) []string {
  158. endpoint := ""
  159. if proxies := clus.proxies(); len(proxies) != 0 {
  160. endpoint = proxies[0].cfg.acurl.String()
  161. } else if backends := clus.backends(); len(backends) != 0 {
  162. endpoint = backends[0].cfg.acurl.String()
  163. }
  164. cmdArgs := []string{"../bin/etcdctl", "--endpoint", endpoint}
  165. if noSync {
  166. cmdArgs = append(cmdArgs, "--no-sync")
  167. }
  168. if clus.cfg.isClientTLS {
  169. cmdArgs = append(cmdArgs, "--ca-file", caPath, "--cert-file", certPath, "--key-file", privateKeyPath)
  170. }
  171. return cmdArgs
  172. }
  173. func etcdctlSet(clus *etcdProcessCluster, key, value string, noSync bool) error {
  174. cmdArgs := append(etcdctlPrefixArgs(clus, noSync), "set", key, value)
  175. return spawnWithExpect(cmdArgs, value)
  176. }
  177. func etcdctlMk(clus *etcdProcessCluster, key, value string, first, noSync bool) error {
  178. cmdArgs := append(etcdctlPrefixArgs(clus, noSync), "mk", key, value)
  179. if first {
  180. return spawnWithExpect(cmdArgs, value)
  181. }
  182. return spawnWithExpect(cmdArgs, "Error: 105: Key already exists")
  183. }
  184. func etcdctlGet(clus *etcdProcessCluster, key, value string, noSync bool) error {
  185. cmdArgs := append(etcdctlPrefixArgs(clus, noSync), "get", key)
  186. return spawnWithExpectedString(cmdArgs, value)
  187. }
  188. func etcdctlRm(clus *etcdProcessCluster, key, value string, first, noSync bool) error {
  189. cmdArgs := append(etcdctlPrefixArgs(clus, noSync), "rm", key)
  190. if first {
  191. return spawnWithExpectedString(cmdArgs, "PrevNode.Value: "+value)
  192. }
  193. return spawnWithExpect(cmdArgs, "Error: 100: Key not found")
  194. }
  195. func etcdctlLs(clus *etcdProcessCluster, key string, noSync bool) error {
  196. cmdArgs := append(etcdctlPrefixArgs(clus, noSync), "ls")
  197. return spawnWithExpect(cmdArgs, key)
  198. }
  199. func etcdctlWatch(clus *etcdProcessCluster, key, value string, noSync bool) <-chan error {
  200. cmdArgs := append(etcdctlPrefixArgs(clus, noSync), "watch", "--after-index 1", key)
  201. errc := make(chan error, 1)
  202. go func() {
  203. errc <- spawnWithExpect(cmdArgs, value)
  204. }()
  205. return errc
  206. }