open-nomad/nomad/serf_test.go

146 lines
3.1 KiB
Go
Raw Normal View History

2015-06-04 10:33:12 +00:00
package nomad
import (
"fmt"
2015-06-04 11:11:35 +00:00
"os"
"path"
2015-06-04 10:33:12 +00:00
"testing"
2015-06-04 11:02:39 +00:00
"github.com/hashicorp/nomad/testutil"
2015-06-04 10:33:12 +00:00
)
func TestNomad_JoinPeer(t *testing.T) {
s1 := testServer(t, nil)
2015-06-04 11:02:39 +00:00
defer s1.Shutdown()
s2 := testServer(t, func(c *Config) {
c.Region = "region2"
})
defer s2.Shutdown()
2015-06-04 10:33:12 +00:00
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)
}
2015-06-04 11:02:39 +00:00
testutil.WaitForResult(func() (bool, error) {
if members := s1.Members(); len(members) != 2 {
return false, fmt.Errorf("bad: %#v", members)
}
if members := s2.Members(); len(members) != 2 {
return false, fmt.Errorf("bad: %#v", members)
}
return true, nil
}, func(err error) {
t.Fatalf("err: %v", err)
})
testutil.WaitForResult(func() (bool, error) {
if len(s1.peers) != 2 {
return false, fmt.Errorf("bad: %#v", s1.peers)
}
if len(s2.peers) != 2 {
return false, fmt.Errorf("bad: %#v", s2.peers)
}
return true, nil
}, func(err error) {
t.Fatalf("err: %v", err)
})
}
func TestNomad_RemovePeer(t *testing.T) {
s1 := testServer(t, nil)
defer s1.Shutdown()
s2 := testServer(t, func(c *Config) {
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)
2015-06-04 10:33:12 +00:00
}
2015-06-04 11:02:39 +00:00
if num != 1 {
t.Fatalf("bad: %d", num)
2015-06-04 10:33:12 +00:00
}
2015-06-04 11:02:39 +00:00
testutil.WaitForResult(func() (bool, error) {
if members := s1.Members(); len(members) != 2 {
return false, fmt.Errorf("bad: %#v", members)
}
if members := s2.Members(); len(members) != 2 {
return false, fmt.Errorf("bad: %#v", members)
}
return true, nil
}, func(err error) {
t.Fatalf("err: %v", err)
})
// Leave immediately
s2.Leave()
s2.Shutdown()
testutil.WaitForResult(func() (bool, error) {
if len(s1.peers) != 1 {
return false, fmt.Errorf("bad: %#v", s1.peers)
}
if len(s2.peers) != 1 {
return false, fmt.Errorf("bad: %#v", s2.peers)
}
return true, nil
}, func(err error) {
t.Fatalf("err: %v", err)
})
2015-06-04 10:33:12 +00:00
}
2015-06-04 11:11:35 +00:00
func TestNomad_BootstrapExpect(t *testing.T) {
dir := tmpDir(t)
defer os.RemoveAll(dir)
s1 := testServer(t, func(c *Config) {
c.BootstrapExpect = 2
c.DevMode = false
c.DataDir = path.Join(dir, "node1")
})
defer s1.Shutdown()
s2 := testServer(t, func(c *Config) {
c.BootstrapExpect = 2
c.DevMode = false
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)
}
testutil.WaitForResult(func() (bool, error) {
peers, err := s1.numOtherPeers()
if err != nil {
return false, err
}
if peers != 1 {
return false, fmt.Errorf("bad: %#v", peers)
}
peers, err = s2.numOtherPeers()
if err != nil {
return false, err
}
if peers != 1 {
return false, fmt.Errorf("bad: %#v", peers)
}
return true, nil
}, func(err error) {
t.Fatalf("err: %v", err)
})
}