embed_test.go 4.9 KB

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