2013-12-19 20:18:06 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2014-10-12 19:27:03 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2017-05-08 16:39:53 +00:00
|
|
|
"os/exec"
|
2015-10-15 21:15:41 +00:00
|
|
|
"path/filepath"
|
2017-02-28 21:41:09 +00:00
|
|
|
"reflect"
|
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
|
|
|
|
2017-02-25 02:11:05 +00:00
|
|
|
"github.com/hashicorp/consul/command/base"
|
2017-05-12 13:41:13 +00:00
|
|
|
"github.com/hashicorp/consul/testutil"
|
2017-04-29 16:34:02 +00:00
|
|
|
"github.com/hashicorp/consul/testutil/retry"
|
2017-03-21 23:36:44 +00:00
|
|
|
"github.com/hashicorp/consul/version"
|
2014-10-12 19:27:03 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2013-12-19 20:18:06 +00:00
|
|
|
)
|
|
|
|
|
2017-02-25 02:11:05 +00:00
|
|
|
func baseCommand(ui *cli.MockUi) base.Command {
|
|
|
|
return base.Command{
|
|
|
|
Flags: base.FlagSetNone,
|
2017-04-21 00:02:42 +00:00
|
|
|
UI: ui,
|
2017-02-25 02:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-19 20:18:06 +00:00
|
|
|
func TestCommand_implements(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2013-12-19 20:18:06 +00:00
|
|
|
var _ cli.Command = new(Command)
|
|
|
|
}
|
2014-06-09 18:57:15 +00:00
|
|
|
|
|
|
|
func TestValidDatacenter(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2014-06-09 18:57:15 +00:00
|
|
|
shouldMatch := []string{
|
|
|
|
"dc1",
|
|
|
|
"east-aws-001",
|
|
|
|
"PROD_aws01-small",
|
|
|
|
}
|
|
|
|
noMatch := []string{
|
|
|
|
"east.aws",
|
|
|
|
"east!aws",
|
|
|
|
"first,second",
|
|
|
|
}
|
|
|
|
for _, m := range shouldMatch {
|
|
|
|
if !validDatacenter.MatchString(m) {
|
|
|
|
t.Fatalf("expected match: %s", m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, m := range noMatch {
|
|
|
|
if validDatacenter.MatchString(m) {
|
|
|
|
t.Fatalf("expected no match: %s", m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-12 19:27:03 +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) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-08 16:39:53 +00:00
|
|
|
tests := []struct {
|
|
|
|
args []string
|
|
|
|
out string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
args: []string{"agent", "-server", "-data-dir", "foo", "-advertise", "0.0.0.0"},
|
|
|
|
out: "==> Advertise address cannot be 0.0.0.0\n",
|
|
|
|
},
|
2017-05-09 07:37:21 +00:00
|
|
|
{
|
|
|
|
args: []string{"agent", "-server", "-data-dir", "foo", "-advertise", "::"},
|
|
|
|
out: "==> Advertise address cannot be ::\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"agent", "-server", "-data-dir", "foo", "-advertise", "[::]"},
|
|
|
|
out: "==> Advertise address cannot be [::]\n",
|
|
|
|
},
|
2017-05-08 16:39:53 +00:00
|
|
|
{
|
|
|
|
args: []string{"agent", "-server", "-data-dir", "foo", "-advertise-wan", "0.0.0.0"},
|
|
|
|
out: "==> Advertise WAN address cannot be 0.0.0.0\n",
|
|
|
|
},
|
2017-05-09 07:37:21 +00:00
|
|
|
{
|
|
|
|
args: []string{"agent", "-server", "-data-dir", "foo", "-advertise-wan", "::"},
|
|
|
|
out: "==> Advertise WAN address cannot be ::\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"agent", "-server", "-data-dir", "foo", "-advertise-wan", "[::]"},
|
|
|
|
out: "==> Advertise WAN address cannot be [::]\n",
|
|
|
|
},
|
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) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2014-10-12 19:27:03 +00:00
|
|
|
|
2017-05-22 11:03:59 +00:00
|
|
|
cfg2 := TestConfig()
|
2017-05-12 13:41:13 +00:00
|
|
|
tmpDir := testutil.TempDir(t, "consul")
|
2014-10-12 19:27:03 +00:00
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
close(shutdownCh)
|
|
|
|
<-doneCh
|
|
|
|
}()
|
|
|
|
|
|
|
|
cmd := &Command{
|
2017-03-21 23:36:44 +00:00
|
|
|
Version: version.Version,
|
2014-10-12 19:27:03 +00:00
|
|
|
ShutdownCh: shutdownCh,
|
2017-05-22 23:26:30 +00:00
|
|
|
Command: baseCommand(cli.NewMockUi()),
|
2014-10-12 19:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
serfAddr := fmt.Sprintf(
|
|
|
|
"%s:%d",
|
2017-05-21 07:11:09 +00:00
|
|
|
a.Config.BindAddr,
|
|
|
|
a.Config.Ports.SerfLan)
|
2014-10-12 19:27:03 +00:00
|
|
|
|
2014-11-19 21:53:17 +00:00
|
|
|
serfWanAddr := fmt.Sprintf(
|
|
|
|
"%s:%d",
|
2017-05-21 07:11:09 +00:00
|
|
|
a.Config.BindAddr,
|
|
|
|
a.Config.Ports.SerfWan)
|
2014-11-19 21:53:17 +00:00
|
|
|
|
2014-10-12 19:27:03 +00:00
|
|
|
args := []string{
|
2014-11-19 21:53:17 +00:00
|
|
|
"-server",
|
2017-05-21 07:11:09 +00:00
|
|
|
"-bind", a.Config.BindAddr,
|
2014-10-12 19:27:03 +00:00
|
|
|
"-data-dir", tmpDir,
|
2017-05-22 11:03:59 +00:00
|
|
|
"-node", fmt.Sprintf(`"%s"`, cfg2.NodeName),
|
2017-05-21 07:11:09 +00:00
|
|
|
"-advertise", a.Config.BindAddr,
|
2014-10-12 19:27:03 +00:00
|
|
|
"-retry-join", serfAddr,
|
|
|
|
"-retry-interval", "1s",
|
2014-11-19 21:53:17 +00:00
|
|
|
"-retry-join-wan", serfWanAddr,
|
|
|
|
"-retry-interval-wan", "1s",
|
2014-10-12 19:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if code := cmd.Run(args); code != 0 {
|
|
|
|
log.Printf("bad: %d", code)
|
|
|
|
}
|
|
|
|
close(doneCh)
|
|
|
|
}()
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2017-05-21 07:11:09 +00:00
|
|
|
if got, want := len(a.LANMembers()), 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
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2015-03-28 14:48:06 +00:00
|
|
|
func TestReadCliConfig(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-12 13:41:13 +00:00
|
|
|
tmpDir := testutil.TempDir(t, "consul")
|
2016-04-01 00:45:14 +00:00
|
|
|
defer os.RemoveAll(tmpDir)
|
2015-03-28 14:48:06 +00:00
|
|
|
|
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
defer close(shutdownCh)
|
|
|
|
|
2016-03-31 21:47:55 +00:00
|
|
|
// Test config parse
|
|
|
|
{
|
|
|
|
cmd := &Command{
|
|
|
|
args: []string{
|
|
|
|
"-data-dir", tmpDir,
|
|
|
|
"-node", `"a"`,
|
|
|
|
"-advertise-wan", "1.2.3.4",
|
2016-11-03 19:58:58 +00:00
|
|
|
"-serf-wan-bind", "4.3.2.1",
|
|
|
|
"-serf-lan-bind", "4.3.2.2",
|
2017-01-11 21:07:11 +00:00
|
|
|
"-node-meta", "somekey:somevalue",
|
2016-03-31 21:47:55 +00:00
|
|
|
},
|
|
|
|
ShutdownCh: shutdownCh,
|
2017-05-22 23:26:30 +00:00
|
|
|
Command: baseCommand(cli.NewMockUi()),
|
2016-03-31 21:47:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config := cmd.readConfig()
|
|
|
|
if config.AdvertiseAddrWan != "1.2.3.4" {
|
|
|
|
t.Fatalf("expected -advertise-addr-wan 1.2.3.4 got %s", config.AdvertiseAddrWan)
|
|
|
|
}
|
2016-11-03 19:58:58 +00:00
|
|
|
if config.SerfWanBindAddr != "4.3.2.1" {
|
|
|
|
t.Fatalf("expected -serf-wan-bind 4.3.2.1 got %s", config.SerfWanBindAddr)
|
|
|
|
}
|
|
|
|
if config.SerfLanBindAddr != "4.3.2.2" {
|
|
|
|
t.Fatalf("expected -serf-lan-bind 4.3.2.2 got %s", config.SerfLanBindAddr)
|
|
|
|
}
|
2017-01-11 21:07:11 +00:00
|
|
|
if len(config.Meta) != 1 || config.Meta["somekey"] != "somevalue" {
|
|
|
|
t.Fatalf("expected somekey=somevalue, got %v", config.Meta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test multiple node meta flags
|
|
|
|
{
|
|
|
|
cmd := &Command{
|
|
|
|
args: []string{
|
|
|
|
"-data-dir", tmpDir,
|
|
|
|
"-node-meta", "somekey:somevalue",
|
|
|
|
"-node-meta", "otherkey:othervalue",
|
|
|
|
},
|
|
|
|
ShutdownCh: shutdownCh,
|
2017-05-22 23:26:30 +00:00
|
|
|
Command: baseCommand(cli.NewMockUi()),
|
2017-01-11 21:07:11 +00:00
|
|
|
}
|
|
|
|
expected := map[string]string{
|
|
|
|
"somekey": "somevalue",
|
|
|
|
"otherkey": "othervalue",
|
|
|
|
}
|
|
|
|
config := cmd.readConfig()
|
|
|
|
if !reflect.DeepEqual(config.Meta, expected) {
|
|
|
|
t.Fatalf("bad: %v %v", config.Meta, expected)
|
|
|
|
}
|
2015-03-28 14:48:06 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 06:39:11 +00:00
|
|
|
// Test LeaveOnTerm and SkipLeaveOnInt defaults for server mode
|
2016-03-31 21:47:55 +00:00
|
|
|
{
|
2017-05-22 23:26:30 +00:00
|
|
|
ui := cli.NewMockUi()
|
2016-03-31 21:47:55 +00:00
|
|
|
cmd := &Command{
|
|
|
|
args: []string{
|
2016-04-01 00:45:14 +00:00
|
|
|
"-node", `"server1"`,
|
|
|
|
"-server",
|
|
|
|
"-data-dir", tmpDir,
|
2016-03-31 21:47:55 +00:00
|
|
|
},
|
|
|
|
ShutdownCh: shutdownCh,
|
2017-02-25 02:11:05 +00:00
|
|
|
Command: baseCommand(ui),
|
2016-04-01 00:45:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config := cmd.readConfig()
|
|
|
|
if config == nil {
|
|
|
|
t.Fatalf(`Expected non-nil config object: %s`, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
if config.Server != true {
|
|
|
|
t.Errorf(`Expected -server to be true`)
|
|
|
|
}
|
2016-09-01 06:39:11 +00:00
|
|
|
if (*config.LeaveOnTerm) != false {
|
|
|
|
t.Errorf(`Expected LeaveOnTerm to be false in server mode`)
|
|
|
|
}
|
2016-04-01 00:45:14 +00:00
|
|
|
if (*config.SkipLeaveOnInt) != true {
|
|
|
|
t.Errorf(`Expected SkipLeaveOnInt to be true in server mode`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-01 06:39:11 +00:00
|
|
|
// Test LeaveOnTerm and SkipLeaveOnInt defaults for client mode
|
2016-04-01 00:45:14 +00:00
|
|
|
{
|
2017-05-22 23:26:30 +00:00
|
|
|
ui := cli.NewMockUi()
|
2016-04-01 00:45:14 +00:00
|
|
|
cmd := &Command{
|
|
|
|
args: []string{
|
|
|
|
"-data-dir", tmpDir,
|
|
|
|
"-node", `"client"`,
|
|
|
|
},
|
|
|
|
ShutdownCh: shutdownCh,
|
2017-02-25 02:11:05 +00:00
|
|
|
Command: baseCommand(ui),
|
2016-04-01 00:45:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config := cmd.readConfig()
|
|
|
|
if config == nil {
|
|
|
|
t.Fatalf(`Expected non-nil config object: %s`, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
if config.Server != false {
|
|
|
|
t.Errorf(`Expected server to be false`)
|
|
|
|
}
|
2016-09-01 06:39:11 +00:00
|
|
|
if (*config.LeaveOnTerm) != true {
|
|
|
|
t.Errorf(`Expected LeaveOnTerm to be true in client mode`)
|
|
|
|
}
|
2016-04-01 00:45:14 +00:00
|
|
|
if *config.SkipLeaveOnInt != false {
|
|
|
|
t.Errorf(`Expected SkipLeaveOnInt to be false in client mode`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test empty node name
|
|
|
|
{
|
|
|
|
cmd := &Command{
|
|
|
|
args: []string{"-node", `""`},
|
|
|
|
ShutdownCh: shutdownCh,
|
2017-05-22 23:26:30 +00:00
|
|
|
Command: baseCommand(cli.NewMockUi()),
|
2016-03-31 21:47:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config := cmd.readConfig()
|
|
|
|
if config != nil {
|
|
|
|
t.Errorf(`Expected -node="" to fail`)
|
|
|
|
}
|
2015-03-28 14:48:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-12 19:27:03 +00:00
|
|
|
func TestRetryJoinFail(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-22 11:03:59 +00:00
|
|
|
cfg := TestConfig()
|
2017-05-12 13:41:13 +00:00
|
|
|
tmpDir := testutil.TempDir(t, "consul")
|
2014-10-12 19:27:03 +00:00
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
defer close(shutdownCh)
|
|
|
|
|
|
|
|
cmd := &Command{
|
|
|
|
ShutdownCh: shutdownCh,
|
2017-05-22 23:26:30 +00:00
|
|
|
Command: baseCommand(cli.NewMockUi()),
|
2014-10-12 19:27:03 +00:00
|
|
|
}
|
|
|
|
|
2017-05-22 11:03:59 +00:00
|
|
|
serfAddr := fmt.Sprintf("%s:%d", cfg.BindAddr, cfg.Ports.SerfLan)
|
2014-10-12 19:27:03 +00:00
|
|
|
|
|
|
|
args := []string{
|
2017-05-22 11:03:59 +00:00
|
|
|
"-bind", cfg.BindAddr,
|
2014-10-12 19:27:03 +00:00
|
|
|
"-data-dir", tmpDir,
|
|
|
|
"-retry-join", serfAddr,
|
|
|
|
"-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) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-22 11:03:59 +00:00
|
|
|
cfg := TestConfig()
|
2017-05-12 13:41:13 +00:00
|
|
|
tmpDir := testutil.TempDir(t, "consul")
|
2014-11-14 15:02:42 +00:00
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
defer close(shutdownCh)
|
|
|
|
|
|
|
|
cmd := &Command{
|
|
|
|
ShutdownCh: shutdownCh,
|
2017-05-22 23:26:30 +00:00
|
|
|
Command: baseCommand(cli.NewMockUi()),
|
2014-11-14 15:02:42 +00:00
|
|
|
}
|
|
|
|
|
2017-05-22 11:03:59 +00:00
|
|
|
serfAddr := fmt.Sprintf("%s:%d", cfg.BindAddr, cfg.Ports.SerfWan)
|
2014-11-14 15:02:42 +00:00
|
|
|
|
|
|
|
args := []string{
|
2014-11-19 21:53:17 +00:00
|
|
|
"-server",
|
2017-05-22 11:03:59 +00:00
|
|
|
"-bind", cfg.BindAddr,
|
2014-11-14 15:02:42 +00:00
|
|
|
"-data-dir", tmpDir,
|
2014-11-17 22:14:59 +00:00
|
|
|
"-retry-join-wan", serfAddr,
|
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
|
|
|
|
2016-11-01 18:45:42 +00:00
|
|
|
func TestDiscoverEC2Hosts(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2016-11-01 22:20:23 +00:00
|
|
|
if os.Getenv("AWS_REGION") == "" {
|
|
|
|
t.Skip("AWS_REGION not set, skipping")
|
|
|
|
}
|
|
|
|
|
2016-11-01 18:45:42 +00:00
|
|
|
if os.Getenv("AWS_ACCESS_KEY_ID") == "" {
|
|
|
|
t.Skip("AWS_ACCESS_KEY_ID not set, skipping")
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("AWS_SECRET_ACCESS_KEY") == "" {
|
|
|
|
t.Skip("AWS_SECRET_ACCESS_KEY not set, skipping")
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &Config{
|
2016-11-02 18:35:37 +00:00
|
|
|
RetryJoinEC2: RetryJoinEC2{
|
2016-11-02 01:20:08 +00:00
|
|
|
Region: os.Getenv("AWS_REGION"),
|
|
|
|
TagKey: "ConsulRole",
|
|
|
|
TagValue: "Server",
|
2016-11-01 18:45:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-11-02 18:35:37 +00:00
|
|
|
servers, err := c.discoverEc2Hosts(&log.Logger{})
|
2016-11-01 18:45:42 +00:00
|
|
|
if err != nil {
|
2016-12-05 08:07:53 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(servers) != 3 {
|
|
|
|
t.Fatalf("bad: %v", servers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDiscoverGCEHosts(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2016-12-05 08:07:53 +00:00
|
|
|
if os.Getenv("GCE_PROJECT") == "" {
|
|
|
|
t.Skip("GCE_PROJECT not set, skipping")
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("GOOGLE_APPLICATION_CREDENTIALS") == "" && os.Getenv("GCE_CONFIG_CREDENTIALS") == "" {
|
|
|
|
t.Skip("GOOGLE_APPLICATION_CREDENTIALS or GCE_CONFIG_CREDENTIALS not set, skipping")
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &Config{
|
|
|
|
RetryJoinGCE: RetryJoinGCE{
|
|
|
|
ProjectName: os.Getenv("GCE_PROJECT"),
|
|
|
|
ZonePattern: os.Getenv("GCE_ZONE"),
|
|
|
|
TagValue: "consulrole-server",
|
|
|
|
CredentialsFile: os.Getenv("GCE_CONFIG_CREDENTIALS"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
servers, err := c.discoverGCEHosts(log.New(os.Stderr, "", log.LstdFlags))
|
|
|
|
if err != nil {
|
2016-11-01 18:45:42 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(servers) != 3 {
|
|
|
|
t.Fatalf("bad: %v", servers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-24 07:19:23 +00:00
|
|
|
func TestDiscoverAzureHosts(t *testing.T) {
|
|
|
|
subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID")
|
|
|
|
tenantID := os.Getenv("ARM_TENANT_ID")
|
|
|
|
clientID := os.Getenv("ARM_CLIENT_ID")
|
|
|
|
clientSecret := os.Getenv("ARM_CLIENT_SECRET")
|
|
|
|
environment := os.Getenv("ARM_ENVIRONMENT")
|
|
|
|
|
|
|
|
if subscriptionID == "" || clientID == "" || clientSecret == "" || tenantID == "" {
|
|
|
|
t.Skip("ARM_SUBSCRIPTION_ID, ARM_CLIENT_ID, ARM_CLIENT_SECRET and ARM_TENANT_ID " +
|
|
|
|
"must be set to test Discover Azure Hosts")
|
|
|
|
}
|
|
|
|
|
|
|
|
if environment == "" {
|
|
|
|
t.Log("Environments other than Public not supported at the moment")
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &Config{
|
|
|
|
RetryJoinAzure: RetryJoinAzure{
|
|
|
|
SubscriptionID: subscriptionID,
|
|
|
|
ClientID: clientID,
|
|
|
|
SecretAccessKey: clientSecret,
|
|
|
|
TenantID: tenantID,
|
|
|
|
TagName: "type",
|
|
|
|
TagValue: "Foundation",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
servers, err := c.discoverAzureHosts(log.New(os.Stderr, "", log.LstdFlags))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(servers) != 3 {
|
|
|
|
t.Fatalf("bad: %v", servers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-15 21:15:41 +00:00
|
|
|
func TestProtectDataDir(t *testing.T) {
|
2017-05-21 07:54:40 +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
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
if err := os.MkdirAll(filepath.Join(dir, "mdb"), 0700); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-12 13:41:13 +00:00
|
|
|
cfgFile := testutil.TempFile(t, "consul")
|
2015-10-15 21:15:41 +00:00
|
|
|
defer os.Remove(cfgFile.Name())
|
|
|
|
|
|
|
|
content := fmt.Sprintf(`{"server": true, "data_dir": "%s"}`, dir)
|
2017-05-12 13:41:13 +00:00
|
|
|
_, err := cfgFile.Write([]byte(content))
|
2015-10-15 21:15:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-22 23:26:30 +00:00
|
|
|
ui := cli.NewMockUi()
|
2015-10-15 21:15:41 +00:00
|
|
|
cmd := &Command{
|
2017-02-25 02:11:05 +00:00
|
|
|
Command: baseCommand(ui),
|
|
|
|
args: []string{"-config-file=" + cfgFile.Name()},
|
2015-10-15 21:15:41 +00:00
|
|
|
}
|
|
|
|
if conf := cmd.readConfig(); conf != nil {
|
|
|
|
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-05-21 07:54:40 +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
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
dataDir := filepath.Join(dir, "mdb")
|
|
|
|
if err := os.MkdirAll(dataDir, 0400); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dataDir)
|
|
|
|
|
2017-05-22 23:26:30 +00:00
|
|
|
ui := cli.NewMockUi()
|
2016-03-28 16:56:24 +00:00
|
|
|
cmd := &Command{
|
2017-02-25 02:11:05 +00:00
|
|
|
Command: baseCommand(ui),
|
|
|
|
args: []string{"-data-dir=" + dataDir, "-server=true"},
|
2016-03-28 16:56:24 +00:00
|
|
|
}
|
|
|
|
if conf := cmd.readConfig(); conf != nil {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|