etcd_test.go 9.4 KB

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