2023-03-28 19:12:30 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 19:12:30 +00:00
|
|
|
|
2017-10-17 10:55:04 +00:00
|
|
|
package agent
|
2013-12-19 20:18:06 +00:00
|
|
|
|
|
|
|
import (
|
2014-10-12 19:27:03 +00:00
|
|
|
"fmt"
|
2021-06-02 21:12:44 +00:00
|
|
|
"io"
|
2014-10-12 19:27:03 +00:00
|
|
|
"os"
|
2017-05-08 16:39:53 +00:00
|
|
|
"os/exec"
|
2015-10-15 21:15:41 +00:00
|
|
|
"path/filepath"
|
2015-08-26 03:22:22 +00:00
|
|
|
"strings"
|
2013-12-19 20:18:06 +00:00
|
|
|
"testing"
|
2014-10-12 19:27:03 +00:00
|
|
|
|
2023-02-10 19:12:13 +00:00
|
|
|
"github.com/hashicorp/consul/command/cli"
|
|
|
|
mcli "github.com/mitchellh/cli"
|
2020-01-28 23:50:41 +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
|
|
|
"github.com/hashicorp/consul/agent"
|
2019-03-27 12:54:56 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2021-06-02 20:29:29 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2013-12-19 20:18:06 +00:00
|
|
|
)
|
|
|
|
|
2017-05-08 16:39:53 +00:00
|
|
|
// TestConfigFail should test command line flags that lead to an immediate error.
|
|
|
|
func TestConfigFail(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-10-17 14:07:48 +00:00
|
|
|
t.Parallel()
|
2017-09-25 18:40:42 +00:00
|
|
|
|
|
|
|
dataDir := testutil.TempDir(t, "consul")
|
|
|
|
|
2017-05-08 16:39:53 +00:00
|
|
|
tests := []struct {
|
|
|
|
args []string
|
|
|
|
out string
|
|
|
|
}{
|
|
|
|
{
|
2017-09-25 18:40:42 +00:00
|
|
|
args: []string{"agent", "-server", "-bind=10.0.0.1", "-datacenter="},
|
|
|
|
out: "==> datacenter cannot be empty\n",
|
|
|
|
},
|
2017-10-20 10:02:05 +00:00
|
|
|
{
|
|
|
|
args: []string{"agent", "-server", "-bind=10.0.0.1", "-datacenter=foo", "some-other-arg"},
|
2020-05-01 22:17:28 +00:00
|
|
|
out: "==> Unexpected extra arguments: [some-other-arg]\n",
|
2017-10-20 10:02:05 +00:00
|
|
|
},
|
2017-09-25 18:40:42 +00:00
|
|
|
{
|
|
|
|
args: []string{"agent", "-server", "-bind=10.0.0.1"},
|
|
|
|
out: "==> data_dir cannot be empty\n",
|
2017-05-08 16:39:53 +00:00
|
|
|
},
|
2017-05-09 07:37:21 +00:00
|
|
|
{
|
2017-09-25 18:40:42 +00:00
|
|
|
args: []string{"agent", "-server", "-data-dir", dataDir, "-advertise", "0.0.0.0", "-bind", "10.0.0.1"},
|
|
|
|
out: "==> Advertise address cannot be 0.0.0.0, :: or [::]\n",
|
2017-05-09 07:37:21 +00:00
|
|
|
},
|
|
|
|
{
|
2017-09-25 18:40:42 +00:00
|
|
|
args: []string{"agent", "-server", "-data-dir", dataDir, "-advertise", "::", "-bind", "10.0.0.1"},
|
|
|
|
out: "==> Advertise address cannot be 0.0.0.0, :: or [::]\n",
|
2017-05-09 07:37:21 +00:00
|
|
|
},
|
2017-05-08 16:39:53 +00:00
|
|
|
{
|
2017-09-25 18:40:42 +00:00
|
|
|
args: []string{"agent", "-server", "-data-dir", dataDir, "-advertise", "[::]", "-bind", "10.0.0.1"},
|
|
|
|
out: "==> Advertise address cannot be 0.0.0.0, :: or [::]\n",
|
2017-05-08 16:39:53 +00:00
|
|
|
},
|
2017-05-09 07:37:21 +00:00
|
|
|
{
|
2017-09-25 18:40:42 +00:00
|
|
|
args: []string{"agent", "-server", "-data-dir", dataDir, "-advertise-wan", "0.0.0.0", "-bind", "10.0.0.1"},
|
|
|
|
out: "==> Advertise WAN address cannot be 0.0.0.0, :: or [::]\n",
|
2017-05-09 07:37:21 +00:00
|
|
|
},
|
|
|
|
{
|
2017-09-25 18:40:42 +00:00
|
|
|
args: []string{"agent", "-server", "-data-dir", dataDir, "-advertise-wan", "::", "-bind", "10.0.0.1"},
|
|
|
|
out: "==> Advertise WAN address cannot be 0.0.0.0, :: or [::]\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"agent", "-server", "-data-dir", dataDir, "-advertise-wan", "[::]", "-bind", "10.0.0.1"},
|
|
|
|
out: "==> Advertise WAN address cannot be 0.0.0.0, :: or [::]\n",
|
2017-05-09 07:37:21 +00:00
|
|
|
},
|
2017-05-08 16:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(strings.Join(tt.args, " "), func(t *testing.T) {
|
|
|
|
cmd := exec.Command("consul", tt.args...)
|
|
|
|
b, err := cmd.CombinedOutput()
|
|
|
|
if got, want := err, "exit status 1"; got == nil || got.Error() != want {
|
|
|
|
t.Fatalf("got err %q want %q", got, want)
|
|
|
|
}
|
|
|
|
if got, want := string(b), tt.out; got != want {
|
|
|
|
t.Fatalf("got %q want %q", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-12 19:27:03 +00:00
|
|
|
func TestRetryJoin(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := agent.NewTestAgent(t, "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2014-10-12 19:27:03 +00:00
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
b := agent.NewTestAgent(t, `
|
2019-07-12 15:52:26 +00:00
|
|
|
retry_join = ["`+a.Config.SerfBindAddrLAN.String()+`"]
|
|
|
|
retry_join_wan = ["`+a.Config.SerfBindAddrWAN.String()+`"]
|
|
|
|
retry_interval = "100ms"
|
|
|
|
`)
|
|
|
|
defer b.Shutdown()
|
2017-10-24 15:31:02 +00:00
|
|
|
|
2019-07-12 15:52:26 +00:00
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
2017-10-24 15:31:02 +00:00
|
|
|
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2021-10-26 20:08:55 +00:00
|
|
|
if got, want := len(a.LANMembersInAgentPartition()), 2; got != want {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("got %d LAN members want %d", got, want)
|
2014-10-12 19:27:03 +00:00
|
|
|
}
|
2019-07-12 15:52:26 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
2017-05-21 07:11:09 +00:00
|
|
|
if got, want := len(a.WANMembers()), 2; got != want {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("got %d WAN members want %d", got, want)
|
2014-11-19 21:53:17 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2014-10-12 19:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetryJoinFail(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-10-17 14:07:48 +00:00
|
|
|
t.Parallel()
|
2017-05-12 13:41:13 +00:00
|
|
|
tmpDir := testutil.TempDir(t, "consul")
|
2014-10-12 19:27:03 +00:00
|
|
|
|
2021-06-02 21:12:44 +00:00
|
|
|
ui := newCaptureUI()
|
2021-06-02 20:29:29 +00:00
|
|
|
cmd := New(ui)
|
2014-10-12 19:27:03 +00:00
|
|
|
|
|
|
|
args := []string{
|
2017-10-24 15:13:37 +00:00
|
|
|
"-bind", "127.0.0.1",
|
2014-10-12 19:27:03 +00:00
|
|
|
"-data-dir", tmpDir,
|
2017-10-24 15:13:37 +00:00
|
|
|
"-retry-join", "127.0.0.1:99",
|
2014-10-12 19:27:03 +00:00
|
|
|
"-retry-max", "1",
|
2017-04-27 00:42:04 +00:00
|
|
|
"-retry-interval", "10ms",
|
2014-10-12 19:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code == 0 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
}
|
2014-11-14 15:02:42 +00:00
|
|
|
|
2014-11-17 22:14:59 +00:00
|
|
|
func TestRetryJoinWanFail(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-10-17 14:07:48 +00:00
|
|
|
t.Parallel()
|
2017-05-12 13:41:13 +00:00
|
|
|
tmpDir := testutil.TempDir(t, "consul")
|
2014-11-14 15:02:42 +00:00
|
|
|
|
2021-06-02 21:12:44 +00:00
|
|
|
ui := newCaptureUI()
|
2021-06-02 20:29:29 +00:00
|
|
|
cmd := New(ui)
|
2014-11-14 15:02:42 +00:00
|
|
|
|
|
|
|
args := []string{
|
2014-11-19 21:53:17 +00:00
|
|
|
"-server",
|
2017-10-24 15:12:27 +00:00
|
|
|
"-bind", "127.0.0.1",
|
2014-11-14 15:02:42 +00:00
|
|
|
"-data-dir", tmpDir,
|
2017-10-24 15:12:27 +00:00
|
|
|
"-retry-join-wan", "127.0.0.1:99",
|
2014-11-19 21:53:17 +00:00
|
|
|
"-retry-max-wan", "1",
|
2017-04-27 00:42:04 +00:00
|
|
|
"-retry-interval-wan", "10ms",
|
2014-11-14 15:02:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code == 0 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
}
|
2015-01-19 17:54:12 +00:00
|
|
|
|
2015-10-15 21:15:41 +00:00
|
|
|
func TestProtectDataDir(t *testing.T) {
|
2017-10-17 14:07:48 +00:00
|
|
|
t.Parallel()
|
2017-05-12 13:41:13 +00:00
|
|
|
dir := testutil.TempDir(t, "consul")
|
2015-10-15 21:15:41 +00:00
|
|
|
|
|
|
|
if err := os.MkdirAll(filepath.Join(dir, "mdb"), 0700); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-09-25 22:17:12 +00:00
|
|
|
cfgDir := testutil.TempDir(t, "consul-config")
|
|
|
|
|
|
|
|
cfgFilePath := filepath.Join(cfgDir, "consul.json")
|
|
|
|
cfgFile, err := os.Create(cfgFilePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create file %v, got error:%v", cfgFilePath, err)
|
|
|
|
}
|
2015-10-15 21:15:41 +00:00
|
|
|
|
2017-09-25 18:40:42 +00:00
|
|
|
content := fmt.Sprintf(`{"server": true, "bind_addr" : "10.0.0.1", "data_dir": "%s"}`, dir)
|
2017-09-25 22:17:12 +00:00
|
|
|
_, err = cfgFile.Write([]byte(content))
|
2015-10-15 21:15:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-06-02 21:12:44 +00:00
|
|
|
ui := newCaptureUI()
|
2021-06-02 20:29:29 +00:00
|
|
|
cmd := New(ui)
|
2017-10-17 10:55:04 +00:00
|
|
|
args := []string{"-config-file=" + cfgFile.Name()}
|
|
|
|
if code := cmd.Run(args); code == 0 {
|
2015-10-15 21:15:41 +00:00
|
|
|
t.Fatalf("should fail")
|
|
|
|
}
|
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, dir) {
|
|
|
|
t.Fatalf("expected mdb dir error, got: %s", out)
|
|
|
|
}
|
|
|
|
}
|
2016-03-28 16:56:24 +00:00
|
|
|
|
|
|
|
func TestBadDataDirPermissions(t *testing.T) {
|
2017-10-17 14:07:48 +00:00
|
|
|
t.Parallel()
|
2017-05-12 13:41:13 +00:00
|
|
|
dir := testutil.TempDir(t, "consul")
|
2016-03-28 16:56:24 +00:00
|
|
|
dataDir := filepath.Join(dir, "mdb")
|
|
|
|
if err := os.MkdirAll(dataDir, 0400); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-06-02 21:12:44 +00:00
|
|
|
ui := newCaptureUI()
|
2021-06-02 20:29:29 +00:00
|
|
|
cmd := New(ui)
|
2017-10-17 10:55:04 +00:00
|
|
|
args := []string{"-data-dir=" + dataDir, "-server=true", "-bind=10.0.0.1"}
|
|
|
|
if code := cmd.Run(args); code == 0 {
|
2016-03-28 16:56:24 +00:00
|
|
|
t.Fatalf("Should fail with bad data directory permissions")
|
|
|
|
}
|
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Permission denied") {
|
|
|
|
t.Fatalf("expected permission denied error, got: %s", out)
|
|
|
|
}
|
|
|
|
}
|
2021-06-02 21:12:44 +00:00
|
|
|
|
|
|
|
type captureUI struct {
|
2023-02-10 19:12:13 +00:00
|
|
|
*mcli.MockUi
|
2021-06-02 21:12:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *captureUI) Stdout() io.Writer {
|
|
|
|
return c.MockUi.OutputWriter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *captureUI) Stderr() io.Writer {
|
|
|
|
return c.MockUi.ErrorWriter
|
|
|
|
}
|
|
|
|
|
2023-02-10 19:12:13 +00:00
|
|
|
func (c *captureUI) HeaderOutput(s string) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *captureUI) ErrorOutput(s string) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *captureUI) WarnOutput(s string) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *captureUI) SuccessOutput(s string) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *captureUI) UnchangedOutput(s string) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *captureUI) Table(tbl *cli.Table) {
|
|
|
|
}
|
|
|
|
|
2021-06-02 21:12:44 +00:00
|
|
|
func newCaptureUI() *captureUI {
|
2023-02-10 19:12:13 +00:00
|
|
|
return &captureUI{MockUi: mcli.NewMockUi()}
|
2021-06-02 21:12:44 +00:00
|
|
|
}
|