2015-09-06 00:06:05 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2015-09-06 01:41:00 +00:00
|
|
|
"fmt"
|
2015-09-06 00:06:05 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-09-11 19:02:22 +00:00
|
|
|
"strings"
|
2015-09-06 00:06:05 +00:00
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
2015-09-06 01:41:00 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad"
|
2015-09-06 00:06:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var nextPort uint32 = 17000
|
|
|
|
|
|
|
|
func getPort() int {
|
|
|
|
return int(atomic.AddUint32(&nextPort, 1))
|
|
|
|
}
|
|
|
|
|
|
|
|
func tmpDir(t *testing.T) string {
|
|
|
|
dir, err := ioutil.TempDir("", "nomad")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeAgent(t *testing.T, cb func(*Config)) (string, *Agent) {
|
|
|
|
dir := tmpDir(t)
|
|
|
|
conf := DevConfig()
|
|
|
|
|
2015-09-06 01:41:00 +00:00
|
|
|
// Customize the server configuration
|
|
|
|
config := nomad.DefaultConfig()
|
|
|
|
conf.NomadConfig = config
|
|
|
|
|
2015-09-25 19:19:04 +00:00
|
|
|
// Bind and set ports
|
|
|
|
conf.BindAddr = "127.0.0.1"
|
|
|
|
conf.Ports = &Ports{
|
|
|
|
HTTP: getPort(),
|
|
|
|
RPC: getPort(),
|
|
|
|
Serf: getPort(),
|
2015-09-06 01:41:00 +00:00
|
|
|
}
|
2015-09-25 19:19:04 +00:00
|
|
|
conf.NodeName = fmt.Sprintf("Node %d", conf.Ports.RPC)
|
2015-09-06 01:41:00 +00:00
|
|
|
|
|
|
|
// Tighten the Serf timing
|
|
|
|
config.SerfConfig.MemberlistConfig.SuspicionMult = 2
|
|
|
|
config.SerfConfig.MemberlistConfig.RetransmitMult = 2
|
|
|
|
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
|
|
|
|
config.RaftConfig.LeaderLeaseTimeout = 20 * time.Millisecond
|
|
|
|
config.RaftConfig.HeartbeatTimeout = 40 * time.Millisecond
|
|
|
|
config.RaftConfig.ElectionTimeout = 40 * time.Millisecond
|
2015-09-07 17:46:41 +00:00
|
|
|
config.RaftConfig.StartAsLeader = true
|
2015-09-06 01:41:00 +00:00
|
|
|
config.RaftTimeout = 500 * time.Millisecond
|
|
|
|
|
2015-09-06 00:06:05 +00:00
|
|
|
if cb != nil {
|
|
|
|
cb(conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
agent, err := NewAgent(conf, os.Stderr)
|
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
return dir, agent
|
|
|
|
}
|
2015-09-06 00:07:36 +00:00
|
|
|
|
|
|
|
func TestAgent_RPCPing(t *testing.T) {
|
|
|
|
dir, agent := makeAgent(t, nil)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := agent.RPC("Status.Ping", struct{}{}, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2015-09-11 19:02:22 +00:00
|
|
|
|
|
|
|
func TestAgent_ServerConfig(t *testing.T) {
|
|
|
|
conf := DefaultConfig()
|
|
|
|
a := &Agent{config: conf}
|
|
|
|
|
|
|
|
// Returns error on bad serf addr
|
|
|
|
conf.AdvertiseAddrs.Serf = "nope"
|
|
|
|
_, err := a.serverConfig()
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "serf advertise") {
|
|
|
|
t.Fatalf("expected serf address error, got: %#v", err)
|
|
|
|
}
|
|
|
|
conf.AdvertiseAddrs.Serf = "127.0.0.1:4000"
|
|
|
|
|
|
|
|
// Returns error on bad rpc addr
|
|
|
|
conf.AdvertiseAddrs.RPC = "nope"
|
|
|
|
_, err = a.serverConfig()
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "rpc advertise") {
|
|
|
|
t.Fatalf("expected rpc address error, got: %#v", err)
|
|
|
|
}
|
|
|
|
conf.AdvertiseAddrs.RPC = "127.0.0.1:4001"
|
|
|
|
|
|
|
|
// Parses the advertise addrs correctly
|
|
|
|
out, err := a.serverConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
serfAddr := out.SerfConfig.MemberlistConfig.AdvertiseAddr
|
|
|
|
if serfAddr != "127.0.0.1" {
|
|
|
|
t.Fatalf("expect 127.0.0.1, got: %s", serfAddr)
|
|
|
|
}
|
|
|
|
serfPort := out.SerfConfig.MemberlistConfig.AdvertisePort
|
|
|
|
if serfPort != 4000 {
|
|
|
|
t.Fatalf("expected 4000, got: %d", serfPort)
|
|
|
|
}
|
|
|
|
if addr := out.RPCAdvertise; addr.IP.String() != "127.0.0.1" || addr.Port != 4001 {
|
|
|
|
t.Fatalf("bad rpc advertise addr: %#v", addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets up the ports properly
|
|
|
|
conf.Ports.RPC = 4003
|
|
|
|
conf.Ports.Serf = 4004
|
|
|
|
|
|
|
|
out, err = a.serverConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if addr := out.RPCAddr.Port; addr != 4003 {
|
|
|
|
t.Fatalf("expect 4003, got: %d", out.RPCAddr.Port)
|
|
|
|
}
|
|
|
|
if port := out.SerfConfig.MemberlistConfig.BindPort; port != 4004 {
|
|
|
|
t.Fatalf("expect 4004, got: %d", port)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prefers the most specific bind addrs
|
|
|
|
conf.BindAddr = "127.0.0.3"
|
|
|
|
conf.Addresses.RPC = "127.0.0.2"
|
|
|
|
conf.Addresses.Serf = "127.0.0.2"
|
|
|
|
|
|
|
|
out, err = a.serverConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if addr := out.RPCAddr.IP.String(); addr != "127.0.0.2" {
|
|
|
|
t.Fatalf("expect 127.0.0.2, got: %s", addr)
|
|
|
|
}
|
|
|
|
if addr := out.SerfConfig.MemberlistConfig.BindAddr; addr != "127.0.0.2" {
|
|
|
|
t.Fatalf("expect 127.0.0.2, got: %s", addr)
|
|
|
|
}
|
|
|
|
|
2015-10-29 13:47:06 +00:00
|
|
|
conf.Server.NodeGCThreshold = "42g"
|
|
|
|
out, err = a.serverConfig()
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "unknown unit") {
|
|
|
|
t.Fatalf("expected unknown unit error, got: %#v", err)
|
|
|
|
}
|
|
|
|
conf.Server.NodeGCThreshold = "10s"
|
|
|
|
out, err = a.serverConfig()
|
|
|
|
if threshold := out.NodeGCThreshold; threshold != time.Second*10 {
|
|
|
|
t.Fatalf("expect 10s, got: %s", threshold)
|
|
|
|
}
|
|
|
|
|
2016-03-04 23:44:12 +00:00
|
|
|
conf.Server.HeartbeatGrace = "42g"
|
|
|
|
out, err = a.serverConfig()
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "unknown unit") {
|
|
|
|
t.Fatalf("expected unknown unit error, got: %#v", err)
|
|
|
|
}
|
|
|
|
conf.Server.HeartbeatGrace = "37s"
|
|
|
|
out, err = a.serverConfig()
|
|
|
|
if threshold := out.HeartbeatGrace; threshold != time.Second*37 {
|
|
|
|
t.Fatalf("expect 37s, got: %s", threshold)
|
|
|
|
}
|
|
|
|
|
2015-09-11 19:02:22 +00:00
|
|
|
// Defaults to the global bind addr
|
|
|
|
conf.Addresses.RPC = ""
|
|
|
|
conf.Addresses.Serf = ""
|
|
|
|
out, err = a.serverConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if addr := out.RPCAddr.IP.String(); addr != "127.0.0.3" {
|
|
|
|
t.Fatalf("expect 127.0.0.3, got: %s", addr)
|
|
|
|
}
|
|
|
|
if addr := out.SerfConfig.MemberlistConfig.BindAddr; addr != "127.0.0.3" {
|
|
|
|
t.Fatalf("expect 127.0.0.3, got: %s", addr)
|
|
|
|
}
|
2015-09-22 21:25:43 +00:00
|
|
|
|
|
|
|
// Properly handles the bootstrap flags
|
|
|
|
conf.Server.BootstrapExpect = 1
|
|
|
|
out, err = a.serverConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !out.Bootstrap {
|
|
|
|
t.Fatalf("should have set bootstrap mode")
|
|
|
|
}
|
|
|
|
if out.BootstrapExpect != 0 {
|
|
|
|
t.Fatalf("boostrap expect should be 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.Server.BootstrapExpect = 3
|
|
|
|
out, err = a.serverConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if out.Bootstrap {
|
|
|
|
t.Fatalf("bootstrap mode should be disabled")
|
|
|
|
}
|
|
|
|
if out.BootstrapExpect != 3 {
|
|
|
|
t.Fatalf("should have bootstrap-expect = 3")
|
|
|
|
}
|
2015-09-11 19:02:22 +00:00
|
|
|
}
|
2016-02-16 21:42:48 +00:00
|
|
|
|
|
|
|
func TestAgent_ClientConfig(t *testing.T) {
|
|
|
|
conf := DefaultConfig()
|
|
|
|
a := &Agent{config: conf}
|
|
|
|
conf.Client.Enabled = true
|
|
|
|
conf.Addresses.HTTP = "127.0.0.1"
|
|
|
|
conf.Ports.HTTP = 5678
|
|
|
|
|
|
|
|
c, err := a.clientConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("got err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedHttpAddr := "127.0.0.1:5678"
|
|
|
|
if c.Node.HTTPAddr != expectedHttpAddr {
|
|
|
|
t.Fatalf("Expected http addr: %v, got: %v", expectedHttpAddr, c.Node.HTTPAddr)
|
|
|
|
}
|
2016-02-17 01:54:17 +00:00
|
|
|
|
|
|
|
conf = DefaultConfig()
|
|
|
|
a = &Agent{config: conf}
|
|
|
|
conf.Client.Enabled = true
|
|
|
|
conf.Addresses.HTTP = "127.0.0.1"
|
|
|
|
|
|
|
|
c, err = a.clientConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("got err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedHttpAddr = "127.0.0.1:4646"
|
|
|
|
if c.Node.HTTPAddr != expectedHttpAddr {
|
|
|
|
t.Fatalf("Expected http addr: %v, got: %v", expectedHttpAddr, c.Node.HTTPAddr)
|
|
|
|
}
|
2016-02-16 21:42:48 +00:00
|
|
|
}
|