2013-12-31 21:06:33 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/consul/command/agent"
|
|
|
|
"github.com/hashicorp/consul/consul"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var offset uint64
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Seed the random number generator
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
|
|
|
type agentWrapper struct {
|
2014-08-21 23:08:21 +00:00
|
|
|
dir string
|
|
|
|
config *agent.Config
|
|
|
|
agent *agent.Agent
|
|
|
|
rpc *agent.AgentRPC
|
|
|
|
http *agent.HTTPServer
|
|
|
|
addr string
|
|
|
|
httpAddr string
|
2013-12-31 21:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *agentWrapper) Shutdown() {
|
|
|
|
a.rpc.Shutdown()
|
|
|
|
a.agent.Shutdown()
|
2014-08-21 23:08:21 +00:00
|
|
|
a.http.Shutdown()
|
2013-12-31 21:06:33 +00:00
|
|
|
os.RemoveAll(a.dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAgent(t *testing.T) *agentWrapper {
|
2014-10-07 18:05:31 +00:00
|
|
|
return testAgentWithConfig(t, func(c *agent.Config) {})
|
2014-09-10 15:49:16 +00:00
|
|
|
}
|
|
|
|
|
2014-10-07 18:05:31 +00:00
|
|
|
func testAgentWithConfig(t *testing.T, cb func(c *agent.Config)) *agentWrapper {
|
2013-12-31 21:06:33 +00:00
|
|
|
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lw := agent.NewLogWriter(512)
|
|
|
|
mult := io.MultiWriter(os.Stderr, lw)
|
|
|
|
|
|
|
|
conf := nextConfig()
|
2014-10-07 18:05:31 +00:00
|
|
|
cb(conf)
|
2013-12-31 21:06:33 +00:00
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "agent")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(fmt.Sprintf("err: %v", err))
|
|
|
|
}
|
|
|
|
conf.DataDir = dir
|
|
|
|
|
|
|
|
a, err := agent.Create(conf, lw)
|
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
t.Fatalf(fmt.Sprintf("err: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
rpc := agent.NewAgentRPC(a, l, mult, lw)
|
2014-08-21 23:08:21 +00:00
|
|
|
|
2014-11-18 16:03:36 +00:00
|
|
|
conf.Addresses.HTTP = "127.0.0.1"
|
2014-08-21 23:08:21 +00:00
|
|
|
httpAddr := fmt.Sprintf("127.0.0.1:%d", conf.Ports.HTTP)
|
2014-11-18 16:03:36 +00:00
|
|
|
http, err := agent.NewHTTPServers(a, conf, os.Stderr)
|
2014-08-21 23:08:21 +00:00
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
t.Fatalf(fmt.Sprintf("err: %v", err))
|
|
|
|
}
|
|
|
|
|
2014-11-18 16:03:36 +00:00
|
|
|
if http == nil || len(http) == 0 {
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
t.Fatalf(fmt.Sprintf("Could not create HTTP server to listen on: %s", httpAddr))
|
|
|
|
}
|
|
|
|
|
2013-12-31 21:06:33 +00:00
|
|
|
return &agentWrapper{
|
2014-08-21 23:08:21 +00:00
|
|
|
dir: dir,
|
|
|
|
config: conf,
|
|
|
|
agent: a,
|
|
|
|
rpc: rpc,
|
2014-11-18 16:03:36 +00:00
|
|
|
http: http[0],
|
2014-08-21 23:08:21 +00:00
|
|
|
addr: l.Addr().String(),
|
|
|
|
httpAddr: httpAddr,
|
2013-12-31 21:06:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func nextConfig() *agent.Config {
|
2014-04-11 23:34:29 +00:00
|
|
|
idx := int(atomic.AddUint64(&offset, 1))
|
2013-12-31 21:06:33 +00:00
|
|
|
conf := agent.DefaultConfig()
|
|
|
|
|
|
|
|
conf.Bootstrap = true
|
|
|
|
conf.Datacenter = "dc1"
|
|
|
|
conf.NodeName = fmt.Sprintf("Node %d", idx)
|
2014-04-11 23:34:29 +00:00
|
|
|
conf.BindAddr = "127.0.0.1"
|
2013-12-31 21:06:33 +00:00
|
|
|
conf.Server = true
|
2014-04-11 23:34:29 +00:00
|
|
|
|
|
|
|
conf.Ports.HTTP = 10000 + 10*idx
|
2014-11-18 22:56:48 +00:00
|
|
|
conf.Ports.HTTPS = 10401 + 10*idx
|
2014-04-11 23:34:29 +00:00
|
|
|
conf.Ports.RPC = 10100 + 10*idx
|
|
|
|
conf.Ports.SerfLan = 10201 + 10*idx
|
|
|
|
conf.Ports.SerfWan = 10202 + 10*idx
|
|
|
|
conf.Ports.Server = 10300 + 10*idx
|
2013-12-31 21:06:33 +00:00
|
|
|
|
|
|
|
cons := consul.DefaultConfig()
|
|
|
|
conf.ConsulConfig = cons
|
|
|
|
|
|
|
|
cons.SerfLANConfig.MemberlistConfig.ProbeTimeout = 100 * time.Millisecond
|
|
|
|
cons.SerfLANConfig.MemberlistConfig.ProbeInterval = 100 * time.Millisecond
|
|
|
|
cons.SerfLANConfig.MemberlistConfig.GossipInterval = 100 * time.Millisecond
|
|
|
|
|
|
|
|
cons.SerfWANConfig.MemberlistConfig.ProbeTimeout = 100 * time.Millisecond
|
|
|
|
cons.SerfWANConfig.MemberlistConfig.ProbeInterval = 100 * time.Millisecond
|
|
|
|
cons.SerfWANConfig.MemberlistConfig.GossipInterval = 100 * time.Millisecond
|
|
|
|
|
2014-05-30 18:57:39 +00:00
|
|
|
cons.RaftConfig.LeaderLeaseTimeout = 20 * time.Millisecond
|
2013-12-31 21:06:33 +00:00
|
|
|
cons.RaftConfig.HeartbeatTimeout = 40 * time.Millisecond
|
|
|
|
cons.RaftConfig.ElectionTimeout = 40 * time.Millisecond
|
|
|
|
|
|
|
|
return conf
|
|
|
|
}
|