embed_test.go 4.7 KB

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