cluster_proxy_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // Copyright 2017 The etcd Authors
  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. // +build cluster_proxy
  15. package e2e
  16. import (
  17. "fmt"
  18. "net"
  19. "net/url"
  20. "os"
  21. "strconv"
  22. "strings"
  23. "go.etcd.io/etcd/pkg/expect"
  24. )
  25. type proxyEtcdProcess struct {
  26. etcdProc etcdProcess
  27. proxyV2 *proxyV2Proc
  28. proxyV3 *proxyV3Proc
  29. }
  30. func newEtcdProcess(cfg *etcdServerProcessConfig) (etcdProcess, error) {
  31. return newProxyEtcdProcess(cfg)
  32. }
  33. func newProxyEtcdProcess(cfg *etcdServerProcessConfig) (*proxyEtcdProcess, error) {
  34. ep, err := newEtcdServerProcess(cfg)
  35. if err != nil {
  36. return nil, err
  37. }
  38. pep := &proxyEtcdProcess{
  39. etcdProc: ep,
  40. proxyV2: newProxyV2Proc(cfg),
  41. proxyV3: newProxyV3Proc(cfg),
  42. }
  43. return pep, nil
  44. }
  45. func (p *proxyEtcdProcess) Config() *etcdServerProcessConfig { return p.etcdProc.Config() }
  46. func (p *proxyEtcdProcess) EndpointsV2() []string { return p.proxyV2.endpoints() }
  47. func (p *proxyEtcdProcess) EndpointsV3() []string { return p.proxyV3.endpoints() }
  48. func (p *proxyEtcdProcess) EndpointsMetrics() []string {
  49. panic("not implemented; proxy doesn't provide health information")
  50. }
  51. func (p *proxyEtcdProcess) Start() error {
  52. if err := p.etcdProc.Start(); err != nil {
  53. return err
  54. }
  55. if err := p.proxyV2.Start(); err != nil {
  56. return err
  57. }
  58. return p.proxyV3.Start()
  59. }
  60. func (p *proxyEtcdProcess) Restart() error {
  61. if err := p.etcdProc.Restart(); err != nil {
  62. return err
  63. }
  64. if err := p.proxyV2.Restart(); err != nil {
  65. return err
  66. }
  67. return p.proxyV3.Restart()
  68. }
  69. func (p *proxyEtcdProcess) Stop() error {
  70. err := p.proxyV2.Stop()
  71. if v3err := p.proxyV3.Stop(); err == nil {
  72. err = v3err
  73. }
  74. if eerr := p.etcdProc.Stop(); eerr != nil && err == nil {
  75. // fails on go-grpc issue #1384
  76. if !strings.Contains(eerr.Error(), "exit status 2") {
  77. err = eerr
  78. }
  79. }
  80. return err
  81. }
  82. func (p *proxyEtcdProcess) Close() error {
  83. err := p.proxyV2.Close()
  84. if v3err := p.proxyV3.Close(); err == nil {
  85. err = v3err
  86. }
  87. if eerr := p.etcdProc.Close(); eerr != nil && err == nil {
  88. // fails on go-grpc issue #1384
  89. if !strings.Contains(eerr.Error(), "exit status 2") {
  90. err = eerr
  91. }
  92. }
  93. return err
  94. }
  95. func (p *proxyEtcdProcess) WithStopSignal(sig os.Signal) os.Signal {
  96. p.proxyV3.WithStopSignal(sig)
  97. p.proxyV3.WithStopSignal(sig)
  98. return p.etcdProc.WithStopSignal(sig)
  99. }
  100. type proxyProc struct {
  101. execPath string
  102. args []string
  103. ep string
  104. murl string
  105. donec chan struct{}
  106. proc *expect.ExpectProcess
  107. }
  108. func (pp *proxyProc) endpoints() []string { return []string{pp.ep} }
  109. func (pp *proxyProc) start() error {
  110. if pp.proc != nil {
  111. panic("already started")
  112. }
  113. proc, err := spawnCmd(append([]string{pp.execPath}, pp.args...))
  114. if err != nil {
  115. return err
  116. }
  117. pp.proc = proc
  118. return nil
  119. }
  120. func (pp *proxyProc) waitReady(readyStr string) error {
  121. defer close(pp.donec)
  122. return waitReadyExpectProc(pp.proc, []string{readyStr})
  123. }
  124. func (pp *proxyProc) Stop() error {
  125. if pp.proc == nil {
  126. return nil
  127. }
  128. if err := pp.proc.Stop(); err != nil && !strings.Contains(err.Error(), "exit status 1") {
  129. // v2proxy exits with status 1 on auto tls; not sure why
  130. return err
  131. }
  132. pp.proc = nil
  133. <-pp.donec
  134. pp.donec = make(chan struct{})
  135. return nil
  136. }
  137. func (pp *proxyProc) WithStopSignal(sig os.Signal) os.Signal {
  138. ret := pp.proc.StopSignal
  139. pp.proc.StopSignal = sig
  140. return ret
  141. }
  142. func (pp *proxyProc) Close() error { return pp.Stop() }
  143. type proxyV2Proc struct {
  144. proxyProc
  145. dataDir string
  146. }
  147. func proxyListenURL(cfg *etcdServerProcessConfig, portOffset int) string {
  148. u, err := url.Parse(cfg.acurl)
  149. if err != nil {
  150. panic(err)
  151. }
  152. host, port, _ := net.SplitHostPort(u.Host)
  153. p, _ := strconv.ParseInt(port, 10, 16)
  154. u.Host = fmt.Sprintf("%s:%d", host, int(p)+portOffset)
  155. return u.String()
  156. }
  157. func newProxyV2Proc(cfg *etcdServerProcessConfig) *proxyV2Proc {
  158. listenAddr := proxyListenURL(cfg, 2)
  159. name := fmt.Sprintf("testname-proxy-%p", cfg)
  160. args := []string{
  161. "--name", name,
  162. "--proxy", "on",
  163. "--listen-client-urls", listenAddr,
  164. "--initial-cluster", cfg.name + "=" + cfg.purl.String(),
  165. }
  166. return &proxyV2Proc{
  167. proxyProc{
  168. execPath: cfg.execPath,
  169. args: append(args, cfg.tlsArgs...),
  170. ep: listenAddr,
  171. donec: make(chan struct{}),
  172. },
  173. name + ".etcd",
  174. }
  175. }
  176. func (v2p *proxyV2Proc) Start() error {
  177. os.RemoveAll(v2p.dataDir)
  178. if err := v2p.start(); err != nil {
  179. return err
  180. }
  181. return v2p.waitReady("httpproxy: endpoints found")
  182. }
  183. func (v2p *proxyV2Proc) Restart() error {
  184. if err := v2p.Stop(); err != nil {
  185. return err
  186. }
  187. return v2p.Start()
  188. }
  189. func (v2p *proxyV2Proc) Stop() error {
  190. if err := v2p.proxyProc.Stop(); err != nil {
  191. return err
  192. }
  193. // v2 proxy caches members; avoid reuse of directory
  194. return os.RemoveAll(v2p.dataDir)
  195. }
  196. type proxyV3Proc struct {
  197. proxyProc
  198. }
  199. func newProxyV3Proc(cfg *etcdServerProcessConfig) *proxyV3Proc {
  200. listenAddr := proxyListenURL(cfg, 3)
  201. args := []string{
  202. "grpc-proxy",
  203. "start",
  204. "--listen-addr", strings.Split(listenAddr, "/")[2],
  205. "--endpoints", cfg.acurl,
  206. // pass-through member RPCs
  207. "--advertise-client-url", "",
  208. }
  209. murl := ""
  210. if cfg.murl != "" {
  211. murl = proxyListenURL(cfg, 4)
  212. args = append(args, "--metrics-addr", murl)
  213. }
  214. tlsArgs := []string{}
  215. for i := 0; i < len(cfg.tlsArgs); i++ {
  216. switch cfg.tlsArgs[i] {
  217. case "--cert-file":
  218. tlsArgs = append(tlsArgs, "--cert", cfg.tlsArgs[i+1], "--cert-file", cfg.tlsArgs[i+1])
  219. i++
  220. case "--key-file":
  221. tlsArgs = append(tlsArgs, "--key", cfg.tlsArgs[i+1], "--key-file", cfg.tlsArgs[i+1])
  222. i++
  223. case "--trusted-ca-file":
  224. tlsArgs = append(tlsArgs, "--cacert", cfg.tlsArgs[i+1], "--trusted-ca-file", cfg.tlsArgs[i+1])
  225. i++
  226. case "--auto-tls":
  227. tlsArgs = append(tlsArgs, "--auto-tls", "--insecure-skip-tls-verify")
  228. case "--peer-trusted-ca-file", "--peer-cert-file", "--peer-key-file":
  229. i++ // skip arg
  230. case "--client-cert-auth", "--peer-auto-tls":
  231. default:
  232. tlsArgs = append(tlsArgs, cfg.tlsArgs[i])
  233. }
  234. }
  235. return &proxyV3Proc{
  236. proxyProc{
  237. execPath: cfg.execPath,
  238. args: append(args, tlsArgs...),
  239. ep: listenAddr,
  240. murl: murl,
  241. donec: make(chan struct{}),
  242. },
  243. }
  244. }
  245. func (v3p *proxyV3Proc) Restart() error {
  246. if err := v3p.Stop(); err != nil {
  247. return err
  248. }
  249. return v3p.Start()
  250. }
  251. func (v3p *proxyV3Proc) Start() error {
  252. if err := v3p.start(); err != nil {
  253. return err
  254. }
  255. return v3p.waitReady("started gRPC proxy")
  256. }