2013-12-31 21:06:33 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"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-05-12 13:41:13 +00:00
|
|
|
"github.com/hashicorp/consul/testutil"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-05-19 09:53:41 +00:00
|
|
|
type server struct {
|
2014-08-21 23:08:21 +00:00
|
|
|
agent *agent.Agent
|
2017-05-19 09:53:41 +00:00
|
|
|
config *agent.Config
|
2014-08-21 23:08:21 +00:00
|
|
|
httpAddr string
|
2017-05-19 09:53:41 +00:00
|
|
|
dir string
|
2013-12-31 21:06:33 +00:00
|
|
|
}
|
|
|
|
|
2017-05-19 09:53:41 +00:00
|
|
|
func (a *server) Shutdown() {
|
2013-12-31 21:06:33 +00:00
|
|
|
a.agent.Shutdown()
|
|
|
|
os.RemoveAll(a.dir)
|
|
|
|
}
|
|
|
|
|
2017-05-19 09:53:41 +00:00
|
|
|
func testAgent(t *testing.T) *server {
|
2017-02-10 00:32:22 +00:00
|
|
|
return testAgentWithConfig(t, nil)
|
2014-09-10 15:49:16 +00:00
|
|
|
}
|
|
|
|
|
2017-05-19 09:53:41 +00:00
|
|
|
func testAgentWithAPIClient(t *testing.T) (*server, *api.Client) {
|
2016-09-26 15:08:47 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-05-19 09:53:41 +00:00
|
|
|
func testAgentWithConfig(t *testing.T, cb func(c *agent.Config)) *server {
|
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
|
|
|
|
2017-05-19 09:53:41 +00:00
|
|
|
conf.DataDir = testutil.TempDir(t, "agent")
|
2017-05-19 15:51:39 +00:00
|
|
|
a, err := agent.NewAgent(conf)
|
2013-12-31 21:06:33 +00:00
|
|
|
if err != nil {
|
2017-05-19 09:53:41 +00:00
|
|
|
os.RemoveAll(conf.DataDir)
|
2017-05-19 15:51:39 +00:00
|
|
|
t.Fatal("Error creating agent:", err)
|
|
|
|
}
|
|
|
|
a.LogOutput = logger.NewLogWriter(512)
|
|
|
|
if err := a.Start(); err != nil {
|
|
|
|
os.RemoveAll(conf.DataDir)
|
|
|
|
t.Fatalf("Error starting agent: %v", err)
|
2013-12-31 21:06:33 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 16:03:36 +00:00
|
|
|
conf.Addresses.HTTP = "127.0.0.1"
|
2017-05-19 09:53:41 +00:00
|
|
|
addr := fmt.Sprintf("%s:%d", conf.Addresses.HTTP, conf.Ports.HTTP)
|
|
|
|
return &server{agent: a, config: conf, httpAddr: addr, dir: conf.DataDir}
|
2013-12-31 21:06:33 +00:00
|
|
|
}
|
|
|
|
|
2017-05-19 09:53:41 +00:00
|
|
|
var nextPort uint64 = 10000
|
2013-12-31 21:06:33 +00:00
|
|
|
|
2017-05-19 09:53:41 +00:00
|
|
|
func nextConfig() *agent.Config {
|
2017-03-27 08:28:54 +00:00
|
|
|
nodeID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-05-19 09:53:41 +00:00
|
|
|
port := int(atomic.AddUint64(&nextPort, 10))
|
|
|
|
|
|
|
|
conf := agent.DefaultConfig()
|
2013-12-31 21:06:33 +00:00
|
|
|
conf.Bootstrap = true
|
|
|
|
conf.Datacenter = "dc1"
|
2017-05-19 09:53:41 +00:00
|
|
|
conf.NodeName = fmt.Sprintf("Node %d", port)
|
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
|
2017-03-21 23:36:44 +00:00
|
|
|
conf.Version = version.Version
|
2017-05-19 09:53:41 +00:00
|
|
|
conf.Ports = agent.PortConfig{
|
|
|
|
DNS: port + 1,
|
|
|
|
HTTP: port + 2,
|
|
|
|
HTTPS: port + 3,
|
|
|
|
SerfLan: port + 4,
|
|
|
|
SerfWan: port + 5,
|
|
|
|
Server: port + 6,
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|