embed_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. }
  39. // 4000x so no conflict with e2e tests
  40. url1, _ := url.Parse("http://localhost:40000")
  41. url2, _ := url.Parse("http://localhost:40001")
  42. url3, _ := url.Parse("http://localhost:40002")
  43. // setup defaults
  44. for i := range tests {
  45. tests[i].cfg = *embed.NewConfig()
  46. }
  47. tests[0].cfg.Durl = "abc"
  48. setupEmbedCfg(&tests[1].cfg, []url.URL{*url1}, []url.URL{*url2})
  49. tests[1].cfg.ACUrls = nil
  50. tests[2].cfg.TickMs = tests[2].cfg.ElectionMs - 1
  51. tests[3].cfg.ElectionMs = 999999
  52. setupEmbedCfg(&tests[4].cfg, []url.URL{*url1}, []url.URL{*url2})
  53. setupEmbedCfg(&tests[5].cfg, []url.URL{*url1}, []url.URL{*url2, *url3})
  54. setupEmbedCfg(&tests[6].cfg, []url.URL{*url1, *url2}, []url.URL{*url3})
  55. dir := path.Join(os.TempDir(), fmt.Sprintf("embed-etcd"))
  56. os.RemoveAll(dir)
  57. defer os.RemoveAll(dir)
  58. for i, tt := range tests {
  59. tests[i].cfg.Dir = dir
  60. e, err := embed.StartEtcd(&tests[i].cfg)
  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 setupEmbedCfg(cfg *embed.Config, curls []url.URL, purls []url.URL) {
  84. cfg.ClusterState = "new"
  85. cfg.LCUrls, cfg.ACUrls = curls, curls
  86. cfg.LPUrls, cfg.APUrls = purls, purls
  87. cfg.InitialCluster = ""
  88. for i := range purls {
  89. cfg.InitialCluster += ",default=" + purls[i].String()
  90. }
  91. cfg.InitialCluster = cfg.InitialCluster[1:]
  92. }