mirror of https://github.com/status-im/consul.git
212 lines
4.1 KiB
Go
212 lines
4.1 KiB
Go
package agent
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/consul/testutil"
|
|
"github.com/mitchellh/cli"
|
|
)
|
|
|
|
func TestCommand_implements(t *testing.T) {
|
|
var _ cli.Command = new(Command)
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
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)
|
|
|
|
serfWanAddr := fmt.Sprintf(
|
|
"%s:%d",
|
|
agent.config.BindAddr,
|
|
agent.config.Ports.SerfWan)
|
|
|
|
args := []string{
|
|
"-server",
|
|
"-data-dir", tmpDir,
|
|
"-node", fmt.Sprintf(`"%s"`, conf2.NodeName),
|
|
"-retry-join", serfAddr,
|
|
"-retry-interval", "1s",
|
|
"-retry-join-wan", serfWanAddr,
|
|
"-retry-interval-wan", "1s",
|
|
}
|
|
|
|
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)
|
|
}
|
|
mem = agent.WANMembers()
|
|
if len(mem) != 2 {
|
|
return false, fmt.Errorf("bad (wan): %#v", mem)
|
|
}
|
|
return true, nil
|
|
}, func(err error) {
|
|
t.Fatalf(err.Error())
|
|
})
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestRetryJoinWanFail(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.SerfWan)
|
|
|
|
args := []string{
|
|
"-server",
|
|
"-data-dir", tmpDir,
|
|
"-retry-join-wan", serfAddr,
|
|
"-retry-max-wan", "1",
|
|
}
|
|
|
|
if code := cmd.Run(args); code == 0 {
|
|
t.Fatalf("bad: %d", code)
|
|
}
|
|
}
|
|
|
|
func TestSetupAgent_UnixSocket_Fails(t *testing.T) {
|
|
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
|
|
|
|
// Set socket address to an existing file. Consul should fail to
|
|
// start and return an error.
|
|
conf.Addresses.RPC = "unix://" + socketPath
|
|
|
|
shutdownCh := make(chan struct{})
|
|
defer close(shutdownCh)
|
|
|
|
cmd := &Command{
|
|
ShutdownCh: shutdownCh,
|
|
Ui: new(cli.MockUi),
|
|
}
|
|
|
|
logWriter := NewLogWriter(512)
|
|
logOutput := new(bytes.Buffer)
|
|
|
|
// Ensure we got an error mentioning the socket file
|
|
err = cmd.setupAgent(conf, logOutput, logWriter)
|
|
if err == nil {
|
|
t.Fatalf("should have failed")
|
|
}
|
|
if !strings.Contains(err.Error(), socketPath) {
|
|
t.Fatalf("expected socket file error, got: %q", err)
|
|
}
|
|
}
|