etcd_process.go 3.3 KB

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