2013-12-31 21:06:33 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-02-10 01:08:25 +00:00
|
|
|
|
2017-05-22 11:59:36 +00:00
|
|
|
"github.com/hashicorp/consul/command/agent"
|
2017-02-10 02:19:25 +00:00
|
|
|
"github.com/hashicorp/consul/command/base"
|
2017-02-10 01:08:25 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2013-12-31 21:06:33 +00:00
|
|
|
)
|
|
|
|
|
2017-02-08 22:14:02 +00:00
|
|
|
func testJoinCommand(t *testing.T) (*cli.MockUi, *JoinCommand) {
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
return ui, &JoinCommand{
|
|
|
|
Command: base.Command{
|
2017-04-21 00:02:42 +00:00
|
|
|
UI: ui,
|
2017-02-08 22:14:02 +00:00
|
|
|
Flags: base.FlagSetClientHTTP,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-31 21:06:33 +00:00
|
|
|
func TestJoinCommand_implements(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2013-12-31 21:06:33 +00:00
|
|
|
var _ cli.Command = &JoinCommand{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJoinCommandRun(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2017-05-22 11:59:36 +00:00
|
|
|
a1 := agent.NewTestAgent(t.Name(), nil)
|
|
|
|
a2 := agent.NewTestAgent(t.Name(), nil)
|
2013-12-31 21:06:33 +00:00
|
|
|
defer a1.Shutdown()
|
|
|
|
defer a2.Shutdown()
|
|
|
|
|
2017-02-08 22:14:02 +00:00
|
|
|
ui, c := testJoinCommand(t)
|
2013-12-31 21:06:33 +00:00
|
|
|
args := []string{
|
2017-05-22 11:59:36 +00:00
|
|
|
"-http-addr=" + a1.HTTPAddr(),
|
|
|
|
fmt.Sprintf("127.0.0.1:%d", a2.Config.Ports.SerfLan),
|
2013-12-31 21:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2017-05-22 11:59:36 +00:00
|
|
|
if len(a1.LANMembers()) != 2 {
|
|
|
|
t.Fatalf("bad: %#v", a1.LANMembers())
|
2013-12-31 21:06:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJoinCommandRun_wan(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2017-05-22 11:59:36 +00:00
|
|
|
a1 := agent.NewTestAgent(t.Name(), nil)
|
|
|
|
a2 := agent.NewTestAgent(t.Name(), nil)
|
2013-12-31 21:06:33 +00:00
|
|
|
defer a1.Shutdown()
|
|
|
|
defer a2.Shutdown()
|
|
|
|
|
2017-02-08 22:14:02 +00:00
|
|
|
ui, c := testJoinCommand(t)
|
2013-12-31 21:06:33 +00:00
|
|
|
args := []string{
|
2017-05-22 11:59:36 +00:00
|
|
|
"-http-addr=" + a1.HTTPAddr(),
|
2013-12-31 21:06:33 +00:00
|
|
|
"-wan",
|
2017-05-22 11:59:36 +00:00
|
|
|
fmt.Sprintf("127.0.0.1:%d", a2.Config.Ports.SerfWan),
|
2013-12-31 21:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2017-05-22 11:59:36 +00:00
|
|
|
if len(a1.WANMembers()) != 2 {
|
|
|
|
t.Fatalf("bad: %#v", a1.WANMembers())
|
2013-12-31 21:06:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJoinCommandRun_noAddrs(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2017-02-08 22:14:02 +00:00
|
|
|
ui, c := testJoinCommand(t)
|
|
|
|
args := []string{"-http-addr=foo"}
|
2013-12-31 21:06:33 +00:00
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 1 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(ui.ErrorWriter.String(), "one address") {
|
|
|
|
t.Fatalf("bad: %#v", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|