2015-06-04 10:33:12 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-06-04 11:11:35 +00:00
|
|
|
"io/ioutil"
|
2016-06-14 05:58:39 +00:00
|
|
|
"log"
|
2015-06-04 10:33:12 +00:00
|
|
|
"net"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2015-11-24 06:22:48 +00:00
|
|
|
|
2016-06-14 05:58:39 +00:00
|
|
|
"github.com/hashicorp/nomad/command/agent/consul"
|
2015-11-24 06:22:48 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2015-06-04 10:33:12 +00:00
|
|
|
)
|
|
|
|
|
2016-06-01 10:48:31 +00:00
|
|
|
var (
|
|
|
|
nextPort uint32 = 15000
|
|
|
|
nodeNumber uint32 = 0
|
|
|
|
)
|
2015-06-04 10:33:12 +00:00
|
|
|
|
|
|
|
func getPort() int {
|
|
|
|
return int(atomic.AddUint32(&nextPort, 1))
|
|
|
|
}
|
|
|
|
|
2015-06-04 11:11:35 +00:00
|
|
|
func tmpDir(t *testing.T) string {
|
|
|
|
dir, err := ioutil.TempDir("", "nomad")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
2015-06-04 10:33:12 +00:00
|
|
|
func testServer(t *testing.T, cb func(*Config)) *Server {
|
|
|
|
// Setup the default settings
|
|
|
|
config := DefaultConfig()
|
2015-08-15 19:59:10 +00:00
|
|
|
config.Build = "unittest"
|
2015-06-04 10:33:12 +00:00
|
|
|
config.DevMode = true
|
|
|
|
config.RPCAddr = &net.TCPAddr{
|
|
|
|
IP: []byte{127, 0, 0, 1},
|
|
|
|
Port: getPort(),
|
|
|
|
}
|
2016-06-16 21:30:59 +00:00
|
|
|
nodeNum := atomic.AddUint32(&nodeNumber, 1)
|
|
|
|
config.NodeName = fmt.Sprintf("nomad-%03d", nodeNum)
|
2015-06-04 10:33:12 +00:00
|
|
|
|
|
|
|
// Tighten the Serf timing
|
|
|
|
config.SerfConfig.MemberlistConfig.BindAddr = "127.0.0.1"
|
|
|
|
config.SerfConfig.MemberlistConfig.BindPort = getPort()
|
|
|
|
config.SerfConfig.MemberlistConfig.SuspicionMult = 2
|
2015-06-04 11:02:39 +00:00
|
|
|
config.SerfConfig.MemberlistConfig.RetransmitMult = 2
|
2015-06-04 10:33:12 +00:00
|
|
|
config.SerfConfig.MemberlistConfig.ProbeTimeout = 50 * time.Millisecond
|
|
|
|
config.SerfConfig.MemberlistConfig.ProbeInterval = 100 * time.Millisecond
|
|
|
|
config.SerfConfig.MemberlistConfig.GossipInterval = 100 * time.Millisecond
|
|
|
|
|
|
|
|
// Tighten the Raft timing
|
2015-10-17 00:53:43 +00:00
|
|
|
config.RaftConfig.LeaderLeaseTimeout = 50 * time.Millisecond
|
|
|
|
config.RaftConfig.HeartbeatTimeout = 50 * time.Millisecond
|
|
|
|
config.RaftConfig.ElectionTimeout = 50 * time.Millisecond
|
2015-08-26 00:36:52 +00:00
|
|
|
config.RaftTimeout = 500 * time.Millisecond
|
2015-06-04 10:33:12 +00:00
|
|
|
|
|
|
|
// Invoke the callback if any
|
|
|
|
if cb != nil {
|
|
|
|
cb(config)
|
|
|
|
}
|
|
|
|
|
2015-09-07 17:46:41 +00:00
|
|
|
// Enable raft as leader if we have bootstrap on
|
|
|
|
config.RaftConfig.StartAsLeader = !config.DevDisableBootstrap
|
|
|
|
|
2016-06-14 05:58:39 +00:00
|
|
|
shutdownCh := make(chan struct{})
|
2016-06-14 23:30:30 +00:00
|
|
|
consulSyncer, err := consul.NewSyncer(config.ConsulConfig, shutdownCh, log.New(config.LogOutput, "", log.LstdFlags))
|
2016-06-14 05:58:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-06-04 10:33:12 +00:00
|
|
|
// Create server
|
2016-06-14 05:58:39 +00:00
|
|
|
server, err := NewServer(config, consulSyncer)
|
2015-06-04 10:33:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
return server
|
|
|
|
}
|
2015-06-05 22:11:16 +00:00
|
|
|
|
2015-06-07 19:03:05 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-05 22:11:16 +00:00
|
|
|
func TestServer_RPC(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := s1.RPC("Status.Ping", struct{}{}, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2015-11-24 06:22:48 +00:00
|
|
|
|
|
|
|
func TestServer_Regions(t *testing.T) {
|
|
|
|
// Make the servers
|
|
|
|
s1 := testServer(t, func(c *Config) {
|
|
|
|
c.Region = "region1"
|
|
|
|
})
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
s2 := testServer(t, func(c *Config) {
|
|
|
|
c.Region = "region2"
|
|
|
|
})
|
|
|
|
defer s2.Shutdown()
|
|
|
|
|
|
|
|
// Join them together
|
|
|
|
s2Addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
s2.config.SerfConfig.MemberlistConfig.BindPort)
|
|
|
|
if n, err := s1.Join([]string{s2Addr}); err != nil || n != 1 {
|
|
|
|
t.Fatalf("Failed joining: %v (%d joined)", err, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try listing the regions
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
out := s1.Regions()
|
|
|
|
if len(out) != 2 || out[0] != "region1" || out[1] != "region2" {
|
|
|
|
return false, fmt.Errorf("unexpected regions: %v", out)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
}
|