embed_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright 2016 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. // TODO: fix race conditions with setupLogging
  16. package integration
  17. import (
  18. "context"
  19. "fmt"
  20. "net/url"
  21. "os"
  22. "path/filepath"
  23. "strings"
  24. "testing"
  25. "time"
  26. "go.etcd.io/etcd/clientv3"
  27. "go.etcd.io/etcd/embed"
  28. )
  29. func TestEmbedEtcd(t *testing.T) {
  30. tests := []struct {
  31. cfg embed.Config
  32. werr string
  33. wpeers int
  34. wclients int
  35. }{
  36. {werr: "multiple discovery"},
  37. {werr: "advertise-client-urls is required"},
  38. {werr: "should be at least"},
  39. {werr: "is too long"},
  40. {wpeers: 1, wclients: 1},
  41. {wpeers: 2, wclients: 1},
  42. {wpeers: 1, wclients: 2},
  43. {werr: "expected IP"},
  44. {werr: "expected IP"},
  45. }
  46. urls := newEmbedURLs(false, 10)
  47. // setup defaults
  48. for i := range tests {
  49. tests[i].cfg = *embed.NewConfig()
  50. tests[i].cfg.Logger = "zap"
  51. tests[i].cfg.LogOutputs = []string{"/dev/null"}
  52. tests[i].cfg.Debug = false
  53. }
  54. tests[0].cfg.Durl = "abc"
  55. setupEmbedCfg(&tests[1].cfg, []url.URL{urls[0]}, []url.URL{urls[1]})
  56. tests[1].cfg.ACUrls = nil
  57. tests[2].cfg.TickMs = tests[2].cfg.ElectionMs - 1
  58. tests[3].cfg.ElectionMs = 999999
  59. setupEmbedCfg(&tests[4].cfg, []url.URL{urls[2]}, []url.URL{urls[3]})
  60. setupEmbedCfg(&tests[5].cfg, []url.URL{urls[4]}, []url.URL{urls[5], urls[6]})
  61. setupEmbedCfg(&tests[6].cfg, []url.URL{urls[7], urls[8]}, []url.URL{urls[9]})
  62. dnsURL, _ := url.Parse("http://whatever.test:12345")
  63. tests[7].cfg.LCUrls = []url.URL{*dnsURL}
  64. tests[8].cfg.LPUrls = []url.URL{*dnsURL}
  65. dir := filepath.Join(os.TempDir(), fmt.Sprintf("embed-etcd"))
  66. os.RemoveAll(dir)
  67. defer os.RemoveAll(dir)
  68. for i, tt := range tests {
  69. tests[i].cfg.Dir = dir
  70. e, err := embed.StartEtcd(&tests[i].cfg)
  71. if e != nil {
  72. <-e.Server.ReadyNotify() // wait for e.Server to join the cluster
  73. }
  74. if tt.werr != "" {
  75. if err == nil || !strings.Contains(err.Error(), tt.werr) {
  76. t.Errorf("%d: expected error with %q, got %v", i, tt.werr, err)
  77. }
  78. if e != nil {
  79. e.Close()
  80. }
  81. continue
  82. }
  83. if err != nil {
  84. t.Errorf("%d: expected success, got error %v", i, err)
  85. continue
  86. }
  87. if len(e.Peers) != tt.wpeers {
  88. t.Errorf("%d: expected %d peers, got %d", i, tt.wpeers, len(e.Peers))
  89. }
  90. if len(e.Clients) != tt.wclients {
  91. t.Errorf("%d: expected %d clients, got %d", i, tt.wclients, len(e.Clients))
  92. }
  93. e.Close()
  94. select {
  95. case err := <-e.Err():
  96. t.Errorf("#%d: unexpected error on close (%v)", i, err)
  97. default:
  98. }
  99. }
  100. }
  101. func TestEmbedEtcdGracefulStopSecure(t *testing.T) { testEmbedEtcdGracefulStop(t, true) }
  102. func TestEmbedEtcdGracefulStopInsecure(t *testing.T) { testEmbedEtcdGracefulStop(t, false) }
  103. // testEmbedEtcdGracefulStop ensures embedded server stops
  104. // cutting existing transports.
  105. func testEmbedEtcdGracefulStop(t *testing.T, secure bool) {
  106. cfg := embed.NewConfig()
  107. if secure {
  108. cfg.ClientTLSInfo = testTLSInfo
  109. cfg.PeerTLSInfo = testTLSInfo
  110. }
  111. urls := newEmbedURLs(secure, 2)
  112. setupEmbedCfg(cfg, []url.URL{urls[0]}, []url.URL{urls[1]})
  113. cfg.Dir = filepath.Join(os.TempDir(), fmt.Sprintf("embed-etcd"))
  114. os.RemoveAll(cfg.Dir)
  115. defer os.RemoveAll(cfg.Dir)
  116. e, err := embed.StartEtcd(cfg)
  117. if err != nil {
  118. t.Fatal(err)
  119. }
  120. <-e.Server.ReadyNotify() // wait for e.Server to join the cluster
  121. clientCfg := clientv3.Config{
  122. Endpoints: []string{urls[0].String()},
  123. }
  124. if secure {
  125. clientCfg.TLS, err = testTLSInfo.ClientConfig()
  126. if err != nil {
  127. t.Fatal(err)
  128. }
  129. }
  130. cli, err := clientv3.New(clientCfg)
  131. if err != nil {
  132. t.Fatal(err)
  133. }
  134. defer cli.Close()
  135. // open watch connection
  136. cli.Watch(context.Background(), "foo")
  137. donec := make(chan struct{})
  138. go func() {
  139. e.Close()
  140. close(donec)
  141. }()
  142. select {
  143. case err := <-e.Err():
  144. t.Fatal(err)
  145. case <-donec:
  146. case <-time.After(2*time.Second + e.Server.Cfg.ReqTimeout()):
  147. t.Fatalf("took too long to close server")
  148. }
  149. }
  150. func newEmbedURLs(secure bool, n int) (urls []url.URL) {
  151. scheme := "unix"
  152. if secure {
  153. scheme = "unixs"
  154. }
  155. for i := 0; i < n; i++ {
  156. u, _ := url.Parse(fmt.Sprintf("%s://localhost:%d%06d", scheme, os.Getpid(), i))
  157. urls = append(urls, *u)
  158. }
  159. return urls
  160. }
  161. func setupEmbedCfg(cfg *embed.Config, curls []url.URL, purls []url.URL) {
  162. cfg.Logger = "zap"
  163. cfg.LogOutputs = []string{"/dev/null"}
  164. cfg.Debug = false
  165. cfg.ClusterState = "new"
  166. cfg.LCUrls, cfg.ACUrls = curls, curls
  167. cfg.LPUrls, cfg.APUrls = purls, purls
  168. cfg.InitialCluster = ""
  169. for i := range purls {
  170. cfg.InitialCluster += ",default=" + purls[i].String()
  171. }
  172. cfg.InitialCluster = cfg.InitialCluster[1:]
  173. }