etcd_test.go 9.8 KB

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