2013-12-31 21:06:33 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
2016-09-26 15:10:58 +00:00
|
|
|
"strings"
|
2013-12-31 21:06:33 +00:00
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2016-09-26 15:08:47 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/command/agent"
|
|
|
|
"github.com/hashicorp/consul/consul"
|
2016-11-04 04:14:56 +00:00
|
|
|
"github.com/hashicorp/consul/logger"
|
2017-03-27 08:28:54 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2017-03-21 23:36:44 +00:00
|
|
|
"github.com/hashicorp/consul/version"
|
2017-03-27 08:28:54 +00:00
|
|
|
"github.com/hashicorp/go-uuid"
|
2016-09-26 15:10:58 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2013-12-31 21:06:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var offset uint64
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Seed the random number generator
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
2017-03-21 23:36:44 +00:00
|
|
|
|
|
|
|
version.Version = "0.8.0"
|
2013-12-31 21:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type agentWrapper struct {
|
2014-08-21 23:08:21 +00:00
|
|
|
dir string
|
|
|
|
config *agent.Config
|
|
|
|
agent *agent.Agent
|
|
|
|
http *agent.HTTPServer
|
|
|
|
httpAddr string
|
2013-12-31 21:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *agentWrapper) 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 {
|
2017-02-10 00:32:22 +00:00
|
|
|
return testAgentWithConfig(t, nil)
|
2014-09-10 15:49:16 +00:00
|
|
|
}
|
|
|
|
|
2016-09-26 15:08:47 +00:00
|
|
|
func testAgentWithAPIClient(t *testing.T) (*agentWrapper, *api.Client) {
|
|
|
|
agent := testAgentWithConfig(t, func(c *agent.Config) {})
|
|
|
|
client, err := api.NewClient(&api.Config{Address: agent.httpAddr})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("consul client: %#v", err)
|
|
|
|
}
|
|
|
|
return agent, client
|
|
|
|
}
|
|
|
|
|
2014-10-07 18:05:31 +00:00
|
|
|
func testAgentWithConfig(t *testing.T, cb func(c *agent.Config)) *agentWrapper {
|
2017-02-10 00:32:22 +00:00
|
|
|
return testAgentWithConfigReload(t, cb, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAgentWithConfigReload(t *testing.T, cb func(c *agent.Config), reloadCh chan chan error) *agentWrapper {
|
2016-11-04 04:14:56 +00:00
|
|
|
lw := logger.NewLogWriter(512)
|
2013-12-31 21:06:33 +00:00
|
|
|
conf := nextConfig()
|
2017-02-10 00:32:22 +00:00
|
|
|
if cb != nil {
|
|
|
|
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
|
|
|
|
|
2017-02-10 00:32:22 +00:00
|
|
|
a, err := agent.Create(conf, lw, nil, reloadCh)
|
2013-12-31 21:06:33 +00:00
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
t.Fatalf(fmt.Sprintf("err: %v", err))
|
|
|
|
}
|
|
|
|
|
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)
|
2015-08-25 23:40:55 +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,
|
2014-11-18 16:03:36 +00:00
|
|
|
http: http[0],
|
2014-08-21 23:08:21 +00:00
|
|
|
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()
|
|
|
|
|
2017-03-27 08:28:54 +00:00
|
|
|
nodeID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2013-12-31 21:06:33 +00:00
|
|
|
conf.Bootstrap = true
|
|
|
|
conf.Datacenter = "dc1"
|
|
|
|
conf.NodeName = fmt.Sprintf("Node %d", idx)
|
2017-03-27 08:28:54 +00:00
|
|
|
conf.NodeID = types.NodeID(nodeID)
|
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
|
|
|
|
2017-03-21 23:36:44 +00:00
|
|
|
conf.Version = version.Version
|
|
|
|
|
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.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
|
|
|
|
}
|
2016-09-26 15:10:58 +00:00
|
|
|
|
|
|
|
func assertNoTabs(t *testing.T, c cli.Command) {
|
|
|
|
if strings.ContainsRune(c.Help(), '\t') {
|
|
|
|
t.Errorf("%#v help output contains tabs", c)
|
|
|
|
}
|
|
|
|
}
|