etcd_test.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. "fmt"
  17. "io/ioutil"
  18. "net/url"
  19. "os"
  20. "strings"
  21. "github.com/coreos/etcd/pkg/expect"
  22. "github.com/coreos/etcd/pkg/fileutil"
  23. )
  24. const (
  25. etcdProcessBasePort = 20000
  26. certPath = "../integration/fixtures/server.crt"
  27. privateKeyPath = "../integration/fixtures/server.key.insecure"
  28. caPath = "../integration/fixtures/ca.crt"
  29. )
  30. type clientConnType int
  31. const (
  32. clientNonTLS clientConnType = iota
  33. clientTLS
  34. clientTLSAndNonTLS
  35. )
  36. var (
  37. configNoTLS = etcdProcessClusterConfig{
  38. clusterSize: 3,
  39. proxySize: 0,
  40. isPeerTLS: false,
  41. initialToken: "new",
  42. }
  43. configAutoTLS = etcdProcessClusterConfig{
  44. clusterSize: 3,
  45. isPeerTLS: true,
  46. isPeerAutoTLS: true,
  47. initialToken: "new",
  48. }
  49. configTLS = etcdProcessClusterConfig{
  50. clusterSize: 3,
  51. proxySize: 0,
  52. clientTLS: clientTLS,
  53. isPeerTLS: true,
  54. initialToken: "new",
  55. }
  56. configClientTLS = etcdProcessClusterConfig{
  57. clusterSize: 3,
  58. proxySize: 0,
  59. clientTLS: clientTLS,
  60. isPeerTLS: false,
  61. initialToken: "new",
  62. }
  63. configClientBoth = etcdProcessClusterConfig{
  64. clusterSize: 1,
  65. proxySize: 0,
  66. clientTLS: clientTLSAndNonTLS,
  67. isPeerTLS: false,
  68. initialToken: "new",
  69. }
  70. configPeerTLS = etcdProcessClusterConfig{
  71. clusterSize: 3,
  72. proxySize: 0,
  73. isPeerTLS: true,
  74. initialToken: "new",
  75. }
  76. configWithProxy = etcdProcessClusterConfig{
  77. clusterSize: 3,
  78. proxySize: 1,
  79. isPeerTLS: false,
  80. initialToken: "new",
  81. }
  82. configWithProxyTLS = etcdProcessClusterConfig{
  83. clusterSize: 3,
  84. proxySize: 1,
  85. clientTLS: clientTLS,
  86. isPeerTLS: true,
  87. initialToken: "new",
  88. }
  89. configWithProxyPeerTLS = etcdProcessClusterConfig{
  90. clusterSize: 3,
  91. proxySize: 1,
  92. isPeerTLS: true,
  93. initialToken: "new",
  94. }
  95. )
  96. func configStandalone(cfg etcdProcessClusterConfig) *etcdProcessClusterConfig {
  97. ret := cfg
  98. ret.clusterSize = 1
  99. return &ret
  100. }
  101. type etcdProcessCluster struct {
  102. cfg *etcdProcessClusterConfig
  103. procs []*etcdProcess
  104. }
  105. type etcdProcess struct {
  106. cfg *etcdProcessConfig
  107. proc *expect.ExpectProcess
  108. donec chan struct{} // closed when Interact() terminates
  109. }
  110. type etcdProcessConfig struct {
  111. args []string
  112. dataDirPath string
  113. acurl string
  114. // additional url for tls connection when the etcd process
  115. // serves both http and https
  116. acurltls string
  117. isProxy bool
  118. }
  119. type etcdProcessClusterConfig struct {
  120. clusterSize int
  121. proxySize int
  122. clientTLS clientConnType
  123. isPeerTLS bool
  124. isPeerAutoTLS bool
  125. initialToken string
  126. quotaBackendBytes int64
  127. }
  128. // newEtcdProcessCluster launches a new cluster from etcd processes, returning
  129. // a new etcdProcessCluster once all nodes are ready to accept client requests.
  130. func newEtcdProcessCluster(cfg *etcdProcessClusterConfig) (*etcdProcessCluster, error) {
  131. etcdCfgs := cfg.etcdProcessConfigs()
  132. epc := &etcdProcessCluster{
  133. cfg: cfg,
  134. procs: make([]*etcdProcess, cfg.clusterSize+cfg.proxySize),
  135. }
  136. // launch etcd processes
  137. for i := range etcdCfgs {
  138. proc, err := newEtcdProcess(etcdCfgs[i])
  139. if err != nil {
  140. epc.Close()
  141. return nil, err
  142. }
  143. epc.procs[i] = proc
  144. }
  145. // wait for cluster to start
  146. readyC := make(chan error, cfg.clusterSize+cfg.proxySize)
  147. readyStr := "enabled capabilities for version"
  148. for i := range etcdCfgs {
  149. go func(etcdp *etcdProcess) {
  150. rs := readyStr
  151. if etcdp.cfg.isProxy {
  152. // rs = "proxy: listening for client requests on"
  153. rs = "proxy: endpoints found"
  154. }
  155. _, err := etcdp.proc.Expect(rs)
  156. readyC <- err
  157. close(etcdp.donec)
  158. }(epc.procs[i])
  159. }
  160. for range etcdCfgs {
  161. if err := <-readyC; err != nil {
  162. epc.Close()
  163. return nil, err
  164. }
  165. }
  166. return epc, nil
  167. }
  168. func newEtcdProcess(cfg *etcdProcessConfig) (*etcdProcess, error) {
  169. if !fileutil.Exist("../bin/etcd") {
  170. return nil, fmt.Errorf("could not find etcd binary")
  171. }
  172. if err := os.RemoveAll(cfg.dataDirPath); err != nil {
  173. return nil, err
  174. }
  175. child, err := spawnCmd(append([]string{"../bin/etcd"}, cfg.args...))
  176. if err != nil {
  177. return nil, err
  178. }
  179. return &etcdProcess{cfg: cfg, proc: child, donec: make(chan struct{})}, nil
  180. }
  181. func (cfg *etcdProcessClusterConfig) etcdProcessConfigs() []*etcdProcessConfig {
  182. clientScheme := "http"
  183. if cfg.clientTLS == clientTLS {
  184. clientScheme = "https"
  185. }
  186. peerScheme := "http"
  187. if cfg.isPeerTLS {
  188. peerScheme = "https"
  189. }
  190. etcdCfgs := make([]*etcdProcessConfig, cfg.clusterSize+cfg.proxySize)
  191. initialCluster := make([]string, cfg.clusterSize)
  192. for i := 0; i < cfg.clusterSize; i++ {
  193. var curls []string
  194. var curl, curltls string
  195. port := etcdProcessBasePort + 2*i
  196. switch cfg.clientTLS {
  197. case clientNonTLS, clientTLS:
  198. curl = (&url.URL{Scheme: clientScheme, Host: fmt.Sprintf("localhost:%d", port)}).String()
  199. curls = []string{curl}
  200. case clientTLSAndNonTLS:
  201. curl = (&url.URL{Scheme: "http", Host: fmt.Sprintf("localhost:%d", port)}).String()
  202. curltls = (&url.URL{Scheme: "https", Host: fmt.Sprintf("localhost:%d", port)}).String()
  203. curls = []string{curl, curltls}
  204. }
  205. purl := url.URL{Scheme: peerScheme, Host: fmt.Sprintf("localhost:%d", port+1)}
  206. name := fmt.Sprintf("testname%d", i)
  207. dataDirPath, derr := ioutil.TempDir("", name+".etcd")
  208. if derr != nil {
  209. panic("could not get tempdir for datadir")
  210. }
  211. initialCluster[i] = fmt.Sprintf("%s=%s", name, purl.String())
  212. args := []string{
  213. "--name", name,
  214. "--listen-client-urls", strings.Join(curls, ","),
  215. "--advertise-client-urls", strings.Join(curls, ","),
  216. "--listen-peer-urls", purl.String(),
  217. "--initial-advertise-peer-urls", purl.String(),
  218. "--initial-cluster-token", cfg.initialToken,
  219. "--data-dir", dataDirPath,
  220. }
  221. if cfg.quotaBackendBytes > 0 {
  222. args = append(args,
  223. "--quota-backend-bytes", fmt.Sprintf("%d", cfg.quotaBackendBytes),
  224. )
  225. }
  226. args = append(args, cfg.tlsArgs()...)
  227. etcdCfgs[i] = &etcdProcessConfig{
  228. args: args,
  229. dataDirPath: dataDirPath,
  230. acurl: curl,
  231. acurltls: curltls,
  232. }
  233. }
  234. for i := 0; i < cfg.proxySize; i++ {
  235. port := etcdProcessBasePort + 2*cfg.clusterSize + i + 1
  236. curl := url.URL{Scheme: clientScheme, Host: fmt.Sprintf("localhost:%d", port)}
  237. name := fmt.Sprintf("testname-proxy%d", i)
  238. dataDirPath, derr := ioutil.TempDir("", name+".etcd")
  239. if derr != nil {
  240. panic("could not get tempdir for datadir")
  241. }
  242. args := []string{
  243. "--name", name,
  244. "--proxy", "on",
  245. "--listen-client-urls", curl.String(),
  246. "--data-dir", dataDirPath,
  247. }
  248. args = append(args, cfg.tlsArgs()...)
  249. etcdCfgs[cfg.clusterSize+i] = &etcdProcessConfig{
  250. args: args,
  251. dataDirPath: dataDirPath,
  252. acurl: curl.String(),
  253. isProxy: true,
  254. }
  255. }
  256. initialClusterArgs := []string{"--initial-cluster", strings.Join(initialCluster, ",")}
  257. for i := range etcdCfgs {
  258. etcdCfgs[i].args = append(etcdCfgs[i].args, initialClusterArgs...)
  259. }
  260. return etcdCfgs
  261. }
  262. func (cfg *etcdProcessClusterConfig) tlsArgs() (args []string) {
  263. if cfg.clientTLS != clientNonTLS {
  264. tlsClientArgs := []string{
  265. "--cert-file", certPath,
  266. "--key-file", privateKeyPath,
  267. "--ca-file", caPath,
  268. }
  269. args = append(args, tlsClientArgs...)
  270. }
  271. if cfg.isPeerTLS {
  272. if cfg.isPeerAutoTLS {
  273. args = append(args, "--peer-auto-tls=true")
  274. } else {
  275. tlsPeerArgs := []string{
  276. "--peer-cert-file", certPath,
  277. "--peer-key-file", privateKeyPath,
  278. "--peer-ca-file", caPath,
  279. }
  280. args = append(args, tlsPeerArgs...)
  281. }
  282. }
  283. return args
  284. }
  285. func (epc *etcdProcessCluster) Close() (err error) {
  286. for _, p := range epc.procs {
  287. if p == nil {
  288. continue
  289. }
  290. os.RemoveAll(p.cfg.dataDirPath)
  291. if curErr := p.proc.Stop(); curErr != nil {
  292. if err != nil {
  293. err = fmt.Errorf("%v; %v", err, curErr)
  294. } else {
  295. err = curErr
  296. }
  297. }
  298. <-p.donec
  299. }
  300. return err
  301. }
  302. func spawnCmd(args []string) (*expect.ExpectProcess, error) {
  303. return expect.NewExpect(args[0], args[1:]...)
  304. }
  305. func spawnWithExpect(args []string, expected string) error {
  306. return spawnWithExpects(args, []string{expected}...)
  307. }
  308. func spawnWithExpects(args []string, xs ...string) error {
  309. proc, err := spawnCmd(args)
  310. if err != nil {
  311. return err
  312. }
  313. // process until either stdout or stderr contains
  314. // the expected string
  315. var (
  316. lines []string
  317. lineFunc = func(txt string) bool { return true }
  318. )
  319. for _, txt := range xs {
  320. for {
  321. l, err := proc.ExpectFunc(lineFunc)
  322. if err != nil {
  323. return fmt.Errorf("%v (expected %q, got %q)", err, txt, lines)
  324. }
  325. lines = append(lines, l)
  326. if strings.Contains(l, txt) {
  327. break
  328. }
  329. }
  330. }
  331. perr := proc.Close()
  332. if err != nil {
  333. return err
  334. }
  335. if len(xs) == 0 && proc.LineCount() != 0 { // expect no output
  336. return fmt.Errorf("unexpected output (got lines %q, line count %d)", lines, proc.LineCount())
  337. }
  338. return perr
  339. }
  340. // proxies returns only the proxy etcdProcess.
  341. func (epc *etcdProcessCluster) proxies() []*etcdProcess {
  342. return epc.procs[epc.cfg.clusterSize:]
  343. }
  344. func (epc *etcdProcessCluster) backends() []*etcdProcess {
  345. return epc.procs[:epc.cfg.clusterSize]
  346. }
  347. func (epc *etcdProcessCluster) endpoints() []string {
  348. eps := make([]string, epc.cfg.clusterSize)
  349. for i, ep := range epc.backends() {
  350. eps[i] = ep.cfg.acurl
  351. }
  352. return eps
  353. }