etcd: cleanup tests

This commit is contained in:
Xiang Li
2014-08-07 17:22:43 -07:00
committed by Yicheng Qin
parent 8e2557697f
commit d71865e094
3 changed files with 60 additions and 69 deletions

View File

@@ -54,7 +54,7 @@ func TestKillLeader(t *testing.T) {
avgTime := totalTime / (time.Duration)(i+1)
fmt.Println("Total time:", totalTime, "; Avg time:", avgTime)
c := conf.New()
c := newTestConfig()
c.DataDir = es[lead].cfg.DataDir
c.Addr = hs[lead].Listener.Addr().String()
id := es[lead].id
@@ -96,7 +96,7 @@ func TestKillRandom(t *testing.T) {
waitLeader(es)
for k := range toKill {
c := conf.New()
c := newTestConfig()
c.DataDir = es[k].cfg.DataDir
c.Addr = hs[k].Listener.Addr().String()
id := es[k].id
@@ -122,7 +122,7 @@ func TestJoinThroughFollower(t *testing.T) {
es := make([]*Server, tt)
hs := make([]*httptest.Server, tt)
for i := 0; i < tt; i++ {
c := conf.New()
c := newTestConfig()
if i > 0 {
c.Peers = []string{hs[i-1].URL}
}
@@ -163,7 +163,7 @@ func TestClusterConfigReload(t *testing.T) {
}
for k := range es {
c := conf.New()
c := newTestConfig()
c.DataDir = es[k].cfg.DataDir
c.Addr = hs[k].Listener.Addr().String()
id := es[k].id
@@ -203,7 +203,7 @@ func TestMultiNodeKillOne(t *testing.T) {
es[idx].Stop()
hs[idx].Close()
c := conf.New()
c := newTestConfig()
c.DataDir = es[idx].cfg.DataDir
c.Addr = hs[idx].Listener.Addr().String()
id := es[idx].id
@@ -245,7 +245,7 @@ func TestMultiNodeKillAllAndRecovery(t *testing.T) {
}
for k := range es {
c := conf.New()
c := newTestConfig()
c.DataDir = es[k].cfg.DataDir
c.Addr = hs[k].Listener.Addr().String()
id := es[k].id

View File

@@ -25,8 +25,6 @@ import (
"sync"
"testing"
"time"
"github.com/coreos/etcd/conf"
)
const (
@@ -55,7 +53,7 @@ func TestBadDiscoveryService(t *testing.T) {
g := garbageHandler{t: t}
ts := httptest.NewServer(&g)
c := conf.New()
c := newTestConfig()
c.Discovery = ts.URL + "/v2/keys/_etcd/registry/1"
e, h := newUnstartedTestServer(c, bootstrapId, false)
err := startServer(t, e)
@@ -82,7 +80,7 @@ func TestBadDiscoveryServiceWithAdvisedPeers(t *testing.T) {
es, hs := buildCluster(1, false)
waitCluster(t, es)
c := conf.New()
c := newTestConfig()
c.Discovery = ts.URL + "/v2/keys/_etcd/registry/1"
c.Peers = []string{hs[0].URL}
e, h := newUnstartedTestServer(c, bootstrapId, false)
@@ -99,7 +97,7 @@ func TestBadDiscoveryServiceWithAdvisedPeers(t *testing.T) {
}
func TestBootstrapByEmptyPeers(t *testing.T) {
c := conf.New()
c := newTestConfig()
id := genId()
e, h := newUnstartedTestServer(c, id, false)
err := startServer(t, e)
@@ -115,10 +113,10 @@ func TestBootstrapByEmptyPeers(t *testing.T) {
}
func TestBootstrapByDiscoveryService(t *testing.T) {
de, dh := newUnstartedTestServer(conf.New(), genId(), false)
de, dh := newUnstartedTestServer(newTestConfig(), genId(), false)
err := startServer(t, de)
c := conf.New()
c := newTestConfig()
c.Discovery = dh.URL + "/v2/keys/_etcd/registry/1"
e, h := newUnstartedTestServer(c, bootstrapId, false)
err = startServer(t, e)
@@ -135,7 +133,7 @@ func TestRunByAdvisedPeers(t *testing.T) {
es, hs := buildCluster(1, false)
waitCluster(t, es)
c := conf.New()
c := newTestConfig()
c.Peers = []string{hs[0].URL}
e, h := newUnstartedTestServer(c, bootstrapId, false)
err := startServer(t, e)
@@ -153,7 +151,7 @@ func TestRunByAdvisedPeers(t *testing.T) {
}
func TestRunByDiscoveryService(t *testing.T) {
de, dh := newUnstartedTestServer(conf.New(), genId(), false)
de, dh := newUnstartedTestServer(newTestConfig(), genId(), false)
err := startServer(t, de)
tc := NewTestClient()
@@ -172,7 +170,7 @@ func TestRunByDiscoveryService(t *testing.T) {
}
resp.Body.Close()
c := conf.New()
c := newTestConfig()
c.Discovery = dh.URL + "/v2/keys/_etcd/registry/1"
e, h := newUnstartedTestServer(c, bootstrapId, false)
err = startServer(t, e)

View File

@@ -90,7 +90,7 @@ func TestAdd(t *testing.T) {
es := make([]*Server, tt)
hs := make([]*httptest.Server, tt)
for i := 0; i < tt; i++ {
c := conf.New()
c := newTestConfig()
if i > 0 {
c.Peers = []string{hs[0].URL}
}
@@ -316,12 +316,7 @@ func TestVersionCheck(t *testing.T) {
func TestSingleNodeRecovery(t *testing.T) {
id := genId()
dataDir, err := ioutil.TempDir(os.TempDir(), "etcd")
if err != nil {
panic(err)
}
c := conf.New()
c.DataDir = dataDir
c := newTestConfig()
e, h := newUnstartedTestServer(c, id, false)
startServer(t, e)
key := "/foo"
@@ -349,9 +344,9 @@ func TestSingleNodeRecovery(t *testing.T) {
time.Sleep(2 * time.Second)
c = conf.New()
c.DataDir = dataDir
e, h = newUnstartedTestServer(c, id, false)
nc := newTestConfig()
nc.DataDir = c.DataDir
e, h = newUnstartedTestServer(nc, id, false)
startServer(t, e)
waitLeader([]*Server{e})
@@ -397,7 +392,7 @@ func TestRestoreSnapshotFromLeader(t *testing.T) {
}
// create one to join the cluster
c := conf.New()
c := newTestConfig()
c.Peers = []string{hs[0].URL}
e, h := newUnstartedTestServer(c, 1, false)
go e.Run()
@@ -447,7 +442,7 @@ func buildCluster(number int, tls bool) ([]*Server, []*httptest.Server) {
var seed string
for i := range es {
c := conf.New()
c := newTestConfig()
if seed != "" {
c.Peers = []string{seed}
}
@@ -470,21 +465,11 @@ func buildCluster(number int, tls bool) ([]*Server, []*httptest.Server) {
return es, hs
}
func newUnstartedTestServer(c *conf.Config, id int64, tls bool) (e *Server, h *httptest.Server) {
if c.DataDir == "" {
n, err := ioutil.TempDir(os.TempDir(), "etcd")
if err != nil {
panic(err)
}
c.DataDir = n
}
addr := c.Addr
srv, err := New(c)
func newUnstartedTestServer(c *conf.Config, id int64, tls bool) (*Server, *httptest.Server) {
e, err := New(c)
if err != nil {
panic(err)
}
e = srv
e.setId(id)
e.SetTick(time.Millisecond * 5)
@@ -494,39 +479,36 @@ func newUnstartedTestServer(c *conf.Config, id int64, tls bool) (e *Server, h *h
m.Handle("/raft/", e.RaftHandler())
m.Handle("/v2/admin/", e.RaftHandler())
if addr == "127.0.0.1:4001" {
if tls {
h = httptest.NewTLSServer(m)
} else {
h = httptest.NewServer(m)
}
} else {
var l net.Listener
var err error
for {
l, err = net.Listen("tcp", addr)
if err == nil {
break
}
if !strings.Contains(err.Error(), "address already in use") {
panic(err)
}
time.Sleep(500 * time.Millisecond)
}
h = &httptest.Server{
Listener: l,
Config: &http.Server{Handler: m},
}
if tls {
h.StartTLS()
} else {
h.Start()
}
u, err := url.Parse(c.Addr)
if err != nil {
panic(err)
}
var l net.Listener
for {
l, err = net.Listen("tcp", u.Host)
if err == nil {
break
}
if !strings.Contains(err.Error(), "address already in use") {
panic(err)
}
time.Sleep(500 * time.Millisecond)
}
h := &httptest.Server{
Listener: l,
Config: &http.Server{Handler: m},
}
if tls {
h.StartTLS()
} else {
h.Start()
}
e.raftPubAddr = h.URL
e.pubAddr = h.URL
return
return e, h
}
func destoryCluster(t *testing.T, es []*Server, hs []*httptest.Server) {
@@ -603,3 +585,14 @@ func checkParticipant(i int, es []*Server) error {
}
return nil
}
func newTestConfig() *conf.Config {
c := conf.New()
c.Addr = "127.0.0.1:0"
n, err := ioutil.TempDir(os.TempDir(), "etcd")
if err != nil {
panic(err)
}
c.DataDir = n
return c
}