etcd_test.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. basePort int
  122. proxySize int
  123. clientTLS clientConnType
  124. isPeerTLS bool
  125. isPeerAutoTLS bool
  126. initialToken string
  127. quotaBackendBytes int64
  128. }
  129. // newEtcdProcessCluster launches a new cluster from etcd processes, returning
  130. // a new etcdProcessCluster once all nodes are ready to accept client requests.
  131. func newEtcdProcessCluster(cfg *etcdProcessClusterConfig) (*etcdProcessCluster, error) {
  132. etcdCfgs := cfg.etcdProcessConfigs()
  133. epc := &etcdProcessCluster{
  134. cfg: cfg,
  135. procs: make([]*etcdProcess, cfg.clusterSize+cfg.proxySize),
  136. }
  137. // launch etcd processes
  138. for i := range etcdCfgs {
  139. proc, err := newEtcdProcess(etcdCfgs[i])
  140. if err != nil {
  141. epc.Close()
  142. return nil, err
  143. }
  144. epc.procs[i] = proc
  145. }
  146. // wait for cluster to start
  147. readyC := make(chan error, cfg.clusterSize+cfg.proxySize)
  148. readyStr := "enabled capabilities for version"
  149. for i := range etcdCfgs {
  150. go func(etcdp *etcdProcess) {
  151. rs := readyStr
  152. if etcdp.cfg.isProxy {
  153. // rs = "proxy: listening for client requests on"
  154. rs = "proxy: endpoints found"
  155. }
  156. _, err := etcdp.proc.Expect(rs)
  157. readyC <- err
  158. close(etcdp.donec)
  159. }(epc.procs[i])
  160. }
  161. for range etcdCfgs {
  162. if err := <-readyC; err != nil {
  163. epc.Close()
  164. return nil, err
  165. }
  166. }
  167. return epc, nil
  168. }
  169. func newEtcdProcess(cfg *etcdProcessConfig) (*etcdProcess, error) {
  170. if !fileutil.Exist("../bin/etcd") {
  171. return nil, fmt.Errorf("could not find etcd binary")
  172. }
  173. if err := os.RemoveAll(cfg.dataDirPath); err != nil {
  174. return nil, err
  175. }
  176. child, err := spawnCmd(append([]string{"../bin/etcd"}, cfg.args...))
  177. if err != nil {
  178. return nil, err
  179. }
  180. return &etcdProcess{cfg: cfg, proc: child, donec: make(chan struct{})}, nil
  181. }
  182. func (cfg *etcdProcessClusterConfig) etcdProcessConfigs() []*etcdProcessConfig {
  183. if cfg.basePort == 0 {
  184. cfg.basePort = etcdProcessBasePort
  185. }
  186. clientScheme := "http"
  187. if cfg.clientTLS == clientTLS {
  188. clientScheme = "https"
  189. }
  190. peerScheme := "http"
  191. if cfg.isPeerTLS {
  192. peerScheme = "https"
  193. }
  194. etcdCfgs := make([]*etcdProcessConfig, cfg.clusterSize+cfg.proxySize)
  195. initialCluster := make([]string, cfg.clusterSize)
  196. for i := 0; i < cfg.clusterSize; i++ {
  197. var curls []string
  198. var curl, curltls string
  199. port := cfg.basePort + 2*i
  200. switch cfg.clientTLS {
  201. case clientNonTLS, clientTLS:
  202. curl = (&url.URL{Scheme: clientScheme, Host: fmt.Sprintf("localhost:%d", port)}).String()
  203. curls = []string{curl}
  204. case clientTLSAndNonTLS:
  205. curl = (&url.URL{Scheme: "http", Host: fmt.Sprintf("localhost:%d", port)}).String()
  206. curltls = (&url.URL{Scheme: "https", Host: fmt.Sprintf("localhost:%d", port)}).String()
  207. curls = []string{curl, curltls}
  208. }
  209. purl := url.URL{Scheme: peerScheme, Host: fmt.Sprintf("localhost:%d", port+1)}
  210. name := fmt.Sprintf("testname%d", i)
  211. dataDirPath, derr := ioutil.TempDir("", name+".etcd")
  212. if derr != nil {
  213. panic("could not get tempdir for datadir")
  214. }
  215. initialCluster[i] = fmt.Sprintf("%s=%s", name, purl.String())
  216. args := []string{
  217. "--name", name,
  218. "--listen-client-urls", strings.Join(curls, ","),
  219. "--advertise-client-urls", strings.Join(curls, ","),
  220. "--listen-peer-urls", purl.String(),
  221. "--initial-advertise-peer-urls", purl.String(),
  222. "--initial-cluster-token", cfg.initialToken,
  223. "--data-dir", dataDirPath,
  224. }
  225. if cfg.quotaBackendBytes > 0 {
  226. args = append(args,
  227. "--quota-backend-bytes", fmt.Sprintf("%d", cfg.quotaBackendBytes),
  228. )
  229. }
  230. args = append(args, cfg.tlsArgs()...)
  231. etcdCfgs[i] = &etcdProcessConfig{
  232. args: args,
  233. dataDirPath: dataDirPath,
  234. acurl: curl,
  235. acurltls: curltls,
  236. }
  237. }
  238. for i := 0; i < cfg.proxySize; i++ {
  239. port := cfg.basePort + 2*cfg.clusterSize + i + 1
  240. curl := url.URL{Scheme: clientScheme, Host: fmt.Sprintf("localhost:%d", port)}
  241. name := fmt.Sprintf("testname-proxy%d", i)
  242. dataDirPath, derr := ioutil.TempDir("", name+".etcd")
  243. if derr != nil {
  244. panic("could not get tempdir for datadir")
  245. }
  246. args := []string{
  247. "--name", name,
  248. "--proxy", "on",
  249. "--listen-client-urls", curl.String(),
  250. "--data-dir", dataDirPath,
  251. }
  252. args = append(args, cfg.tlsArgs()...)
  253. etcdCfgs[cfg.clusterSize+i] = &etcdProcessConfig{
  254. args: args,
  255. dataDirPath: dataDirPath,
  256. acurl: curl.String(),
  257. isProxy: true,
  258. }
  259. }
  260. initialClusterArgs := []string{"--initial-cluster", strings.Join(initialCluster, ",")}
  261. for i := range etcdCfgs {
  262. etcdCfgs[i].args = append(etcdCfgs[i].args, initialClusterArgs...)
  263. }
  264. return etcdCfgs
  265. }
  266. func (cfg *etcdProcessClusterConfig) tlsArgs() (args []string) {
  267. if cfg.clientTLS != clientNonTLS {
  268. tlsClientArgs := []string{
  269. "--cert-file", certPath,
  270. "--key-file", privateKeyPath,
  271. "--ca-file", caPath,
  272. }
  273. args = append(args, tlsClientArgs...)
  274. }
  275. if cfg.isPeerTLS {
  276. if cfg.isPeerAutoTLS {
  277. args = append(args, "--peer-auto-tls=true")
  278. } else {
  279. tlsPeerArgs := []string{
  280. "--peer-cert-file", certPath,
  281. "--peer-key-file", privateKeyPath,
  282. "--peer-ca-file", caPath,
  283. }
  284. args = append(args, tlsPeerArgs...)
  285. }
  286. }
  287. return args
  288. }
  289. func (epc *etcdProcessCluster) Close() (err error) {
  290. for _, p := range epc.procs {
  291. if p == nil {
  292. continue
  293. }
  294. os.RemoveAll(p.cfg.dataDirPath)
  295. if curErr := p.proc.Stop(); curErr != nil {
  296. if err != nil {
  297. err = fmt.Errorf("%v; %v", err, curErr)
  298. } else {
  299. err = curErr
  300. }
  301. }
  302. <-p.donec
  303. }
  304. return err
  305. }
  306. func spawnCmd(args []string) (*expect.ExpectProcess, error) {
  307. return expect.NewExpect(args[0], args[1:]...)
  308. }
  309. func spawnWithExpect(args []string, expected string) error {
  310. return spawnWithExpects(args, []string{expected}...)
  311. }
  312. func spawnWithExpects(args []string, xs ...string) error {
  313. proc, err := spawnCmd(args)
  314. if err != nil {
  315. return err
  316. }
  317. // process until either stdout or stderr contains
  318. // the expected string
  319. var (
  320. lines []string
  321. lineFunc = func(txt string) bool { return true }
  322. )
  323. for _, txt := range xs {
  324. for {
  325. l, err := proc.ExpectFunc(lineFunc)
  326. if err != nil {
  327. return fmt.Errorf("%v (expected %q, got %q)", err, txt, lines)
  328. }
  329. lines = append(lines, l)
  330. if strings.Contains(l, txt) {
  331. break
  332. }
  333. }
  334. }
  335. perr := proc.Close()
  336. if err != nil {
  337. return err
  338. }
  339. if len(xs) == 0 && proc.LineCount() != 0 { // expect no output
  340. return fmt.Errorf("unexpected output (got lines %q, line count %d)", lines, proc.LineCount())
  341. }
  342. return perr
  343. }
  344. // proxies returns only the proxy etcdProcess.
  345. func (epc *etcdProcessCluster) proxies() []*etcdProcess {
  346. return epc.procs[epc.cfg.clusterSize:]
  347. }
  348. func (epc *etcdProcessCluster) backends() []*etcdProcess {
  349. return epc.procs[:epc.cfg.clusterSize]
  350. }
  351. func (epc *etcdProcessCluster) endpoints() []string {
  352. eps := make([]string, epc.cfg.clusterSize)
  353. for i, ep := range epc.backends() {
  354. eps[i] = ep.cfg.acurl
  355. }
  356. return eps
  357. }