2017-05-21 07:10:52 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2017-05-30 23:05:21 +00:00
|
|
|
"log"
|
2017-05-21 07:10:52 +00:00
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-05-23 14:03:52 +00:00
|
|
|
"strconv"
|
2017-05-21 07:10:52 +00:00
|
|
|
"strings"
|
2019-02-14 15:59:14 +00:00
|
|
|
"testing"
|
2017-05-21 07:10:52 +00:00
|
|
|
"time"
|
|
|
|
|
2017-09-26 06:11:19 +00:00
|
|
|
metrics "github.com/armon/go-metrics"
|
2018-05-09 16:15:29 +00:00
|
|
|
uuid "github.com/hashicorp/go-uuid"
|
|
|
|
|
2017-09-25 18:40:42 +00:00
|
|
|
"github.com/hashicorp/consul/agent/config"
|
2018-05-10 16:04:33 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
pkg refactor
command/agent/* -> agent/*
command/consul/* -> agent/consul/*
command/agent/command{,_test}.go -> command/agent{,_test}.go
command/base/command.go -> command/base.go
command/base/* -> command/*
commands.go -> command/commands.go
The script which did the refactor is:
(
cd $GOPATH/src/github.com/hashicorp/consul
git mv command/agent/command.go command/agent.go
git mv command/agent/command_test.go command/agent_test.go
git mv command/agent/flag_slice_value{,_test}.go command/
git mv command/agent .
git mv command/base/command.go command/base.go
git mv command/base/config_util{,_test}.go command/
git mv commands.go command/
git mv consul agent
rmdir command/base/
gsed -i -e 's|package agent|package command|' command/agent{,_test}.go
gsed -i -e 's|package agent|package command|' command/flag_slice_value{,_test}.go
gsed -i -e 's|package base|package command|' command/base.go command/config_util{,_test}.go
gsed -i -e 's|package main|package command|' command/commands.go
gsed -i -e 's|base.Command|BaseCommand|' command/commands.go
gsed -i -e 's|agent.Command|AgentCommand|' command/commands.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/commands.go
gsed -i -e 's|base\.||' command/commands.go
gsed -i -e 's|command\.||' command/commands.go
gsed -i -e 's|command|c|' main.go
gsed -i -e 's|range Commands|range command.Commands|' main.go
gsed -i -e 's|Commands: Commands|Commands: command.Commands|' main.go
gsed -i -e 's|base\.BoolValue|BoolValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.DurationValue|DurationValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.StringValue|StringValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.UintValue|UintValue|' command/operator_autopilot_set.go
gsed -i -e 's|\bCommand\b|BaseCommand|' command/base.go
gsed -i -e 's|BaseCommand Options|Command Options|' command/base.go
gsed -i -e 's|base.Command|BaseCommand|' command/*.go
gsed -i -e 's|c\.Command|c.BaseCommand|g' command/*.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/*_test.go
gsed -i -e 's|base\.||' command/*_test.go
gsed -i -e 's|\bCommand\b|AgentCommand|' command/agent{,_test}.go
gsed -i -e 's|cmd.AgentCommand|cmd.BaseCommand|' command/agent.go
gsed -i -e 's|cli.AgentCommand = new(Command)|cli.Command = new(AgentCommand)|' command/agent_test.go
gsed -i -e 's|exec.AgentCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|exec.BaseCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|NewTestAgent|agent.NewTestAgent|' command/agent_test.go
gsed -i -e 's|= TestConfig|= agent.TestConfig|' command/agent_test.go
gsed -i -e 's|: RetryJoin|: agent.RetryJoin|' command/agent_test.go
gsed -i -e 's|\.\./\.\./|../|' command/config_util_test.go
gsed -i -e 's|\bverifyUniqueListeners|VerifyUniqueListeners|' agent/config{,_test}.go command/agent.go
gsed -i -e 's|\bserfLANKeyring\b|SerfLANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bserfWANKeyring\b|SerfWANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bNewAgent\b|agent.New|g' command/agent{,_test}.go
gsed -i -e 's|\bNewAgent|New|' agent/{acl_test,agent,testagent}.go
gsed -i -e 's|\bAgent\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bBool\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDefaultConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDevConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bMergeConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bReadConfigPaths\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bParseMetaPair\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfLANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfWANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|circonus\.agent|circonus|g' command/agent{,_test}.go
gsed -i -e 's|logger\.agent|logger|g' command/agent{,_test}.go
gsed -i -e 's|metrics\.agent|metrics|g' command/agent{,_test}.go
gsed -i -e 's|// agent.Agent|// agent|' command/agent{,_test}.go
gsed -i -e 's|a\.agent\.Config|a.Config|' command/agent{,_test}.go
gsed -i -e 's|agent\.AppendSliceValue|AppendSliceValue|' command/{configtest,validate}.go
gsed -i -e 's|consul/consul|agent/consul|' GNUmakefile
gsed -i -e 's|\.\./test|../../test|' agent/consul/server_test.go
# fix imports
f=$(grep -rl 'github.com/hashicorp/consul/command/agent' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/command/agent|github.com/hashicorp/consul/agent|' $f
goimports -w $f
f=$(grep -rl 'github.com/hashicorp/consul/consul' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/consul|github.com/hashicorp/consul/agent/consul|' $f
goimports -w $f
goimports -w command/*.go main.go
)
2017-06-09 22:28:28 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul"
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2017-05-22 11:59:36 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2017-05-21 07:10:52 +00:00
|
|
|
"github.com/hashicorp/consul/logger"
|
2019-04-24 13:11:08 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/freeport"
|
2019-03-27 12:54:56 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2019-02-14 15:59:14 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2017-05-21 07:10:52 +00:00
|
|
|
)
|
|
|
|
|
2017-05-21 07:54:40 +00:00
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano()) // seed random number generator
|
|
|
|
}
|
|
|
|
|
2017-05-21 18:59:19 +00:00
|
|
|
// TempDir defines the base dir for temporary directories.
|
|
|
|
var TempDir = os.TempDir()
|
2017-05-21 07:10:52 +00:00
|
|
|
|
|
|
|
// TestAgent encapsulates an Agent with a default configuration and
|
|
|
|
// startup procedure suitable for testing. It panics if there are errors
|
|
|
|
// during creation or startup instead of returning errors. It manages a
|
|
|
|
// temporary data directory which is removed after shutdown.
|
|
|
|
type TestAgent struct {
|
|
|
|
// Name is an optional name of the agent.
|
|
|
|
Name string
|
|
|
|
|
2017-09-25 18:40:42 +00:00
|
|
|
HCL string
|
|
|
|
|
2018-05-22 14:11:13 +00:00
|
|
|
// ExpectConfigError can be set to prevent the agent retrying Start on errors
|
|
|
|
// and eventually blowing up with runtime.Goexit. This enables tests to assert
|
|
|
|
// that some specific bit of config actually does prevent startup entirely in
|
|
|
|
// a reasonable way without reproducing a lot of the boilerplate here.
|
|
|
|
ExpectConfigError bool
|
|
|
|
|
2017-05-21 07:10:52 +00:00
|
|
|
// Config is the agent configuration. If Config is nil then
|
|
|
|
// TestConfig() is used. If Config.DataDir is set then it is
|
|
|
|
// the callers responsibility to clean up the data directory.
|
|
|
|
// Otherwise, a temporary data directory is created and removed
|
|
|
|
// when Shutdown() is called.
|
2017-09-25 18:40:42 +00:00
|
|
|
Config *config.RuntimeConfig
|
2017-05-21 07:10:52 +00:00
|
|
|
|
|
|
|
// LogOutput is the sink for the logs. If nil, logs are written
|
|
|
|
// to os.Stderr.
|
|
|
|
LogOutput io.Writer
|
|
|
|
|
|
|
|
// LogWriter is used for streaming logs.
|
|
|
|
LogWriter *logger.LogWriter
|
|
|
|
|
|
|
|
// DataDir is the data directory which is used when Config.DataDir
|
|
|
|
// is not set. It is created automatically and removed when
|
|
|
|
// Shutdown() is called.
|
|
|
|
DataDir string
|
|
|
|
|
|
|
|
// Key is the optional encryption key for the LAN and WAN keyring.
|
|
|
|
Key string
|
|
|
|
|
2017-11-07 23:06:59 +00:00
|
|
|
// UseTLS, if true, will disable the HTTP port and enable the HTTPS
|
|
|
|
// one.
|
|
|
|
UseTLS bool
|
|
|
|
|
2017-05-21 07:10:52 +00:00
|
|
|
// dns is a reference to the first started DNS endpoint.
|
|
|
|
// It is valid after Start().
|
|
|
|
dns *DNSServer
|
|
|
|
|
|
|
|
// srv is a reference to the first started HTTP endpoint.
|
|
|
|
// It is valid after Start().
|
|
|
|
srv *HTTPServer
|
|
|
|
|
|
|
|
// Agent is the embedded consul agent.
|
|
|
|
// It is valid after Start().
|
|
|
|
*Agent
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTestAgent returns a started agent with the given name and
|
2019-02-14 15:59:14 +00:00
|
|
|
// configuration. It fails the test if the Agent could not be started. The
|
2017-05-21 07:10:52 +00:00
|
|
|
// caller should call Shutdown() to stop the agent and remove temporary
|
|
|
|
// directories.
|
2019-02-14 15:59:14 +00:00
|
|
|
func NewTestAgent(t *testing.T, name string, hcl string) *TestAgent {
|
2017-09-25 18:40:42 +00:00
|
|
|
a := &TestAgent{Name: name, HCL: hcl}
|
2019-02-14 15:59:14 +00:00
|
|
|
a.Start(t)
|
2017-05-21 07:10:52 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func NewUnstartedAgent(t *testing.T, name string, hcl string) (*Agent, error) {
|
|
|
|
c := TestConfig(config.Source{Name: name, Format: "hcl", Data: hcl})
|
|
|
|
a, err := New(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
2019-02-14 15:59:14 +00:00
|
|
|
// Start starts a test agent. It fails the test if the agent could not be started.
|
|
|
|
func (a *TestAgent) Start(t *testing.T) *TestAgent {
|
|
|
|
require := require.New(t)
|
|
|
|
require.Nil(a.Agent, "TestAgent already started")
|
2017-09-25 18:40:42 +00:00
|
|
|
var hclDataDir string
|
|
|
|
if a.DataDir == "" {
|
2017-05-21 07:10:52 +00:00
|
|
|
name := "agent"
|
|
|
|
if a.Name != "" {
|
|
|
|
name = a.Name + "-agent"
|
|
|
|
}
|
|
|
|
name = strings.Replace(name, "/", "_", -1)
|
|
|
|
d, err := ioutil.TempDir(TempDir, name)
|
2019-02-14 15:59:14 +00:00
|
|
|
require.NoError(err, fmt.Sprintf("Error creating data dir %s: %s", filepath.Join(TempDir, name), err))
|
2017-09-25 18:40:42 +00:00
|
|
|
hclDataDir = `data_dir = "` + d + `"`
|
2017-05-21 07:10:52 +00:00
|
|
|
}
|
2017-05-23 14:03:52 +00:00
|
|
|
|
2019-05-24 18:36:56 +00:00
|
|
|
var id string
|
2017-05-31 09:32:31 +00:00
|
|
|
for i := 10; i >= 0; i-- {
|
2017-09-25 18:40:42 +00:00
|
|
|
a.Config = TestConfig(
|
2018-08-17 18:44:25 +00:00
|
|
|
randomPortsSource(a.UseTLS),
|
2017-09-25 18:40:42 +00:00
|
|
|
config.Source{Name: a.Name, Format: "hcl", Data: a.HCL},
|
|
|
|
config.Source{Name: a.Name + ".data_dir", Format: "hcl", Data: hclDataDir},
|
|
|
|
)
|
2019-05-24 18:36:56 +00:00
|
|
|
id = string(a.Config.NodeID)
|
2017-06-03 03:53:20 +00:00
|
|
|
|
2017-05-23 14:03:52 +00:00
|
|
|
// write the keyring
|
|
|
|
if a.Key != "" {
|
|
|
|
writeKey := func(key, filename string) {
|
|
|
|
path := filepath.Join(a.Config.DataDir, filename)
|
2019-02-14 15:59:14 +00:00
|
|
|
err := initKeyring(path, key)
|
|
|
|
require.NoError(err, fmt.Sprintf("Error creating keyring %s: %s", path, err))
|
2017-05-23 14:03:52 +00:00
|
|
|
}
|
pkg refactor
command/agent/* -> agent/*
command/consul/* -> agent/consul/*
command/agent/command{,_test}.go -> command/agent{,_test}.go
command/base/command.go -> command/base.go
command/base/* -> command/*
commands.go -> command/commands.go
The script which did the refactor is:
(
cd $GOPATH/src/github.com/hashicorp/consul
git mv command/agent/command.go command/agent.go
git mv command/agent/command_test.go command/agent_test.go
git mv command/agent/flag_slice_value{,_test}.go command/
git mv command/agent .
git mv command/base/command.go command/base.go
git mv command/base/config_util{,_test}.go command/
git mv commands.go command/
git mv consul agent
rmdir command/base/
gsed -i -e 's|package agent|package command|' command/agent{,_test}.go
gsed -i -e 's|package agent|package command|' command/flag_slice_value{,_test}.go
gsed -i -e 's|package base|package command|' command/base.go command/config_util{,_test}.go
gsed -i -e 's|package main|package command|' command/commands.go
gsed -i -e 's|base.Command|BaseCommand|' command/commands.go
gsed -i -e 's|agent.Command|AgentCommand|' command/commands.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/commands.go
gsed -i -e 's|base\.||' command/commands.go
gsed -i -e 's|command\.||' command/commands.go
gsed -i -e 's|command|c|' main.go
gsed -i -e 's|range Commands|range command.Commands|' main.go
gsed -i -e 's|Commands: Commands|Commands: command.Commands|' main.go
gsed -i -e 's|base\.BoolValue|BoolValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.DurationValue|DurationValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.StringValue|StringValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.UintValue|UintValue|' command/operator_autopilot_set.go
gsed -i -e 's|\bCommand\b|BaseCommand|' command/base.go
gsed -i -e 's|BaseCommand Options|Command Options|' command/base.go
gsed -i -e 's|base.Command|BaseCommand|' command/*.go
gsed -i -e 's|c\.Command|c.BaseCommand|g' command/*.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/*_test.go
gsed -i -e 's|base\.||' command/*_test.go
gsed -i -e 's|\bCommand\b|AgentCommand|' command/agent{,_test}.go
gsed -i -e 's|cmd.AgentCommand|cmd.BaseCommand|' command/agent.go
gsed -i -e 's|cli.AgentCommand = new(Command)|cli.Command = new(AgentCommand)|' command/agent_test.go
gsed -i -e 's|exec.AgentCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|exec.BaseCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|NewTestAgent|agent.NewTestAgent|' command/agent_test.go
gsed -i -e 's|= TestConfig|= agent.TestConfig|' command/agent_test.go
gsed -i -e 's|: RetryJoin|: agent.RetryJoin|' command/agent_test.go
gsed -i -e 's|\.\./\.\./|../|' command/config_util_test.go
gsed -i -e 's|\bverifyUniqueListeners|VerifyUniqueListeners|' agent/config{,_test}.go command/agent.go
gsed -i -e 's|\bserfLANKeyring\b|SerfLANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bserfWANKeyring\b|SerfWANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bNewAgent\b|agent.New|g' command/agent{,_test}.go
gsed -i -e 's|\bNewAgent|New|' agent/{acl_test,agent,testagent}.go
gsed -i -e 's|\bAgent\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bBool\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDefaultConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDevConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bMergeConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bReadConfigPaths\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bParseMetaPair\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfLANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfWANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|circonus\.agent|circonus|g' command/agent{,_test}.go
gsed -i -e 's|logger\.agent|logger|g' command/agent{,_test}.go
gsed -i -e 's|metrics\.agent|metrics|g' command/agent{,_test}.go
gsed -i -e 's|// agent.Agent|// agent|' command/agent{,_test}.go
gsed -i -e 's|a\.agent\.Config|a.Config|' command/agent{,_test}.go
gsed -i -e 's|agent\.AppendSliceValue|AppendSliceValue|' command/{configtest,validate}.go
gsed -i -e 's|consul/consul|agent/consul|' GNUmakefile
gsed -i -e 's|\.\./test|../../test|' agent/consul/server_test.go
# fix imports
f=$(grep -rl 'github.com/hashicorp/consul/command/agent' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/command/agent|github.com/hashicorp/consul/agent|' $f
goimports -w $f
f=$(grep -rl 'github.com/hashicorp/consul/consul' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/consul|github.com/hashicorp/consul/agent/consul|' $f
goimports -w $f
goimports -w command/*.go main.go
)
2017-06-09 22:28:28 +00:00
|
|
|
writeKey(a.Key, SerfLANKeyring)
|
|
|
|
writeKey(a.Key, SerfWANKeyring)
|
2017-05-21 07:54:40 +00:00
|
|
|
}
|
|
|
|
|
pkg refactor
command/agent/* -> agent/*
command/consul/* -> agent/consul/*
command/agent/command{,_test}.go -> command/agent{,_test}.go
command/base/command.go -> command/base.go
command/base/* -> command/*
commands.go -> command/commands.go
The script which did the refactor is:
(
cd $GOPATH/src/github.com/hashicorp/consul
git mv command/agent/command.go command/agent.go
git mv command/agent/command_test.go command/agent_test.go
git mv command/agent/flag_slice_value{,_test}.go command/
git mv command/agent .
git mv command/base/command.go command/base.go
git mv command/base/config_util{,_test}.go command/
git mv commands.go command/
git mv consul agent
rmdir command/base/
gsed -i -e 's|package agent|package command|' command/agent{,_test}.go
gsed -i -e 's|package agent|package command|' command/flag_slice_value{,_test}.go
gsed -i -e 's|package base|package command|' command/base.go command/config_util{,_test}.go
gsed -i -e 's|package main|package command|' command/commands.go
gsed -i -e 's|base.Command|BaseCommand|' command/commands.go
gsed -i -e 's|agent.Command|AgentCommand|' command/commands.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/commands.go
gsed -i -e 's|base\.||' command/commands.go
gsed -i -e 's|command\.||' command/commands.go
gsed -i -e 's|command|c|' main.go
gsed -i -e 's|range Commands|range command.Commands|' main.go
gsed -i -e 's|Commands: Commands|Commands: command.Commands|' main.go
gsed -i -e 's|base\.BoolValue|BoolValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.DurationValue|DurationValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.StringValue|StringValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.UintValue|UintValue|' command/operator_autopilot_set.go
gsed -i -e 's|\bCommand\b|BaseCommand|' command/base.go
gsed -i -e 's|BaseCommand Options|Command Options|' command/base.go
gsed -i -e 's|base.Command|BaseCommand|' command/*.go
gsed -i -e 's|c\.Command|c.BaseCommand|g' command/*.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/*_test.go
gsed -i -e 's|base\.||' command/*_test.go
gsed -i -e 's|\bCommand\b|AgentCommand|' command/agent{,_test}.go
gsed -i -e 's|cmd.AgentCommand|cmd.BaseCommand|' command/agent.go
gsed -i -e 's|cli.AgentCommand = new(Command)|cli.Command = new(AgentCommand)|' command/agent_test.go
gsed -i -e 's|exec.AgentCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|exec.BaseCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|NewTestAgent|agent.NewTestAgent|' command/agent_test.go
gsed -i -e 's|= TestConfig|= agent.TestConfig|' command/agent_test.go
gsed -i -e 's|: RetryJoin|: agent.RetryJoin|' command/agent_test.go
gsed -i -e 's|\.\./\.\./|../|' command/config_util_test.go
gsed -i -e 's|\bverifyUniqueListeners|VerifyUniqueListeners|' agent/config{,_test}.go command/agent.go
gsed -i -e 's|\bserfLANKeyring\b|SerfLANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bserfWANKeyring\b|SerfWANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bNewAgent\b|agent.New|g' command/agent{,_test}.go
gsed -i -e 's|\bNewAgent|New|' agent/{acl_test,agent,testagent}.go
gsed -i -e 's|\bAgent\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bBool\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDefaultConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDevConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bMergeConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bReadConfigPaths\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bParseMetaPair\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfLANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfWANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|circonus\.agent|circonus|g' command/agent{,_test}.go
gsed -i -e 's|logger\.agent|logger|g' command/agent{,_test}.go
gsed -i -e 's|metrics\.agent|metrics|g' command/agent{,_test}.go
gsed -i -e 's|// agent.Agent|// agent|' command/agent{,_test}.go
gsed -i -e 's|a\.agent\.Config|a.Config|' command/agent{,_test}.go
gsed -i -e 's|agent\.AppendSliceValue|AppendSliceValue|' command/{configtest,validate}.go
gsed -i -e 's|consul/consul|agent/consul|' GNUmakefile
gsed -i -e 's|\.\./test|../../test|' agent/consul/server_test.go
# fix imports
f=$(grep -rl 'github.com/hashicorp/consul/command/agent' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/command/agent|github.com/hashicorp/consul/agent|' $f
goimports -w $f
f=$(grep -rl 'github.com/hashicorp/consul/consul' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/consul|github.com/hashicorp/consul/agent/consul|' $f
goimports -w $f
goimports -w command/*.go main.go
)
2017-06-09 22:28:28 +00:00
|
|
|
agent, err := New(a.Config)
|
2019-02-14 15:59:14 +00:00
|
|
|
require.NoError(err, fmt.Sprintf("Error creating agent: %s", err))
|
2017-05-21 07:54:40 +00:00
|
|
|
|
2017-05-30 23:05:21 +00:00
|
|
|
logOutput := a.LogOutput
|
|
|
|
if logOutput == nil {
|
|
|
|
logOutput = os.Stderr
|
|
|
|
}
|
|
|
|
agent.LogOutput = logOutput
|
2017-05-23 14:03:52 +00:00
|
|
|
agent.LogWriter = a.LogWriter
|
2017-09-25 18:40:42 +00:00
|
|
|
agent.logger = log.New(logOutput, a.Name+" - ", log.LstdFlags|log.Lmicroseconds)
|
2017-11-07 05:50:04 +00:00
|
|
|
agent.MemSink = metrics.NewInmemSink(1*time.Second, time.Minute)
|
2017-05-23 14:03:52 +00:00
|
|
|
|
|
|
|
// we need the err var in the next exit condition
|
|
|
|
if err := agent.Start(); err == nil {
|
|
|
|
a.Agent = agent
|
|
|
|
break
|
|
|
|
} else if i == 0 {
|
2019-02-14 15:59:14 +00:00
|
|
|
require.Fail("%s %s Error starting agent: %s", id, a.Name, err)
|
2018-05-22 14:11:13 +00:00
|
|
|
} else if a.ExpectConfigError {
|
|
|
|
// Panic the error since this can be caught if needed. Pretty gross way to
|
|
|
|
// detect errors but enough for now and this is a tiny edge case that I'd
|
|
|
|
// otherwise not have a way to test at all...
|
|
|
|
panic(err)
|
2017-05-23 14:03:52 +00:00
|
|
|
} else {
|
2017-06-20 07:29:20 +00:00
|
|
|
agent.ShutdownAgent()
|
|
|
|
agent.ShutdownEndpoints()
|
2017-05-31 09:33:25 +00:00
|
|
|
wait := time.Duration(rand.Int31n(2000)) * time.Millisecond
|
|
|
|
fmt.Println(id, a.Name, "retrying in", wait)
|
|
|
|
time.Sleep(wait)
|
2017-05-23 14:03:52 +00:00
|
|
|
}
|
2017-06-03 03:53:20 +00:00
|
|
|
|
|
|
|
// Clean out the data dir if we are responsible for it before we
|
|
|
|
// try again, since the old ports may have gotten written to
|
|
|
|
// the data dir, such as in the Raft configuration.
|
|
|
|
if a.DataDir != "" {
|
|
|
|
if err := os.RemoveAll(a.DataDir); err != nil {
|
2019-02-14 15:59:14 +00:00
|
|
|
require.Fail("%s %s Error resetting data dir: %s", id, a.Name, err)
|
2017-06-03 03:53:20 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-23 14:03:52 +00:00
|
|
|
}
|
2017-08-28 12:17:16 +00:00
|
|
|
|
|
|
|
// Start the anti-entropy syncer
|
|
|
|
a.Agent.StartSync()
|
2017-05-23 14:03:52 +00:00
|
|
|
|
2017-05-21 07:10:52 +00:00
|
|
|
var out structs.IndexedNodes
|
2019-02-14 15:59:14 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2017-05-21 07:10:52 +00:00
|
|
|
if len(a.httpServers) == 0 {
|
2017-05-23 00:43:31 +00:00
|
|
|
r.Fatal(a.Name, "waiting for server")
|
2017-05-21 07:10:52 +00:00
|
|
|
}
|
2017-09-25 18:40:42 +00:00
|
|
|
if a.Config.Bootstrap && a.Config.ServerMode {
|
2017-05-21 07:10:52 +00:00
|
|
|
// Ensure we have a leader and a node registration.
|
2017-05-23 00:43:07 +00:00
|
|
|
args := &structs.DCSpecificRequest{
|
|
|
|
Datacenter: a.Config.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
MinQueryIndex: out.Index,
|
|
|
|
MaxQueryTime: 25 * time.Millisecond,
|
|
|
|
},
|
|
|
|
}
|
2017-05-21 07:10:52 +00:00
|
|
|
if err := a.RPC("Catalog.ListNodes", args, &out); err != nil {
|
2017-05-23 00:43:31 +00:00
|
|
|
r.Fatal(a.Name, "Catalog.ListNodes failed:", err)
|
2017-05-21 07:10:52 +00:00
|
|
|
}
|
|
|
|
if !out.QueryMeta.KnownLeader {
|
2017-05-23 00:43:31 +00:00
|
|
|
r.Fatal(a.Name, "No leader")
|
2017-05-21 07:10:52 +00:00
|
|
|
}
|
|
|
|
if out.Index == 0 {
|
2017-08-28 12:17:16 +00:00
|
|
|
r.Fatal(a.Name, ": Consul index is 0")
|
2017-05-21 07:10:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/agent/self", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err := a.httpServers[0].AgentSelf(resp, req)
|
|
|
|
if err != nil || resp.Code != 200 {
|
2018-03-19 16:56:00 +00:00
|
|
|
r.Fatal(a.Name, "failed OK response", err)
|
2017-05-21 07:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
a.dns = a.dnsServers[0]
|
|
|
|
a.srv = a.httpServers[0]
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown stops the agent and removes the data directory if it is
|
|
|
|
// managed by the test agent.
|
|
|
|
func (a *TestAgent) Shutdown() error {
|
2017-09-25 18:40:42 +00:00
|
|
|
/* Removed this because it was breaking persistence tests where we would
|
|
|
|
persist a service and load it through a new agent with the same data-dir.
|
|
|
|
Not sure if we still need this for other things, everywhere we manually make
|
|
|
|
a data dir we already do 'defer os.RemoveAll()'
|
2017-05-21 07:10:52 +00:00
|
|
|
defer func() {
|
|
|
|
if a.DataDir != "" {
|
|
|
|
os.RemoveAll(a.DataDir)
|
|
|
|
}
|
2017-09-25 18:40:42 +00:00
|
|
|
}()*/
|
2017-06-20 07:29:20 +00:00
|
|
|
|
|
|
|
// shutdown agent before endpoints
|
|
|
|
defer a.Agent.ShutdownEndpoints()
|
|
|
|
return a.Agent.ShutdownAgent()
|
2017-05-21 07:10:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 18:40:42 +00:00
|
|
|
func (a *TestAgent) DNSAddr() string {
|
|
|
|
if a.dns == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return a.dns.Addr
|
|
|
|
}
|
|
|
|
|
2017-05-22 11:59:36 +00:00
|
|
|
func (a *TestAgent) HTTPAddr() string {
|
|
|
|
if a.srv == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return a.srv.Addr
|
|
|
|
}
|
|
|
|
|
2017-08-14 14:36:07 +00:00
|
|
|
func (a *TestAgent) SegmentAddr(name string) string {
|
|
|
|
if server, ok := a.Agent.delegate.(*consul.Server); ok {
|
|
|
|
return server.LANSegmentAddr(name)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-05-22 11:59:36 +00:00
|
|
|
func (a *TestAgent) Client() *api.Client {
|
|
|
|
conf := api.DefaultConfig()
|
|
|
|
conf.Address = a.HTTPAddr()
|
|
|
|
c, err := api.NewClient(conf)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Error creating consul API client: %s", err))
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2017-06-29 14:42:17 +00:00
|
|
|
// DNSDisableCompression disables compression for all started DNS servers.
|
|
|
|
func (a *TestAgent) DNSDisableCompression(b bool) {
|
|
|
|
for _, srv := range a.dnsServers {
|
2019-04-24 18:11:54 +00:00
|
|
|
cfg := srv.config.Load().(*dnsConfig)
|
|
|
|
cfg.DisableCompression = b
|
2017-06-29 14:42:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:10:52 +00:00
|
|
|
func (a *TestAgent) consulConfig() *consul.Config {
|
|
|
|
c, err := a.Agent.consulConfig()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:54:40 +00:00
|
|
|
// pickRandomPorts selects random ports from fixed size random blocks of
|
|
|
|
// ports. This does not eliminate the chance for port conflict but
|
2018-03-19 16:56:00 +00:00
|
|
|
// reduces it significantly with little overhead. Furthermore, asking
|
2017-05-21 07:54:40 +00:00
|
|
|
// the kernel for a random port by binding to port 0 prolongs the test
|
|
|
|
// execution (in our case +20sec) while also not fully eliminating the
|
|
|
|
// chance of port conflicts for concurrently executed test binaries.
|
|
|
|
// Instead of relying on one set of ports to be sufficient we retry
|
|
|
|
// starting the agent with different ports on port conflict.
|
2017-11-07 23:06:59 +00:00
|
|
|
func randomPortsSource(tls bool) config.Source {
|
|
|
|
ports := freeport.Get(6)
|
|
|
|
if tls {
|
|
|
|
ports[1] = -1
|
|
|
|
} else {
|
|
|
|
ports[2] = -1
|
|
|
|
}
|
2017-09-25 18:40:42 +00:00
|
|
|
return config.Source{
|
|
|
|
Name: "ports",
|
|
|
|
Format: "hcl",
|
|
|
|
Data: `
|
|
|
|
ports = {
|
|
|
|
dns = ` + strconv.Itoa(ports[0]) + `
|
|
|
|
http = ` + strconv.Itoa(ports[1]) + `
|
2017-11-07 23:06:59 +00:00
|
|
|
https = ` + strconv.Itoa(ports[2]) + `
|
|
|
|
serf_lan = ` + strconv.Itoa(ports[3]) + `
|
|
|
|
serf_wan = ` + strconv.Itoa(ports[4]) + `
|
|
|
|
server = ` + strconv.Itoa(ports[5]) + `
|
2017-09-25 18:40:42 +00:00
|
|
|
}
|
|
|
|
`,
|
|
|
|
}
|
2017-05-21 07:54:40 +00:00
|
|
|
}
|
2017-05-21 07:10:52 +00:00
|
|
|
|
2017-09-25 18:40:42 +00:00
|
|
|
func NodeID() string {
|
|
|
|
id, err := uuid.GenerateUUID()
|
2017-05-21 07:10:52 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-09-25 18:40:42 +00:00
|
|
|
return id
|
|
|
|
}
|
2017-05-21 07:10:52 +00:00
|
|
|
|
2017-09-25 18:40:42 +00:00
|
|
|
// TestConfig returns a unique default configuration for testing an
|
|
|
|
// agent.
|
|
|
|
func TestConfig(sources ...config.Source) *config.RuntimeConfig {
|
|
|
|
nodeID := NodeID()
|
|
|
|
testsrc := config.Source{
|
|
|
|
Name: "test",
|
|
|
|
Format: "hcl",
|
|
|
|
Data: `
|
|
|
|
bind_addr = "127.0.0.1"
|
|
|
|
advertise_addr = "127.0.0.1"
|
|
|
|
datacenter = "dc1"
|
|
|
|
bootstrap = true
|
|
|
|
server = true
|
|
|
|
node_id = "` + nodeID + `"
|
|
|
|
node_name = "Node ` + nodeID + `"
|
2018-04-30 03:44:40 +00:00
|
|
|
connect {
|
|
|
|
enabled = true
|
2018-05-10 16:04:33 +00:00
|
|
|
ca_config {
|
|
|
|
cluster_id = "` + connect.TestClusterID + `"
|
|
|
|
}
|
2018-04-30 03:44:40 +00:00
|
|
|
}
|
2017-09-25 18:40:42 +00:00
|
|
|
performance {
|
|
|
|
raft_multiplier = 1
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
}
|
2017-05-22 11:03:59 +00:00
|
|
|
|
2017-09-25 18:40:42 +00:00
|
|
|
b, err := config.NewBuilder(config.Flags{})
|
|
|
|
if err != nil {
|
|
|
|
panic("NewBuilder failed: " + err.Error())
|
|
|
|
}
|
|
|
|
b.Head = append(b.Head, testsrc)
|
|
|
|
b.Tail = append(b.Tail, config.DefaultConsulSource(), config.DevConsulSource())
|
|
|
|
b.Tail = append(b.Tail, sources...)
|
2017-05-22 11:03:59 +00:00
|
|
|
|
2017-09-25 18:40:42 +00:00
|
|
|
cfg, err := b.BuildAndValidate()
|
|
|
|
if err != nil {
|
|
|
|
panic("Error building config: " + err.Error())
|
|
|
|
}
|
2017-05-22 11:03:59 +00:00
|
|
|
|
2017-09-25 18:40:42 +00:00
|
|
|
for _, w := range b.Warnings {
|
|
|
|
fmt.Println("WARNING:", w)
|
|
|
|
}
|
2017-05-22 11:03:59 +00:00
|
|
|
|
2018-06-15 22:35:15 +00:00
|
|
|
// Disable connect proxy execution since it causes all kinds of problems with
|
|
|
|
// self-executing tests etc.
|
|
|
|
cfg.ConnectTestDisableManagedProxies = true
|
2019-01-10 12:46:11 +00:00
|
|
|
// Effectively disables the delay after root rotation before requesting CSRs
|
|
|
|
// to make test deterministic. 0 results in default jitter being applied but a
|
|
|
|
// tiny delay is effectively thre same.
|
|
|
|
cfg.ConnectTestCALeafRootChangeSpread = 1 * time.Nanosecond
|
2018-06-15 22:35:15 +00:00
|
|
|
|
2017-09-25 18:40:42 +00:00
|
|
|
return &cfg
|
2017-05-21 07:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestACLConfig returns a default configuration for testing an agent
|
|
|
|
// with ACLs.
|
2017-09-25 18:40:42 +00:00
|
|
|
func TestACLConfig() string {
|
|
|
|
return `
|
|
|
|
acl_datacenter = "dc1"
|
|
|
|
acl_default_policy = "deny"
|
|
|
|
acl_master_token = "root"
|
|
|
|
acl_agent_token = "root"
|
|
|
|
acl_agent_master_token = "towel"
|
|
|
|
acl_enforce_version_8 = true
|
|
|
|
`
|
2017-05-21 07:10:52 +00:00
|
|
|
}
|
2019-04-30 23:00:57 +00:00
|
|
|
|
|
|
|
func TestACLConfigNew() string {
|
|
|
|
return `
|
|
|
|
primary_datacenter = "dc1"
|
|
|
|
acl {
|
|
|
|
enabled = true
|
|
|
|
default_policy = "deny"
|
|
|
|
tokens {
|
|
|
|
master = "root"
|
|
|
|
agent = "root"
|
|
|
|
agent_master = "towel"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|