embed_test.go 4.8 KB

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