2013-12-19 20:18:06 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2015-01-19 17:54:12 +00:00
|
|
|
"bytes"
|
2014-10-12 19:27:03 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2013-12-19 20:18:06 +00:00
|
|
|
"testing"
|
2014-10-12 19:27:03 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/testutil"
|
|
|
|
"github.com/mitchellh/cli"
|
2013-12-19 20:18:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCommand_implements(t *testing.T) {
|
|
|
|
var _ cli.Command = new(Command)
|
|
|
|
}
|
2014-06-09 18:57:15 +00:00
|
|
|
|
|
|
|
func TestValidDatacenter(t *testing.T) {
|
|
|
|
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
|
|
|
|
|
|
|
func TestRetryJoin(t *testing.T) {
|
|
|
|
dir, agent := makeAgent(t, nextConfig())
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
conf2 := nextConfig()
|
|
|
|
tmpDir, err := ioutil.TempDir("", "consul")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
close(shutdownCh)
|
|
|
|
<-doneCh
|
|
|
|
}()
|
|
|
|
|
|
|
|
cmd := &Command{
|
|
|
|
ShutdownCh: shutdownCh,
|
|
|
|
Ui: new(cli.MockUi),
|
|
|
|
}
|
|
|
|
|
|
|
|
serfAddr := fmt.Sprintf(
|
|
|
|
"%s:%d",
|
|
|
|
agent.config.BindAddr,
|
|
|
|
agent.config.Ports.SerfLan)
|
|
|
|
|
2014-11-19 21:53:17 +00:00
|
|
|
serfWanAddr := fmt.Sprintf(
|
|
|
|
"%s:%d",
|
|
|
|
agent.config.BindAddr,
|
|
|
|
agent.config.Ports.SerfWan)
|
|
|
|
|
2014-10-12 19:27:03 +00:00
|
|
|
args := []string{
|
2014-11-19 21:53:17 +00:00
|
|
|
"-server",
|
2014-10-12 19:27:03 +00:00
|
|
|
"-data-dir", tmpDir,
|
2014-10-15 00:49:17 +00:00
|
|
|
"-node", fmt.Sprintf(`"%s"`, conf2.NodeName),
|
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)
|
|
|
|
}()
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
mem := agent.LANMembers()
|
|
|
|
if len(mem) != 2 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", mem)
|
|
|
|
}
|
2014-11-19 21:53:17 +00:00
|
|
|
mem = agent.WANMembers()
|
|
|
|
if len(mem) != 2 {
|
|
|
|
return false, fmt.Errorf("bad (wan): %#v", mem)
|
|
|
|
}
|
2014-10-12 19:27:03 +00:00
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-03-28 14:48:06 +00:00
|
|
|
func TestReadCliConfig(t *testing.T) {
|
|
|
|
|
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
defer close(shutdownCh)
|
|
|
|
|
|
|
|
tmpDir, err := ioutil.TempDir("", "consul")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := &Command{
|
|
|
|
args: []string{
|
|
|
|
"-data-dir", tmpDir,
|
|
|
|
"-node", `"a"`,
|
|
|
|
"-advertise-wan", "1.2.3.4",
|
|
|
|
},
|
|
|
|
ShutdownCh: shutdownCh,
|
|
|
|
Ui: new(cli.MockUi),
|
|
|
|
}
|
|
|
|
|
|
|
|
config := cmd.readConfig()
|
|
|
|
if config.AdvertiseAddrWan != "1.2.3.4" {
|
|
|
|
t.Fatalf("expected -advertise-addr-wan 1.2.3.4 got %s", config.AdvertiseAddrWan)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-12 19:27:03 +00:00
|
|
|
func TestRetryJoinFail(t *testing.T) {
|
|
|
|
conf := nextConfig()
|
|
|
|
tmpDir, err := ioutil.TempDir("", "consul")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
defer close(shutdownCh)
|
|
|
|
|
|
|
|
cmd := &Command{
|
|
|
|
ShutdownCh: shutdownCh,
|
|
|
|
Ui: new(cli.MockUi),
|
|
|
|
}
|
|
|
|
|
|
|
|
serfAddr := fmt.Sprintf("%s:%d", conf.BindAddr, conf.Ports.SerfLan)
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-data-dir", tmpDir,
|
|
|
|
"-retry-join", serfAddr,
|
|
|
|
"-retry-max", "1",
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2014-11-14 15:02:42 +00:00
|
|
|
conf := nextConfig()
|
|
|
|
tmpDir, err := ioutil.TempDir("", "consul")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
defer close(shutdownCh)
|
|
|
|
|
|
|
|
cmd := &Command{
|
|
|
|
ShutdownCh: shutdownCh,
|
|
|
|
Ui: new(cli.MockUi),
|
|
|
|
}
|
|
|
|
|
|
|
|
serfAddr := fmt.Sprintf("%s:%d", conf.BindAddr, conf.Ports.SerfWan)
|
|
|
|
|
|
|
|
args := []string{
|
2014-11-19 21:53:17 +00:00
|
|
|
"-server",
|
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",
|
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-01-20 22:13:36 +00:00
|
|
|
func TestSetupAgent_RPCUnixSocket_FileExists(t *testing.T) {
|
2015-01-19 17:54:12 +00:00
|
|
|
conf := nextConfig()
|
|
|
|
tmpDir, err := ioutil.TempDir("", "consul")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
tmpFile, err := ioutil.TempFile("", "consul")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tmpFile.Name())
|
|
|
|
socketPath := tmpFile.Name()
|
|
|
|
|
|
|
|
conf.DataDir = tmpDir
|
|
|
|
conf.Server = true
|
|
|
|
conf.Bootstrap = true
|
|
|
|
|
2015-01-20 22:13:36 +00:00
|
|
|
// Set socket address to an existing file.
|
2015-01-19 17:54:12 +00:00
|
|
|
conf.Addresses.RPC = "unix://" + socketPath
|
|
|
|
|
2015-01-21 00:57:00 +00:00
|
|
|
// Custom mode for socket file
|
2015-01-21 02:53:18 +00:00
|
|
|
conf.UnixSockets.Perms = "0777"
|
2015-01-21 00:57:00 +00:00
|
|
|
|
2015-01-19 17:54:12 +00:00
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
defer close(shutdownCh)
|
|
|
|
|
|
|
|
cmd := &Command{
|
|
|
|
ShutdownCh: shutdownCh,
|
|
|
|
Ui: new(cli.MockUi),
|
|
|
|
}
|
|
|
|
|
|
|
|
logWriter := NewLogWriter(512)
|
|
|
|
logOutput := new(bytes.Buffer)
|
|
|
|
|
2015-01-20 22:13:36 +00:00
|
|
|
// Ensure the server is created
|
|
|
|
if err := cmd.setupAgent(conf, logOutput, logWriter); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the file was replaced by the socket
|
|
|
|
fi, err := os.Stat(socketPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
2015-01-19 17:54:12 +00:00
|
|
|
}
|
2015-01-20 22:13:36 +00:00
|
|
|
if fi.Mode()&os.ModeSocket == 0 {
|
|
|
|
t.Fatalf("expected socket to replace file")
|
2015-01-19 17:54:12 +00:00
|
|
|
}
|
2015-01-21 00:57:00 +00:00
|
|
|
|
|
|
|
// Ensure permissions were applied to the socket file
|
|
|
|
if fi.Mode().String() != "Srwxrwxrwx" {
|
|
|
|
t.Fatalf("bad permissions: %s", fi.Mode())
|
|
|
|
}
|
2015-01-19 17:54:12 +00:00
|
|
|
}
|