nomad: refactor test code

This commit is contained in:
Armon Dadgar 2015-06-07 12:03:05 -07:00
parent 2a1767594e
commit 405c03d873
3 changed files with 19 additions and 61 deletions

View File

@ -22,16 +22,7 @@ func TestLeader_LeftServer(t *testing.T) {
})
defer s3.Shutdown()
servers := []*Server{s1, s2, s3}
// Try to join
addr := fmt.Sprintf("127.0.0.1:%d",
s1.config.SerfConfig.MemberlistConfig.BindPort)
if _, err := s2.Join([]string{addr}); err != nil {
t.Fatalf("err: %v", err)
}
if _, err := s3.Join([]string{addr}); err != nil {
t.Fatalf("err: %v", err)
}
testJoin(t, s1, s2, s3)
for _, s := range servers {
testutil.WaitForResult(func() (bool, error) {
@ -78,16 +69,7 @@ func TestLeader_LeftLeader(t *testing.T) {
})
defer s3.Shutdown()
servers := []*Server{s1, s2, s3}
// Try to join
addr := fmt.Sprintf("127.0.0.1:%d",
s1.config.SerfConfig.MemberlistConfig.BindPort)
if _, err := s2.Join([]string{addr}); err != nil {
t.Fatalf("err: %v", err)
}
if _, err := s3.Join([]string{addr}); err != nil {
t.Fatalf("err: %v", err)
}
testJoin(t, s1, s2, s3)
for _, s := range servers {
testutil.WaitForResult(func() (bool, error) {
@ -132,13 +114,7 @@ func TestLeader_MultiBootstrap(t *testing.T) {
s2 := testServer(t, nil)
defer s2.Shutdown()
servers := []*Server{s1, s2}
// Try to join
addr := fmt.Sprintf("127.0.0.1:%d",
s1.config.SerfConfig.MemberlistConfig.BindPort)
if _, err := s2.Join([]string{addr}); err != nil {
t.Fatalf("err: %v", err)
}
testJoin(t, s1, s2)
for _, s := range servers {
testutil.WaitForResult(func() (bool, error) {

View File

@ -16,15 +16,7 @@ func TestNomad_JoinPeer(t *testing.T) {
c.Region = "region2"
})
defer s2.Shutdown()
s2Addr := fmt.Sprintf("127.0.0.1:%d", s2.config.SerfConfig.MemberlistConfig.BindPort)
num, err := s1.Join([]string{s2Addr})
if err != nil {
t.Fatalf("err: %v", err)
}
if num != 1 {
t.Fatalf("bad: %d", num)
}
testJoin(t, s1, s2)
testutil.WaitForResult(func() (bool, error) {
if members := s1.Members(); len(members) != 2 {
@ -64,15 +56,7 @@ func TestNomad_RemovePeer(t *testing.T) {
c.Region = "region2"
})
defer s2.Shutdown()
s2Addr := fmt.Sprintf("127.0.0.1:%d", s2.config.SerfConfig.MemberlistConfig.BindPort)
num, err := s1.Join([]string{s2Addr})
if err != nil {
t.Fatalf("err: %v", err)
}
if num != 1 {
t.Fatalf("bad: %d", num)
}
testJoin(t, s1, s2)
testutil.WaitForResult(func() (bool, error) {
if members := s1.Members(); len(members) != 2 {
@ -119,15 +103,7 @@ func TestNomad_BootstrapExpect(t *testing.T) {
c.DataDir = path.Join(dir, "node2")
})
defer s2.Shutdown()
s2Addr := fmt.Sprintf("127.0.0.1:%d", s2.config.SerfConfig.MemberlistConfig.BindPort)
num, err := s1.Join([]string{s2Addr})
if err != nil {
t.Fatalf("err: %v", err)
}
if num != 1 {
t.Fatalf("bad: %d", num)
}
testJoin(t, s1, s2)
testutil.WaitForResult(func() (bool, error) {
peers, err := s1.numOtherPeers()
@ -168,13 +144,7 @@ func TestNomad_BadExpect(t *testing.T) {
})
defer s2.Shutdown()
servers := []*Server{s1, s2}
// Try to join
addr := fmt.Sprintf("127.0.0.1:%d",
s1.config.SerfConfig.MemberlistConfig.BindPort)
if _, err := s2.Join([]string{addr}); err != nil {
t.Fatalf("err: %v", err)
}
testJoin(t, s1, s2)
// Serf members should update
testutil.WaitForResult(func() (bool, error) {

View File

@ -60,6 +60,18 @@ func testServer(t *testing.T, cb func(*Config)) *Server {
return server
}
func testJoin(t *testing.T, s1 *Server, other ...*Server) {
addr := fmt.Sprintf("127.0.0.1:%d",
s1.config.SerfConfig.MemberlistConfig.BindPort)
for _, s2 := range other {
if num, err := s2.Join([]string{addr}); err != nil {
t.Fatalf("err: %v", err)
} else if num != 1 {
t.Fatalf("bad: %d", num)
}
}
}
func TestServer_RPC(t *testing.T) {
s1 := testServer(t, nil)
defer s1.Shutdown()