2018-04-18 16:18:18 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2018-05-25 21:12:13 +00:00
|
|
|
"time"
|
2018-04-18 16:18:18 +00:00
|
|
|
|
2018-06-13 22:33:25 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2018-04-18 16:18:18 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2018-05-04 20:21:17 +00:00
|
|
|
type MockDiscover struct {
|
|
|
|
ReceivedAddrs string
|
|
|
|
}
|
|
|
|
|
|
|
|
const stubAddress = "127.0.0.1"
|
2018-04-18 22:25:11 +00:00
|
|
|
|
|
|
|
func (m *MockDiscover) Addrs(s string, l *log.Logger) ([]string, error) {
|
2018-05-04 20:21:17 +00:00
|
|
|
m.ReceivedAddrs = s
|
|
|
|
return []string{stubAddress}, nil
|
2018-04-18 22:25:11 +00:00
|
|
|
}
|
|
|
|
func (m *MockDiscover) Help() string { return "" }
|
|
|
|
func (m *MockDiscover) Names() []string {
|
|
|
|
return []string{""}
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:18:18 +00:00
|
|
|
func TestRetryJoin_Integration(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-05-30 23:51:55 +00:00
|
|
|
|
|
|
|
// Create two agents and have one retry join the other
|
2018-04-18 16:18:18 +00:00
|
|
|
agent := NewTestAgent(t, t.Name(), nil)
|
|
|
|
defer agent.Shutdown()
|
|
|
|
|
2018-05-30 23:51:55 +00:00
|
|
|
agent2 := NewTestAgent(t, t.Name(), func(c *Config) {
|
|
|
|
c.NodeName = "foo"
|
|
|
|
if c.Server.ServerJoin == nil {
|
|
|
|
c.Server.ServerJoin = &ServerJoin{}
|
|
|
|
}
|
|
|
|
c.Server.ServerJoin.RetryJoin = []string{agent.Config.normalizedAddrs.Serf}
|
|
|
|
c.Server.ServerJoin.RetryInterval = 1 * time.Second
|
|
|
|
})
|
|
|
|
defer agent2.Shutdown()
|
2018-04-18 16:18:18 +00:00
|
|
|
|
2018-05-30 23:51:55 +00:00
|
|
|
// Create a fake command and have it wrap the second agent and run the retry
|
|
|
|
// join handler
|
2018-04-18 16:18:18 +00:00
|
|
|
cmd := &Command{
|
|
|
|
Ui: &cli.BasicUi{
|
|
|
|
Reader: os.Stdin,
|
|
|
|
Writer: os.Stdout,
|
|
|
|
ErrorWriter: os.Stderr,
|
|
|
|
},
|
2018-05-30 23:51:55 +00:00
|
|
|
agent: agent2.Agent,
|
2018-04-18 16:18:18 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 23:51:55 +00:00
|
|
|
if err := cmd.handleRetryJoin(agent2.Config); err != nil {
|
|
|
|
t.Fatalf("handleRetryJoin failed: %v", err)
|
2018-04-18 16:18:18 +00:00
|
|
|
}
|
|
|
|
|
2018-06-01 21:53:08 +00:00
|
|
|
// Ensure the retry join occurred.
|
2018-04-18 16:18:18 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
mem := agent.server.Members()
|
|
|
|
if len(mem) != 2 {
|
|
|
|
return false, fmt.Errorf("bad :%#v", mem)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-05-21 22:41:28 +00:00
|
|
|
func TestRetryJoin_Server_NonCloud(t *testing.T) {
|
2018-04-18 16:18:18 +00:00
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
2018-05-11 19:52:05 +00:00
|
|
|
serverJoin := &ServerJoin{
|
|
|
|
RetryMaxAttempts: 1,
|
|
|
|
RetryJoin: []string{"127.0.0.1"},
|
2018-04-18 16:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var output []string
|
|
|
|
|
|
|
|
mockJoin := func(s []string) (int, error) {
|
|
|
|
output = s
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
joiner := retryJoiner{
|
2018-05-11 19:52:05 +00:00
|
|
|
discover: &MockDiscover{},
|
|
|
|
serverJoin: mockJoin,
|
|
|
|
serverEnabled: true,
|
2018-09-13 17:43:40 +00:00
|
|
|
logger: testlog.HCLogger(t),
|
2018-05-11 19:52:05 +00:00
|
|
|
errCh: make(chan struct{}),
|
2018-04-18 16:18:18 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 19:52:05 +00:00
|
|
|
joiner.RetryJoin(serverJoin)
|
2018-04-18 16:18:18 +00:00
|
|
|
|
|
|
|
require.Equal(1, len(output))
|
2018-05-04 20:21:17 +00:00
|
|
|
require.Equal(stubAddress, output[0])
|
2018-04-18 16:18:18 +00:00
|
|
|
}
|
2018-04-18 22:09:46 +00:00
|
|
|
|
2018-05-21 22:41:28 +00:00
|
|
|
func TestRetryJoin_Server_Cloud(t *testing.T) {
|
2018-04-18 22:09:46 +00:00
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
2018-05-11 19:52:05 +00:00
|
|
|
serverJoin := &ServerJoin{
|
|
|
|
RetryMaxAttempts: 1,
|
|
|
|
RetryJoin: []string{"provider=aws, tag_value=foo"},
|
2018-04-18 22:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var output []string
|
|
|
|
|
|
|
|
mockJoin := func(s []string) (int, error) {
|
|
|
|
output = s
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2018-05-04 20:21:17 +00:00
|
|
|
mockDiscover := &MockDiscover{}
|
2018-04-18 22:09:46 +00:00
|
|
|
joiner := retryJoiner{
|
2018-05-11 19:52:05 +00:00
|
|
|
discover: mockDiscover,
|
|
|
|
serverJoin: mockJoin,
|
|
|
|
serverEnabled: true,
|
2018-09-13 17:43:40 +00:00
|
|
|
logger: testlog.HCLogger(t),
|
2018-05-11 19:52:05 +00:00
|
|
|
errCh: make(chan struct{}),
|
2018-04-18 22:09:46 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 19:52:05 +00:00
|
|
|
joiner.RetryJoin(serverJoin)
|
2018-04-18 22:09:46 +00:00
|
|
|
|
|
|
|
require.Equal(1, len(output))
|
2018-05-04 20:21:17 +00:00
|
|
|
require.Equal("provider=aws, tag_value=foo", mockDiscover.ReceivedAddrs)
|
|
|
|
require.Equal(stubAddress, output[0])
|
2018-04-18 22:09:46 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 22:41:28 +00:00
|
|
|
func TestRetryJoin_Server_MixedProvider(t *testing.T) {
|
2018-04-18 22:09:46 +00:00
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
2018-05-11 19:52:05 +00:00
|
|
|
serverJoin := &ServerJoin{
|
|
|
|
RetryMaxAttempts: 1,
|
|
|
|
RetryJoin: []string{"provider=aws, tag_value=foo", "127.0.0.1"},
|
2018-04-18 22:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var output []string
|
|
|
|
|
|
|
|
mockJoin := func(s []string) (int, error) {
|
|
|
|
output = s
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2018-05-04 20:21:17 +00:00
|
|
|
mockDiscover := &MockDiscover{}
|
2018-04-18 22:09:46 +00:00
|
|
|
joiner := retryJoiner{
|
2018-05-11 19:52:05 +00:00
|
|
|
discover: mockDiscover,
|
|
|
|
serverJoin: mockJoin,
|
|
|
|
serverEnabled: true,
|
2018-09-13 17:43:40 +00:00
|
|
|
logger: testlog.HCLogger(t),
|
2018-05-11 19:52:05 +00:00
|
|
|
errCh: make(chan struct{}),
|
2018-04-18 22:09:46 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 19:52:05 +00:00
|
|
|
joiner.RetryJoin(serverJoin)
|
2018-04-18 22:09:46 +00:00
|
|
|
|
|
|
|
require.Equal(2, len(output))
|
2018-05-04 20:21:17 +00:00
|
|
|
require.Equal("provider=aws, tag_value=foo", mockDiscover.ReceivedAddrs)
|
|
|
|
require.Equal(stubAddress, output[0])
|
2018-04-18 22:09:46 +00:00
|
|
|
}
|
2018-05-11 19:52:05 +00:00
|
|
|
|
|
|
|
func TestRetryJoin_Client(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
serverJoin := &ServerJoin{
|
|
|
|
RetryMaxAttempts: 1,
|
|
|
|
RetryJoin: []string{"127.0.0.1"},
|
|
|
|
}
|
|
|
|
|
|
|
|
var output []string
|
|
|
|
|
|
|
|
mockJoin := func(s []string) (int, error) {
|
|
|
|
output = s
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
joiner := retryJoiner{
|
|
|
|
discover: &MockDiscover{},
|
|
|
|
clientJoin: mockJoin,
|
|
|
|
clientEnabled: true,
|
2018-09-13 17:43:40 +00:00
|
|
|
logger: testlog.HCLogger(t),
|
2018-05-11 19:52:05 +00:00
|
|
|
errCh: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
joiner.RetryJoin(serverJoin)
|
|
|
|
|
|
|
|
require.Equal(1, len(output))
|
|
|
|
require.Equal(stubAddress, output[0])
|
|
|
|
}
|
2018-05-22 18:14:41 +00:00
|
|
|
|
|
|
|
func TestRetryJoin_Validate(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
type validateExpect struct {
|
|
|
|
config *Config
|
|
|
|
isValid bool
|
|
|
|
reason string
|
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []*validateExpect{
|
|
|
|
{
|
|
|
|
config: &Config{
|
|
|
|
Server: &ServerConfig{
|
|
|
|
ServerJoin: &ServerJoin{
|
|
|
|
RetryJoin: []string{"127.0.0.1"},
|
|
|
|
RetryMaxAttempts: 0,
|
2018-05-25 21:12:13 +00:00
|
|
|
RetryInterval: 0,
|
2018-05-22 18:14:41 +00:00
|
|
|
StartJoin: []string{},
|
|
|
|
},
|
|
|
|
RetryJoin: []string{"127.0.0.1"},
|
|
|
|
RetryMaxAttempts: 0,
|
2018-05-30 23:51:55 +00:00
|
|
|
RetryInterval: 0,
|
2018-05-22 18:14:41 +00:00
|
|
|
StartJoin: []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
isValid: false,
|
|
|
|
reason: "server_join cannot be defined if retry_join is defined on the server stanza",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: &Config{
|
|
|
|
Server: &ServerConfig{
|
|
|
|
ServerJoin: &ServerJoin{
|
|
|
|
RetryJoin: []string{"127.0.0.1"},
|
|
|
|
RetryMaxAttempts: 0,
|
2018-05-25 21:12:13 +00:00
|
|
|
RetryInterval: 0,
|
2018-05-22 18:14:41 +00:00
|
|
|
StartJoin: []string{},
|
|
|
|
},
|
|
|
|
StartJoin: []string{"127.0.0.1"},
|
|
|
|
RetryMaxAttempts: 0,
|
2018-05-30 23:51:55 +00:00
|
|
|
RetryInterval: 0,
|
2018-05-22 18:14:41 +00:00
|
|
|
RetryJoin: []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
isValid: false,
|
|
|
|
reason: "server_join cannot be defined if start_join is defined on the server stanza",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: &Config{
|
|
|
|
Server: &ServerConfig{
|
|
|
|
ServerJoin: &ServerJoin{
|
|
|
|
RetryJoin: []string{"127.0.0.1"},
|
|
|
|
RetryMaxAttempts: 0,
|
2018-05-25 21:12:13 +00:00
|
|
|
RetryInterval: 0,
|
2018-05-22 18:14:41 +00:00
|
|
|
StartJoin: []string{},
|
|
|
|
},
|
|
|
|
StartJoin: []string{},
|
|
|
|
RetryMaxAttempts: 1,
|
2018-05-30 23:51:55 +00:00
|
|
|
RetryInterval: 0,
|
2018-05-22 18:14:41 +00:00
|
|
|
RetryJoin: []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
isValid: false,
|
|
|
|
reason: "server_join cannot be defined if retry_max_attempts is defined on the server stanza",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: &Config{
|
|
|
|
Server: &ServerConfig{
|
|
|
|
ServerJoin: &ServerJoin{
|
|
|
|
RetryJoin: []string{"127.0.0.1"},
|
|
|
|
RetryMaxAttempts: 0,
|
2018-05-25 21:12:13 +00:00
|
|
|
RetryInterval: time.Duration(1),
|
2018-05-22 18:14:41 +00:00
|
|
|
StartJoin: []string{},
|
|
|
|
},
|
|
|
|
StartJoin: []string{},
|
|
|
|
RetryMaxAttempts: 0,
|
2018-05-30 23:51:55 +00:00
|
|
|
RetryInterval: 3 * time.Second,
|
2018-05-22 18:14:41 +00:00
|
|
|
RetryJoin: []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
isValid: false,
|
|
|
|
reason: "server_join cannot be defined if retry_interval is defined on the server stanza",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: &Config{
|
2018-05-22 20:54:05 +00:00
|
|
|
Server: &ServerConfig{
|
2018-05-22 18:14:41 +00:00
|
|
|
ServerJoin: &ServerJoin{
|
|
|
|
RetryJoin: []string{"127.0.0.1"},
|
|
|
|
RetryMaxAttempts: 0,
|
2018-05-25 21:12:13 +00:00
|
|
|
RetryInterval: 0,
|
2018-05-22 18:14:41 +00:00
|
|
|
StartJoin: []string{"127.0.0.1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
isValid: false,
|
2018-05-22 20:54:05 +00:00
|
|
|
reason: "start_join and retry_join should not both be defined",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: &Config{
|
|
|
|
Client: &ClientConfig{
|
|
|
|
ServerJoin: &ServerJoin{
|
|
|
|
RetryJoin: []string{},
|
|
|
|
RetryMaxAttempts: 0,
|
2018-05-25 21:12:13 +00:00
|
|
|
RetryInterval: 0,
|
2018-05-22 20:54:05 +00:00
|
|
|
StartJoin: []string{"127.0.0.1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
isValid: false,
|
2018-05-22 18:14:41 +00:00
|
|
|
reason: "start_join should not be defined on the client",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: &Config{
|
|
|
|
Client: &ClientConfig{
|
|
|
|
ServerJoin: &ServerJoin{
|
|
|
|
RetryJoin: []string{"127.0.0.1"},
|
|
|
|
RetryMaxAttempts: 0,
|
2018-05-25 21:12:13 +00:00
|
|
|
RetryInterval: 0,
|
2018-05-22 18:14:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
isValid: true,
|
|
|
|
reason: "client server_join should be valid",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: &Config{
|
|
|
|
Server: &ServerConfig{
|
|
|
|
ServerJoin: &ServerJoin{
|
|
|
|
RetryJoin: []string{"127.0.0.1"},
|
2018-05-30 23:51:55 +00:00
|
|
|
RetryMaxAttempts: 1,
|
|
|
|
RetryInterval: 1,
|
2018-05-22 18:14:41 +00:00
|
|
|
StartJoin: []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
isValid: true,
|
|
|
|
reason: "server server_join should be valid",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
joiner := retryJoiner{}
|
|
|
|
for _, scenario := range scenarios {
|
2018-05-30 23:51:55 +00:00
|
|
|
t.Run(scenario.reason, func(t *testing.T) {
|
|
|
|
err := joiner.Validate(scenario.config)
|
|
|
|
if scenario.isValid {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
})
|
2018-05-22 18:14:41 +00:00
|
|
|
}
|
|
|
|
}
|