From d71865e094dc82bf0938e37c17aa7ad57480ed06 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Thu, 7 Aug 2014 17:22:43 -0700 Subject: [PATCH] etcd: cleanup tests --- etcd/etcd_functional_test.go | 12 ++--- etcd/etcd_start_test.go | 18 +++---- etcd/etcd_test.go | 99 +++++++++++++++++------------------- 3 files changed, 60 insertions(+), 69 deletions(-) diff --git a/etcd/etcd_functional_test.go b/etcd/etcd_functional_test.go index c69a5b050..074ae211c 100644 --- a/etcd/etcd_functional_test.go +++ b/etcd/etcd_functional_test.go @@ -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 diff --git a/etcd/etcd_start_test.go b/etcd/etcd_start_test.go index 97b754073..efabd1e3b 100644 --- a/etcd/etcd_start_test.go +++ b/etcd/etcd_start_test.go @@ -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) diff --git a/etcd/etcd_test.go b/etcd/etcd_test.go index ac676ab1c..7c573aac0 100644 --- a/etcd/etcd_test.go +++ b/etcd/etcd_test.go @@ -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 +}