embed_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/filepath"
  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. }
  39. urls := newEmbedURLs(10)
  40. // setup defaults
  41. for i := range tests {
  42. tests[i].cfg = *embed.NewConfig()
  43. }
  44. tests[0].cfg.Durl = "abc"
  45. setupEmbedCfg(&tests[1].cfg, []url.URL{urls[0]}, []url.URL{urls[1]})
  46. tests[1].cfg.ACUrls = nil
  47. tests[2].cfg.TickMs = tests[2].cfg.ElectionMs - 1
  48. tests[3].cfg.ElectionMs = 999999
  49. setupEmbedCfg(&tests[4].cfg, []url.URL{urls[2]}, []url.URL{urls[3]})
  50. setupEmbedCfg(&tests[5].cfg, []url.URL{urls[4]}, []url.URL{urls[5], urls[6]})
  51. setupEmbedCfg(&tests[6].cfg, []url.URL{urls[7], urls[8]}, []url.URL{urls[9]})
  52. dir := filepath.Join(os.TempDir(), fmt.Sprintf("embed-etcd"))
  53. os.RemoveAll(dir)
  54. defer os.RemoveAll(dir)
  55. for i, tt := range tests {
  56. tests[i].cfg.Dir = dir
  57. e, err := embed.StartEtcd(&tests[i].cfg)
  58. if e != nil {
  59. <-e.Server.ReadyNotify() // wait for e.Server to join the cluster
  60. }
  61. if tt.werr != "" {
  62. if err == nil || !strings.Contains(err.Error(), tt.werr) {
  63. t.Errorf("%d: expected error with %q, got %v", i, tt.werr, err)
  64. }
  65. if e != nil {
  66. e.Close()
  67. }
  68. continue
  69. }
  70. if err != nil {
  71. t.Errorf("%d: expected success, got error %v", i, err)
  72. continue
  73. }
  74. if len(e.Peers) != tt.wpeers {
  75. t.Errorf("%d: expected %d peers, got %d", i, tt.wpeers, len(e.Peers))
  76. }
  77. if len(e.Clients) != tt.wclients {
  78. t.Errorf("%d: expected %d peers, got %d", i, tt.wclients, len(e.Clients))
  79. }
  80. e.Close()
  81. }
  82. }
  83. func newEmbedURLs(n int) (urls []url.URL) {
  84. for i := 0; i < n; i++ {
  85. u, _ := url.Parse(fmt.Sprintf("unix://localhost:%d%06d", os.Getpid(), i))
  86. urls = append(urls, *u)
  87. }
  88. return
  89. }
  90. func setupEmbedCfg(cfg *embed.Config, curls []url.URL, purls []url.URL) {
  91. cfg.ClusterState = "new"
  92. cfg.LCUrls, cfg.ACUrls = curls, curls
  93. cfg.LPUrls, cfg.APUrls = purls, purls
  94. cfg.InitialCluster = ""
  95. for i := range purls {
  96. cfg.InitialCluster += ",default=" + purls[i].String()
  97. }
  98. cfg.InitialCluster = cfg.InitialCluster[1:]
  99. }