2015-09-22 21:04:40 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2015-12-02 19:55:29 +00:00
|
|
|
"fmt"
|
2015-09-22 21:04:40 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2015-12-02 19:55:29 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2015-09-22 21:04:40 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCommand_Implements(t *testing.T) {
|
2017-07-20 05:42:15 +00:00
|
|
|
t.Parallel()
|
2015-09-22 21:04:40 +00:00
|
|
|
var _ cli.Command = &Command{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommand_Args(t *testing.T) {
|
2017-07-20 05:42:15 +00:00
|
|
|
t.Parallel()
|
2015-09-22 21:04:40 +00:00
|
|
|
tmpDir, err := ioutil.TempDir("", "nomad")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
type tcase struct {
|
|
|
|
args []string
|
|
|
|
errOut string
|
|
|
|
}
|
|
|
|
tcases := []tcase{
|
|
|
|
{
|
|
|
|
[]string{},
|
2015-10-01 23:46:42 +00:00
|
|
|
"Must specify either server, client or dev mode for the agent.",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-client", "-data-dir=" + tmpDir, "-bootstrap-expect=1"},
|
2015-09-22 21:04:40 +00:00
|
|
|
"Bootstrap requires server mode to be enabled",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-data-dir=" + tmpDir, "-server", "-bootstrap-expect=1"},
|
|
|
|
"WARNING: Bootstrap mode enabled!",
|
|
|
|
},
|
2015-10-01 23:46:42 +00:00
|
|
|
{
|
|
|
|
[]string{"-server"},
|
|
|
|
"Must specify data directory",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-client", "-alloc-dir="},
|
|
|
|
"Must specify both the state and alloc dir if data-dir is omitted.",
|
|
|
|
},
|
2015-09-22 21:04:40 +00:00
|
|
|
}
|
|
|
|
for _, tc := range tcases {
|
|
|
|
// Make a new command. We pre-emptively close the shutdownCh
|
|
|
|
// so that the command exits immediately instead of blocking.
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
close(shutdownCh)
|
|
|
|
cmd := &Command{
|
|
|
|
Ui: ui,
|
|
|
|
ShutdownCh: shutdownCh,
|
|
|
|
}
|
|
|
|
|
2016-11-09 00:22:04 +00:00
|
|
|
// To prevent test failures on hosts whose hostname resolves to
|
|
|
|
// a loopback address, we must append a bind address
|
2016-11-09 19:37:41 +00:00
|
|
|
tc.args = append(tc.args, "-bind=169.254.0.1")
|
2015-09-22 21:04:40 +00:00
|
|
|
if code := cmd.Run(tc.args); code != 1 {
|
|
|
|
t.Fatalf("args: %v\nexit: %d\n", tc.args, code)
|
|
|
|
}
|
|
|
|
|
|
|
|
if expect := tc.errOut; expect != "" {
|
|
|
|
out := ui.ErrorWriter.String()
|
|
|
|
if !strings.Contains(out, expect) {
|
|
|
|
t.Fatalf("expect to find %q\n\n%s", expect, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-02 19:55:29 +00:00
|
|
|
|
2017-07-20 05:42:15 +00:00
|
|
|
// TODO Why is this failing
|
2015-12-02 19:55:29 +00:00
|
|
|
func TestRetryJoin(t *testing.T) {
|
2017-07-20 05:42:15 +00:00
|
|
|
t.Parallel()
|
|
|
|
agent := NewTestAgent(t.Name(), nil)
|
2015-12-02 19:55:29 +00:00
|
|
|
defer agent.Shutdown()
|
|
|
|
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
close(shutdownCh)
|
|
|
|
<-doneCh
|
|
|
|
}()
|
|
|
|
|
|
|
|
cmd := &Command{
|
|
|
|
ShutdownCh: shutdownCh,
|
2016-11-15 23:28:21 +00:00
|
|
|
Ui: &cli.BasicUi{
|
|
|
|
Reader: os.Stdin,
|
|
|
|
Writer: os.Stdout,
|
|
|
|
ErrorWriter: os.Stderr,
|
|
|
|
},
|
2015-12-02 19:55:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 05:42:15 +00:00
|
|
|
serfAddr := agent.Config.normalizedAddrs.Serf
|
2015-12-02 19:55:29 +00:00
|
|
|
|
|
|
|
args := []string{
|
2016-11-15 23:28:21 +00:00
|
|
|
"-dev",
|
2017-07-20 05:42:15 +00:00
|
|
|
"-node", "foo",
|
2015-12-02 19:55:29 +00:00
|
|
|
"-retry-join", serfAddr,
|
|
|
|
"-retry-interval", "1s",
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if code := cmd.Run(args); code != 0 {
|
2015-12-11 23:02:13 +00:00
|
|
|
t.Logf("bad: %d", code)
|
2015-12-02 19:55:29 +00:00
|
|
|
}
|
|
|
|
close(doneCh)
|
|
|
|
}()
|
|
|
|
|
|
|
|
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())
|
|
|
|
})
|
|
|
|
}
|