etcd_process.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. package e2e
  15. import (
  16. "fmt"
  17. "net/url"
  18. "os"
  19. "go.etcd.io/etcd/pkg/expect"
  20. "go.etcd.io/etcd/pkg/fileutil"
  21. )
  22. var (
  23. etcdServerReadyLines = []string{"enabled capabilities for version", "published"}
  24. binPath string
  25. ctlBinPath string
  26. )
  27. // etcdProcess is a process that serves etcd requests.
  28. type etcdProcess interface {
  29. EndpointsV2() []string
  30. EndpointsV3() []string
  31. EndpointsMetrics() []string
  32. Start() error
  33. Restart() error
  34. Stop() error
  35. Close() error
  36. WithStopSignal(sig os.Signal) os.Signal
  37. Config() *etcdServerProcessConfig
  38. }
  39. type etcdServerProcess struct {
  40. cfg *etcdServerProcessConfig
  41. proc *expect.ExpectProcess
  42. donec chan struct{} // closed when Interact() terminates
  43. }
  44. type etcdServerProcessConfig struct {
  45. execPath string
  46. args []string
  47. tlsArgs []string
  48. dataDirPath string
  49. keepDataDir bool
  50. name string
  51. purl url.URL
  52. acurl string
  53. murl string
  54. initialToken string
  55. initialCluster string
  56. }
  57. func newEtcdServerProcess(cfg *etcdServerProcessConfig) (*etcdServerProcess, error) {
  58. if !fileutil.Exist(cfg.execPath) {
  59. return nil, fmt.Errorf("could not find etcd binary")
  60. }
  61. if !cfg.keepDataDir {
  62. if err := os.RemoveAll(cfg.dataDirPath); err != nil {
  63. return nil, err
  64. }
  65. }
  66. return &etcdServerProcess{cfg: cfg, donec: make(chan struct{})}, nil
  67. }
  68. func (ep *etcdServerProcess) EndpointsV2() []string { return []string{ep.cfg.acurl} }
  69. func (ep *etcdServerProcess) EndpointsV3() []string { return ep.EndpointsV2() }
  70. func (ep *etcdServerProcess) EndpointsMetrics() []string { return []string{ep.cfg.murl} }
  71. func (ep *etcdServerProcess) Start() error {
  72. if ep.proc != nil {
  73. panic("already started")
  74. }
  75. proc, err := spawnCmd(append([]string{ep.cfg.execPath}, ep.cfg.args...))
  76. if err != nil {
  77. return err
  78. }
  79. ep.proc = proc
  80. return ep.waitReady()
  81. }
  82. func (ep *etcdServerProcess) Restart() error {
  83. if err := ep.Stop(); err != nil {
  84. return err
  85. }
  86. ep.donec = make(chan struct{})
  87. return ep.Start()
  88. }
  89. func (ep *etcdServerProcess) Stop() (err error) {
  90. if ep == nil || ep.proc == nil {
  91. return nil
  92. }
  93. err = ep.proc.Stop()
  94. if err != nil {
  95. return err
  96. }
  97. ep.proc = nil
  98. <-ep.donec
  99. ep.donec = make(chan struct{})
  100. if ep.cfg.purl.Scheme == "unix" || ep.cfg.purl.Scheme == "unixs" {
  101. err = os.Remove(ep.cfg.purl.Host + ep.cfg.purl.Path)
  102. if err != nil {
  103. return err
  104. }
  105. }
  106. return nil
  107. }
  108. func (ep *etcdServerProcess) Close() error {
  109. if err := ep.Stop(); err != nil {
  110. return err
  111. }
  112. return os.RemoveAll(ep.cfg.dataDirPath)
  113. }
  114. func (ep *etcdServerProcess) WithStopSignal(sig os.Signal) os.Signal {
  115. ret := ep.proc.StopSignal
  116. ep.proc.StopSignal = sig
  117. return ret
  118. }
  119. func (ep *etcdServerProcess) waitReady() error {
  120. defer close(ep.donec)
  121. return waitReadyExpectProc(ep.proc, etcdServerReadyLines)
  122. }
  123. func (ep *etcdServerProcess) Config() *etcdServerProcessConfig { return ep.cfg }