integration: test embedded etcd

release-3.1
Anthony Romano 2016-07-11 17:02:49 -07:00
parent f4f33ea767
commit c0299ca6f4
1 changed files with 103 additions and 0 deletions

103
integration/embed_test.go Normal file
View File

@ -0,0 +1,103 @@
// Copyright 2016 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package integration
import (
"fmt"
"net/url"
"os"
"path"
"strings"
"testing"
"github.com/coreos/etcd/embed"
)
func TestEmbedEtcd(t *testing.T) {
tests := []struct {
cfg embed.Config
werr string
wpeers int
wclients int
}{
{werr: "multiple discovery"},
{werr: "advertise-client-urls is required"},
{werr: "should be at least"},
{werr: "is too long"},
{wpeers: 1, wclients: 1},
{wpeers: 2, wclients: 1},
{wpeers: 1, wclients: 2},
}
// 4000x so no conflict with e2e tests
url1, _ := url.Parse("http://localhost:40000")
url2, _ := url.Parse("http://localhost:40001")
url3, _ := url.Parse("http://localhost:40002")
// setup defaults
for i := range tests {
tests[i].cfg = *embed.NewConfig()
}
tests[0].cfg.Durl = "abc"
setupEmbedCfg(&tests[1].cfg, []url.URL{*url1}, []url.URL{*url2})
tests[1].cfg.ACUrls = nil
tests[2].cfg.TickMs = tests[2].cfg.ElectionMs - 1
tests[3].cfg.ElectionMs = 999999
setupEmbedCfg(&tests[4].cfg, []url.URL{*url1}, []url.URL{*url2})
setupEmbedCfg(&tests[5].cfg, []url.URL{*url1}, []url.URL{*url2, *url3})
setupEmbedCfg(&tests[6].cfg, []url.URL{*url1, *url2}, []url.URL{*url3})
dir := path.Join(os.TempDir(), fmt.Sprintf("embed-etcd"))
os.RemoveAll(dir)
defer os.RemoveAll(dir)
for i, tt := range tests {
tests[i].cfg.Dir = dir
e, err := embed.StartEtcd(&tests[i].cfg)
if tt.werr != "" {
if err == nil || !strings.Contains(err.Error(), tt.werr) {
t.Errorf("%d: expected error with %q, got %v", i, tt.werr, err)
}
if e != nil {
e.Close()
}
continue
}
if err != nil {
t.Errorf("%d: expected success, got error %v", i, err)
continue
}
if len(e.Peers) != tt.wpeers {
t.Errorf("%d: expected %d peers, got %d", i, tt.wpeers, len(e.Peers))
}
if len(e.Clients) != tt.wclients {
t.Errorf("%d: expected %d peers, got %d", i, tt.wclients, len(e.Clients))
}
e.Close()
}
}
func setupEmbedCfg(cfg *embed.Config, curls []url.URL, purls []url.URL) {
cfg.ClusterState = "new"
cfg.LCUrls, cfg.ACUrls = curls, curls
cfg.LPUrls, cfg.APUrls = purls, purls
cfg.InitialCluster = ""
for i := range purls {
cfg.InitialCluster += ",default=" + purls[i].String()
}
cfg.InitialCluster = cfg.InitialCluster[1:]
}