embed_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. "fmt"
  17. "net/url"
  18. "os"
  19. "path"
  20. "strings"
  21. "testing"
  22. "github.com/coreos/etcd/embed"
  23. )
  24. func TestEmbedEtcd(t *testing.T) {
  25. tests := []struct {
  26. cfg embed.Config
  27. werr string
  28. wpeers int
  29. wclients int
  30. }{
  31. {werr: "multiple discovery"},
  32. {werr: "advertise-client-urls is required"},
  33. {werr: "should be at least"},
  34. {werr: "is too long"},
  35. {wpeers: 1, wclients: 1},
  36. {wpeers: 2, wclients: 1},
  37. {wpeers: 1, wclients: 2},
  38. {werr: "expected IP"},
  39. {werr: "expected IP"},
  40. }
  41. urls := newEmbedURLs(10)
  42. // setup defaults
  43. for i := range tests {
  44. tests[i].cfg = *embed.NewConfig()
  45. }
  46. tests[0].cfg.Durl = "abc"
  47. setupEmbedCfg(&tests[1].cfg, []url.URL{urls[0]}, []url.URL{urls[1]})
  48. tests[1].cfg.ACUrls = nil
  49. tests[2].cfg.TickMs = tests[2].cfg.ElectionMs - 1
  50. tests[3].cfg.ElectionMs = 999999
  51. setupEmbedCfg(&tests[4].cfg, []url.URL{urls[2]}, []url.URL{urls[3]})
  52. setupEmbedCfg(&tests[5].cfg, []url.URL{urls[4]}, []url.URL{urls[5], urls[6]})
  53. setupEmbedCfg(&tests[6].cfg, []url.URL{urls[7], urls[8]}, []url.URL{urls[9]})
  54. dnsURL, _ := url.Parse("http://whatever.test:12345")
  55. tests[7].cfg.LCUrls = []url.URL{*dnsURL}
  56. tests[8].cfg.LPUrls = []url.URL{*dnsURL}
  57. dir := path.Join(os.TempDir(), fmt.Sprintf("embed-etcd"))
  58. os.RemoveAll(dir)
  59. defer os.RemoveAll(dir)
  60. for i, tt := range tests {
  61. tests[i].cfg.Dir = dir
  62. e, err := embed.StartEtcd(&tests[i].cfg)
  63. if e != nil {
  64. <-e.Server.ReadyNotify() // wait for e.Server to join the cluster
  65. }
  66. if tt.werr != "" {
  67. if err == nil || !strings.Contains(err.Error(), tt.werr) {
  68. t.Errorf("%d: expected error with %q, got %v", i, tt.werr, err)
  69. }
  70. if e != nil {
  71. e.Close()
  72. }
  73. continue
  74. }
  75. if err != nil {
  76. t.Errorf("%d: expected success, got error %v", i, err)
  77. continue
  78. }
  79. if len(e.Peers) != tt.wpeers {
  80. t.Errorf("%d: expected %d peers, got %d", i, tt.wpeers, len(e.Peers))
  81. }
  82. if len(e.Clients) != tt.wclients {
  83. t.Errorf("%d: expected %d peers, got %d", i, tt.wclients, len(e.Clients))
  84. }
  85. e.Close()
  86. }
  87. }
  88. func newEmbedURLs(n int) (urls []url.URL) {
  89. for i := 0; i < n; i++ {
  90. u, _ := url.Parse(fmt.Sprintf("unix://localhost:%d%06d", os.Getpid(), i))
  91. urls = append(urls, *u)
  92. }
  93. return
  94. }
  95. func setupEmbedCfg(cfg *embed.Config, curls []url.URL, purls []url.URL) {
  96. cfg.ClusterState = "new"
  97. cfg.LCUrls, cfg.ACUrls = curls, curls
  98. cfg.LPUrls, cfg.APUrls = purls, purls
  99. cfg.InitialCluster = ""
  100. for i := range purls {
  101. cfg.InitialCluster += ",default=" + purls[i].String()
  102. }
  103. cfg.InitialCluster = cfg.InitialCluster[1:]
  104. }