mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
etcd: replace buildServer with runServer in tests
This commit is contained in:
parent
0881021e54
commit
3229c91dbb
@ -58,7 +58,8 @@ func TestKillLeader(t *testing.T) {
|
|||||||
c.DataDir = es[lead].cfg.DataDir
|
c.DataDir = es[lead].cfg.DataDir
|
||||||
c.Addr = hs[lead].Listener.Addr().String()
|
c.Addr = hs[lead].Listener.Addr().String()
|
||||||
id := es[lead].id
|
id := es[lead].id
|
||||||
e, h, err := buildServer(t, c, id)
|
e, h := initTestServer(c, id, false)
|
||||||
|
err := startServer(t, e)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("#%d.%d: %v", i, j, err)
|
t.Fatalf("#%d.%d: %v", i, j, err)
|
||||||
}
|
}
|
||||||
@ -99,7 +100,8 @@ func TestKillRandom(t *testing.T) {
|
|||||||
c.DataDir = es[k].cfg.DataDir
|
c.DataDir = es[k].cfg.DataDir
|
||||||
c.Addr = hs[k].Listener.Addr().String()
|
c.Addr = hs[k].Listener.Addr().String()
|
||||||
id := es[k].id
|
id := es[k].id
|
||||||
e, h, err := buildServer(t, c, id)
|
e, h := initTestServer(c, id, false)
|
||||||
|
err := startServer(t, e)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -148,10 +150,10 @@ func TestClusterConfigReload(t *testing.T) {
|
|||||||
waitCluster(t, es)
|
waitCluster(t, es)
|
||||||
|
|
||||||
lead, _ := waitLeader(es)
|
lead, _ := waitLeader(es)
|
||||||
conf := conf.NewClusterConfig()
|
cc := conf.NewClusterConfig()
|
||||||
conf.ActiveSize = 15
|
cc.ActiveSize = 15
|
||||||
conf.RemoveDelay = 60
|
cc.RemoveDelay = 60
|
||||||
if err := es[lead].p.setClusterConfig(conf); err != nil {
|
if err := es[lead].p.setClusterConfig(cc); err != nil {
|
||||||
t.Fatalf("#%d: setClusterConfig err = %v", i, err)
|
t.Fatalf("#%d: setClusterConfig err = %v", i, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -165,7 +167,8 @@ func TestClusterConfigReload(t *testing.T) {
|
|||||||
c.DataDir = es[k].cfg.DataDir
|
c.DataDir = es[k].cfg.DataDir
|
||||||
c.Addr = hs[k].Listener.Addr().String()
|
c.Addr = hs[k].Listener.Addr().String()
|
||||||
id := es[k].id
|
id := es[k].id
|
||||||
e, h, err := buildServer(t, c, id)
|
e, h := initTestServer(c, id, false)
|
||||||
|
err := startServer(t, e)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -176,8 +179,8 @@ func TestClusterConfigReload(t *testing.T) {
|
|||||||
lead, _ = waitLeader(es)
|
lead, _ = waitLeader(es)
|
||||||
// wait for msgAppResp to commit all entries
|
// wait for msgAppResp to commit all entries
|
||||||
time.Sleep(2 * defaultHeartbeat * es[lead].tickDuration)
|
time.Sleep(2 * defaultHeartbeat * es[lead].tickDuration)
|
||||||
if g := es[lead].p.clusterConfig(); !reflect.DeepEqual(g, conf) {
|
if g := es[lead].p.clusterConfig(); !reflect.DeepEqual(g, cc) {
|
||||||
t.Errorf("#%d: clusterConfig = %+v, want %+v", i, g, conf)
|
t.Errorf("#%d: clusterConfig = %+v, want %+v", i, g, cc)
|
||||||
}
|
}
|
||||||
|
|
||||||
destoryCluster(t, es, hs)
|
destoryCluster(t, es, hs)
|
||||||
@ -204,7 +207,8 @@ func TestMultiNodeKillOne(t *testing.T) {
|
|||||||
c.DataDir = es[idx].cfg.DataDir
|
c.DataDir = es[idx].cfg.DataDir
|
||||||
c.Addr = hs[idx].Listener.Addr().String()
|
c.Addr = hs[idx].Listener.Addr().String()
|
||||||
id := es[idx].id
|
id := es[idx].id
|
||||||
e, h, err := buildServer(t, c, id)
|
e, h := initTestServer(c, id, false)
|
||||||
|
err := startServer(t, e)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("#%d.%d: %v", i, j, err)
|
t.Fatalf("#%d.%d: %v", i, j, err)
|
||||||
}
|
}
|
||||||
@ -245,7 +249,8 @@ func TestMultiNodeKillAllAndRecovery(t *testing.T) {
|
|||||||
c.DataDir = es[k].cfg.DataDir
|
c.DataDir = es[k].cfg.DataDir
|
||||||
c.Addr = hs[k].Listener.Addr().String()
|
c.Addr = hs[k].Listener.Addr().String()
|
||||||
id := es[k].id
|
id := es[k].id
|
||||||
e, h, err := buildServer(t, c, id)
|
e, h := initTestServer(c, id, false)
|
||||||
|
err := startServer(t, e)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("#%d.%d: %v", i, k, err)
|
t.Fatalf("#%d.%d: %v", i, k, err)
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,8 @@ func TestBadDiscoveryService(t *testing.T) {
|
|||||||
|
|
||||||
c := conf.New()
|
c := conf.New()
|
||||||
c.Discovery = ts.URL + "/v2/keys/_etcd/registry/1"
|
c.Discovery = ts.URL + "/v2/keys/_etcd/registry/1"
|
||||||
_, _, err := buildServer(t, c, bootstrapId)
|
e, h := initTestServer(c, bootstrapId, false)
|
||||||
|
err := startServer(t, e)
|
||||||
w := `discovery service error`
|
w := `discovery service error`
|
||||||
if err == nil || !strings.HasPrefix(err.Error(), w) {
|
if err == nil || !strings.HasPrefix(err.Error(), w) {
|
||||||
t.Errorf("err = %v, want %s prefix", err, w)
|
t.Errorf("err = %v, want %s prefix", err, w)
|
||||||
@ -69,6 +70,8 @@ func TestBadDiscoveryService(t *testing.T) {
|
|||||||
t.Fatal("Discovery server never called")
|
t.Fatal("Discovery server never called")
|
||||||
}
|
}
|
||||||
ts.Close()
|
ts.Close()
|
||||||
|
|
||||||
|
destroyServer(t, e, h)
|
||||||
afterTest(t)
|
afterTest(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,13 +85,15 @@ func TestBadDiscoveryServiceWithAdvisedPeers(t *testing.T) {
|
|||||||
c := conf.New()
|
c := conf.New()
|
||||||
c.Discovery = ts.URL + "/v2/keys/_etcd/registry/1"
|
c.Discovery = ts.URL + "/v2/keys/_etcd/registry/1"
|
||||||
c.Peers = []string{hs[0].URL}
|
c.Peers = []string{hs[0].URL}
|
||||||
_, _, err := buildServer(t, c, bootstrapId)
|
e, h := initTestServer(c, bootstrapId, false)
|
||||||
|
err := startServer(t, e)
|
||||||
w := `discovery service error`
|
w := `discovery service error`
|
||||||
if err == nil || !strings.HasPrefix(err.Error(), w) {
|
if err == nil || !strings.HasPrefix(err.Error(), w) {
|
||||||
t.Errorf("err = %v, want %s prefix", err, w)
|
t.Errorf("err = %v, want %s prefix", err, w)
|
||||||
}
|
}
|
||||||
|
|
||||||
destoryCluster(t, es, hs)
|
destoryCluster(t, es, hs)
|
||||||
|
destroyServer(t, e, h)
|
||||||
ts.Close()
|
ts.Close()
|
||||||
afterTest(t)
|
afterTest(t)
|
||||||
}
|
}
|
||||||
@ -96,24 +101,27 @@ func TestBadDiscoveryServiceWithAdvisedPeers(t *testing.T) {
|
|||||||
func TestBootstrapByEmptyPeers(t *testing.T) {
|
func TestBootstrapByEmptyPeers(t *testing.T) {
|
||||||
c := conf.New()
|
c := conf.New()
|
||||||
id := genId()
|
id := genId()
|
||||||
e, h, err := buildServer(t, c, id)
|
e, h := initTestServer(c, id, false)
|
||||||
|
err := startServer(t, e)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
if e.p.node.Leader() != id {
|
if e.p.node.Leader() != id {
|
||||||
t.Error("leader = %x, want %x", e.p.node.Leader(), id)
|
t.Errorf("leader = %x, want %x", e.p.node.Leader(), id)
|
||||||
}
|
}
|
||||||
destroyServer(t, e, h)
|
destroyServer(t, e, h)
|
||||||
afterTest(t)
|
afterTest(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBootstrapByDiscoveryService(t *testing.T) {
|
func TestBootstrapByDiscoveryService(t *testing.T) {
|
||||||
de, dh, _ := buildServer(t, conf.New(), genId())
|
de, dh := initTestServer(conf.New(), genId(), false)
|
||||||
|
err := startServer(t, de)
|
||||||
|
|
||||||
c := conf.New()
|
c := conf.New()
|
||||||
c.Discovery = dh.URL + "/v2/keys/_etcd/registry/1"
|
c.Discovery = dh.URL + "/v2/keys/_etcd/registry/1"
|
||||||
e, h, err := buildServer(t, c, bootstrapId)
|
e, h := initTestServer(c, bootstrapId, false)
|
||||||
|
err = startServer(t, e)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("build server err = %v, want nil", err)
|
t.Fatalf("build server err = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@ -129,7 +137,8 @@ func TestRunByAdvisedPeers(t *testing.T) {
|
|||||||
|
|
||||||
c := conf.New()
|
c := conf.New()
|
||||||
c.Peers = []string{hs[0].URL}
|
c.Peers = []string{hs[0].URL}
|
||||||
e, h, err := buildServer(t, c, bootstrapId)
|
e, h := initTestServer(c, bootstrapId, false)
|
||||||
|
err := startServer(t, e)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("build server err = %v, want nil", err)
|
t.Fatalf("build server err = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@ -144,7 +153,8 @@ func TestRunByAdvisedPeers(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRunByDiscoveryService(t *testing.T) {
|
func TestRunByDiscoveryService(t *testing.T) {
|
||||||
de, dh, _ := buildServer(t, cfg.New(), genId())
|
de, dh := initTestServer(conf.New(), genId(), false)
|
||||||
|
err := startServer(t, de)
|
||||||
|
|
||||||
tc := NewTestClient()
|
tc := NewTestClient()
|
||||||
v := url.Values{}
|
v := url.Values{}
|
||||||
@ -162,9 +172,10 @@ func TestRunByDiscoveryService(t *testing.T) {
|
|||||||
}
|
}
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
|
|
||||||
c := cfg.New()
|
c := conf.New()
|
||||||
c.Discovery = dh.URL + "/v2/keys/_etcd/registry/1"
|
c.Discovery = dh.URL + "/v2/keys/_etcd/registry/1"
|
||||||
e, h, err := buildServer(t, c, bootstrapId)
|
e, h := initTestServer(c, bootstrapId, false)
|
||||||
|
err = startServer(t, e)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("build server err = %v, want nil", err)
|
t.Fatalf("build server err = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@ -182,18 +193,17 @@ func TestRunByDataDir(t *testing.T) {
|
|||||||
TestSingleNodeRecovery(t)
|
TestSingleNodeRecovery(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildServer(t *testing.T, c *cfg.Config, id int64) (e *Server, h *httptest.Server, err error) {
|
func startServer(t *testing.T, e *Server) error {
|
||||||
e, h = initTestServer(c, id, false)
|
var err error
|
||||||
go func() { err = e.Run() }()
|
go func() { err = e.Run() }()
|
||||||
for {
|
for {
|
||||||
if e.mode.Get() == participantMode {
|
if e.mode.Get() == participantMode {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.Close()
|
return err
|
||||||
return nil, nil, err
|
|
||||||
}
|
}
|
||||||
time.Sleep(10 * time.Millisecond)
|
time.Sleep(10 * time.Millisecond)
|
||||||
}
|
}
|
||||||
return e, h, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -90,7 +90,7 @@ func TestAdd(t *testing.T) {
|
|||||||
es := make([]*Server, tt)
|
es := make([]*Server, tt)
|
||||||
hs := make([]*httptest.Server, tt)
|
hs := make([]*httptest.Server, tt)
|
||||||
for i := 0; i < tt; i++ {
|
for i := 0; i < tt; i++ {
|
||||||
c := cfg.New()
|
c := conf.New()
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
c.Peers = []string{hs[0].URL}
|
c.Peers = []string{hs[0].URL}
|
||||||
}
|
}
|
||||||
@ -149,7 +149,7 @@ func TestRemove(t *testing.T) {
|
|||||||
waitCluster(t, es)
|
waitCluster(t, es)
|
||||||
|
|
||||||
lead, _ := waitLeader(es)
|
lead, _ := waitLeader(es)
|
||||||
cfg := cfg.NewClusterConfig()
|
cfg := conf.NewClusterConfig()
|
||||||
cfg.ActiveSize = 0
|
cfg.ActiveSize = 0
|
||||||
if err := es[lead].p.setClusterConfig(cfg); err != nil {
|
if err := es[lead].p.setClusterConfig(cfg); err != nil {
|
||||||
t.Fatalf("#%d: setClusterConfig err = %v", k, err)
|
t.Fatalf("#%d: setClusterConfig err = %v", k, err)
|
||||||
@ -216,7 +216,7 @@ func TestBecomeStandby(t *testing.T) {
|
|||||||
}
|
}
|
||||||
id := int64(i)
|
id := int64(i)
|
||||||
|
|
||||||
cfg := cfg.NewClusterConfig()
|
cfg := conf.NewClusterConfig()
|
||||||
cfg.SyncInterval = 1000
|
cfg.SyncInterval = 1000
|
||||||
|
|
||||||
cfg.ActiveSize = size - 1
|
cfg.ActiveSize = size - 1
|
||||||
@ -320,9 +320,10 @@ func TestSingleNodeRecovery(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
c := cfg.New()
|
c := conf.New()
|
||||||
c.DataDir = dataDir
|
c.DataDir = dataDir
|
||||||
e, h, _ := buildServer(t, c, id)
|
e, h := initTestServer(c, id, false)
|
||||||
|
startServer(t, e)
|
||||||
key := "/foo"
|
key := "/foo"
|
||||||
|
|
||||||
ev, err := e.p.Set(key, false, "bar", time.Now().Add(time.Second*100))
|
ev, err := e.p.Set(key, false, "bar", time.Now().Add(time.Second*100))
|
||||||
@ -348,9 +349,10 @@ func TestSingleNodeRecovery(t *testing.T) {
|
|||||||
|
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
|
|
||||||
c = cfg.New()
|
c = conf.New()
|
||||||
c.DataDir = dataDir
|
c.DataDir = dataDir
|
||||||
e, h, _ = buildServer(t, c, id)
|
e, h = initTestServer(c, id, false)
|
||||||
|
startServer(t, e)
|
||||||
|
|
||||||
waitLeader([]*Server{e})
|
waitLeader([]*Server{e})
|
||||||
w, err = e.p.Watch(key, false, false, ev.Index())
|
w, err = e.p.Watch(key, false, false, ev.Index())
|
||||||
@ -395,7 +397,7 @@ func TestRestoreSnapshotFromLeader(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create one to join the cluster
|
// create one to join the cluster
|
||||||
c := cfg.New()
|
c := conf.New()
|
||||||
c.Peers = []string{hs[0].URL}
|
c.Peers = []string{hs[0].URL}
|
||||||
e, h := initTestServer(c, 1, false)
|
e, h := initTestServer(c, 1, false)
|
||||||
go e.Run()
|
go e.Run()
|
||||||
@ -445,7 +447,7 @@ func buildCluster(number int, tls bool) ([]*Server, []*httptest.Server) {
|
|||||||
var seed string
|
var seed string
|
||||||
|
|
||||||
for i := range es {
|
for i := range es {
|
||||||
c := cfg.New()
|
c := conf.New()
|
||||||
if seed != "" {
|
if seed != "" {
|
||||||
c.Peers = []string{seed}
|
c.Peers = []string{seed}
|
||||||
}
|
}
|
||||||
@ -468,7 +470,7 @@ func buildCluster(number int, tls bool) ([]*Server, []*httptest.Server) {
|
|||||||
return es, hs
|
return es, hs
|
||||||
}
|
}
|
||||||
|
|
||||||
func initTestServer(c *cfg.Config, id int64, tls bool) (e *Server, h *httptest.Server) {
|
func initTestServer(c *conf.Config, id int64, tls bool) (e *Server, h *httptest.Server) {
|
||||||
if c.DataDir == "" {
|
if c.DataDir == "" {
|
||||||
n, err := ioutil.TempDir(os.TempDir(), "etcd")
|
n, err := ioutil.TempDir(os.TempDir(), "etcd")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -136,16 +136,16 @@ func TestGetAdminConfigEndPoint(t *testing.T) {
|
|||||||
t.Errorf("#%d: ContentType = %d, want application/json", i, g)
|
t.Errorf("#%d: ContentType = %d, want application/json", i, g)
|
||||||
}
|
}
|
||||||
|
|
||||||
conf := new(conf.ClusterConfig)
|
cc := new(conf.ClusterConfig)
|
||||||
err = json.NewDecoder(r.Body).Decode(conf)
|
err = json.NewDecoder(r.Body).Decode(cc)
|
||||||
r.Body.Close()
|
r.Body.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("%v", err)
|
t.Errorf("%v", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
w := conf.NewClusterConfig()
|
w := conf.NewClusterConfig()
|
||||||
if !reflect.DeepEqual(conf, w) {
|
if !reflect.DeepEqual(cc, w) {
|
||||||
t.Errorf("#%d: config = %+v, want %+v", i, conf, w)
|
t.Errorf("#%d: config = %+v, want %+v", i, cc, w)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user